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)
參考鏈接
Part C 作業(yè):
A. 游戲要求:
- 程序必須從 words.txt 文件中隨機(jī)挑選一個(gè)單詞芯勘。hangman.py 文件中已經(jīng)實(shí)現(xiàn)了加載單詞列表 和 隨機(jī)選擇單詞 的功能零酪。
- 用戶一開始有6次機(jī)會(huì)
- 游戲一開始弦蹂,提示用戶有單詞中有多少個(gè)字母踪栋,提示用戶的機(jī)會(huì)次數(shù)。
- 程序需要判斷用戶還有哪些字母可以猜
B. 用戶交互:
The game must be interactive and flow as follows:
- 每次在用戶猜測之前,你需要顯示以下信息:
a. 提示用戶所剩的次數(shù)
b. 還有哪些字母用戶還沒有猜測 - 提示用戶每次只能猜一個(gè)字母
- 每次等用戶輸入后复唤,需要告訴用戶所輸入的單詞是否在單詞中
- 等用戶輸入后,你還需要顯示結(jié)果烛卧,哪些才對(duì)哪些還沒有猜到佛纫,使用下劃線來標(biāo)識(shí)沒有猜到的字母位置。
- 最后总放,輸出(-----)符號(hào)呈宇,用戶區(qū)分每一輪游戲
C. 用戶輸入要求:
- 你可以假設(shè)用戶每次只輸入一個(gè)字符,但用戶也許會(huì)輸入數(shù)字局雄、特殊符號(hào)甥啄、字母,你的程序只接收小寫字母
- 如果用戶輸入了字母意外的內(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ī)則
- 初始【警告次數(shù)【為3
- 如果用戶輸入了非字母的內(nèi)容衣形,【警告次數(shù)】減一;沒有警告次數(shù)姿鸿,游戲失敗
- 如果用戶輸入了已經(jīng)輸入過的內(nèi)容谆吴,【警告次數(shù)】減一 ;
- 輔音:如果用戶輸入了輔音字母苛预,且沒有猜中句狼,【猜測機(jī)會(huì)】減1
- 元音:如果用戶輸入的原因字母,沒有猜過热某,且沒有猜中腻菇,【猜測機(jī)會(huì)】減2
E. 游戲終止條件
- 當(dāng)用戶猜對(duì)了所有字母 或者 次數(shù)消耗完時(shí)終止游戲
- 次數(shù)消耗完并且沒有完成字母,告訴用戶結(jié)果并顯示正確的字母昔馋。
- 用戶贏了的話筹吐,輸出恭喜信息,并顯示用戶分?jǐn)?shù)
- 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)