背景
我正在尝试在python中编写一个基本的字母游戏。在游戏中,计算机主持人从可能的单词列表中选择一个单词。每个玩家(计算机AI和人类)都会显示一系列空白,每个字母对应一个字母。然后每个玩家猜出一个字母和一个位置,然后告诉其中一个:
那封信属于那个位置(最好的结果) 那封信是在这个词中,但不在那个位置 那封信不在任何剩余的空白处 当单词被完全显示时,正确猜出大多数字母的玩家将赢得一个点。计算机主持人选择另一个单词并重新开始。第一个获得5分的玩家赢得了比赛。在基本游戏中,两个玩家共享他们正在填充的同一组空白,因此玩家可以从彼此的工作中受益。
问题
我正在处理计算机AI部分(代码底部)。我希望它从尚未猜到的字母列表中选择一个随机字母。最好的方法是什么?
import random
#set initial values
player1points= 0
ai= 0
userCorrectLetters= ''
aiCorrectLetters=''
wrongPlace=''
wrongLetters=''
correctLetters = ''
notInWord = ''
endGame = False
alreadyGuessed = 'a'
userGuessPosition = 0
###import wordlist, create mask
with open('/Users/jamyn/Documents/workspace/Lab3/Lab3/wordlist.txt') as wordList:
secretWord = random.choice(wordList.readlines()).strip()
print (secretWord)
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 ()
def addAlreadyGuessed():
alreadyGuessed= userCorrectLetters + aiCorrectLetters + wrongLetters + correctLetters
def displayGame():
print ('letters are in word but not in correct location:', wrongPlace)
print ('letters not in word:', notInWord)
##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:
print ('which character place would you like to guess. Enter number?')
userGuessPosition = int(input())
slice1 = userGuessPosition - 1
print (secretWord)
##player types in letter
guess = getUserGuess(wrongLetters + correctLetters)
if guess== (secretWord[slice1:userGuessPosition]):
correctLetters = correctLetters + guess
print ('you got it right! ')
break
elif guess in secretWord:
userCorrectLetters = userCorrectLetters + guess
correctLetters = correctLetters + guess
print ('that letter is in the word, but not in that position')
break
else:
wrongLetters = wrongLetters + guess
print ('nope. that letter is not in the word')
break
print ('its the computers turn')
aiTurn=True
while aiTurn == True:
aiGuess=random.choice('abcdefghijklmnopqrstuvwxyz')
print (aiGuess)
答案 0 :(得分:5)
使用pythons set,保留一个包含所有26个字母的集合,以及一组猜测的集合,并且只询问大集合中不在较大集合http://docs.python.org/2/library/sets.html中的元素...然后拉你从该结果中随机选择
allletters = set(list('abcdefghijklmnopqrstuvwxyz'))
usedletters = set() # update this as you go
availletters = allletters.difference(usedletters) #s - t new set with elements in s but not in t
很好地打印出来,你可以做到
print sorted(availletters)
或
print ', '.join(sorted(availletters))
回答关于添加猜测的后续内容,这是一个简单的例子
allletters = set(list('abcdefghijklmnopqrstuvwxyz'))
usedletters = set() # update this as you go
while( len(usedletters) != len(allletters) ):
guessedletter = raw_input("pick a letter")
availletters = allletters.difference(usedletters)
usedletters.update(guessedletter)
你也可以只有一个列表并减去他们猜到的字母,例如:
allletters = set(list('abcdefghijklmnopqrstuvwxyz'))
while( len(usedletters) != len(allletters) ):
guessedletter = raw_input("pick a letter")
allletters.difference_update(guessedletter)