More kata baby steps
So after some consideration of the issues I came up with after the initial implementation, I decided to make a small refactoring. I split the PokerGame class into two: a PokerGame and PokerRound The PokerGame class now represents a set of rounds (PokerRound instances). Essentially, all the methods from PokerGame were pushed to the PokerRound:
PokerGame
+ newRound : PokerRound
PokerRound
+ deal
+ holeCards
+ dealFlop
+ dealTurn
+ dealRiver
Pretty straight forward. Now, I pick up the next issue - no ability to fold. This deserves to be the second test:
@Test
public void when_one_player_folds_then_the_other_player_should_be_the_winner() {
PokerGame game = new PokerGame();
PokerRound round = game.newRound();
round.deal("john").holeCards("4d", "2d");
round.deal("jane").holeCards("Th", "3c");
round.dealFlop("Ks", "8d", "4d");
round.fold("jane");
Assert.assertEquals("john: 4d 2d Ks 8d 4d (Winner)\n" +
"jane: Th 3c Ks 8d 4d [folded]", game.results());
}
The fold method doesn't exist, so I add it. As I need to report who folded in the results method, I add a field to the class: Set<String> foldedPlayers. In the fold method, I simply add the player to this set. Finally, it's a simple case of updating the results method to get this test to pass:
if (foldedPlayers.contains(playerAndTheirCards.getKey())) results.append(" [folded]");
else results.append(" (Winner)");
Unfortunately, this didn't pass. Jane was listed before John. That's simply because I'm iterating through a Map which has no defined order. I could create a comparator and change to a sorted map, but for now, I think the simplest option is to create a list of players. The list is simply populated in the same order as the deal method is called. I can also do away with the lastPlayerDealt field - although I still have the concept of the last player dealt - it's now that last element of the list instead.
The results method now iterates over the players list, selecting the cards from the Map. And with that change, we're back to green.
I'm feeling confident now. I've updated the issues file and there's one glaring feature currently missing - actually determining the winning hand. It's been slow and steady progress so far, but this is without doubt the biggest step forward and I'll have to be careful once more to take a similarly unhurried approach.
