正如标题所示,游戏运行良好,但似乎我有一个我无法弄清楚的错误。
在第一场比赛之后,当它询问你是否想再次参加比赛时,它会继续下一场比赛就好了,你可以打那场比赛,然后在那场比赛之后,它不会问如果你想再次玩,但是自动开始一个新的游戏,用刚刚发生在前一个游戏中的单词。
我无法弄清楚它是我的play_again函数还是我的check_win函数或者是什么。
感谢您的帮助
import random
def main():
hangman_word = generate_random()
while True:
play_game(hangman_word)
if play_again() == 2:
return False
break
else:
hangman_word = generate_random()
play_game(hangman_word)
############################################
# The game!
############################################
def play_game(random_word):
clear()
guesses = ""
tries = 9
while True:
winning_number = 0
hangmanInterface(tries)
print("WORD:", end="")
for char in random_word:
if char in guesses:
print(" {} ".format(char), end="")
else:
print(" _ ", end="")
winning_number += 1
if win_check(winning_number, tries) == True:
break
print_x(1)
print(" Tried:", end="")
for j in range(len(guesses)):
print("[{}]".format(guesses[j]), end="")
spacing_fix(2)
guess = get_guess()
guesses += guess
print_x(2)
if guess not in random_word:
tries -= 1
return
##########################################
##########################################
# generates a random word
##########################################
def generate_random():
#opens the dictionary and initializes a random word
with open("dict.txt") as fp:
dictwords = []
for line in fp:
dictwords.append(line.rstrip("\n"))
#makes sure the word is in lower case
rand = random.choice(dictwords)
randlower = rand.lower()
return randlower
##########################################
##########################################
# Checks if the game should restart
##########################################
def play_again():
print("Play again?")
print("1. yes!")
print("2. no :(")
ans = get_ans()
return ans
##########################################
def get_ans():
ans1 = input()
ans = int(ans1)
if ans == 1 or ans == 2:
return ans
else:
print("Please type 1 or 2")
get_ans()
##########################################
##########################################
# checks for the win
##########################################
def win_check(a, b):
if a == 0:
print_x(2)
print("######################")
print("# #")
print("# W I N N E R #")
print("# #")
print("######################")
return True
elif b == 0:
print_x(2)
print("######################")
print("# #")
print("# L O S E R #")
print("# #")
print("######################")
return True
##########################################
##########################################
# gets users guess while ensuring only
# one alpha, lowercase char is entered
##########################################
def get_guess():
get = input(" Guess: ")
a = get.lower()
if len(a) > 1:
print("One letter only")
get_guess()
elif not a.isalpha():
print("One letter only")
get_guess()
return a
##########################################
##########################################
# Aesthetic Functions
##########################################
def clear():
for i in range(25):
print ('\n')
#########################
def print_x(x):
for i in range(x):
print("\n")
#########################
def spacing_fix(tmp):
if tmp == 0:
tmp = 1
return tmp
if tmp == 2:
print()
tmp = 3
return tmp
##########################################
##########################################
# prints board state
##########################################
def hangmanInterface(index):
if index==0:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /|\ | ')
print(' / \ | ')
print(' | ')
print(' ________|_')
return
if index==1:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /|\ | ')
print(' / | ')
print(' | ')
print(' ________|_')
return
if index==2:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /|\ | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==3:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /| | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==4:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' | | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==5:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==6:
clear()
print(' _____ ')
print(' | | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==7:
clear()
print(' _____ ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==8:
clear()
print(' ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==9:
clear()
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ________|_')
return
##########################################
if __name__ == "__main__":
main()
答案 0 :(得分:0)
问题在于这个循环:
while True:
play_game(hangman_word)
if play_again() == 2:
return False
break
else:
hangman_word = generate_random()
play_game(hangman_word)
此循环以play_game(...)
开头和结尾,因此您可以让用户玩两个游戏,而不需要在两者之间输入。拨打play_game
就足够了。
答案 1 :(得分:0)
而不是:
1 3
2 2
3 1
a 123
b 321
c bca
试试这个:
def main():
hangman_word = generate_random()
while True:
play_game(hangman_word)
if play_again() == 2:
return False
break
else:
hangman_word = generate_random()
play_game(hangman_word)
你在原作中做了什么:
首次调用def main():
while True:
hangman_word = generate_random() ## get ONE word
play_game(hangman_word) ## play ONE game
if play_again() == 2: ## if user doesn't want to play anymore, quit
break ## you could also return False,
## just don't do both because only one will be called
时,您玩过零游戏。
在main()
正下方的第一个play_game(...)
,您正在玩第一场比赛(第一场比赛)。之后,如果while True:
为1,那么您正在玩第二个刽子手游戏(在play_again()
子句下)。在这两个游戏之后,你循环回到顶部并再次在else:
下玩游戏。
实际上,原作只能玩奇数游戏(1,3,5,7 ......)。