2017-07-02 4 views
0

タイトルが示唆しているように、ゲームはうまく動作しますが、私は把握できないバグを持っているようです。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) 

これは、開始をループし、ユーザーがその間の入力を求めずに、2つのゲームをプレイしますので、play_game(...)で終わります。 play_gameへの1回の呼び出しで十分です。これに代えて

+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:の直下に、あなたは最初のゲームをしています(ゲーム1)。その後、play_again()が1の場合は、(else:句の下で)2番目のハングマンゲームをプレイしています。両方のゲームの後、再びループバックしてwhile True:の下でゲームをプレイします。

実際には、オリジナルは奇数のゲーム(1,3,5,7、...)しか再生できません。

+0

ありがとう私はそれを逃した方法を知らない!あなたの助けに感謝 – martyworm

関連する問題