2013-04-07 79 views
0

我正在完成我的第一个简单的程序在Python,一个2人(用户与电脑AI)字/字母猜谜游戏。蟒蛇循环,2人游戏

我已经完成了大部分的代码,但我试图让我的循环整理出来,以便游戏能够正确地在用户和AI之间切换。我想让游戏在用户和AI轮流之间来回切换,直到世界充分显现。此时,玩家正确猜测最多的字母会赢得一分。电脑版主挑选另一个字,然后重新开始。第一个到五点的玩家赢得比赛。

我不确定从哪里开始。我对python /编码一般还很陌生,而且我很难理解事件发生的顺序。我知道我需要某种主循环,只要这个词还没有完全显示出来,那么它仍然是真的,但这就是它。

此外,任何其他建议如何优化下面的代码或无论如何清理它,将不胜感激!

import random 


#set initial values 
player1points= 0 
ai= 0 
userCorrectLetters= '' 
aiCorrectLetters='' 
wrongLetters='' 
wrongPlace= '' 
correctLetters = '' 
notInWord = '' 
endGame = False 
allLetters = set(list('abcdefghijklmnopqrstuvwxyz')) 
alreadyGuessed = set() 
userGuessPosition = 0 
availLetters = allLetters.difference(alreadyGuessed) 


#import wordlist, create mask 
with open('wordlist.txt') as wordList: 
    secretWord = random.choice(wordList.readlines()).strip() 
print (secretWord) 
secretWordLength = len(secretWord) 








def displayGame(): 
    mask = '_' * len(secretWord) 
    for i in range (len(secretWord)): 
     if secretWord[i] in correctLetters: 
      mask = mask[:i] + secretWord[i] + mask [i+1:] 
    for letter in mask: 
     print (letter, end='') 
    print (' ') 
    print ('letters in word but not in correct location:', wrongPlace) 
    print ('letters not in word:', wrongLetters) 



    ##asks the user for a guess, assigns input to variable 

def getUserGuess(alreadyGuessed): 


    while True: 
     print ('enter your letter') 
     userGuess = input() 
     userGuess= userGuess.lower() 
     if len(userGuess) != 1: 
      print ('please enter only one letter') 
     elif userGuess in alreadyGuessed: 
      print ('that letter has already been guessed. try again') 
     elif userGuess not in 'abcdefjhijklmnopqrstuvwxyz': 
      print ('only letters are acceptable guesses. try again.') 
     else: 
      return userGuess 

def newGame(): 
    print ('yay. that was great. do you want to play again? answer yes or no.') 
    return input().lower().startswith('y') 


userTurn=True  
while userTurn == True: 
    displayGame() 
    print ('which character place would you like to guess. Enter number?') 
    userGuessPosition = int(input()) 

    slice1 = userGuessPosition - 1 


    ##player types in letter 
    guess = getUserGuess(wrongLetters + correctLetters) 
    if guess== (secretWord[slice1:userGuessPosition]): 
     correctLetters = correctLetters + guess 
     print ('you got it right! ') 
     displayGame() 
     break 
    elif guess in secretWord: 
      wrongPlace = wrongPlace + guess 
      print ('that letter is in the word, but not in that position') 
      displayGame() 
      break 
    else: 
      wrongLetters = wrongLetters + guess 
      print ('nope. that letter is not in the word') 
      displayGame() 
      break 

print ("it's the computers turn") 

aiTurn=True 

while aiTurn == True: 
    aiGuessPosition = random.randint(1, secretWordLength) 
    print (aiGuessPosition) 

    aiGuess=random.sample(availLetters, 1) 
    print ('the computer has guessed', aiGuess, "in position", + aiGuessPosition) 
    if str(aiGuess) == (secretWord[slice1:userGuessPosition]): 
      correctLetters = correctLetters + guess 
      print ('this letter is correct ') 
      break 
    elif str(aiGuess) in secretWord: 
      aiCorrectLetters = aiCorrectLetters + guess 
      correctLetters = correctLetters + guess 
      print ('that letter is in the word, but not in that position') 
      break 
    else: 
      wrongLetters = wrongLetters + guess 
      print ('that letter is not in the word') 
      break 


    displayGame() 
    break  

回答

1

看起来它只会做两个回合然后退出?

发生了什么事是它击中了你的第一个while循环,并开始评估里面的代码。它做了一些事情,然后点击break,这会让你跳出循环,并在第一次循环结束后恢复执行。然后它会对第二个循环做同样的事情,击中你的程序并退出。

我建议以下重构:

  1. 把所有的你有东西在你的while循环分为两个功能user_playcomputer_play
  2. 写一个循环像

usr_pts = 0 
cmp_pts = 0 

while (usr_pts < 5 and cmp_pts < 5): 
    solved_word = False 
    # set up word 
    user_turn = False 
    user_correct_guess = 0 
    ai_correct_guess = 0 
    while not solved_word: 
     user_turn = not user_turn 
     if user_turn: 
      guess = play_user(...) 
     else: 
      guess = computer_play(...) 
     # what ever accounting you need to do 
     is_guess_in_word = test_guess_in_word(guess, ...) 
     if is_guess_in_word: 
      if user_turn: 
       user_correct_guess += 1 
      else: 
       ai_correct_guess += 1 
     solved_word = sort_out_if_word_solved(...) 

    if user_correct_guess > ai_correct_guess: 
     usr_pts += 1 
    elif user_correct_guess < ai_correct_guess: 
     cmp_pts +=1 
    else: 
     # a tie 
     pass 
+0

currentl y,它只做两个回合,而不是两个回合(一轮是正确猜测的整个单词)。 感谢您的建议,我现在就试试。 – jamyn 2013-04-07 15:38:37

+0

@jamyn note我调整了一下逻辑 – tacaswell 2013-04-07 15:39:27

+0

忍受我在这里,我仍然是一个相当初学者的编码 - 在每个“回合”,计算机和AI应该轮流猜测,直到这个词被揭示。在哪一点,谁正确猜测了更多的信件赢得了一轮。赢得一轮比赛应该赢得一分。 从我可以告诉你所提供的代码,它看起来像电脑和AI只是轮流交替?或者我错过了什么? – jamyn 2013-04-07 17:29:55