The final sprint

It all comes together in the end! There's been a few twists and turns getting up to identifying a straight, but it turns out the rest are easy. Here's the highlights of the remaining hands.

Flush

This follows a similar pattern to the previous identifiers except for one difference: grouping by suit instead of face value. The implementation looks like this:

Map<Suit, Collection<Card>> groupedBySuit = Iterables.groupBy(cards, new CardSuit());
Collection<KeyValue<Suit, Collection<Card>>> flush = Iterables.where(Iterables.keyValues(groupedBySuit), new HavingNumberCards<Suit>(5, SameValueMatchType.Minimum));
if (flush.isEmpty()) return null;
return new RankedHand(player, cards, this, Iterables.sort(flush.iterator().next().getValue(), new FaceValueOrder()).subList(0, 5));

There's one new class, CardSuit which simply returns the suit of the card from grouping, and a rename: ContainsSameValuedCards to HavingNumberCards.

Full House

This was a little more complicated. To have a full house, we need to either have a three-of-a-kind and one or two pairs, or two three-of-a-kind's. If there are two three-of-a-kind's, we need to take the best one, then two cards from the second three-of-a-kind.

If the hand is made up of a three-of-a-kind and a two pairs, take the three-of-a-kind and the best pair.

This is all pretty simple as the Hands.existSameValuedCards method does all the grouping and sorting, so its really just a matter of piecing it all together.

Four of a Kind

This one was very easy. There can only be at most one four of a kind, so we use the same Hands.existSameValuedCards method, and then return the cards if it exists.

Straight flush

Finally, the straight flush. Should be easy, right? We've already done a straight and a flush, so just combine them somehow! Well, it's almost like that: I first group by suit, and if there are five or more of the same, use the straight identifier to see if there's a straight within all those that make up the flush. I didn’t use the flush identifier as this only returns the best five of a flush; I need all the flush cards to see if there's a straight.

One to go

There is one final item in the issue log: folding pre-flop. I solve this by simply adding an initial 'deal' which represents the hole cards (i.e. a deal with no community cards). Any folding pre-flop is then simply recorded against this deal instance.

Next: I plan to write a short summary of this kata and review the resulting solution.


Comment Guidelines
See the FAQ for details on the full rules and guidelines. No Spam. Write clearly and thoughtfully - no bad language.