Exemple #1
0
    def test_check_winner(self):
        d = Dealer()
        board = Board()
        players = [MockPlayer(1,"a",100),MockPlayer(2,"b",15)]

        # Case1
        community = [Card(3,4),Card(5,4),Card(7,2),Card(6,4),Card(10,2)]
        hole1 = [Card(2,4),Card(9,2)] # no pair
        hole2 = [Card(3,4),Card(8,2)] # one pair
        board.addCards(community)
        players[0].setHoleCards(hole1)
        players[1].setHoleCards(hole2)
        deactive = []
        winner, result = d.check_winner(players, deactive, board)
        ok_(len(winner)==1 and winner[0]==players[1])
        print result
        # Case2
        board.reset()
        community = [Card(3,4),Card(3,4),Card(3,2),Card(6,4),Card(10,2)]
        hole1 = [Card(3,4),Card(9,2)] # four card
        hole2 = [Card(4,4),Card(6,2)] # full house
        hole3 = [Card(2,4),Card(8,2)] # three card
        board.addCards(community)
        players.append(MockPlayer(3,"c",200))
        players[0].setHoleCards(hole1)
        players[1].setHoleCards(hole2)
        players[2].setHoleCards(hole3)
        deactive = []
        winner, result = d.check_winner(players, deactive, board)
        ok_(len(winner)==1 and winner[0]==players[0])
        print result

        deactive = [players[0].pid]
        winner, result = d.check_winner(players, deactive, board)
        ok_(len(winner)==1 and winner[0]==players[1])
        print result
Exemple #2
0
class Table(object):

    def __init__(self):
        self.AUTO = True
        self.players = None
        self.sb_chip = None
        self.sb_pos = None
        self.round_count = 0
        self.retire = []  # who lost all money
        self.deactive = []  # who foled the round
        self.allin = []  # who did all-in in the round

        self.D = Dealer()
        self.E = HandEvaluator()
        self.deck = Deck()
        self.board = Board()
        self.pot = Pot()

    # You need to call this method before start the game.
    def setup(self, players, sb_chip):
        random.shuffle(players)
        self.players = players
        self.sb_pos = len(players)-1   # for init_round
        self.sb_chip = sb_chip
    
    # You need to call this method every time new round starts
    def init_round(self):
        self.round_count += 1
        self.board.reset()
        self.pot.reset()
        self.deck.shuffle(True)
        self.D.deal_card(self.deck, self.players, self.retire)
        self.sb_pos = (self.sb_pos+1)%len(self.players)
        self.allin = []
        self.deactive = []
        self.deactive += self.retire
        info = GameInfo(GameInfo.NEWGAME, self.sb_pos, self.players,\
                self.pot, self.board, self.deactive, [])

        # send new game information to all players
        for player in self.players:
            player.action(info)

    # public method to start the poker game
    def start_game(self, n):
        # Play round n times
        for i in range(n):
            # if one player beats the others then finish the game.
            if len(self.retire)+1 == len(self.players): break
            if not self.AUTO and i != 0: subprocess.call('clear')
            self.init_round()
            ui.round_info(self.round_count, n, self.players, self.sb_pos)
            if not self.AUTO:
                raw_input('> Type any button to start the round...')
            self.play_round()
    
    # play one round
    def play_round(self):
        # start street
        self.preflop()
        if len(self.deactive)+1 != len(self.players): self.street(GameInfo.FLOP)
        if len(self.deactive)+1 != len(self.players): self.street(GameInfo.TURN)
        if len(self.deactive)+1 != len(self.players): self.street(GameInfo.RIVER)
        self.showoff()

        if not self.AUTO:
            print '\n> Enter any input to start next round ...\n'
            raw_input()

    def preflop(self):
        """ Preflop task
            1. collect blind
            2. change order of player (big blind is last player in pre-flop)
            3. ask action to players
        """
        self.D.collect_blind(self.pot, self.players, self.sb_pos, self.sb_chip)
        n = len(self.players)
        order = [(self.sb_pos+2+i)%n for i in range(n)]
        info = GameInfo(GameInfo.PREFLOP, self.sb_pos, self.players, self.pot, self.board, self.deactive, [])
        self.D.ask_action(self.players, self.pot, self.deactive, self.allin, order, info)

    def street(self, street):
        """ FLOP, TURN, RIVER task is almost the same
            1. reset bet (again starts from CHECK:0,FOLD,RAISE:10)
            2. add cards on board
            3. change order of player to sb -> bb -> ...
            4. ask action
        """
        self.pot.reset_bet()
        if street == GameInfo.FLOP:  # draw 3 cards in FLOP
            self.board.addCards(self.deck.drawCards(3))
        else:
            self.board.addCard(self.deck.drawCard())
        if not self.AUTO: self.board.display()

        # the case when left player is allin and call or
        #   the case when all player allin
        if len(self.deactive)+len(self.allin)+1 == len(self.players) or \
                len(self.deactive)+len(self.allin) == len(self.players):
            return

        # ask action to player
        n = len(self.players)
        order = [(self.sb_pos+i)%n for i in range(n)]
        info = GameInfo(street, self.sb_pos, self.players, self.pot, self.board, self.deactive, [])
        self.D.ask_action(self.players, self.pot, self.deactive, self.allin, order, info)

    def showoff(self):
        winner, result = self.D.check_winner(self.players, self.deactive, self.board)
        self.D.money_to_winner(self.pot, self.players, winner, self.allin, self.retire)
        ui.round_result(self.round_count, winner, result)