Changeset 12

Show
Ignore:
Timestamp:
02/03/08 16:52:57 (4 years ago)
Author:
mike
Message:

Added hand checking code

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • client.py

    r11 r12  
    1111from twisted.internet import defer 
    1212from pprint import PrettyPrinter 
     13from string import split 
    1314 
    1415pprinter = PrettyPrinter().pprint 
    1516 
    16 def dealWithData(data): 
    17     pprinter(data) 
     17def makeAction( action, value): 
     18    msg = { "method":"action", 
     19            "action":action, 
     20            "value":value } 
    1821 
    1922class PokerClientProtocol(NetstringReceiver): 
     23    def dealWithData(self,data): 
     24        print "---------------" 
     25        pprinter(data) 
     26        if data["you"]["required_action"]: 
     27            print "---------------" 
     28            print "We need some input. Possible actions:" 
     29            p_acts = data["you"]["possible_actions"] 
     30            vals = [None,None] 
     31            while not vals[0] in p_acts: 
     32                pprinter(p_acts) 
     33                str = raw_input('Action? format: <action> OR <action>,<value>:\n') 
     34                vals = split(str,",") 
     35                vals.append(None) 
     36            self.sendString( cjson.encode(makeAction(vals[0], vals[1]))) 
     37 
    2038    def __init__(self,table_name, user_id, client_id ): 
    2139        self.table_name = table_name 
     
    3351         
    3452        decoded = cjson.decode(line) 
    35         dealWithData(decoded) 
     53        self.dealWithData(decoded) 
    3654         
    3755    def connectionLost(self, reason): 
  • holdemtable.py

    r10 r12  
    1111        self.money = self.starting_money 
    1212        self.cards = [] 
     13        self.money_in_pot = None  #money in pot before previous round 
     14        self.__action_history = [] 
     15        self.status = "need implementing" 
     16 
     17    def nextHand(self): 
     18        self.__in_hand = True 
     19        self.money_in_pot = 0.0 
     20        self.cards = [] 
     21        self.__action_history = [] 
     22 
     23    def moneyInPhase(self): 
     24        m = 0.0 
     25        for (a,b) in self.action_history if b: 
     26            m += b 
     27        return m 
     28 
     29    def nextPhase(self): 
    1330        self.action_history = [] 
    14         self.status = "need implementing" 
    15  
    16     def nextHand(self): 
    17         self.cards = [] 
    18         self.actionHistory = [] 
     31        self.money_in_pot += self.moneyInPhase() 
     32 
    1933 
    2034    def publicData(self): 
     
    2842               "cards"          : self.cards} 
    2943        return dat 
     44 
    3045    def actionHistory(self): 
    31         return self.action_history 
     46        return self.__action_history 
    3247 
    3348    def dealCard(self, card): 
    3449        self.cards.append(card) 
    3550    #returns money added to pot if any 
    36     def doAction(self, action): 
    37         self.action_history.append( action ) 
     51    def doAction(self, action, value): 
     52        if value and value > self.money: return False 
     53        else: 
     54            self.money -= value 
     55            self.__action_history.append((action,value)) 
     56            return True 
    3857 
    3958    def inHand(self): 
    4059        #TODO need to change this 
    41         return True  
     60        return self.__in_hand 
    4261 
    4362PHASES = ["blinds", "flop", "turn", "river"] 
    4463class holdemround: 
    45     def __init__(self, players): 
     64    def __init__(self,players,blinds): 
    4665        self.deck = deck.deck() 
     66        self.blinds = blinds 
    4767        #keep both of these on hand so we can quickly find a player 
    4868        self.players = players 
     
    5373        self.phase = 0 
    5474        self.community_cards = None 
    55         self.pot = None 
     75        self.possible_actions = None #to cache possible actions 
    5676        pass 
     77 
     78    #hand over? 
     79 
     80    def isHandOver(self): 
     81        total = sum([ 1 for p in self.player_array if p.inHand() ]) 
     82        if total == 0: return True 
     83 
     84        self.phase == len(PHASES): return True 
     85        return False 
    5786 
    5887    #return false if game is over 
    5988    def nextHand(self): 
    6089        self.deck.shuffle() 
    61         self.action_on = 0 
    6290        #rotate the players 
    6391        self.player_array.append(self.player_array.pop(0)) 
    64         self.phase = 0 
     92        self.phase = None 
    6593        self.pot = 0.0 
    6694        self.community_cards = [] 
     
    6896        for p in self.player_array: p.nextHand() 
    6997        #deal cards 
    70         for _ in xrange(2): 
     98        for _ in range(2): 
    7199            for p in self.player_array: 
    72100                p.dealCard(self.deck.deal()) 
    73         return True 
     101 
     102        return nextPhase() 
    74103 
    75104    #returns false if hand is over 
    76105    def nextPhase(self): 
    77         self.phase += 1 
    78         if self.phase == len(PHASES): return False 
    79         return True 
     106        self.action_on = None 
     107 
     108        if self.phase != None: self.phase += 1 
     109        else: self.phase = 0 
     110 
     111        if self.isHandOver(): return False 
     112        else: return nextAction() 
    80113     
     114 
    81115    #returns false if phase is over 
    82116    def nextAction(self): 
    83         self.action_on = ( self.button_on + 1 ) % len(self.player_array) 
     117        self.possible_actions = None 
     118 
     119        #first check if we have just one player left.  then we have an easy winner 
     120 
     121        if self.isHandOver(): return False 
     122 
     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) 
     128        else: self.action = 0 
    84129        return True 
    85130 
    86     def requiredAction( self, player_id): 
    87         self.action_on and player_id == self.player_array[self.action_on].id 
     131    def requiredAction(self, player_id): 
     132        return self.action_on != None and player_id == self.player_array[self.action_on].id 
    88133 
    89134    #returns possible actions for a player 
    90135    def possibleActions(self, player_id): 
    91         if player_id != self.player_array[self.action_on].id: return None 
    92         else: return {} #TODO add stuff here 
     136        if possible_actions != None: 
     137            return possible_actions 
     138 
     139        me = self.players[player_id] 
     140        possible_actions = {"fold":None} 
     141 
     142        max_money = max([p.moneyInPhase() for p in self.player_array]) 
     143 
     144        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 
     147        #TODO this needs to change if it's the river 
     148        if max_money == 0: possible_actions["bet"] = self.blinds[1] 
     149        #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 
     153        #TODO fix so unlimited raises aren't allowed 
     154 
    93155 
    94156    def getState(self): 
     
    100162 
    101163        for (n,p) in self.players.items(): 
    102             if p.inHand(): p_actions[n] = p.publicData() 
     164            if p.inHand(): p_actions[n] = p.actionHistory() 
    103165        state["player_actions"] = p_actions 
    104166 
     
    109171 
    110172class holdemtable: 
    111     max_players = 2 
    112     def __init__(self, name): 
     173    def __init__(self, name, max_players): 
     174        self.max_players = max_players 
    113175        self.players={} 
    114176        self.game_started = False 
     
    145207        you = self.players[p_id].privateData() 
    146208 
    147         req_action = self.game_started and\ 
    148                      self.game_state.requiredAction(p_id) 
    149  
    150         you["reqired_action"] = req_action 
     209        req_action = self.game_started and self.game_state.requiredAction(p_id) 
     210        if self.game_started: print self.game_state.requiredAction(p_id) 
     211 
     212        print req_action 
     213        you["required_action"] = req_action 
    151214        if req_action: you["possible_actions"] = self.game_state.possibleActions(p_id) 
    152215 
     
    168231        return root 
    169232 
     233    def action(self, p_id, action, value): 
     234        pass 
     235 
    170236    def addDeferred(self, p_id, deferred): 
    171237        print "Adding player id(%s) to deferred" % p_id 
  • tabled.py

    r11 r12  
    1313from twisted.internet import defer 
    1414 
    15 tables = {"test_table":holdemtable("test_table")} 
     15tables = {} 
    1616 
    1717class PokerTableProtocol(NetstringReceiver): 
     
    4242                        protocol.MakeError(103,"Already connected")) ) 
    4343                    return 
     44                table_name = decoded["table_name"] 
    4445 
    45                 self.table = tables[decoded["table_name"]] 
     46                if not table_name in tables: 
     47                    tables[table_name]=holdemtable(table_name) 
     48 
     49                self.table = tables[table_name] 
    4650                if self.table.isFull(): 
    4751                    self.sendString( cjson.encode(