Monday, 9 August 2010

Project Euler – Problem 54

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 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 were 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.

  1. private static WinningHandEnum DetermineIfWinOnRoyalFlush(PokerHand player1Hand, PokerHand player2Hand)
  2. {
  3.     var player1Pair = HandValidationRules.RoyalFlush(player1Hand);
  4.     var player2Pair = HandValidationRules.RoyalFlush(player2Hand);
  6.     if (player1Pair == null && player2Pair == null)
  7.         return WinningHandEnum.None;
  9.     if (player1Pair == null)
  10.         return WinningHandEnum.Player2;
  11.     if (player2Pair == null)
  12.         return WinningHandEnum.Player1;
  14.     return WinningHandEnum.None;
  15. }

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();

No comments:

Post a Comment