Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Using a New Deck for Each Hand

Running the tests, we get an odd error, an IndexOutOfBoundsException. Since our Game code doesn’t use a new deck each time, it’s dealing each hand from the same deck. No good! Prior to dealing the second hand in testHandFlow, we try to peek at an entire deck of 52 cards, but it has only 38. The first hand dealt represents 3 players times 2 hole cards each (6 cards) plus 5 community cards plus 3 burn cards, for a total of 14 cards from the 52 cards in the deck, leaving 38.

Fixing the production code to use a new deck for each hand requires one new line of code in the Game class:

public void startHand() {
  deck = new Deck();

Reviewing the existing test methods on Game, the name of each method accurately summarizes the functionality specified within. Stepping through the code within each test method, a new developer can quickly understand the sequencing of operations and what can be asserted at each step. We aggregate some of the steps and assertions into method calls. These utility method abstractions allow a new developer to understand the test flow without having to understand every specific implementation detail behind it. The new developer can step into utility methods to comprehend them if necessary.

  • + Share This
  • 🔖 Save To Your Account