该程序的后两个规范出现问题。
1)格瑟(Guesser)最初有6个未中(第7次进攻他们被淘汰)。如果他们赢得了这场比赛,他们将再次比赛,但只有5次未中,依此类推。当他们未能以指定的猜测数猜出单词或直到赢得0次未中的游戏时,游戏即告结束。
-我已经尝试过,但是我不知道如何让其他函数中的所有数字反映新的变化。我想保留重播选项,以便他们可以随时退出。
2)在每个猜测之前,显示尚未被猜测的字母列表。
-我知道这比较简单,应该知道,但是我认为我已经动了脑筋。
我还有文档要做,对于任何错误我深表歉意。任何清理总是值得赞赏的。预先感谢。
import random
import string
word_list = ["no", "hi", "bee", "car", "seat", "bear", "see", "chip"]
available_letters = string.ascii_lowercase # pop guessed letter from here
used_letters = [] # Add it to here, and display avaiable_letters each time a letter is guessed.
missed_letters = ""
correct_letters = ""
secret_word = random.choice(word_list).lower()
def get_random(word_list):
secret_word = random.choice(word_list).lower()
return secret_word
def display_board(missed_letters, correct_letters, secret_word):
print("Current Score:")
for letter in missed_letters:
print(letter)
print()
blanks = '-' * len(secret_word)
for i in range(len(secret_word)):
if secret_word[i] in correct_letters:
blanks = blanks[:i] + secret_word[i] + blanks[i+1:]
for letter in blanks:
print(letter)
print()
def get_player_guess(guessed):
while True:
guess = input("Enter a letter: ") # try guess = input(blah blah).lower()
guess = guess.lower()
if len(guess) != 1:
print("1 Letter at a time!")
elif guess in guessed:
print("Whoops, you already guessed that one!")
elif guess not in "abcdefghijklomnopqrstuvwxyz":
print("Letters only please!")
else:
return guess
def replay_game():
replay = input("Do you want to play again? y or n ")
if replay == "y":
play_game(missed_letters, correct_letters, secret_word)
else:
print("Bye!")
def play_game(missed_letters, correct_letters, secret_word):
count = 0
chance = 7
game_over = False
print("Welcome To Hangman.")
while True:
display_board(missed_letters, correct_letters, secret_word)
guess = get_player_guess(missed_letters + correct_letters)
if guess in secret_word:
correct_letters = correct_letters + guess
done = True
for i in range(len(secret_word)):
if secret_word[i] not in correct_letters:
done = False
break
if done:
print("You win!")
game_over = True
else:
missed_letters = missed_letters + guess
count += 1
chance -= 1
if count == 1:
print("You've got " + str(count) + " Strike. You have " + str(chance) + " Chances left")
elif count > 1 and count < 6:
print("You've got " + str(count) + " Strikes. You have " + str(chance) + " Chances left")
elif count == 6 and chance == 1:
print("You've got " + str(count) + " Strikes. You have " + str(chance) + " Chance left")
if len(missed_letters) == 7:
display_board(missed_letters, correct_letters, secret_word)
print("Sorry! You've run out of guesses! The right word was " + secret_word + "!")
game_over = True
if game_over:
if replay_game():
missed_letters = ""
correct_letters = ""
secret_word = get_random(word_list)
else:
break
play_game(missed_letters, correct_letters, secret_word)
答案 0 :(得分:1)
目前,您不会更改replay_game时的单词,因为永远不会调用结尾if replay_game
之后的文本。
对于第一个问题,我会将游戏的总机会记录为play_game的输入变量,并将replay_game更改为仅返回true或false并将其他代码移至play_game
def replay_game():
replay = input("Do you want to play again? y or n ")
# Changed to just return true or false depending on input.
if replay == "y":
return True
else:
print("Bye!")
return False
然后将play_game的开始更改为
def play_game(missed_letters, correct_letters, secret_word, total_chances=7):
count = 0
# chance is now variable
chance = total_chances
game_over = False
并在play_game
的末尾替换:
if len(missed_letters) == 7:
使用
if chance == 0:
并将呼叫if game_over
更改为
if game_over:
if replay_game():
missed_letters = ""
correct_letters = ""
secret_word = get_random(word_list)
play_game(missed_letters, correct_letters, secret_word, total_chances - 1)
else:
break
这将意味着您正在重置字母的代码将立即被调用,并且每个重新启动的游戏开始的机会都比前一个少。
您必须添加一些处理,以防止他们获胜而不允许错过。
对于第二个问题,只需将以下内容添加到显示板中即可:
print("Available letters:")
print(','.join(sorted(set(available_letters) - set(missed_letters) - set(correct_letters))))
这将三组字母中的每组组成一组,然后除去已经猜出的字母,然后将它们按一行显示,按字母顺序排序并以逗号分隔。
答案 1 :(得分:1)
关于函数display_board,我会稍稍清理一下代码,以便在正确猜中的情况下用正确的字母替换空格中的'_':
for i in range(len(secret_word)):
if secret_word[i] in correct_letters:
blanks[i] = secret_word[i]
看起来更干净,并且空白元素的重新分配更少。