背景

我正在尝试用python编写一个基本的字母游戏。在游戏中,计算机主持人从可能的单词列表中选择一个单词。每个播放器(计算机人工智能和人类)显示一系列空白,每个字对应一个空白。然后,每个玩家猜测一个字母和一个位置,并被告知以下内容之一:

那封信属于那个位置(最好的结果)那封信在字面上,但不是在那个位置上那封信不在剩下的空白处当这个词被完全揭穿后,猜对大多数字母的玩家将赢得一分。计算机主持人选择另一个词并重新开始。先得五分的选手获胜。在基本游戏中,两个玩家共享相同的空白区域,因此他们从彼此的工作中受益。

问题

我现在正在研究计算机的人工智能部分(代码的底部)。我想让它从一个尚未猜到的字母列表中选择一个随机字母。最好的方法是什么?

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)

每次猜到的字母是单词的一部分时,我需要弄清楚如何从列表中分类字母。也就是说,从列表开始为[abcdefghiklmnopqrstuvwxyz],如果用户猜测是"A",则列表将被修改为读取[a bcdefg…等]。然后我应该可以使用random.choice让人工智能从这个列表中选择它的猜测。

使用pythons集合,保留一个包含所有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)

隐马尔可夫模型。。。我遇到一个错误--"设置对象不支持索引"

你建议我在进行下去时如何更新可用的数字集?

是的,如果你想索引,你需要把它转换回一个列表。列表(myset)'

@约翰,什么可用数字集(你指的是什么变量?)…作为常规注释,可以使用".update"和".difference"作为就地操作在集合中添加或删除元素。

谢谢你的帮助@pyinsky。我知道你要用这种方法做什么,但我似乎不能再使用random.choice从列表中选择。

#选择时需要将集合强制转换为列表:>>>A=set(list('abcde'))>>>random.choice(list(a))'E'