Hangman

Prerequisites

You should have completed the function sets for the first five chapters. (The last of those, the chapter on strings, is particularly important.)

You should also have done the Number Guess Project.

Instructions

Write a program that plays hangman. The program will have two users, a non-player and a player. The non-player (who I will call "Game Master", or "G-Masta" for short) will chooose a puzzle string and the number of guesses the player is allowed to make. The player (who I will call "Playa") will then play hangman.

So, when the game begins, ask G-Masta for two inputs: a puzzle string and the number of guesses. Check G-Masta 's choices. For the puzzle, she should be allowed to input only letters and spaces; if she inputs any character other than a letter or space, make her input again. Moreover, eliminate extraneous spaces from the puzzle string; these are spaces before or after the puzzle, and multiple spaces between words. (Remember space_cull?) For the number of guesses, accept only a positive integer.

After G-Masta has input the puzzle string and the number of guesses, clear the screen and then begin the game. When the game begins, show Playa her puzzle. Initially this will be the puzzle string with all non-spaces replaced by dashes. So if the puzzle string was "SPAM AND EGGS", Playa would first see "---- --- ----". Now ask Playa for a guess. If the guess isn't a letter, inform her of this and then make her guess again. After each guess, show Playa the new state of the puzzle and tell her how many guesses remain. (Note that Playa loses a guess only when she guesses incorrectly.) So for instance, if her first guess was "S" and the puzzle was "SPAM AND EGGS", she would see "S--- --- ---S".

Once the game has ended (either because Playa correctly guessed the puzzle or because she has no more guesses), ask her whether she wants to play again.

A few other points:

  • The puzzle should not be case sensitive. "a" = "A", "b" = "B", etc. Be consistent about how you print letters - either all uppercase or all lowercase.

  • If Playa guesses a letter she had guessed before, tell her that; and if that repeated guess is incorrect, don't subtract from the number of guesses that remain.

  • The game should not crash no matter what G-Masta and Playa input. Handle invalid input; don't let it crash your code.

Sample Game

Below is an example of how the program might interact with the two users. Messages generated by the computer are in italics. (The game below isn't complete. Remember too that you're supposed to clear the screen after G-Masta has input the puzzle and the number of guesses.)

G-Masta, give me a puzzle string: SPAM AND EGGS

G-Masta, how many guesses should I given the player? 5

Playa, get ready!

Playa, you have 5 guesses.

---- --- ----

Playa, make a guess: S

Correct! The puzzle contains two S's: S--- --- ---S

5 guesses left.

Playa, make a guess: T

Wrong! 4 guesses left.

Playa, make a guess: A

Correct! The puzzle contains two A's: S-A- A-- ---S

4 guesses left.

Playa, make a guess: E

Correct! The puzzle contains one E: S-A- A-- E--S

4 guesses left.

Playa, make a guess: O

Wrong! 3 guesses left.

Suggested Structure

Here's a suggested structure for your code. Follow it if it seems good. But whatever you do, write many functions; and keep each function as short and simple as you can. Remember our mantra: WRITE FUNCTIONS, MAKE EACH SINGLE-TASK!

# Begin will all imports: import os, time, random ... whatever you need.


# Now for the helper functions, the ones that will be called by one_game and game_loop.

# Here are the names of mine:

# space_cull (remove all extraneous spaces)

# is_alpha (return True if a string is nothing but spaces and letters, False otherwise)

# get_puzzle (ask G-Masta for a puzzle and return her choice)

# get_pos_int(used get the number of choices)

# get_letter (used to get Playa's guess)

# show_puzzle (return the puzzle string with dashes replaced by letters guessed correctly)


# After all helper functions come one_game and game_loop.

# The former plays one game; the latter calls one_game

# and then once the game is done, asks Playa if she wants to play again.


def one_game():

# Set up a game:

# 1. Get a puzzle from G-Masta

# 2. Get the number of guesses from G-Masta

# 3. Initialize a variable that will hold that guesses made. A list maybe?

game_over = False

while not game_over:

# Play a game of hangman:

# 1. Show Playa the current state of the puzzle;

# that's the string with dashes for not-yet-guessed letters.

# 2. Get a guess from Playa.

# 3. Tell her whether it's in the puzzle.

# 4. If Playa has won, tell her that and set game_over to True.

# 5. If Playa has run out of guesses, tell her that and set game_over to True.


def game_loop():

play_again = True

while play_again:

one_game()

# Now ask Playa if she wishes to play again.

# If she doesn't, set play_again to False.