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

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

打卡記錄

打卡時(shí)間: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ī)挑選一個(gè)單詞芯勘。hangman.py 文件中已經(jīng)實(shí)現(xiàn)了加載單詞列表 和 隨機(jī)選擇單詞 的功能零酪。
  2. 用戶一開始有6次機(jī)會(huì)
  3. 游戲一開始弦蹂,提示用戶有單詞中有多少個(gè)字母踪栋,提示用戶的機(jī)會(huì)次數(shù)。
  4. 程序需要判斷用戶還有哪些字母可以猜

B. 用戶交互:

The game must be interactive and flow as follows:

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

C. 用戶輸入要求:

  1. 你可以假設(shè)用戶每次只輸入一個(gè)字符,但用戶也許會(huì)輸入數(shù)字局雄、特殊符號(hào)甥啄、字母,你的程序只接收小寫字母
  2. 如果用戶輸入了字母意外的內(nèi)容炬搭,你需要告訴用戶只能輸入字母蜈漓。每當(dāng)用戶輸入非字母字符、或者已經(jīng)輸入過的字母宫盔,將會(huì)減少一次警告的機(jī)會(huì)融虽,如果沒有了警告機(jī)會(huì),游戲就會(huì)結(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ī)會(huì)】減1
  5. 元音:如果用戶輸入的原因字母,沒有猜過热某,且沒有猜中腻菇,【猜測機(jī)會(huì)】減2

E. 游戲終止條件

  1. 當(dāng)用戶猜對(duì)了所有字母 或者 次數(shù)消耗完時(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 的寫法

程序代碼(完成三個(gè)基本輔助函數(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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末洋侨,一起剝皮案震驚了整個(gè)濱河市舍扰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌希坚,老刑警劉巖边苹,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異裁僧,居然都是意外死亡个束,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門聊疲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來播急,“玉大人,你說我怎么就攤上這事售睹∽” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵昌妹,是天一觀的道長捶枢。 經(jīng)常有香客問我,道長飞崖,這世上最難降的妖魔是什么烂叔? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮固歪,結(jié)果婚禮上蒜鸡,老公的妹妹穿的比我還像新娘。我一直安慰自己牢裳,他們只是感情好逢防,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蒲讯,像睡著了一般忘朝。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上判帮,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天局嘁,我揣著相機(jī)與錄音,去河邊找鬼晦墙。 笑死悦昵,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的晌畅。 我是一名探鬼主播但指,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了枚赡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤谓谦,失蹤者是張志新(化名)和其女友劉穎贫橙,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體反粥,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卢肃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了才顿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片莫湘。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖郑气,靈堂內(nèi)的尸體忽然破棺而出幅垮,到底是詐尸還是另有隱情,我是刑警寧澤尾组,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布忙芒,位于F島的核電站,受9級(jí)特大地震影響讳侨,放射性物質(zhì)發(fā)生泄漏呵萨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一跨跨、第九天 我趴在偏房一處隱蔽的房頂上張望潮峦。 院中可真熱鬧,春花似錦勇婴、人聲如沸忱嘹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽德谅。三九已至,卻和暖如春萨螺,著一層夾襖步出監(jiān)牢的瞬間窄做,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國打工慰技, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留椭盏,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓吻商,卻偏偏與公主長得像掏颊,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容