我得到了我第一次真正尝试python程序的大部分——一个字母猜测游戏。在
我已经完成了大部分的工作,但最后一点我还没完成
我想让它让游戏在用户和人工智能之间来回交替,直到世界被完全揭示。到目前为止我还不错。在这一点上,我想让玩家猜对最多的字母就能赢得一分。计算机管理员挑选另一个单词,然后重新开始。第一个得五分的人获胜。在
我有一个while循环,在用户/人工智能之间交替,但我不能让它在这个词完全公开后正常中断?之后,只需比较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
您正在比较列表
correctLetters
的字符串表示形式与字符串secretWord
。例如:尝试将由正确字母组成的字符串与机密单词进行比较:
^{pr2}$问题在于:
您可能希望
^{pr2}$str(['a', 'b', 'c'])
返回'abc',但它没有。它返回"['a', 'b', 'c']"
。 应将该行替换为:您的代码还有一个问题,除了这个问题: 假设正确的单词是
foobar
。如果用户猜到前5个字母,但顺序相反,correctLetters
将是['a', 'b', 'o', 'o', 'f']
,而if "".join(correctLetters) in secretWord:
将计算为False
,因为'aboof'
不在'foobar'
中。在您可以通过将
if "".join(correctLetters) in secretWord:
替换为:基本上,只要用户猜出5个正确的字母,程序的执行就会结束。不需要检查字母是否在
secretWord
中,因为已经在userTurn
函数中进行了检查。在相关问题 更多 >
编程相关推荐