When there's a word that has a letter more than once, if that letter is already confirmed in the right spot, but you tell it the next instance of that letter is wrong, it crashes. You can trick it into working by saying that it's just in the wrong spot.
from english_words import get_english_words_set
web2lowerset = get_english_words_set(['web2'], lower=True)
import random
from os import system, name
def clear_screen():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
board = [
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
]
startingWords = ["SOARE", "SAREE", "SEARE", "STARE", "ROATE"]
def print_board():
clear_screen()
for r in board:
print (*r)
def validInput(w):
if len(w) != 5:
return False
else:
return True
def solver():
fiveLetterWords = set()
for word in web2lowerset:
if len(word) == 5:
fiveLetterWords.add(word.upper())
initialInput = input("Would you like me to provide a starting word?")
startingWord = 0
if "y" in initialInput.lower():
startingWord = random.choice(startingWords)
print (startingWord)
elif "n" in initialInput.lower():
while True:
userWord = input("What was your starting word?")
if validInput(userWord) == True:
startingWord = userWord.upper()
break
else:
print ("Invalid word length")
print (startingWord)
else:
print("Invalid answer")
board[0][0] = startingWord[0]
board[0][1] = startingWord[1]
board[0][2] = startingWord[2]
board[0][3] = startingWord[3]
board[0][4] = startingWord[4]
moveCount = 0
currentWord = [*startingWord]
while True:
print_board()
print("Enter 'G' to generate a new word \nUse Y to represent present letters in the right spot, \nN to represent absent letters, \nand W to represent present letters in the wrong spot")
print("E.G. NNYWW")
results = input()
currentResult = [*results]
resultIndex = 0
tempSet = set()
#chatGPT suggested loop
tempWordSet = fiveLetterWords.copy()
resultIndex = 0
for result in currentResult:
if result == "N":
tempWordSet = {word for word in tempWordSet if currentWord[resultIndex] not in word}
elif result == "Y":
tempWordSet = {wordY for wordY in tempWordSet if currentWord[resultIndex] == wordY[resultIndex]}
elif result == "W":
tempWordSet = {wordW for wordW in tempWordSet if currentWord[resultIndex] != wordW[resultIndex] and currentWord[resultIndex] in wordW}
elif result == "G":
currentWord = [*fiveLetterWords.pop()]
moveCount -= 1
resultIndex += 1
if resultIndex == 5:
resultIndex = 0
moveCount += 1
# print (fiveLetterWords)
# fiveLetterWords.clear()
fiveLetterWords = tempWordSet
currentWord = [*fiveLetterWords.pop()]
board[moveCount][0] = currentWord[0]
board[moveCount][1] = currentWord[1]
board[moveCount][2] = currentWord[2]
board[moveCount][3] = currentWord[3]
board[moveCount][4] = currentWord[4]
while True:
solver()
I tried making it to where the index of the letter within the string has to match with the index of the user input, but that comes with it's own problem, namely it will keep trying that letter in different spots.