骰子扑克游戏的expectiminimax树

时间:2016-07-27 19:15:25

标签: python tree artificial-intelligence minimax expectiminimax

我目前正在用Python编写骰子扑克游戏。规则是,如the game found in The Witcher 2,而是基于一段时间以前从App Store中删除的旧移动骰子扑克游戏。

规则如下:

  • 玩家和AI最初每次推出5 poker dice
  • 玩家和AI选择要握住哪个骰子并滚动其余部分,将结果透露给对方。
  • 重复上述步骤。
  • 无论谁拥有更高的排名(见下文),绝对的高牌都可以作为决胜局。

    1. 五种
    2. 四种
    3. 满屋
    4. 三种
    5. 两对
    6. 一对
    7. 高卡

相关代码如下:

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))
    # ...

这层结构是否正确?或者应该重新安排最小,最大和机会层?

也欢迎其他一般性评论。

1 个答案:

答案 0 :(得分:0)

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