Wordle求解器Python脚本重复字母崩溃问题技术问询
问题解决:Wordle助手处理重复字母时的崩溃问题
问题描述
当猜词存在重复字母时,若其中一个字母实例被标记为正确位置(Y),但另一个相同字母实例被标记为不存在(N),脚本会崩溃。例如目标词是APPLE,猜词为APPLY,此时前两个P是正确位置(Y),第三个P标记为N(因为目标词中该位置没有P,且已确认存在两个P),原脚本会直接移除所有含P的单词,导致候选词集合为空,最终调用pop()时触发错误。
原代码问题分析
原代码处理N结果的逻辑存在缺陷:
tempWordSet = {word for word in tempWordSet if currentWord[resultIndex] not in word}
该逻辑直接移除所有包含当前字母的单词,完全忽略了一种关键场景:当同一个字母已经有Y或W标记时,说明目标词中确实存在该字母,只是当前这个实例是多余的。此时应该保留那些包含该字母、但出现次数不超过已确认数量的单词,而非一刀切移除所有含该字母的单词。
修复方案
通过分阶段处理结果标记,结合字母出现次数统计,实现精准的候选词过滤:
- 先处理
Y标记,锁定单词的正确位置,确保候选词必须满足对应位置的字母要求 - 处理
W标记,收集需要存在但位置错误的字母,过滤掉不包含该字母或字母位置正确的单词 - 处理
N标记时,分两种情况:- 若该字母无
Y/W标记:移除所有含该字母的单词 - 若该字母已有
Y/W标记:移除那些该字母出现次数超过已确认数量的单词
- 若该字母无
修复后的完整代码
from english_words import get_english_words_set web2lowerset = get_english_words_set(['web2'], lower=True) import random from os import system, name from collections import Counter 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 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 = None 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): startingWord = userWord.upper() break else: print("Invalid word length") print(startingWord) else: print("Invalid answer") return # 初始化第一行棋盘 for i in range(5): board[0][i] = startingWord[i] moveCount = 0 currentWord = list(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().strip().upper() # 输入合法性检查 if len(results) != 5 and results != "G": print("Invalid input, please enter 5 characters or 'G'") continue # 处理生成新单词的请求 if results == "G": if not fiveLetterWords: print("No more words available!") break currentWord = list(fiveLetterWords.pop()) moveCount -= 1 for i in range(5): board[moveCount+1][i] = currentWord[i] continue currentResult = list(results) tempWordSet = fiveLetterWords.copy() # 统计已确认的字母信息 correct_positions = {} # 存储正确位置的字母:{索引: 字母} present_letter_counts = Counter() # 存储已确认存在的字母及次数 # 第一步:过滤正确位置(Y标记) for idx, res in enumerate(currentResult): if res == "Y": char = currentWord[idx] correct_positions[idx] = char present_letter_counts[char] += 1 # 保留所有在对应位置有正确字母的单词 tempWordSet = {word for word in tempWordSet if all(word[idx] == char for idx, char in correct_positions.items())} # 第二步:过滤存在但位置错误(W标记) for idx, res in enumerate(currentResult): if res == "W": char = currentWord[idx] present_letter_counts[char] += 1 # 保留包含该字母但不在当前位置的单词 tempWordSet = {word for word in tempWordSet if char in word and word[idx] != char} # 第三步:过滤不存在的字母(N标记) for idx, res in enumerate(currentResult): if res == "N": char = currentWord[idx] if present_letter_counts[char] > 0: # 目标词中该字母的数量等于已确认的次数,过滤掉数量不符的单词 required_count = present_letter_counts[char] tempWordSet = {word for word in tempWordSet if Counter(word)[char] == required_count} else: # 该字母完全不存在,移除所有含该字母的单词 tempWordSet = {word for word in tempWordSet if char not in word} # 更新候选词集合 fiveLetterWords = tempWordSet if not fiveLetterWords: print("No possible words left!") break moveCount += 1 currentWord = list(fiveLetterWords.pop()) # 更新棋盘 for i in range(5): board[moveCount][i] = currentWord[i] while True: solver()
修复说明
- 引入
collections.Counter简化字母出现次数的统计,更高效处理重复字母场景 - 分阶段处理标记,优先锁定正确位置,再处理存在性,最后处理不存在的情况,逻辑更清晰
- 针对重复字母的N标记做特殊处理,避免错误移除符合条件的目标词
- 增加输入合法性检查和无候选词时的提示,提升脚本稳定性和用户体验
内容的提问来源于stack exchange,提问作者Cason Berry
相关产品推荐
相关产品推荐

