Changeset 21

Show
Ignore:
Timestamp:
02/04/08 08:40:59 (4 years ago)
Author:
mike
Message:

Fixed hand ranking algorithm

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • deck.py

    r9 r21  
    55 
    66def card_vals(cards): 
    7     return [deck.cards.index(card) for card in cards] 
     7    print cards 
     8    return [(CARDS.index(card),b) for card,b in cards] 
    89 
    910class deck: 
  • hand.py

    r9 r21  
    1010                "four_of_a_kind", 
    1111                "straight_flush" ] 
     12 
     13#def format_hand(hand): 
     14#input all 7 cards 
     15def best_hand(cards): 
     16    all_poosible_hands = list(combNoReplace(cards,5)) 
     17    for h in all_poosible_hands: h.sort( cmp = lambda a,b: cmp(b,a) ) 
     18    pos_hands = [judge_hand(h) for h in all_poosible_hands] 
     19    return max(pos_hands) 
     20     
    1221 
    1322#returns the best combination for the 5 cards 
     
    6473                highest_n = current_n 
    6574                highest_card = a 
     75        else: 
     76            last = a 
     77            current_n = 1 
    6678    if highest_n == 1: return ("high_card",card_vals) 
    6779    #check for full house 
  • holdemtable.py

    r20 r21  
    11import deck 
    22import random 
     3import hand 
    34from twisted.internet import defer 
    45 
     
    78 
    89class InsufficientFundsException(Exception): 
     10    pass 
     11 
     12class ImproperPhaseException(Exception): 
    913    pass 
    1014 
     
    4246 
    4347    def __publicData(self): 
    44         return {"owner" : self.user_id, 
     48        dat = {"owner" : self.user_id, 
    4549                "status": self.status, 
    4650                "money" : self.money, 
    4751                "seat"  : self.seat} 
     52        if self.__cards_shown: 
     53            dat["cards"] = self.cards 
     54        return dat 
    4855 
    4956    def __privateData(self): 
     
    115122        return False 
    116123 
     124    #wrap up the hand. Who wins?  Etc. 
     125    def finishHand(self): 
     126        if not self.hand_over: 
     127            raise ImproperPhaseException("Hand is not over") 
     128        #now, let's get a list of people who are still in. 
     129        still_in = [ p for p in self.__player_array if not p.has_folded ] 
     130        winner = None 
     131        if len(still_in) == 1: 
     132            winner = still_in[0] 
     133        else: 
     134            #create list of tuples of (id,hand) 
     135            hands = [ (p.id,hand.best_hand(deck.card_vals(p.cards + self.__community_cards))) 
     136                    for p in still_in ] 
     137            #TODO add ties 
     138            hands.sort(cmp = lambda a,b: cmp(a[1],b[1])) 
     139            winner = hands.pop() 
     140            print "%s winds the hand with %s" % (winner[0], hand.HAND_ORDERS[winner[1][0]]) 
     141 
     142             
     143 
     144 
    117145    #return false if game is over 
    118146    def nextHand(self): 
     
    122150        self.__phase = None 
    123151        self.__community_cards = [] 
     152        self.__last_bet_raise = None 
    124153        if self.__hand_count == None: 
    125154            self.__hand_count = 0 
     
    169198        elif pn == "river": 
    170199            self.__community_cards.append(self.__deck.deal()) 
     200        elif pn == "showdown": 
     201            #If somebody bet or raised, they have to show 
     202            if self.__last_bet_raise != None: 
     203                self.__player_array[self.__last_bet_raise].doAction("show",None) 
     204                self.__action_on = self.__last_bet_raise 
     205            else: 
     206                #Else we start fromt he button 
     207                self.__action_on = None 
    171208 
    172209 
     
    265302            raise InvalidActionException("Value out of range") 
    266303        self.current_player.doAction(action,value) 
     304        if action == "bet" or action == "raise": 
     305            self.__last_bet_raise = self.__action_on 
    267306        if not self.nextAction(): 
    268307            if not self.nextPhase(): 
     308                self.finishHand() 
    269309                if not self.nextHand(): 
    270310                    #TODO add game end code handing hurr 
     
    328368    def checkStart(self): 
    329369        if self.isFull(): 
    330             self.__game_state = holdemround(self.__players,self.__blinds,self.__format) 
     370            self.__game_state = holdemround(self.__players,self.__format) 
    331371            self.__game_started = True 
    332372            self.__game_state.nextHand()