from __future__ import print_function import random HANGMANPICS = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | /|\ | | | =========''', ''' +---+ | | O | /|\ | / | | =========''', ''' +---+ | | O | /|\ | / \ | | ========='''] #Word bank of animals words = ('ant baboon badger bat bear beaver camel cat clam cobra cougar ' 'coyote crow deer dog donkey duck eagle ferret fox frog goat ' 'goose hawk lion lizard llama mole monkey moose mouse mule newt ' 'otter owl panda parrot pigeon python rabbit ram rat raven ' 'rhino salmon seal shark sheep skunk sloth snake spider ' 'stork swan tiger toad trout turkey turtle weasel whale wolf ' 'wombat zebra ').split() class Hangman(): def __init__(self, words): """Initializes the game Selects the secret word for the game randomly """ self._missed_letters = '' self._correct_letters = '' self._secret_word = random.choice(words) self._game_is_done = False def hangman_display(self): """Displays status of the game""" print(HANGMANPICS[len(self._missed_letters)]) print('Missed letters:', end=' ') for letter in self._missed_letters: print(letter, end=' ') print("\n") #Censors the word so they don't see secret blanks = '_' * len(self._secret_word) # replace blanks with correctly guessed letters for i in range(len(self._secret_word)): if self._secret_word[i] in self._correct_letters: blanks = blanks[:i] + self._secret_word[i] + blanks[i+1:] # show the secret word with spaces in between each letter for letter in blanks: print(letter, end=' ') def word(self, word): '''takes the word and checks if it matches secret word''' if word == self._secret_word: print("Congratulations you got it!..") self._check_win() if self._check_win() == True: self._game_is_done = True else: print("Try again, that's not it :)") def _get_guess(self, already_guessed): """Gets the input from the user. Makes sure that the input entered is a letter and the letter entered is not already guessed by the user. """ #This code makes sure the user doesn't enter more than one character and only letter while True: guess = raw_input('Guess one letter or the entire word: ').lower() if len(guess) > 1: self.word(guess) elif guess in already_guessed: print('You have already guessed that letter. Choose again.') elif guess not in 'abcdefghijklmnopqrstuvwxyz': print('Please enter a LETTER.') else: return guess def _check_win(self): """Returns True if the user has won, False otherwise. Checks if the user has correctly guessed the secret word. """ for i in range(len(self._secret_word)): if self._secret_word[i] not in self._correct_letters: return False #Guessed the word correctly and it shows the word it has chosen print('Yes! The secret word is "{0}"! ' 'You have won!'.format(self._secret_word)) return True def _check_lost(self): """Returns True if the user has lost, False otherwise. Alerts the user if all his chances have been used, without guessing the secret word. """ if len(self._missed_letters) == len(HANGMANPICS) - 1: self.hangman_display() #This is when the user runs out of guesses and loses the game missed = len(self._missed_letters) correct = len(self._correct_letters) word = self._secret_word print('You have run out of guesses!') print('After {0} missed guesses and {1} correct guesses, ' 'the word was "{2}"'.format(missed, correct, word)) return True return False def run(self): """Initializes the game play and coordinates the game activities.""" print('H A N G M A N\n') print("Guess a letter or type in the word you think it is...The theme is animals") while not self._game_is_done: self.hangman_display() guessed_letters = self._missed_letters + self._correct_letters guess = self._get_guess(guessed_letters) if guess in self._secret_word: self._correct_letters = self._correct_letters + guess self._game_is_done = self._check_win() else: self._missed_letters = self._missed_letters + guess self._game_is_done = self._check_lost() def play_again(): #Asks if the user would like to play the game again if they have lost or won if raw_input('Do you want to play again? (yes or no): ').lower() == 'yes': return True else: return False def main(): """Main application entry point.""" current_game = Hangman(words) while True: current_game.run() if play_again(): current_game = Hangman(words) else: break if __name__ == "__main__": main()