The challenge set by Problem 54 was to determine the number poker games payer 1 won given a text file detailing 1,000 hands dealt to 2 players. Given the logical nature of the rules, the solution was just a case of finding the best way to 1) implement the rules and 2) duplicate the rule hierarchy. I quickly re-factored my first attempt that attempted to place the ruled based logic inside of the PokerHand class as I found whilst this made it easy to compare the matching combinations of a single hand it was much hard to compare two hands and determine the winning combination – this was made even harder when two hands contained the same winning combination and the next highest combination had to be selected from each to be compared.

At this point, I simplified the PokerHand class to just being a container for an IEnumerable Card collection and some basic logic to confirm the hand was valid (i.e. contained 5 cards). The logic to find the winning player was migrated into a PokerEngine class, which took two populated instances of PokerHand (player 1 and player 2). After some basic validation, both hands are passed into a rule which checks to see which hand (if any) wins. If neither hand wins or draw, then the next rule in the hierarchy is checked, until either a matching hand is found, or the round is deemed a draw (as the documentation clearly stated that each hand had a clear winner this situation would result in an exception being thrown). To help until testing and the re-factoring of the original code, each validation rule was still performed against a single hand, returning the outcome of the check. The outcome of the checks performed against both hands was used to determine a winner.

The following code is the first logic check to find the winning hand, the first combination to check for is the Royal Flush as it beats any other. If both hands contain a Royal Flush, a quick short short cut can be called to see which hand has the highest card

var winningPlayer = DetermineIfWinOnRoyalFlush(player1Hand, player2Hand); if (winningPlayer != WinningHandEnum.None) { return winningPlayer; }

The above code calls the function shown below. This function utilises the separate rule hander that checks hands in isolation. This means each method on HandValidationRules can be unit tested in isolation of each other.

private static WinningHandEnum DetermineIfWinOnRoyalFlush(PokerHand player1Hand, PokerHand player2Hand) { var player1Pair = HandValidationRules.RoyalFlush(player1Hand); var player2Pair = HandValidationRules.RoyalFlush(player2Hand); if (player1Pair == null && player2Pair == null) return WinningHandEnum.None; if (player1Pair == null) return WinningHandEnum.Player2; if (player2Pair == null) return WinningHandEnum.Player1; return WinningHandEnum.None; }

The validation rules for RoyalFlush are shown below:

internal static RoyalFlushWinningCombination RoyalFlush(PokerHand pokerHander) { if (!pokerHander.ValidHand) { return null; } if (ValueOfHighestCard(pokerHander) != 14) { return null; } if (!AllOfSameSuite(pokerHander) || !ConsecutiveValues(pokerHander)) { return null; } return new RoyalFlushWinningCombination(); }

Over the past couple of years I keep on coming back to Project Euler, it’s nice when you just want a quick challenge, or to try your maths skills. It’s highlighted how rusty some of my concepts were, and I’ve learnt a whole load of new ones along the way. I really like the way that the example problem given is quite quick to determine using brute force, but the desired answer always needs a . . .

I’m trying to start up on project Euler again, one thing I do like is that it highlights how poor some of my maths knowledge actually is. Neither school or college covered many of these algorithms; which is a bit of a surprise given that I did a four-year mechanical apprenticeship with applied mathematics…..still never too late to learn So I (re)started on problem 24 which read: A permutation is an ordered arrangement of objects. . . .