Python プログラムでの最初の実際の試み (文字推測ゲーム) の大部分を取得しました。
作業の大部分は完了しましたが、最後の部分で立ち往生しています
世界が完全に明らかになるまで、ゲームがユーザーと AI のターンを交互に繰り返すようにしたいと考えています。ここまでは元気です。この時点で、最も多くの文字を正しく推測したプレーヤーがポイントを獲得できるようにしたいと考えています。コンピューターの司会者は別の単語を選び、最初からやり直します。最初に 5 ポイントを獲得したプレイヤーがゲームに勝ちます。
ユーザーと AI のターンを交互に繰り返す while ループがありますが、単語が完全に公開された後、適切に中断することはできませんか? その後、userCorrectLetters の数と aiCorrectLetters の数を比較し、それを使用してラウンドのポイントを獲得する人を決定するのは非常に簡単です。
次に、プレイヤーの 1 人が 5 ポイントに達するまで中断しない while ループ内にすべてを入れる必要があると想定します。
私が問題を抱えているもう1つのことは、ユーザーがすでに解決されているキャラクターの位置を再推測することを禁止する方法です。
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