2013-04-09 229 views
1

我得到了我第一次真正嘗試在Python程序 - 一個字母猜謎遊戲的大部分。python字母猜謎遊戲

我有大頭所做的工作,但我卡在最後一點點

我想要讓這個遊戲交替來回用戶和AI之間轉動,直到世界已經充分顯現。我很好,直到這裏。在這一點上,我想要讓玩家正確地猜出最多的字母贏得一分。電腦版主挑選另一個字,然後重新開始。第一個到五點的玩家贏得比賽。

我有一個while循環在用戶/ AI輪流之間交替,但是一旦這個詞已經完全公開,我無法讓它正常打破?之後,只需比較userCorrectLetters的數量和aiCorrectLetters的數量並使用它來確定誰贏得該回合的點數就相當簡單。

然後我假設整個事情應該在一個while循環中進行,直到其中一個玩家達到5分纔會休息。

我遇到的另一個問題是如何禁止用戶重新猜測已經解決的字符位置。

import random 


#set initial values 
player1points= 0 
ai= 0 
userCorrectLetters= [] 
aiCorrectLetters=[] 
wrongLetters=[] 
wrongPlace= [] 
correctLetters = [] 
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') 

def userTurn(wrongLetters, wrongPlace, correctLetters): 
    print ('\n') 

    displayGame() 
    print ('which character place would you like to guess. Enter number?') 
    userGuessPosition = input() 
    if userGuessPosition not in ('123456789'): 
     print ('please enter a NUMBER') 
     userGuessPosition = input() 
    slice1 = int(userGuessPosition) - 1 


    ##player types in letter 
    guess = getUserGuess(wrongLetters + correctLetters) 
    if guess== (secretWord[slice1:int(userGuessPosition)]): 
     print ('you got it right! ') 
     correctLetters.append(guess) 
     userCorrectLetters.append(guess) 
     displayGame() 

    elif guess in secretWord: 
      wrongPlace.append(guess) 
      print ('that letter is in the word, but not in that position') 
      displayGame() 

    else: 
      wrongLetters.append(guess) 
      print ('nope. that letter is not in the word') 
      displayGame() 




def aiTurn(wrongLetters,wrongPlace, correctLetters): 
    print ('\n') 
    print ("it's the computers turn") 

    aiGuessPosition = random.randint(1, secretWordLength) 

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

    else: 
      wrongLetters.append(aiGuess) 
      print ('that letter is not in the word') 
      return 



wordSolved = False 
while wordSolved == False: 

    userTurn(wrongLetters, wrongPlace, correctLetters) 
    aiTurn(wrongLetters, wrongPlace, correctLetters) 
    if str(correctLetters) in secretWord: 
     break 
+0

是你要爲worldSolved爲真? – Gareth 2013-04-09 18:51:51

+0

剛試過,仍然沒有正確地打破。我認爲這是因爲str(correctLetters)和secretWord之間的比較存在問題? – jamyn 2013-04-09 18:55:49

+0

你想在'secretWord'的'str(correctLetters)中做什麼? – 2013-04-09 18:59:49

回答

2

的問題是在這裏:

if str(correctLetters) in secretWord: 

你可能想到的是str(['a', 'b', 'c'])返回 'ABC',但事實並非如此。它返回"['a', 'b', 'c']"。 您應該替換該行:

if "".join(correctLetters) in secretWord: 

還有更多的一個問題你的代碼,除了這一個: 假設正確的詞是foobar。如果用戶猜測前5個字母,但是按相反順序,correctLetters將爲['a', 'b', 'o', 'o', 'f'],並且行if "".join(correctLetters) in secretWord:將評估爲False,因爲'aboof'不在'foobar'中。

您也可以用替換if "".join(correctLetters) in secretWord:解決這個問題:

if len(correctLetters) > 4: 

基本上,用戶猜測正確的5封,這將盡快結束程序的執行。沒有必要檢查這些字母是否在secretWord中,因爲您已在userTurn函數中執行該操作。

0

您正在比較列表correctLetters的字符串表示形式與字符串secretWord。例如:

>>> secretWord = 'ab' 
>>> correctLetters = ['a','b'] 
>>> str(correctLetters) 
"['a', 'b']" 
>>> str(correctLetters) in secretWord 
False 

嘗試做出比較正確的字母來加密詞的字符串:

>>> ''.join(correctLetters) == secretWord 
True