python猜单词游戏_python中的字母/单词猜谜游戏

背景

我试图用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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值