2017-07-02 69 views
0

正如標題所示,遊戲運行正常,但似乎我有一個我無法弄清楚的錯誤。Hang子手遊戲在Python中 - 我的休息似乎只在其他時間玩遊戲

第一場比賽結束後,當它詢問你是否想再次參加比賽時,它會繼續進行下一場比賽,你可以參加比賽,最後在比賽結束後,它不問你是否想要重新玩,但自動開始一個新的遊戲,用剛剛在前一個遊戲中出現的單詞。

我找不出它是我的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就足夠了。

+0

謝謝我不知道我是如何錯過了!感謝您的幫助 – martyworm

0

取而代之的是:

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:下,你正在玩你的第一場比賽(第一場比賽)。之後,如果play_again()爲1,則表示正在玩第二個hang子手遊戲(在else:子句下)。在這兩場比賽之後,您將循環回到頂端並再次在while True:之下玩遊戲。

實際上,原始遊戲只能玩奇數遊戲(1,3,5,7,...)。

+0

謝謝我不知道我是如何錯過的!感謝你的幫助 – martyworm