Last week I had a bit of free time and decided to see how difficult it would be to write a Texas Hold'em poker simulation in Groovy. My goal wasn't to come up with a full blown game, but something simple. Create a "deck" of cards, shuffle the cards, deal the cards to the players and deal out a set of community cards. If you're not familiar with Texas Hold'em the game is pretty straightforward: 2 to 10 players each receive two down ("hole") cards and then five community cards are dealt face up in three stages: three at first (called the "flop"), then two rounds of one card (called the "turn" and the "river" respectively). A card is "burned" or discarded prior to each round of community cards. The players make their best five card poker hand using the seven cards - their two hole cards and the five community cards. They can use any combination - use of the hole cards is not necessary to make their "best" hand. That's pretty much all there is to it - I won't get into betting or strategy here as that's beyond the scope of the discussion for these purposes.
So now that we've explained the game, let's take a look at what I came up with. I'm not claiming this is the most efficient or "right" way to build a hold'em simulation, just how I completed the exercise.
The first step in my mind was to build the deck. Obviously I could have simply created an array containing all of the 52 possible cards in a deck of playing cards, but being a programmer our first inclination is often to find patterns and use techniques to solve a problem instead of using "brute force". The obvious pattern in a deck of playing cards is that there are 4 different suits - hearts, spades, clubs and diamonds and 13 repeating cards (from Ace to King) in each deck. So that was my starting point - create an array of suits and an array of cards and an empty array for the resulting deck:
Yes, I did think that emoji were a perfectly valid solution in this case. It's not every day that you get to use emoji in your codebase, so I went "all in" on that approach. The next step was to build the deck. Groovy gives us a really nice way to do this with its built in Collection methods so that part just takes creating a
List that contains the cards and the suits and calling
combinations() on it. Then I loop over it and "join" the results to get the representation of a card
Now we have to "shuffle" the deck to randomize the cards within it. Java gives us the
Collections.shuffle() method, so let's use that:
One of the handiest features of Grails is the "flash" scope. The flash scope is "A temporary storage map that stores objects within the session for...
One of the cooler things about the Spark Java framework is built in websocket support thanks to the embedded Jetty server. I've long been fascinated...
The latest journey in my quest to see just how many new technologies and frameworks I can learn in one week involves Morphia. Morphia is a "Java ...