# Hangman 1. **Set a puzzle** ← here 2. Guess a word 3. Automatic player 4. Better strategies ## Filtering the dictionary What's the best way to filter the dictionary of invalid words? --- layout: true .indexlink[[Index](index.html)] --- # Data for Hangman ## Creating a game * 'List' of words to choose from * Pick one at random ## Game state
Data
Operations
* Target word * Discovered letters * In order in the word * Lives left * Wrong letters?
* Get a guess * Update discovered letters * Update lives * Show discovered word * Detect game end, report * Detect game win or loss, report
--- # Next steps We've got a list of valid words. * How to pick one? * look in the `random` module * Once we have a word, how to represent what's been guessed? --- # Representing the game state Five data items: * `target`, the target word: a `string` * `discovered`, what's been found so far: a `list` of letters (each a length-one `string`) * unguessed letters are `_` * `guess`, the letter that's just been guessed: a length-one `string` * `lives`, the lives remaining: an `int` * `wrong_letters`, the incorrect guesses: a `list` (optional) * (but it makes the automated players easier if they don't have to track wrong guesses) --- # Challenges 1. Print a `discovered` word 2. Read a letter 3. Check if the `guess`ed letter is in the `target` 4. Update `discovered` word with the newly-`guess`ed letter 5. Update `lives` and `wrong_letters` if the guess is wrong ## Hints 1. Remember `
.join(
)` ? 2. `input('prompt')` (or `guess = input('prompt').strip().lower()[0]` ) 3. Walk over `target`, looking for `guess`. * Explicit iteration (`for i in range(len(target)):`) * With a comprehension (`[... for i, l in enumerate(target) if... ]`) 4. `target[n] = guess`. * Note that you need the numerical position, and you need *all* the positions for repeated letters. 5. How do you know the guess is wrong?