How I Enjoyed the Rock Paper Azure Competition

Rock Paper AzureIn mid-December, I saw an ad on StackOverflow.com and was immediately intrigued. “Rock, Paper, Azure!” was a contest run by Microsoft wherein programmers design bots to compete in a modified game of Rock, Paper, Scissors. The bots had to be hosted in Microsoft’s cloud computing platform, Azure, so you can easily see Microsoft’s motivation to give away some small prizes to influence developers into trying and (hopefully) adopting Azure.

Although I had plenty of things to keep me busy leading up to Christmas, the Rock, Paper, Azure marketing worked on me. I figured I could take 1 or 2 hours out of my time and write the best algorithm I could in that time. Besides, I would be entered into the grand-prize contest drawing just for competing with even the most simple of bots.

Bugs LanguageI was immediately reminded of a school project from an early Computer Science course at Ohio State. The contest back then pitted “bug bots” from teams of students in the course against each other. Each team started out with a handful of bugs on a large virtual checker board. A bug could “convert” another student’s bug by facing it and issuing the “bite” command. The bitten bug would then become a member of the “biting” bug’s army. The game continues until one team has converted all bugs. If I remember correctly, there were only a few possible commands:

  • “Detect” if an object (like a wall or another bug) was in front of it
  • “Move” forward 1 square
  • “Rotate” left or right
  • “Bite”

 

It may have evolved since then, but our bot did surprisingly well back then despite a very simple algorithm:

  1. If something in front, turn left, bite.
  2. Else, move forward, bite.
  3. Repeat Step 1.

 

I’ve often wondered what additional strategy I would write into my bot if given another opportunity in such a competition. Rock, Paper, Azure was the challenge I was looking for.

Microsoft’s version of “roshambo” came with a few twists, such as the introduction of the dynamite and water balloon moves. Check out all the details and rules here. I liked that it was a simple game but with competition against other developers’ bots came many options for creative strategy. Additionally, I was extremely impressed with how simple it was to build the basic bot.

Game Rule Highlights:

  • Bots compete each other throwing one of Rock, Paper, Scissors, Dynamite, or Water Balloon
  • Normal rules apply except that the Dynamite beats everything but Water Balloon and Water Balloon loses to everything but Dynamite
  • Each bot only gets to use Dynamite 100 times
  • First bot to win 1000 times wins the entire match
  • Ties carry over, so the next round could be worth more than 1 win (similar to “Skins” in golf)

 

It took me some iteration to come up with my eventual strategy, which turned out to be admittedly mediocre (98th place out of 162). I realized that my bot can keep track of the history of moves that it has made as well as the moves of my opponent. My plan was to try and detect if my opponent was falling into a sort of pattern. I was especially concerned about the end of the round when we both would be desperately throwing dynamite to close out the match. As you can see, my strategy only had a small amount of success.

Nonetheless, I thoroughly enjoyed my time creating and deploying my bot. I encourage Microsoft to search for more clever ways to get developers interested in learning and using their development platforms. In this contest, I got to expand my mind, learn more about Azure, and I even got a free t-shirt. Here’s to the next competition!

Advertisements

5 Things I Hate about my Favorite Programming Language: C#

In episode 73 of the StackOverflow podcast , Jeff Atwood mentions one of his favorite questions to ask developers, “What are 5 things that you hate about your favorite programming language?”

It got me thinking. I definitely like some of the language features of C#, especially when developing within Visual Studio. Some of my favorites are Generics, Intellisense, and Short-Circuited conditionals. However, there are other pains that I encounter repeatedly when it comes to language syntax.

 

  1. In C#, there is the nifty syntax of following a variable with “??” allowing the developer to specify a replacement value in the case of the variable being null. In theory this is a great feature. However, I never end up getting to use this and I imagine that is the case for most developers. I find I usually end up using the “?” and “:” syntax because I typically want to use a member of the null object like “user.Identity.ToString()” but I have to check if “user” is null or I will get an object reference error.
  2. Why do I always have to lookup how to format a DateTime object in code without the minutes?
  3. If “ToString()” is a member of every object, why should I have to explicitly call it when setting the value of a string variable from another non-string variable? As an example, why can’t a string value be inferred from my int variable by just calling “ToString()” when there would otherwise be a type issue?


    int i = 0;

    string j = String.Empty;

    j = i;

  4. The compiler considers the use of “=” valid within an if condition. This can be confusing and often causes accidental issues where “==” is the intended code. As a solution, I use a coding standard of typing the constant value to be compared first because it cannot be assigned to (i.e. “if (0 == count)”).
  5. Nullable types seem like a great idea but they are not always intuitive. In the example below, the code seems like it should work. However, I receive a compiler error on the second line.


    int? i = null;

    i = (sender != null) ? sender.GetHashCode() : null;

 

Most of the above complaints were compiled off the top of my head. If you have found a better way to leverage the C# language so that you do not run into these issues, please share them with me in the comments. Perhaps it is my mis-use of the language features that has caused my agony.

Analyzing my Choice of Attending Ohio State

It’s that time of year

We are into the heart of college football season which means I have a date with the television every Saturday around noon to watch my Buckeyes. Can I blame the inconsistency in my blog-posting schedule on football season? I suppose so, but I made a 2010 football season resolution to not make stupid excuses.

In spending so much time thinking about the Buckeyes football team, visiting campus, and discussing school among friends I have recently begun to reflect upon The Ohio State University and whether or not it was the best choice of college for me. While in school, I generally knew that those would be the best days of my life. Ohio State meant a lot to me and I had even gone as far as referring to it as “the Greatest University in the World.” Looking back, it was definitely an excellent choice, but could I have done it better?

To properly analyze the decision means to review the benefits and drawbacks to my personal career and education situation.

The Great

Let’s start out with the obvious. Ohio State has an elite athletics department. At the time of this writing, it is one of the few universities to win a Division I championship in each Baseball, Basketball, and Football and is the reigning 5 time Big Ten Conference football champion. Although it may not seem important in supporting my career, the prestige of the program has made it convenient to connect or keep in contact with fellow alumni. I have yet to meet a fellow Ohio State graduate who did not care about the direction of the football program, providing for a useful icebreaker.

Speaking of alumni, did I mention the sheer size of Ohio State’s Undergraduate class? It is routinely ranked in the top 5 in the nation, sometimes as high as 50,000 students enrolled. Such a high number of students yields a high number of alumni, many of whom have taken jobs at leading companies or have established networks in remote locations. Fellow alumni are more likely to network and pull favors than some other successful stranger.

(OSU alumni have a tradition of taking pictures of this O-H-I-O formation in exotic locations)

(The Columbus Skyline – by voteprime)

With Ohio State being as huge as it is it must accommodate a wide range of needs. The University offers diverse majors, libraries, science & computer labs, and recreational facilities. Essentially, if you can think of a resource that should be available for students, you will be able to find it somewhere on campus. The problem is that many students do not realize what is available until it is too late. Perhaps a more specialized school would not run into this issue.

When I attended OSU, it was ranked respectably in its Computer and Information Science (CIS) department. The program provided a fundamental knowledge of theoretical computer science concepts. According to this site, Ohio State’s Engineering & IT ranking is 157 in the world.

A subtle benefit to Ohio State is the location. Although Columbus, OH does not boast many geographical advantages (it is flat with limited bodies of water), its development has been well-planned and it is of considerable size. Contrasting Ohio State with other schools, like Ohio University, which is clearly the primary attraction of its city, residing in Columbus enables students to find quality careers, co-ops, and interests without traveling a great distance.

The Disappointing

I am proud to be a Buckeye and I still live in Ohio. However, I would like to think that I am objective about the school’s educational program. In my time on campus and afterward, I have met some truly elite individuals. Unfortunately, the curriculum is not as challenging as it is at prestigious academic institutions. Therefore, the average undergraduate student is not very motivated. To draw on a previous point, students at Ohio State have incredible opportunity for success due to vast resources, but in order to take advantage of opportunities requires serious self-discipline.

During my early years at Ohio State (99-01), it was disappointing to realize that the school was more well-known for “riots” than for any of its brilliant research. It seemed as though any off-campus party involving multiple houses quickly turned into an angry mob throwing beer bottles at COPs. Going to class the next week I could hear the frustration in my professors’ voices that their hard-work had translated into negative national headlines.

My biggest regret about choosing Ohio State involves the aforementioned limited geography. Ohio State is located in Columbus, Ohio, right in the middle of the Midwest. The following big cities are within a 3 hour drive: Detroit, Indianapolis, Cincinnati, Cleveland, and Pittsburgh. While convenient for those wanting to visit 5 NFL teams within a short drive, it is not exactly close to any technology hotspots such as San Francisco, Seattle, Boston, nor even Chicago. Additionally, the region lacks exciting recreational activities. There are no beaches, mountains, nor warm days in November. If I could search for colleges again, my new strategy would be to at least research gorgeous campuses in exotic locales. I have heard Pepperdine University is one example of such a beauty as opposed to the “concrete campus” that was my destiny.

 

Having graduated and entered the workforce, I look back on my decision to attend Ohio State with satisfaction. Although there were some drawbacks, its size helped me stretch my capabilities socially and introduced me to a vast network of professional connections. I wouldn’t be the same person if I were not a Buckeye.

I Heart Karnaugh Maps

Have you ever found yourself writing a long Boolean condition in your code like the line below?


if ((policy.Type == PolicyType.AutoInsurance && policy.PolicyHolder.PriorAccidents == 0) || (policyPaidInFull && policy.Type == PolicyType.AutoInsurance || policy.IsPremium) || (policy.Type == PolicyType.AutoInsurance && policy.PolicyHolder.PriorAccidents == 0 && policy.IsPremium))

 

I Heart Karnaugh MapsPerhaps that line of code above is the first and easiest way you thought about all the conditions that have to occur in your application’s business logic. You write that line of code, test it in many different scenarios and it works so you think you have done a good job. Well, there are ways to improve upon that line of code by removing logically equivalent Boolean expressions, not to mention some style improvements that might make it more understandable.

We owe gratitude to our dear friends the Electrical Engineers for developing a clever tool, named Karnaugh Maps, to help with this dilemma, usually for cases of no more than 6 variable conditions. Karnaugh Maps (pronounced “car-no” and often simply called “K-Maps”) are a system for reducing Boolean expressions into a more simplistic form. They originated from the need to reduce electrical wiring gates, or circuit minimization, but they are still useful for the high-level software developer.

 

Benefits of reduced Boolean expressions

  1. Increased program performance
  2. Increased readability of code
  3. Less code results in easier to change code

Reducing our example expression

The 1st step is to let letters represent each of the conditions in our expression. In our case:

  • a :     policy.Type == PolicyType.AutoInsurance
  • b :     policyPaidInFull
  • c :     policy.PolicyHolder.PriorAccidents == 0
  • d :     policy.IsPremium

Then, draw a graphical square like the one pictured below. This represents each possible combination of our Boolean conditions. The boxes are blank because we have not yet entered what Boolean results we want in our resultant expression.

Blank Karnaugh Map

Let’s take the first, simplified condition in parentheses, if (a && c), and put it into the map. The result would look like the below image, because we only need to fill in the boxes where a and c are both 1.

Sample Karnaugh Map

Following this example, we can use the entire Boolean expression to fill out the whole map. The completed Karnaugh Map is pictured below.

Completed Karnaugh Map

Now circle any square or straight line of boxes since they correspond to an expression that differs by only two bits.

Circled Karnaugh Map

Because of the way we have arranged our variables around the outside of the map, we can eliminate variables based on boxes filled with 1s being adjacent to each other. In our example, the vertical “circle” exhibits a scenario where the expression should always be true as long as both the a bit and the b bit are 1, hence the condition ( a && b ). Likewise, the square “circle” exhibits true cases whenever the c bit is 1 and the a bit is 1. Because the b bit and d bit are true no matter if their values are 0 or 1, they can be deleted from the resulting simplified expression, which would be || ( a && c ). We use a similar rule to find the final d condition.

In one sentence, this rule can be summarized as “the circled boxes can be grouped together and the two variables that differ can be discarded.”

The resulting Boolean expression is:

if ( ( a && b ) || ( d ) || ( a && c ) )

which, using Boolean algebra, can be further reduced to:

if ( ( a && ( b || c ) ) || ( d ) )

We were not able to completely remove any variables, but we did simplify the expression quite a bit. The original Boolean conditions can be substituted for our letter variables, and we can rewrite the original expression as below:


bool policyIsAuto = ( PolicyType.AutoInsurance == policy.Type );
bool zeroPriorAccidents = ( 0 == policy.PolicyHolder.PriorAccidents );
if ( policyIsAuto && ( policyPaidInFull || zeroPriorAccidents ) || policy.IsPremium )

Seems easy, right? It is. And I am sorry if my steps went too fast for you. My intention is not to teach how to use Karnaugh Maps for all circumstances but instead to show you how easy and useful it is to simplify your Boolean logic.

For help with different scenarios, find the book Bebop to the Boolean Boogie – an Unconventional Guide to Electronics at your local library, check out this Wikipedia link, or you can even download software to perform the rules for you.

Happy Karnaugh Mapping!

In the mean time, I hope I was able to show you how fun and easy using a system like this can be. Feel free to post questions in the comments.

 

– Karnaugh Map Images taken from Bebop to the Boolean Boogie – an Unconventional Guide to Electronics

– Digital Logic image created by Garrett Crawford