首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一种用于骰子扑克游戏的最大期望树

一种用于骰子扑克游戏的最大期望树
EN

Stack Overflow用户
提问于 2016-07-27 19:15:24
回答 1查看 1.5K关注 0票数 1

我目前正在用Python编写一个骰子扑克游戏。这些规则不像“巫婆2”中的游戏,而是基于一种古老的移动骰子扑克游戏,这款游戏是在一段时间前从are上删除的。

这些规则如下:

  • 玩家和AI最初各滚动5 扑克骰子
  • 玩家和AI选择哪个骰子保持和滚动其余的,向对方透露结果。
  • 重复上述步骤。
  • 谁拥有更高的排名(见下文)的胜利,与绝对高卡作为平局。
代码语言:javascript
复制
1. Five of a Kind
2. Four of a Kind
3. Straight
4. Full House
5. Three of a Kind
6. Two Pair
7. One Pair
8. High Card

有关守则如下:

代码语言:javascript
复制
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))
    # ...

这层结构正确吗?还是应该重新安排最小层、最大层和机会层?

其他一般性意见也受到欢迎。

EN

回答 1

Stack Overflow用户

发布于 2016-07-27 19:26:05

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38621382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档