2

与有关该主题的许多问题不同,我的不是作业。我构建了一个可以工作的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”?显然我可以将此示例编码为异常,但我想知道这个问题的一般解决方案。一般来说,计算机现在打败了我,因为它会在决定选择哪个字母之前查找所有可能以我结尾的单词列表。但是在“危险”示例中,他只是被卡住了,我想让他知道他会在前几步被卡住,这样他就不会一开始就处于这个位置......

非常感谢提前!

添加于 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]
4

1 回答 1

2

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

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

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

最后,如果你想让他使用机器学习,你可能想要一本字典,比如memory = {}每次他玩和输的时候,他都会将他的选择列表添加到记忆中,并在下次避免这种模式。他也可以通过这种方式调整常数。要保留内存,您应该将其保存到文件中或使用 python 的 pickle 模块对其进行序列化。

于 2013-09-26T19:18:50.143 回答