Python Ghost Bot

时间:2013-09-26 18:04:35

标签: python machine-learning artificial-intelligence

与此主题的许多问题不同,我的不是家庭作业。我建立了一个可行的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]

1 个答案:

答案 0 :(得分:2)

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

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

虽然循环通过他将消除任何保证损失的动作。如果他没有剩下的动作,那意味着每一个动作都会导致他失败,所以他只会选择随机字母,直到他输了。否则他将选择他可能失去的最少可能的移动,或者他可以赢得的最多方式,可能是两者的线性组合,通过实验确定的常数。你需要聪明的循环或递归函数。

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