Python 幽灵机器人

标签 python machine-learning artificial-intelligence

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

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

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

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

关于Python 幽灵机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19035459/

相关文章:

python - Flask-登录。我在哪里存储用户以便 user_loader 函数找到他们?

computer-science - 进化算法: Optimal Repopulation Breakdowns

neural-network - 通过神经网络进行时间序列预测

python - 我可以打印从多字符串正则表达式搜索中匹配的字符串格式吗

python - 在 Python Pandas DataFrame 中设置索引名称的最佳方法

machine-learning - 为什么使用 keras Conv2D 层时出现错误?

machine-learning - 将 .pb 文件(Tensorflow 模型文件)转换为人类可读格式

scala - 我无法使用流模式在 apache Spark 中使用 scala 进行在线预测来制作数据帧

java - 通过神经网络和/或强化学习提升我的遗传算法

python - 使用 Flask 服务器和 xlsxwriter 导出 Excel