Source: https://github.com/ardalis/kata-catalog
Hangman is a well-known word guessing game in which a secret word is shown as hidden letters, and the player guesses letters. Correct guesses unmask letters in their appropriate position of the secret word. Incorrect guesses are counted against the player. If the number of incorrect guesses exceeds the limit before the word is correctly revealed, the player loses. If they can reveal the entire word before running out of incorrect guesses, they win!
This kata is designed to help with test-first coding and refactoring and design. You can stick to just writing the library code, or produce a working version of the game using a UI of your choice (e.g. console, WinForms, WPF, web, etc.).
- Create a class
Hangman
to represent the game.- When created, the class should accept a secret word and store it in ALLCAPS.
- When created, the class should store the number of incorrect guesses that will result in losing the game.
- When created, the class should expose a property indicating that the game is in progress.
- Add a Guess method to the class which returns a result.
- A guess should accept a letter.
- If the letter isn't a valid character ("A-Z" or whatever you want to define valid to be), return an invalid guess result.
- If the letter isn't in the word, return an incorrect guess result and add the letter to a list of incorrect guesses.
- If the letter was previously guessed (correctly or not), return a duplicate guess result.
- After each guess, calculate the new game state.
- If all letters have been guessed, the game is won.
- If total incorrect guess count equals the number set when the game was created, the game is lost.
- Otherwise, the game is in progress.
You're free to design the class(es) however you want. In the References below you'll find two approval tests you can use that rely on your design outputting the results and game state as strings.
If you build a UI for the game, it should have the following features:
- Display the masked secret word at game start and after each guess. Display correctly guessed letters in the proper position in the word.
- Display a list of incorrectly guessed letters.
- If the letter is in the word, unmask all instances of the letter in the word.
- Display how many guesses the player has left. This should be positive unless the game was just lost.
- If the game is won, display a message announcing this.
- If the game is lost, display a message announcing this.
- Accept a new guess from the player while the game is in progress.
- Thanks happybits for the idea for this kata!
- Example Approval Tests Two approval tests that can be used to validate your implementation of the game engine.