# MIT OCW 6.0001 課程 Problem Set 2 作業(yè)(Part 1)

MIT OCW 6.0001 課程 Problem Set 2 作業(yè)(Part 1)

打卡記錄

打卡時間:2019.07.30
打卡天數(shù):D05
學(xué)習(xí)內(nèi)容:MIT OCW 6.0001 課程 Problem Set 2 作業(yè)(Part 1)

參考鏈接

課程視頻
作業(yè)鏈接

Part C 作業(yè):

A. 游戲要求:

  1. 程序必須從 words.txt 文件中隨機(jī)挑選一個單詞。hangman.py 文件中已經(jīng)實(shí)現(xiàn)了加載單詞列表 和 隨機(jī)選擇單詞 的功能。
  2. 用戶一開始有6次機(jī)會
  3. 游戲一開始,提示用戶有單詞中有多少個字母,提示用戶的機(jī)會次數(shù)。
  4. 程序需要判斷用戶還有哪些字母可以猜

B. 用戶交互:

The game must be interactive and flow as follows:

  1. 每次在用戶猜測之前,你需要顯示以下信息:
    a. 提示用戶所剩的次數(shù)
    b. 還有哪些字母用戶還沒有猜測
  2. 提示用戶每次只能猜一個字母
  3. 每次等用戶輸入后,需要告訴用戶所輸入的單詞是否在單詞中
  4. 等用戶輸入后,你還需要顯示結(jié)果,哪些才對哪些還沒有猜到,使用下劃線來標(biāo)識沒有猜到的字母位置。
  5. 最后,輸出(-----)符號,用戶區(qū)分每一輪游戲

C. 用戶輸入要求:

  1. 你可以假設(shè)用戶每次只輸入一個字符,但用戶也許會輸入數(shù)字、特殊符號、字母,你的程序只接收小寫字母
  2. 如果用戶輸入了字母意外的內(nèi)容,你需要告訴用戶只能輸入字母。每當(dāng)用戶輸入非字母字符、或者已經(jīng)輸入過的字母,將會減少一次警告的機(jī)會,如果沒有了警告機(jī)會,游戲就會結(jié)束。

可以靈活運(yùn)用一下函數(shù):

  • str.isalpha('your string')
  • str.lower('Your String')

D. 游戲規(guī)則

  1. 初始【警告次數(shù)【為3
  2. 如果用戶輸入了非字母的內(nèi)容,【警告次數(shù)】減一;沒有警告次數(shù),游戲失敗
  3. 如果用戶輸入了已經(jīng)輸入過的內(nèi)容,【警告次數(shù)】減一 ;
  4. 輔音:如果用戶輸入了輔音字母,且沒有猜中,【猜測機(jī)會】減1
  5. 元音:如果用戶輸入的原因字母,沒有猜過,且沒有猜中,【猜測機(jī)會】減2

E. 游戲終止條件

  1. 當(dāng)用戶猜對了所有字母 或者 次數(shù)消耗完時終止游戲
  2. 次數(shù)消耗完并且沒有完成字母,告訴用戶結(jié)果并顯示正確的字母。
  3. 用戶贏了的話,輸出恭喜信息,并顯示用戶分?jǐn)?shù)
  4. Total score = guesses_remaining* number unique letters in secret_word(總分 = 剩余猜測次數(shù) x 單詞字母數(shù)(去重))

作業(yè)心得

  • 學(xué)習(xí) not in list 的寫法

程序代碼(完成三個基本輔助函數(shù))

# Problem Set 2, hangman.py
# Name: 
# Collaborators:
# Time spent:

# Hangman Game
# -----------------------------------
# Helper code
# You don't need to understand this helper code,
# but you will have to know how to use the functions
# (so be sure to read the docstrings!)
import random
import string

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')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist



def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)
    
    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code

# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = load_words()


def is_word_guessed(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing; assumes all letters are
      lowercase; 用戶需要猜的單詞,假設(shè)所有字母都是小寫
    letters_guessed: list (of letters), which letters have been guessed so far;
      assumes that all letters are lowercase; 用戶已經(jīng)猜過的字母列表
    returns: boolean, True if all the letters of secret_word are in letters_guessed;
      False otherwise; 如果 secret_word 的字母,都在 letters_guessed 中,返回 True,否則返回 False
    '''
    for letter in secret_word:
        if letter not in letters_guessed:
            return False
    return True

def get_guessed_word(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing; 用戶需要猜的單詞
    letters_guessed: list (of letters), which letters have been guessed so far; 目前猜過的字母
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    res = ""
    for letter in secret_word:
        if letter not in letters_guessed:
            res += "_ "
        else:
            res += letter
    return res

def get_available_letters(letters_guessed):
    '''
    letters_guessed: list (of letters), which letters have been guessed so far; 目前猜過的字母
    returns: string (of letters), comprised of letters that represents which letters have not
      yet been guessed.
    '''
    res = ""
    for letter in string.ascii_lowercase:
        if letter not in letters_guessed:
            res += letter
    return res

def hangman(secret_word):
    '''
    secret_word: 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 secret_word contains and how many guesses s/he starts with.
      
    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.
    
    * Ask the user to supply one guess per round. Remember to make
      sure that the user puts in a letter!
    
    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.
    
    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



# When you've completed your hangman function, scroll down to the bottom
# of the file and uncomment the first two lines to test
#(hint: you might want to pick your own
# secret_word while you're doing your own testing)


# -----------------------------------



def match_with_gaps(my_word, other_word):
    '''
    my_word: string with _ characters, current guess of secret word
    other_word: string, regular English word
    returns: boolean, True if all the actual letters of my_word match the 
        corresponding letters of other_word, or the letter is the special symbol
        _ , and my_word and other_word are of the same length;
        False otherwise: 
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



def show_possible_matches(my_word):
    '''
    my_word: string with _ characters, current guess of secret word
    returns: nothing, but should print out every word in wordlist that matches my_word
             Keep in mind that in hangman when a letter is guessed, all the positions
             at which that letter occurs in the secret word are revealed.
             Therefore, the hidden letter(_ ) cannot be one of the letters in the word
             that has already been revealed.

    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



def hangman_with_hints(secret_word):
    '''
    secret_word: 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 secret_word contains and how many guesses s/he starts with.
      
    * The user should start with 6 guesses
    
    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.
    
    * Ask the user to supply one guess per round. Make sure to check that the user guesses a letter
      
    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.
      
    * If the guess is the symbol *, print out all words in wordlist that
      matches the current guessed word. 
    
    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    pass



# When you've completed your hangman_with_hint function, comment the two similar
# lines above that were used to run the hangman function, and then uncomment
# these two lines and run this file to test!
# Hint: You might want to pick your own secret_word while you're testing.


if __name__ == "__main__":
    # pass

    # To test part 2, comment out the pass line above and
    # uncomment the following two lines.
    
    secret_word = choose_word(wordlist)
    hangman(secret_word)

###############
    
    # To test part 3 re-comment out the above lines and 
    # uncomment the following two lines. 
    
    #secret_word = choose_word(wordlist)
    #hangman_with_hints(secret_word)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容