2013-09-26 42 views
2

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

我有问题的代码如下:

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”?很明显,我可以将这个例子作为例外编码,但我想知道这个问题的一般解决方案。一般来说,现在电脑打我,因为他在决定选择哪封信之前会查找所有可能的单词列表,这些单词将以我的名字结尾。但在“危险”的例子中,他只是被卡住了,我想让他知道他会卡在前面几步,这样他就不会处于第一位...

非常感谢提前!

ADDED 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] 

回答

2

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

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

通过循环他将消除任何保证损失的举措。如果他没有剩下的动作,这意味着一举一动都会让他输,所以他只会选择随机字母,直到他输了。否则,他会选择最可能失败的方式,或者他可以赢得的最多方式,可能是实验确定的常数的线性组合。您将需要巧妙的循环或递归函数。

最后如果你想让他使用机器学习,你可能希望有一本词典,例如memory = {},然后每次他玩和失去时他会添加他的选择列表到内存中,并在下次避免该模式。他也可以这样调整常量。为了保持内存,你应该将它保存到一个文件中,或者使用python的pickle模块对它进行序列化。

+0

谢谢乔尔。这正是我所期待的! – user7186

+0

如果遇到问题,请留言。 :) –

+0

fyi我刚刚在上面添加了我的代码,似乎基本上做的伎俩。我看了一大堆Tree实现(非常有帮助,感谢您的建议!)。我发现直觉很有帮助,但是我最终发现自己编写的这样一个简短的函数具有相当的“搜索”和“遍历”操作似乎可以做到这一点。再次感谢! – user7186