我是Python的新手(也是在Stack Overflow上的新手,这是我第一次提问),我自学了几周。我在做一些初级项目时,决定制作一个吊人游戏的AI。
#importingimport randomimport timeimport sysfrom collections import Counter#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#defining some variableslist_of_words = open("dictionary.txt", "r")list_of_words = list_of_words.read().split()SYMBOL = "abcdefghijklmnopqrstuvwxyz"#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#main game loopdef main(): while True: print("\nGenerating word...") word = list_of_words[random.randint(0, len(list_of_words) - 1)].lower() word_guess = [] wrong_attempts = 0 wrong_letters = [] game_state = True for symbol in word: if symbol in SYMBOL: word_guess.append("_") else: word_guess.append(symbol) word_show = " ".join(word_guess) word = list(word) while game_state != False: print("\n" + word_show) print("\nWrong attempts [{0}/5]" .format(wrong_attempts)) if len(wrong_letters) > 0: print("\nLetters guessed [{0}]" .format(", ".join(wrong_letters))) letter = "-" while letter not in SYMBOL or letter == "" or len(letter) > 1: try: letter = input("\nGuess a letter or enter 0 to call the ai: ") except: print("\nUnexpected error ocurred, try again") if letter == "0": correct_letters = [letter for letter in word_guess if letter in SYMBOL] letter = ai_solver(wrong_letters, word_guess) elif letter in wrong_letters or letter in word_guess: print("\nYou already guessed letter [{0}]" .format(letter)) letter = "" if letter in word: for i in range(len(word)): if letter == word[i]: word_guess[i] = letter else: wrong_letters.append(letter) wrong_attempts += 1 word_show = " ".join(word_guess) if "".join(word_guess) == "".join(word): print("\nYou won!") game_state = False elif wrong_attempts == 5: print("\nYou lost!") print("The word was [{0}]" .format("".join(word))) game_state = False option = input("\nWant to play again?[Y/N]: ") if option.lower().startswith("n"): sys.exit(0)def ai_solver(letters_attempted, word_guess): letters_attempted = letters_attempted if len(letters_attempted) != 0 else "" available_words = [] for word in list_of_words: append = False if len(word) == len(word_guess): append = True for i in range(len(word_guess)): if word[i] in letters_attempted: append = False break if word_guess[i] != "_": if word[i] != word_guess[i]: append = False break if append == True: print("[{0}]" .format(word)) available_words.append(word) common_letters = [letter for letter in "".join(available_words) if letter not in word_guess] common_letters = Counter("".join(common_letters)).most_common(1) return common_letters[0][0]main()
我尝试做的就是,筛选出所有与word_guess
长度相同的可能单词。然后通过检查letters_attempted
来筛选掉包含错误猜测字母的单词。
然后它会筛选掉所有与word_guess
不匹配的单词的字母。
if word_guess[i] != "_": if word[i] != word_guess[i]: append = False break
虽然它运行得很好,但有时候会输,我可以添加什么来增加获胜的几率?谢谢!
回答:
你的两个筛选步骤是一个很好的开始。有几种不同的步骤你可以尝试来改进。让我们把到目前为止符合条件的单词称为候选单词。
第一步是分析所有候选单词,并找出哪个字母在候选单词中出现得最频繁。(不重复计算重复的字母。)那个字母将是一个很好的下一个猜测。
一个稍微复杂的方法是看猜测的信息增益。也就是说,可能有一半的候选单词包含’s’,但所有这些单词都以’s’结尾。可能有稍微少一些的候选单词包含’t’,但’t’可以出现在单词的任何位置。所以,当你猜’t’时,你实际上获得了更多关于单词可能是什么的信息,因为当你正确猜测时,你会看到’t’的位置。特别是当你没有足够的猜测来弄清楚每个单词时,这样的策略可能会帮助你在你有的猜测中弄清楚更多的单词。