Skip to main content

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);
  5.  
  6.     if (player1Pair == null && player2Pair == null)
  7.         return WinningHandEnum.None;
  8.  
  9.     if (player1Pair == null)
  10.         return WinningHandEnum.Player2;
  11.     if (player2Pair == null)
  12.         return WinningHandEnum.Player1;
  13.  
  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();
}

Comments

Popular posts from this blog

Mocking HttpCookieCollection in HttpRequestBase

When unit testing ASP.NET MVC2 projects the issue of injecting HttpContext is quickly encountered.  There seem to be many different ways / recommendations for mocking HttpContextBase to improve the testability of controllers and their actions.  My investigations into that will probably be a separate blog post in the near future but for now I want to cover something that had me stuck for longer than it probably should have.  That is how to mock non abstract/interfaced classes within HttpRequestBase and HttpResponseBase – namely the HttpCookieCollection class.   The code sample below illustrates how it can be used within a mocked instance of HttpRequestBase.  Cookies can be added / modified within the unit test code prior to being passed into the code being tested.   After it’s been called, using a combination of MOQ’s Verify and NUnit’s Assert it is possible to check how many times the collection is accessed (but you have to include the set up calls) and that the relevant cookies have …

Injecting HttpContextBase into an MVC Controller

It is a shame that when the ASP.NET MVC framework was released they did not think to build IoC support into the infrastructure. All the major components of the MVC engine appear to magically inherit instances of HttpContext and it’s related objects – which can cause no end of problems if you are trying to utilise Unit Testing and IoC. Reading around various articles on the subject just to get around this one problem requires the implementation of several different concepts and you are still left with a work around. The code below, along with the other links referenced in this article is my stab at resolving the issue. There’s probably nothing new here, but it does attempt to relate all the information needed to do this for Castle Windsor. The overview is that all controllers will need to inherit from a base controller, which takes an instance of HttpContext into it’s constructor. It then overrides the property HttpContext in the main controller class, supplying it’s own version…

Problem installing AWS CLI

It never feels like a good start when you're trying to start out with something and the install fails with an obscure error! I was just trying to install the Amazon CLI following the instructions at https://aws.amazon.com/cli/ and ran into the following error when running 'pip install awscli': Collecting awscli Could not find a version that satisfies the requirement awscli (from versions: ) No matching distribution found for awscli I appeared to have a correct version of Python installed (v2.7) and checking "PIP -v" indicated that 9.0.1 was installed. That all seemed to tick the required boxes but digging around a little more I did see that some people had had issues with various versions of PIP so I found / ran the following to upgrade to the latest vesion: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py This installed v9.0.3 of PIP which burst into life when I re-ran 'pip install awscli' and everything seems to be ok. Like…