# 6.S189: Hangman solution # Import statements: DO NOT delete these! DO NOT write code above this! from random import randrange from string import * # ----------------------------------- # Helper code # (you don't need to understand this helper code) # Import hangman words WORDLIST_FILENAME = "words.txt" def load_words(): """ Returns a list of valid words. Words are strings of lowercase letters. Depending on the size of the word list, this function may take a while to finish. """ print "Loading word list from file..." # inFile: file inFile = open(WORDLIST_FILENAME, 'r', 0) # line: string line = inFile.readline() # wordlist: list of strings wordlist = split(line) print " ", len(wordlist), "words loaded." print 'Enter play_hangman() to play a game of hangman!' return wordlist # actually load the dictionary of words and point to it with # the words_dict variable so that it can be accessed from anywhere # in the program words_dict = load_words() # Run get_word() within your program to generate a random secret word # by using a line like this within your program: # secret_word = get_word() def get_word(): """ Returns a random word from the word list """ word=words_dict[randrange(0,len(words_dict))] return word # end of helper code # ----------------------------------- # CONSTANTS MAX_GUESSES = 6 # GLOBAL VARIABLES secret_word = 'claptrap' letters_guessed = [] # From part 3b: def word_guessed(): ''' Returns True if the player has successfully guessed the word, and False otherwise. ''' global secret_word global letters_guessed # Go through the secret_word and see if we've guessed all the letters for letter in secret_word: # If at least one letter isn't guessed yet, we know to return False if letter not in letters_guessed: return False # If we're here, we've gone through all the letters and we haven't returned # False, so we must have guessed all the letters return True def print_guessed(): ''' Prints a string that contains the word with a dash "-" in place of letters not guessed yet. ''' global secret_word global letters_guessed # Initalize a list to hold our characters character_list = [] # Go through each letter in secret word to check if we've guessed it for letter in secret_word: if letter in letters_guessed: # If we've guessed it, we want to display it character_list.append(letter) else: # If we haven't guessed it, we want to display a dash character_list.append('-') # Turn it into a string and return that string print join(character_list, '') def reset_env(): ''' A function that resets the game, if we want to play again. ''' global secret_word global letters_guessed secret_word = get_word() letters_guessed = [] def play_hangman(): # Actually play the hangman game # Update secret_word. Uncomment these two lines when you're # sure that EVERYTHING else works! global secret_word global letters_guessed secret_word = get_word() mistakes_made = 0 playing = True while playing: # Print guesses & word blanks out to the user print "You have", (MAX_GUESSES-mistakes_made), "guesses left" print print_guessed() # Get user's guess; force it to be lower case guess = lower(raw_input("Enter a letter to guess: ")) # In case they enter more than one letter: force them to enter only 1 while len(guess) > 1: guess = lower(raw_input("Enter a SINGLE letter to guess: ")) # If we've already guessed this, try again if guess in letters_guessed: print "You've already guessed the letter", guess # Else if not guessed, see if in word elif guess in secret_word: # Add the guess to the letters_guessed list letters_guessed.append(guess) # Print a message to the user print "Yes, the letter", guess, "is correct." # See if we've guessed the word yet if word_guessed(): print "Congrats! You've guessed correctly; the word was", secret_word # Ask the user if they want to play again play_again = lower(raw_input("Play again? Type yes or no: ")) if play_again == 'yes': # If we want to play again, we need to reset the environment reset_env() mistakes_made = 0 else: # Otherwise we want to exit the loop playing = False # Otherwise, it was a miss else: # Add the guess to the letters_guessed list letters_guessed.append(guess) # Increment the mistakes-made counter mistakes_made += 1 # Print a message to the user print "Sorry, that is not in the word." # Check if we're out of guesses if (MAX_GUESSES - mistakes_made) == 0: print "Sorry, you're out of guesses. The word was", secret_word print "See? That wasn't so hard." # Ask the user if they want to play again play_again = lower(raw_input("Play again? Type yes or no: ")) if play_again == 'yes': # If we want to play again, we need to reset the environment reset_env() mistakes_made = 0 else: # Otherwise we want to exit the loop playing = False