理解python脚本创建Kuhn扑克游戏树

理解python脚本创建Kuhn扑克游戏树,python,class,nodes,decision-tree,poker,Python,Class,Nodes,Decision Tree,Poker,对于一个大学项目,我试图遵循一个脚本,我可以访问该脚本,该脚本可能会为两人Kuhn Poker创建游戏树结构——使用Python的类结构编写 在运行时,它遇到了决策节点的子节点和父节点属性的问题,我无法理解代码是如何一步一步地进行的 有人能帮我澄清一下吗?我花了几天时间仔细研究代码,觉得有见识的意见能够发现问题 以下是相关代码: import random from copy import deepcopy from itertools import permutations from iter

对于一个大学项目,我试图遵循一个脚本,我可以访问该脚本,该脚本可能会为两人Kuhn Poker创建游戏树结构——使用Python的类结构编写

在运行时,它遇到了决策节点的子节点和父节点属性的问题,我无法理解代码是如何一步一步地进行的

有人能帮我澄清一下吗?我花了几天时间仔细研究代码,觉得有见识的意见能够发现问题

以下是相关代码:

import random
from copy import deepcopy
from itertools import permutations
from itertools import combinations

J=11
Q=12
K=13
A=14

class specifications(object):
    def __init__(self, deck, size_of_bet, maximum_allowed_bet, ante):
            self.deck = deck
            self.ante = ante
            self.size_of_bet = size_of_bet
            self.maximum_allowed_bet = maximum_allowed_bet

class tree(object):
    def __init__(self, specifications):
            self.specifications = deepcopy(specifications)
            self.info = []
            self.root = None

    def start_game_and_deal_card(self):
        in_the_pot = [True] * 2
        players_contribution = [self.specifications.ante] * 2
        acting_player = 0
        bets = [0] * 2
        minimum_moves = in_the_pot.count(True)
        moves = 0
        game_so_far = ""
        possible_cards = permutations(combinations(self.specifications.deck,1),2)
        for cards in possible_cards:
            dealt_cards = ()
            cards = list(cards)
            for i,card in enumerate(cards):
                if in_the_pot[i]:
                    cards[i] = card + cards[i]
            for card in cards:
                dealt_cards += card
            deal = deal_cards_node(node, players_contribution, player_cards, self.specifications.deck, "")
            self.tree_structure(deal, acting_player, in_the_pot, players_contribution, cards, self.specifications.deck, game_so_far, minimum_moves, moves, bets)
        return deal

    def tree_structure(self, node, acting_player, in_the_pot, players_contribution, player_cards, deck, game_so_far, minimum_moves, moves, bets):
        if in_the_pot.count(True) ==1:
            self.showdown(node, in_the_pot, players_contribution, player_cards, self.specifications.deck, game_so_far)
            return
        if moves>=minimum_moves and bets[node.player]==betlevel:
            self.showdown(node, in_the_pot, players_contribution, player_cards, self.specifications.deck, game_so_far)
            return
        decision = decision_node(node, players_contribution, player_cards, self.specifications.deck, game_so_far, acting_player)
        # update information set
        self.info.append(decision.player)
        acting_player = (acting_player + 1) % 2
        # fold child
        if players_contribution[decision.player] < max(players_contribution):
            game_so_far += 'f'
            in_the_pot[decision.player] = False
            self.tree_structure(node, acting_player, in_the_pot, players_contribution, player_cards, deck, game_so_far, minimum_moves, moves + 1,bets)
            in_the_pot[decision.player] = True
            node.fold_decision = node.children[-1]
        # raise child
        if self.specifications.maximum_allowed_bet > max(bets):
            game_so_far += 'r'
            players_contribution[decision.player]=players_contribution[decision.player]+1
            bets[decision.player]=bets[decision.player]+1
            self.tree_structure(node, acting_player, in_the_pot, players_contribution, player_cards, deck, game_so_far, minimum_moves, moves + 1, bets)
            node.raise_decision = node.children[-1]
        # call child
        game_so_far =+ 'c'
        players_contribution[node.player]=players_contribution[(node.player+1)%2]
        bets[node.player]=bets[(node.player+1)%2]
        self.tree_structure(node, acting_player, in_the_pot, players_contribution, player_cards, deck, game_so_far, minimum_moves, moves + 1, bets)
        node.call_decision = node.children[-1]
        return decision

    def showdown(self, node, in_the_pot, players_contribution, player_cards, deck, game_so_far):
        if in_the_pot.count(True) == 1:
            winners = in_the_pot
        else:
            winners = []
            if player_cards[0] > player_cards[1]:
                winners.append(0)
            if player_cards[0] < player_cards[1]:
                winners.append(1)
        total = sum(players_contribution)
        payoffs = winners.extend([total])
        return terminal_node(node, players_contribution, player_cards, deck, game_so_far, payoffs, in_the_pot)

class node(object):
    def __init__(self, parent, players_contribution, player_cards, deck, game_so_far):
        self.deck = deepcopy(deck)
        self.players_contribution = deepcopy(players_contribution)
        self.player_cards = deepcopy(player_cards)
        self.parent = parent

class terminal_node(node):
    def __init__(self, parent, players_contribution, player_cards, deck, game_so_far, payoffs, in_the_pot):
        node.__init__(self, parent, players_contribution, player_cards, deck, game_so_far)
        self.children = []

class deal_cards_node(node):
    def __init__(self, parent, players_contribution, player_cards, deck, game_so_far):
        node.__init__(self, parent, players_contribution, player_cards, deck, game_so_far)
        self.children = []

class decision_node(node):
    def __init__(self, parent, players_contribution, player_cards, deck, game_so_far, player):
        node.__init__(self, parent, players_contribution, player_cards, deck, game_so_far)
        self.player = player
        self.children = []
        self.raise_decision = None
        self.call_decision = None
        self.fold_decision = None
随机导入
从复制导入deepcopy
从itertools导入置换
从itertools导入组合
J=11
Q=12
K=13
A=14
类别规格(对象):
定义初始(自身、牌组、下注大小、允许下注的最大下注、下注):
self.deck=甲板
self.ante=ante
self.size\u of \u bet=size\u of \u bet
self.maximum_allowed_bet=maximum_allowed_bet
类树(对象):
定义初始化(自身,规格):
self.specifications=deepcopy(规范)
self.info=[]
self.root=None
def开始游戏和交易卡(自我):
在\u pot中=[True]*2
玩家贡献=[self.specifications.ante]*2
扮演者=0
下注=[0]*2
最小移动量=在池中计数(真)
移动=0
game_so_far=“”
可能的卡片=排列(组合(自我规范牌组,1),2)
对于可能的_卡中的卡:
发牌=()
卡片=列表(卡片)
对于i,枚举中的卡片(卡片):
如果在锅里[i]:
卡片[i]=卡片+卡片[i]
对于卡片中的卡片:
已发卡+=卡
交易=交易卡节点(节点,玩家贡献,玩家卡,self.specifications.deck,“”)
self.tree_结构(交易、代理玩家、池中玩家、贡献、卡片、self.specifications.deck、游戏迄今为止、最小移动、移动、下注)
回报交易
def树结构(自身、节点、代理玩家、池中玩家、玩家贡献、玩家卡、牌组、游戏迄今为止、最小移动、移动、下注):
如果在容器中,则计数(真)=1:
自我决战(节点、池中、玩家贡献、玩家卡、自我规范牌、游戏至今)
回来
如果移动>=最小移动和下注[node.player]==betlevel:
自我决战(节点、池中、玩家贡献、玩家卡、自我规范牌、游戏至今)
回来
决策=决策节点(节点、玩家贡献、玩家卡、self.specifications.deck、游戏迄今、代理玩家)
#更新信息集
self.info.append(decision.player)
代理玩家=(代理玩家+1)%2
#折叠儿童
如果玩家贡献[decision.player]max(bets):
游戏迄今为止+='r'
玩家贡献[decision.player]=玩家贡献[decision.player]+1
下注[decision.player]=下注[decision.player]+1
自树结构(节点、代理玩家、池中玩家、玩家贡献、玩家卡、牌组、游戏迄今为止、最小移动、移动+1、下注)
node.raise_decision=node.children[-1]
#叫孩子
game_sou_far=+'c'
玩家贡献[节点.玩家]=玩家贡献[(节点.玩家+1)%2]
下注[节点.玩家]=下注[(节点.玩家+1)%2]
自树结构(节点、代理玩家、池中玩家、玩家贡献、玩家卡、牌组、游戏迄今为止、最小移动、移动+1、下注)
node.call_decision=node.children[-1]
返回决策
def决战(自我、节点、池中、玩家贡献、玩家卡、牌组、游戏至今):
如果在容器中,则计数(真)=1:
赢家=在锅里
其他:
优胜者=[]
如果玩家卡[0]>玩家卡[1]:
winners.append(0)
如果玩家卡[0]<玩家卡[1]:
获奖者。附加(1)
总计=总和(参与者贡献)
支付=赢家。扩展([总数])
返回终端节点(节点、玩家贡献、玩家卡、牌组、迄今为止的游戏、支付、池中)
类节点(对象):
定义初始(自我、父母、玩家贡献、玩家卡、牌组、游戏至今):
self.deck=deepcopy(deck)
self.players\u contribution=deepcopy(players\u contribution)
self.player\u cards=deepcopy(player\u cards)
self.parent=parent
类终端节点(节点):
定义初始(自我、父母、玩家贡献、玩家卡、牌组、迄今为止的游戏、支付、池中):
节点。uuuu初始(自我、家长、玩家贡献、玩家卡、牌组、游戏迄今为止)
self.children=[]
类别交易卡节点(节点):
定义初始(自我、父母、玩家贡献、玩家卡、牌组、游戏至今):
节点。uuuu初始(自我、家长、玩家贡献、玩家卡、牌组、游戏迄今为止)
self.children=[]
类决策节点(节点):
定义初始(自我、父母、玩家贡献、玩家卡、牌组、游戏至今、玩家):
节点。uuuu初始(自我、家长、玩家贡献、玩家卡、牌组、游戏迄今为止)
self.player=玩家
self.children=[]
self.raise\u decision=无
self.call\u decision=无
self.fold\u decision=None

此代码乱七八糟,不完整。如果有,它也不是传统的库恩扑克