【发布时间】:2016-07-27 19:15:24
【问题描述】:
我目前正在用 Python 编写一个骰子扑克游戏。这些规则不类似于the game found in The Witcher 2,而是基于不久前从 App Store 下架的一款旧的移动骰子扑克游戏。
规则如下:
- 玩家和 AI 最初各掷出 5 个poker dice。
- 玩家和 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))
# ...
这个层结构正确吗?或者是否应该重新排列最小、最大和机会层?
也欢迎其他通用cmets。
【问题讨论】:
标签: python tree artificial-intelligence minimax expectiminimax