世界求解器python脚本.重复字母问题



当一个单词不止一次出现一个字母时,如果这个字母已经在正确的位置被确认,但你告诉它这个字母的下一个实例是错误的,它就会崩溃。你可以欺骗它,说它只是在错误的位置。

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()

我试图使字符串中字母的索引必须与用户输入的索引匹配,但这有它自己的问题,即它将继续在不同的位置尝试该字母。

这看起来像一个非常有趣的项目!

代码中的问题似乎在currentResult"中的"for result中。循环。当一个字母不在单词(N)中时,代码将从tempWordSet中删除包含该字母的每个单词。这导致了一些问题,因为如果同一个字母在单词的另一个位置被标记为Y,那么这个字母应该被标记为N。为了纠正这一点,我将决定单词是否包含字母的循环分开。循环应该看起来更像这样(如果我正确理解了G输入):

if "G" in currentResult:
currentWord = [*fiveLetterWords.pop()]
moveCount -= 1
else:
for i in range(0, 5):
if currentResult[i] == "Y":
tempWordSet = {wordY for wordY in tempWordSet if currentWord[i] == wordY[i]}
elif currentResult[i] == "W":
tempWordSet = {wordW for wordW in tempWordSet if currentWord[i] != wordW[i] and currentWord[i] in wordW}
for i in range(0, 5):
if currentResult[i] == "N":
if (currentWord.count(currentWord[i]) > 1):
tempWordSet = {word for word in tempWordSet if word.count(currentWord[i]) == currentWord.count(currentWord[i])-1}
else:
tempWordSet = {word for word in tempWordSet if currentWord[i] not in word}

也用于其他一般改进:

  • 我建议使用更好的单词库。使用世界通用的"银行"这个词会更实际一些。

  • validInput函数可以通过

    缩短为一行
    return len(w) == 5
    
  • 与其从库中随机抓取满足所需条件的单词,不如像本文中那样使用一些数学策略。

编码快乐!

我让它按照我想要的方式工作。我还有一个方法,它不会随机给你一个符合条件的单词。它是一个。csv,包含最常用的英语单词,以及相关的数字。当你输入"G"时,它还会从wordle_words.txt中弹出任何无效的单词。(即使我拉的列表应该是直接从游戏)。

我相信有更好、更紧凑的方法来处理某些逻辑,但它运行得很快,几乎每次都能解决难题。

import random
from os import system, name
import os
import csv
with open('commonFiveLetter.csv') as c:
common=[tuple(line) for line in csv.reader(c)]
c.close()
original_file = "wordle_words.txt"
temp_file = "temp.txt"
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):
return len(w) == 5

def convert(s):
new = ""
for x in s:
new += x
return new

def solver():
fiveLetterWords = set(open('wordle_words.txt').read().upper().split())
initialInput = input("Would you like me to provide a starting word?")
startingWord = 0
if "y" in initialInput.lower():
startingWord = random.choice(startingWords)
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")
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")
results = input().upper()
print("E.G. NNYWW")
currentResult = [*results]
tempWordSet = fiveLetterWords.copy()
contains = []
containsIndex = []
wrongSpot = []
wrongSpotIndex = []
absent = []
sortaAbsent = []
sortaAbsentIndex = []
if "G" in currentResult:
string_to_delete = [convert(currentWord)]
with open(original_file, "r") as inputT:
with open(temp_file, "w") as output:
for line in inputT:
for wordT in string_to_delete:
line = line.replace(wordT, "")
output.write(line)
os.replace('temp.txt', 'wordle_words.txt')
currentWord = [*fiveLetterWords.pop()]
moveCount -= 1
else:
for j in range(0, 5):
if currentResult[j] == "Y":
contains.append(currentWord[j])
containsIndex.append(j)
for i in range(0, 5):
if currentResult[i] == "W":
wrongSpot.append(currentWord[i])
wrongSpotIndex.append(i)
elif currentResult[i] == "N" and (currentWord[i] in contains or currentWord[i] in wrongSpot):
sortaAbsent.append(currentWord[i])
sortaAbsentIndex.append(i)
elif currentResult[i] == "N" and (currentWord[i] not in contains and currentWord[i] not in wrongSpot):
absent.append(currentWord[i])
if contains:
for lettersY in range(len(contains)):
tempWordSet = {wordY for wordY in tempWordSet if contains[lettersY] in wordY and contains[lettersY] == wordY[int(containsIndex[lettersY])]}
if wrongSpot:
for lettersW in range(len(wrongSpot)):
if wrongSpot[lettersW] in contains:
tempWordSet = {wordW for wordW in tempWordSet if wrongSpot[lettersW] in wordW and wrongSpot[lettersW] != wordW[int(wrongSpotIndex[lettersW])] and wordW.count(wrongSpot[lettersW]) > 1}
else:
tempWordSet = {wordW for wordW in tempWordSet if wrongSpot[lettersW] in wordW and wrongSpot[lettersW] != wordW[int(wrongSpotIndex[lettersW])]}
if sortaAbsent:
for lettersSN in range(len(sortaAbsent)):
tempWordSet = {wordSN for wordSN in tempWordSet if sortaAbsent[lettersSN] in wordSN and sortaAbsent[lettersSN] != wordSN[int(sortaAbsentIndex[lettersSN])]}           
if absent:
for lettersN in range(len(absent)):
tempWordSet = {wordN for wordN in tempWordSet if absent[lettersN] not in wordN}
moveCount += 1
fiveLetterWords = tempWordSet
tempIntersect = []
for pair in common:
if pair[0].upper() in fiveLetterWords:
tempIntersect.append(pair)
if tempIntersect:
highest = 0
highestWord = 0
for pairI in tempIntersect:
if int(pairI[1]) > int(highest):
highest = pairI[1]
highestWord = pairI[0].upper()
currentWord = [*highestWord]
else:
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()

最新更新