Geeks With Blogs
Liam McLennan hackingon.net

Michael Minutillo posted the texas hold ‘em coding challenge to the oz alt.net mailing list. The challenge is to write a program that can take poker cards as input and return information about what hands players have and who is the winner.

Solving this problem in my 9-to-5 language (c#) is not interesting to me, so I chose to do it with my beloved CoffeeScript.

Compiling CoffeeScript

The first challenge I faced was one that I have been working on for months – how to compile CoffeeScript on Windows. CoffeeScript has no runtime, it must be compiled to javascript and the supported engine for doing this is Node.js, which doesn’t work very well on Windows. Following much trial and error I eventually settled on a JavaScript environment based on jslibs put together by Alisey Lebedev. I use a slightly modified version that makes it easier to compile multiple CoffeeScript files at once.

TDD

Exercises like this one lend themselves to TDD. CoffeeScript compiles to JavaScript, and there are many test frameworks available for JavaScript. I chose QUnit because I am familiar with it from my JavaScript Koans work. QUnit allows me to write tests like:

module('card');

test('construction', function() {
  var two_clubs = new Card('2c');
  equals(two_clubs.index, '2');
  equals(two_clubs.suit, 'c');
});

and when I run the tests I can a nice output like:

QUnit output

Publishing

The goal of these challenges is to publish solutions so we can learn from each other. I used this as a motivation to learn git and github. For other windows developers wanting to start down this road my advice is:

I don’t usually like screencasts (except mine ;) but Bobby’s is perfect for git beginners. I literally typed along with Bobby.

My Solution

My solution is available in all its CoffeeScript glory. It’s not entirely complete but this has taken forever and I had to draw the line. If you download the source you can run the tests by opening the file tests.htm in any browser other than IE. If you want to experiment with the solution you can make changes to the CoffeeScript source files and recompile with:

bake compile

To run the application once compiled

bake run

you should see the following output:

Texas Hold em coffeescript output

Posted on Friday, October 15, 2010 7:04 PM | Back to top

Copyright © Liam McLennan | Powered by: GeeksWithBlogs.net