Ending the game
In this section, we will complete the tests and production code we need to drive out detecting the end of a game. This will happen when we do either of the following:
- Guess the word correctly
- Make our final allowed attempt, based on a maximum number
We can make a start by coding the end-of-game detection when we guess the word correctly.
Responding to a correct guess
In this case, the player guesses the target word correctly. The game is over, and the player is awarded a number of points, based on how few attempts were needed before the correct guess was made. We need to communicate that the game is over and how many points have been awarded, leading to two new fields in our class GuessResult
. We can add a test to our existing class GuessTest
as follows:
@Test void reportsGameOverOnCorrectGuess(){ var player = new Player(); Game game = new Game(player, "ARISE", 0); ...