Skip to main content
 首页 » 编程设计

Python 幽灵机器人

2024年11月01日13bjzhanghao

与有关此主题的许多问题不同,我的问题不是家庭作业。我建了一个 Ghost有效的机器人。我的最终目标是建立一个扑克机器人作为一种爱好,但 Ghost 似乎是一个更容易考虑开始的游戏。

我的问题代码如下:

def computer_prompt(playerName,word_string): 
  length_string = len(word_string) 
  for possibilities in wordlist: 
    if possibilities[:length_string].lower() == word_string: 
      if len(possibilities) > 3 and len(possibilities) % 2 != 0: 
        if check_game.is_valid_word(possibilities[length_string],wordlist): 
          if not check_game.word_formed(possibilities[:length_string + 1],wordlist): 
            print(possibilities) 
            return possibilities[:length_string + 1] 

目前,我只希望计算机始终排在第二位,而人类始终排在第一位。问题是,虽然电脑几乎总是打败我,但有几次我仍然可以胜过他。比如我打h,他打a,我打z,他打a,我打r,然后他就报错(因为他不认输:)).

我怎样才能改变它,让他知道在这种情况下不要在我说“z”之后说“a”?显然我可以将这个例子编码为一个异常(exception),但我想知道这个问题的一般解决方案。一般来说,计算机现在打败了我,因为在决定选择哪个字母之前,他会查找所有可能以我结尾的单词列表。但在“危险”的例子中,他只是被卡住了,我想让他知道他会在前面几步被卡住,这样他就不会一开始就进入这个位置......

非常感谢!

9 月 27 日添加

对于任何有兴趣的人,下面的代码似乎比我以前的代码要好一些。仍然不完美,虽然......:

def all_possibilities(word_string, length_string): 
  possibilities = [] 
  for possibility in wordlist: 
      if possibility[:length_string].lower() == word_string: 
        possibilities.append(possibility) 
  return possibilities 
 
def clear_loser(possibilities): 
  clear_losers = [] 
  for item in possibilities: 
      if len(item) % 2 == 0: 
        clear_losers.append(item) 
  return clear_losers 
 
def first_n_letters(sub_optimal_computer_possibilities, length_string): 
  first_n_Letters = [] 
  for item in sub_optimal_computer_possibilities: 
    first_n_Letters.append(item[:length_string + 1]) 
  return list(set(first_n_Letters)) 
 
def existing_Optimal_Move(FIRSTNLETTERS,first_letters_of_clear_losers): 
  length_sub_opt_list = len(FIRSTNLETTERS) 
  new_list = [] 
  for item in FIRSTNLETTERS: 
    if not item in first_letters_of_clear_losers: 
      new_list.append(item) 
  return new_list 
 
def computer_prompt(word_string): 
  length_string = len(word_string) 
  possibilities = all_possibilities(word_string, length_string) 
  clear_losers = clear_loser(possibilities) #Create list of words that will end on computer 
  sub_optimal_computer_possibilities = [x for x in possibilities if x not in clear_losers] #Create list of words that will end on human (including words that might be suboptimal for me because smart human will make it end on me before getting to this word 
  FIRSTNLETTERS = first_n_letters(sub_optimal_computer_possibilities, length_string) 
  first_letters_of_clear_losers = first_n_letters(clear_losers, length_string) 
  optimalMove = existing_Optimal_Move(FIRSTNLETTERS, first_letters_of_clear_losers) 
  if optimalMove: 
    print("OPTIMAL MOVE") 
    for item in optimalMove: 
        #print(optimalMove) 
      return item[:length_string + 1]    
  else: 
    for item in FIRSTNLETTERS: 
        #print(FIRSTNLETTERS) 
      return item[:length_string + 1] 

请您参考如下方法:

查看三元搜索树数据结构: http://en.wikipedia.org/wiki/Ternary_search_tree

您可以从您的单词表构建一个三元搜索树。然后你可以让你的计算机循环遍历你在树中当前位置的 child 。他将消除任何失败的 Action (字母选择没有 child ),然后遍历所有 child 。如果计算机的任何可能移动都有所有失败的 child (他们自己没有 child ),那么他会选择那个选择,因为它保证获胜。

在循环过程中,他将消除任何可能导致失败的 Action 。如果他没有剩余的棋子,那意味着每一步都会让他输,所以他只会选择随机字母直到他输。否则,他将选择输的可能性最少的方法,或者获胜的方法最多的方法,可能是两者的线性组合,常数是通过实验确定的。您将需要巧妙的循环或递归函数。

最后,如果您希望他使用机器学习,您可能需要一个字典,例如 memory = {} 然后每次他玩和输了他都会将他的选择列表添加到内存并避免下次出现这种模式。他也可以通过这种方式调整常量。要保留内存,您应该将其保存到文件或使用 python 的 pickle 模块将其序列化。