Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于骰子扑克游戏的expectiminimax树_Python_Tree_Artificial Intelligence_Minimax_Expectiminimax - Fatal编程技术网

Python 用于骰子扑克游戏的expectiminimax树

Python 用于骰子扑克游戏的expectiminimax树,python,tree,artificial-intelligence,minimax,expectiminimax,Python,Tree,Artificial Intelligence,Minimax,Expectiminimax,我目前正在用Python编写一个骰子扑克游戏。这些规则与应用商店不久前推出的一款旧的移动骰子扑克游戏不同,取而代之的是它 规则如下: 玩家和AI最初每人掷5个骰子 玩家和AI选择要持有的骰子,并滚动其余的骰子,将结果显示给彼此 重复上述步骤 排名较高的人(见下文)获胜,绝对高牌将成为平局的破坏者 五种 一类四个 直的 满座 一类三个 两对 一对 高牌 相关代码如下: class Tree(object): '''Generic tree node''' def __init

我目前正在用Python编写一个骰子扑克游戏。这些规则与应用商店不久前推出的一款旧的移动骰子扑克游戏不同,取而代之的是它

规则如下:

  • 玩家和AI最初每人掷5个骰子
  • 玩家和AI选择要持有的骰子,并滚动其余的骰子,将结果显示给彼此
  • 重复上述步骤
  • 排名较高的人(见下文)获胜,绝对高牌将成为平局的破坏者

  • 五种
  • 一类四个
  • 直的
  • 满座
  • 一类三个
  • 两对
  • 一对
  • 高牌
相关代码如下:

class Tree(object):
    '''Generic tree node'''
    def __init__(self, children=None, value=None, type=None):
        self.children = []
        self.value = value
        self.type = type  # either 'player', 'ai', or 'chance'
        if children is not None:
            for child in children:
                self.add_child(child)

    def add_child(self, node):
        assert isinstance(node, Tree)
        self.children.append(node)

    def is_terminal(self):
        return len(self.children) == 0


def expectiminimax(node):
    '''Adapted from Wikipedia's pseudocode'''
    MAX_INT = 1e20
    if node.is_terminal():
        return node.value
    if node.type == 'player':
        q = MAX_INT
        for child in node.children:
            q = min(q, expectiminimax(child))
    elif node.type == 'ai':
        q = -MAX_INT
        for child in node.children:
            q = max(q, expectiminimax(child))
    elif node.type == 'chance':
        q = 0
        for child in node.children:
            # All children are equally probable
            q += len(node.children)**-1 * expectiminimax(child)
    return q


def ai_choose(ai_hand, player_hand):
    '''
    Given an AI hand and a player hand, choose which cards to hold onto
    for best outcome.
    '''
    def construct_tree(ai_hand, player_hand):
        '''
        Construct a 5-layer (?) tree for use with expectiminimax.
                      Δ                MAX
                    /   \
                   O ... O             CHANCE - Possible AI moves
                 /  \   /  \
                ∇ .. ∇ ∇ .. ∇          MIN - Possible card dice rolls
              /   \    ........
             O ... O  ...........      CHANCE - Possible player moves
           /  \   /  \ ............
          ▢ .. ▢ ▢ .. ▢ .............  END - Possible card dice rolls
        '''
        tree_structure = ['ai', 'chance', 'player', 'chance', 'ai']
        tree = Tree(type=tree_structure[0])
        for subset in powerset(ai_hand.hand):
            tree.add_child(Tree(value=subset))
    # ...
这层结构正确吗?或者应该重新排列最小层、最大层和偶然层


也欢迎其他一般性意见。

据我所知,分层是正确的。不久前我做了类似的事情,我认为您可以在没有树数据结构的情况下实现它,它应该是可行的,而且可能更干净,因为您不需要机会类型。

据我所知,分层是正确的。不久前我做了类似的事情,我认为您可以在没有树数据结构的情况下实现它,它应该是可行的,而且可能更干净,因为您不需要机会类型。

等等,为什么我不需要机会类型?游戏不是决定性的。对不起,我的错。当我写这个的时候,我还在想我自己的程序。你的显然是不确定的。但是如果你想写一个人工智能,那么玩家得到的骰子就不重要了。除非你能投降。人工智能应该计算出哪个对他最好。因为这不会对球员产生影响。@Allafesta嗯,我想会的,对吧?因为玩家所拥有的可能会激发AI的更高风险。如果AI有一个满屋子,它应该根据玩家是否有一对和玩家是否有直道做出不同的决定。等等,为什么我不需要机会类型?游戏不是决定性的。对不起,我的错。当我写这个的时候,我还在想我自己的程序。你的显然是不确定的。但是如果你想写一个人工智能,那么玩家得到的骰子就不重要了。除非你能投降。人工智能应该计算出哪个对他最好。因为这不会对球员产生影响。@Allafesta嗯,我想会的,对吧?因为玩家所拥有的可能会激发AI的更高风险。如果AI有一个满屋子,它应该根据玩家是否有一对和玩家是否有一对直人做出不同的决定。