Changeset 13 for holdemtable.py

Show
Ignore:
Timestamp:
02/03/08 18:29:26 (4 years ago)
Author:
mike
Message:

Added some actioN

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • holdemtable.py

    r12 r13  
    22import random 
    33from twisted.internet import defer 
     4 
     5class InvalidActionException(Exception): 
     6    pass 
     7 
     8class InsufficientFundsException(Exception): 
     9    pass 
    410 
    511class player: 
     
    1117        self.money = self.starting_money 
    1218        self.cards = [] 
    13         self.money_in_pot = None  #money in pot before previous round 
     19        self.__money_in_pot = None  #money in pot before previous round 
    1420        self.__action_history = [] 
    1521        self.status = "need implementing" 
     
    1723    def nextHand(self): 
    1824        self.__in_hand = True 
    19         self.money_in_pot = 0.0 
     25        self.__money_in_pot = 0.0 
    2026        self.cards = [] 
    2127        self.__action_history = [] 
    2228 
     29    def moneyInPot(self): 
     30        return __money_in_pot 
     31 
    2332    def moneyInPhase(self): 
    24         m = 0.0 
    25         for (a,b) in self.action_history if b: 
    26             m += b 
    27         return m 
     33        return sum([ b for a,b in self.__action_history if b != None]) 
    2834 
    2935    def nextPhase(self): 
    30         self.action_history = [] 
    31         self.money_in_pot += self.moneyInPhase() 
     36        self.__action_history = [] 
     37        self.__money_in_pot += self.moneyInPhase() 
    3238 
    3339 
     
    5056    #returns money added to pot if any 
    5157    def doAction(self, action, value): 
    52         if value and value > self.money: return False 
     58        print value 
     59        print self.money 
     60        if value != None and value > self.money: 
     61            raise InsufficientFundsException("Insufficient Funds") 
    5362        else: 
    5463            self.money -= value 
    5564            self.__action_history.append((action,value)) 
    56             return True 
    5765 
    5866    def inHand(self): 
    5967        #TODO need to change this 
    6068        return self.__in_hand 
     69 
    6170 
    6271PHASES = ["blinds", "flop", "turn", "river"] 
     
    7079        self.player_array.sort( cmp=lambda x,y: x.seat - y.seat)  
    7180        print self.player_array 
    72         self.action_on = None 
     81        self.__action_on = None 
    7382        self.phase = 0 
    7483        self.community_cards = None 
     
    7685        pass 
    7786 
     87    def actionOn(self): 
     88        return self.__action_on 
    7889    #hand over? 
    7990 
     
    8293        if total == 0: return True 
    8394 
    84         self.phase == len(PHASES): return True 
     95        if self.phase == len(PHASES): return True 
    8596        return False 
    8697 
     
    100111                p.dealCard(self.deck.deal()) 
    101112 
    102         return nextPhase() 
     113        return self.nextPhase() 
    103114 
    104115    #returns false if hand is over 
    105116    def nextPhase(self): 
    106         self.action_on = None 
     117        self.__action_on = None 
    107118 
    108119        if self.phase != None: self.phase += 1 
    109120        else: self.phase = 0 
    110121 
     122        pn = PHASES[self.phase] 
     123        if pn == "blinds": 
     124            self.player_array[0].doAction("small_blinds",self.blinds[0]) 
     125            self.player_array[1].doAction("large_blinds",self.blinds[1]) 
     126            self.__action_on = 1 
     127        elif pn == "flop": 
     128            for _ in range(3): 
     129                self.community_cards.append(self.deck.deal) 
     130        elif pn == "turn": 
     131            self.community_cards.append(self.deck.deal) 
     132        elif pn == "river": 
     133            self.community_cards.append(self.deck.deal) 
     134 
     135 
    111136        if self.isHandOver(): return False 
    112         else: return nextAction() 
     137        else: return self.nextAction() 
    113138     
    114139 
     
    121146        if self.isHandOver(): return False 
    122147 
    123         #increment the acction 
    124         if self.action != None: 
    125             self.action_on = ( self.button_on + 1 ) % len(self.player_array) 
    126             while not self.player_array[self.action_on].inHand(): 
    127                 self.action_on = ( self.button_on + 1 ) % len(self.player_array) 
     148        #increment the action 
     149        if self.__action_on != None: 
     150            self.__action_on = ( self.__action_on + 1 ) % len(self.player_array) 
     151            while not self.player_array[self.__action_on].inHand(): 
     152                self.__action_on = ( self.__action_on + 1 ) % len(self.player_array) 
    128153        else: self.action = 0 
    129154        return True 
    130155 
    131156    def requiredAction(self, player_id): 
    132         return self.action_on != None and player_id == self.player_array[self.action_on].id 
     157        return self.__action_on != None and player_id == self.player_array[self.__action_on].id 
    133158 
    134159    #returns possible actions for a player 
    135     def possibleActions(self, player_id): 
    136         if possible_actions != None: 
    137             return possible_actions 
    138  
    139         me = self.players[player_id] 
    140         possible_actions = {"fold":None} 
     160    def possibleActions(self): 
     161        if self.possible_actions != None: 
     162            return self.possible_actions 
     163 
     164        me = self.player_array[self.__action_on] 
     165        self.possible_actions = {"fold":(None,None)} 
    141166 
    142167        max_money = max([p.moneyInPhase() for p in self.player_array]) 
    143168 
    144169        money_in_phase = me.moneyInPhase() 
    145         if max_money == money_in_phase: possible_actions["check"] = None 
    146         if max_money > money: possible_actions["call"] = max_money 
     170        if max_money == money_in_phase: self.possible_actions["check"] = (None,None) 
     171        if max_money > money_in_phase: self.possible_actions["call"] =\ 
     172                (max_money - money_in_phase, max_money - money_in_phase) 
    147173        #TODO this needs to change if it's the river 
    148         if max_money == 0: possible_actions["bet"] = self.blinds[1] 
     174        if max_money == 0: self.possible_actions["bet"] = (self.blinds[1], me.money) 
    149175        #TODO this needs to change to reflect the last bet 
    150         else: possible_actions["raise"] = self.blinds[1] + max_money 
    151  
    152         return possible_actions 
     176        else: self.possible_actions["raise"] = (self.blinds[1] + max_money - money_in_phase, me.money) 
     177 
     178        return self.possible_actions 
    153179        #TODO fix so unlimited raises aren't allowed 
    154180 
     
    156182    def getState(self): 
    157183        state = {"phase": PHASES[self.phase], 
    158                  "action_on": self.player_array[self.action_on].seat, 
     184                 "action_on": self.player_array[self.__action_on].seat, 
    159185                 "pot_before_phase": self.pot, 
    160186                 "community_cards": self.community_cards } 
     
    167193        return state 
    168194 
     195    def potSize(self): 
     196        return sum([ p.moneyInPot for p in self.player_array ]) 
     197 
     198    def doAction(self, p_id, action, value): 
     199        if p_id != self.player_array[self.__action_on].id: 
     200            raise InvalidActionException("Action not on player " + p_id) 
     201 
     202        pa = self.possibleActions() 
     203        if not action in pa: 
     204            raise InvalidActionException("Action not in possible list") 
     205 
     206        range = pa[action] 
     207        if not (range[0] <= value <= range[1]): 
     208            print range[0] 
     209            print value 
     210            print range[1] 
     211            raise InvalidActionException("Value out of range") 
     212        self.player_array[self.__action_on].doAction(action,value) 
     213        self.nextAction() 
     214 
     215 
     216         
     217 
     218 
    169219 
    170220 
    171221 
    172222class holdemtable: 
    173     def __init__(self, name, max_players): 
     223    def __init__(self, name, max_players = 4): 
    174224        self.max_players = max_players 
    175225        self.players={} 
     
    190240    def checkStart(self): 
    191241        if self.isFull(): 
    192             self.game_state = holdemround(self.players) 
     242            self.game_state = holdemround(self.players,self.blinds) 
    193243            self.game_started = True 
    194244            self.game_state.nextHand() 
     
    212262        print req_action 
    213263        you["required_action"] = req_action 
    214         if req_action: you["possible_actions"] = self.game_state.possibleActions(p_id) 
     264        if req_action: you["possible_actions"] = self.game_state.possibleActions() 
    215265 
    216266        root["you"] = you 
     
    232282 
    233283    def action(self, p_id, action, value): 
    234         pass 
     284        self.game_state.doAction(p_id,action,value) 
     285        self.stateChanged("Actioned") 
    235286 
    236287    def addDeferred(self, p_id, deferred): 
    237         print "Adding player id(%s) to deferred" % p_id 
    238288        self.deferred.append((p_id, deferred)) 
    239289