MITx 6.00 Problem Set 3 hangman游戏

全部代码参见Github:https://github.com/pxjw/MITx-6.00.1-2-Problem-Set/tree/master/6.00.1x/proSet3

HANGMAN PART 1: IS THE WORD GUESSED?

Please read the Hangman Introduction before starting this problem. The helper functions you will be creating in the next three exercises are simply suggestions, but you DO have to implement them if you want to get points for this Hangman Problem Set. If you'd prefer to structure your Hangman program in a different way, feel free to redo this Problem Set in a different way. However, if you're new to programming, or at a loss of how to construct this program, we strongly suggest that you implement the next three helper functions before continuing on to Hangman Part 2.

We'll start by writing 3 simple functions that will help us easily code the Hangman problem. First, implement the function isWordGuessed that takes in two parameters - a string, secretWord, and a list of letters, lettersGuessed. This function returns a boolean - True if secretWord has been guessed (ie, all the letters of secretWord are in lettersGuessed) and False otherwise.

Example Usage:

>>> secretWord = 'apple' 
>>> lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
>>> print isWordGuessed(secretWord, lettersGuessed)
False

For this function, you may assume that all the letters in secretWord and lettersGuessed are lowercase.

def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    # FILL IN YOUR CODE HERE...
    for x in secretWord:
        if x not in lettersGuessed:
            return False
    return True

 

PRINTING OUT THE USER'S GUESS

 

(5.0/5.0 points)

Next, implement the function getGuessedWord that takes in two parameters - a string, secretWord, and a list of letters, lettersGuessed. This function returns a string that is comprised of letters and underscores, based on what letters in lettersGuessed are in secretWord. This shouldn't be too different from isWordGuessed!

Example Usage:

>>> secretWord = 'apple' 
>>> lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
>>> print getGuessedWord(secretWord, lettersGuessed)
'_ pp_ e'

When inserting underscores into your string, it's a good idea to add at least a space after each one, so it's clear to the user how many unguessed letters are left in the string (compare the readability of ____ with_ _ _ _ ). This is called usability - it's very important, when programming, to consider the usability of your program. If users find your program difficult to understand or operate, they won't use it!

For this problem, you are free to use spacing in any way you wish - our grader will only check that the letters and underscores are in the proper order; it will not look at spacing. We do encourage you to think about usability when designing.

For this function, you may assume that all the letters in secretWord and lettersGuessed are lowercase.

def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    string = ""
    for x in secretWord:
        if x in lettersGuessed:
            string += x
        else:
            string +='_'
    return string

 

PRINTING OUT ALL AVAILABLE LETTERS

 

(5.0/5.0 points)

Next, implement the function getAvailableLetters that takes in one parameter - a list of letters,lettersGuessed. This function returns a string that is comprised of lowercase English letters - all lowercase English letters that are not in lettersGuessed.

Example Usage:

>>> lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
>>> print getAvailableLetters(lettersGuessed)
abcdfghjlmnoqtuvwxyz

Note that this function should return the letters in alphabetical order, as in the example above.

For this function, you may assume that all the letters in lettersGuessed are lowercase.

def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''
    # FILL IN YOUR CODE HERE...
    notGuessed = []
    # notGuessed = string.ascii_lowercase
    for x in range(26):
        notGuessed += chr(x + ord('a'))

    for y in lettersGuessed:
        notGuessed.remove(y)

    string = ""
    for z in notGuessed:
        string += z
    return string

 

HANGMAN PART 2: THE GAME

 

(15.0/15.0 points)

Now you will implement the function hangman, which takes one parameter - the secretWord the user is to guess. This starts up an interactive game of Hangman between the user and the computer. Be sure you take advantage of the three helper functions, isWordGuessedgetGuessedWord, and getAvailableLetters, that you've defined in the previous part.

Hints:

  • You should start by noticing where we're using the provided functions (at the top of ps3_hangman.py) to load the words and pick a random one. Note that the functions loadWords and chooseWord should only be used on your local machine, not in the tutor. When you enter in your solution in the tutor, you only need to give your hangman function.

  • Consider using lower() to convert user input to lower case. For example:

    guess = 'A'
    guessInLowerCase = guess.lower()
  • Consider writing additional helper functions if you need them!

  • There are four important pieces of information you may wish to store:

    1. secretWord: The word to guess.
    2. lettersGuessed: The letters that have been guessed so far.
    3. mistakesMade: The number of incorrect guesses made so far.
    4. availableLetters: The letters that may still be guessed. Every time a player guesses a letter, the guessed letter must be removed from availableLetters (and if they guess a letter that is not inavailableLetters, you should print a message telling them they've already guessed that - so try again!).
      def hangman(secretWord):
          '''
          secretWord: string, the secret word to guess.
      
          Starts up an interactive game of Hangman.
      
          * At the start of the game, let the user know how many 
            letters the secretWord contains.
      
          * Ask the user to supply one guess (i.e. letter) per round.
      
          * The user should receive feedback immediately after each guess 
            about whether their guess appears in the computers word.
      
          * After each round, you should also display to the user the 
            partially guessed word so far, as well as letters that the 
            user has not yet guessed.
      
          Follows the other limitations detailed in the problem write-up.
          '''
          # FILL IN YOUR CODE HERE...
          print("Welcome to the game Hangman!")
          print("I am thinking of a word that is " + str(len(secretWord)) +" letters long")
          print("-----------")
          lettersGuessed = []
          guesses = 8
          # while all the letters of secretWord are not yet in lettersGuessed and guesses left is more than 0
          while not isWordGuessed(secretWord, lettersGuessed) and guesses > 0:
              # print first line
              print("You have " + str(guesses) +" guesses left")
              # print second line
              print("Available Letters: " + getAvailableLetters(lettersGuessed))
              # print third line
              # if user input a letter that has already been entered, reprompt for letter
              while True:
                  guessLetter = raw_input("Please guess a letter: ").lower()
                  if guessLetter in lettersGuessed:
                      print("Oops! You've already guessed that letter: " + getGuessedWord(secretWord, lettersGuessed))
                      print("-----------")
                      print("    You have " + str(guesses) +" guesses left")
                      print("Available Letters: " + getAvailableLetters(lettersGuessed))
                  else:
                      break
              lettersGuessed += guessLetter
              # print last line
              # if correctly guessed secret word
              if isWordGuessed(secretWord, lettersGuessed):
                  print("Good guess: " + getGuessedWord(secretWord, lettersGuessed))
                  print("-----------")
                  print("Congratulations, you won!")
                  break
              # else if the guess letter is in secret word
              elif guessLetter in secretWord:
                   print("Good guess: " + getGuessedWord(secretWord, lettersGuessed))
                   print("-----------")
              # else the guess letter is not in secret word
              else:
                  print("Oops! That letter is not in my word: " + getGuessedWord(secretWord, lettersGuessed))
                  print("-----------")
                  guesses -= 1
              # if ran out of guesses
              if guesses == 0:
                  print("Sorry, you ran out of guesses. The word was " + secretWord + ".")

       

posted @ 2015-03-09 18:49  Pxjw  阅读(4068)  评论(0编辑  收藏  举报