# -*- mode: python -*- # Copyright (C) 2004, 2005, 2006 Mekensleep # # Mekensleep # 24 rue vieille du temple # 75004 Paris # licensing@mekensleep.com # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. # # Authors: # Loic Dachary # import sys, os sys.path.insert(0, "@top_srcdir@") sys.path.insert(0, "..") from pprint import pprint import unittest from pokerengine.pokergame import PokerGameServer class TestBlinds(unittest.TestCase): def setUp(self): self.verbose = int(os.environ.get('VERBOSE_T', 3)) self.game = PokerGameServer("poker.%s.xml", [ "../conf", "@top_srcdir@/conf" ]) self.game.verbose = self.verbose self.game.setVariant("holdem") self.game.setBettingStructure("2-4-limit") def tearDown(self): del self.game def log(self, string): print string def make_new_bot(self, serial, seat): game = self.game self.failUnless(game.addPlayer(serial, seat)) self.failUnless(game.payBuyIn(serial, game.bestBuyIn())) self.failUnless(game.sit(serial)) game.botPlayer(serial) game.noAutoBlindAnte(serial) def make_new_player(self, serial, seat): game = self.game self.failUnless(game.addPlayer(serial, seat)) self.failUnless(game.payBuyIn(serial, game.bestBuyIn())) self.failUnless(game.sit(serial)) def pay_blinds(self): game = self.game for serial in game.serialsAll(): game.autoBlindAnte(serial) for serial in game.serialsAll(): game.noAutoBlindAnte(serial) def check_blinds(self, descriptions): players = self.game.playersAll() players.sort(lambda a,b: int(a.seat - b.seat)) fail = False for player in players: (blind, missed, wait) = descriptions.pop(0) if(blind != player.blind or missed != player.missed_blind or wait != player.wait_for): if self.verbose >= 0: print "check_blinds FAILED actual %s != from expected %s" % ( (player.blind, player.missed_blind, player.wait_for), (blind, missed, wait) ) fail = True else: if self.verbose > 0: print "check_blinds %s == %s" % ( (player.blind, player.missed_blind, player.wait_for), (blind, missed, wait) ) if fail: self.fail() def test1(self): for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3)): self.make_new_bot(serial, seat) self.game.beginTurn(1) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 ('small', None, False), # 2 ('big', None, False), # 3 (None, None, False), # 4 ] ) self.pay_blinds() self.game.beginTurn(2) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 (None, None, False), # 2 ('small', None, False), # 3 ('big', None, False), # 4 ] ) self.pay_blinds() self.game.beginTurn(3) # (blind, missed, wait) self.check_blinds([('big', None, False), # 1 (None, None, False), # 2 (None, None, False), # 3 ('small', None, False), # 4 ] ) self.pay_blinds() self.game.beginTurn(4) # (blind, missed, wait) self.check_blinds([('small', None, False), # 1 ('big', None, False), # 2 (None, None, False), # 3 (None, None, False), # 4 ] ) self.pay_blinds() def test2(self): """ Two new players enter the game and both pay the big blind """ for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 8)): self.make_new_bot(serial, seat) self.game.beginTurn(1) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 ('small', None, False), # 2 ('big', None, False), # 3 (None, None, False), # 4 ] ) self.pay_blinds() for (serial, seat) in ((10, 4), (11, 5)): self.make_new_bot(serial, seat) self.game.beginTurn(2) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 (None, None, False), # 2 ('small', None, False), # 3 ('big', 'n/a', False), # 10 ('late', 'n/a', False), # 11 (None, None, False), # 4 ] ) self.pay_blinds() def test3(self): """ Two new players enter the game between the small and big blind. They are allowed to play during the second turn because they cannot be awarded the button as they arrive. """ for (serial, seat) in ((1, 0), (2, 1), (3, 7), (4, 8)): self.make_new_bot(serial, seat) self.game.beginTurn(1) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 ('small', None, False), # 2 ('big', None, False), # 3 (None, None, False), # 4 ] ) self.pay_blinds() for (serial, seat) in ((10, 4), (11, 5)): self.make_new_bot(serial, seat) self.game.beginTurn(2) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 (None, None, False), # 2 (None, 'n/a', 'late'), # 10 (None, 'n/a', 'late'), # 11 ('small', None, False), # 3 ('big', None, False), # 4 ] ) self.pay_blinds() # players who did not pay the big blind are removed from # the history by historyReduce game_index = 0 player_list_index = 7 serial2chips_index = 9 history = self.game.turn_history self.assertEqual(history[game_index][player_list_index], [1, 2, 10, 11, 3, 4]) self.assertEqual(history[game_index][serial2chips_index].keys(), [1, 2, 3, 4, 10, 11]) self.game.historyReduce() self.assertEqual(history[game_index][player_list_index], [1, 2, 3, 4]) self.assertEqual(history[game_index][serial2chips_index].keys(), [1, 2, 3, 4]) self.game.beginTurn(3) # (blind, missed, wait) self.check_blinds([('big', None, False), # 1 (None, None, False), # 2 ('late', 'n/a', False), # 10 ('late', 'n/a', False), # 11 (None, None, False), # 3 ('small', None, False), # 4 ] ) self.pay_blinds() self.game.beginTurn(4) # (blind, missed, wait) self.check_blinds([('small', None, False), # 1 ('big', None, False), # 2 (None, None, False), # 10 (None, None, False), # 11 (None, None, False), # 3 (None, None, False), # 4 ] ) self.pay_blinds() def test4(self): """ Less than 6 players, player 4 missed the big and small blinds and must pay the big blind when back in the game. """ for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3)): self.make_new_bot(serial, seat) self.game.beginTurn(1) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 ('small', None, False), # 2 ('big', None, False), # 3 (None, None, False), # 4 ] ) self.pay_blinds() self.game.sitOut(4) self.game.beginTurn(2) # (blind, missed, wait) self.check_blinds([('big', None, False), # 1 (None, None, False), # 2 ('small', None, False), # 3 (None, 'big', False), # 4 ] ) self.pay_blinds() self.game.beginTurn(3) # (blind, missed, wait) self.check_blinds([('small', None, False), # 1 ('big', None, False), # 2 (None, None, False), # 3 (None, 'big', False), # 4 ] ) self.pay_blinds() self.game.sit(4) self.game.beginTurn(4) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 ('small', None, False), # 2 ('big', None, False), # 3 ('late', 'big', False), # 4 ] ) self.pay_blinds() def test5(self): """ More than 5 players, player 4 missed the big and small blinds and must pay the big and the small blind when back in the game. """ for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5)): self.make_new_bot(serial, seat) self.game.beginTurn(1) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 ('small', None, False), # 2 ('big', None, False), # 3 (None, None, False), # 4 (None, None, False), # 5 (None, None, False), # 6 ] ) self.pay_blinds() self.game.sitOut(4) self.game.beginTurn(2) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 (None, None, False), # 2 ('small', None, False), # 3 (None, 'big', False), # 4 ('big', None, False), # 5 (None, None, False), # 6 ] ) self.pay_blinds() self.game.beginTurn(3) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 (None, None, False), # 2 (None, None, False), # 3 (None, 'big', False), # 4 ('small', None, False), # 5 ('big', None, False), # 6 ] ) self.pay_blinds() self.game.sit(4) self.game.beginTurn(4) # (blind, missed, wait) self.check_blinds([('big', None, False), # 1 (None, None, False), # 2 (None, None, False), # 3 ('big_and_dead', 'big', False), # 4 (None, None, False), # 5 ('small', None, False), # 6 ] ) self.pay_blinds() def test6(self): """ """ for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3), (5, 4)): self.make_new_bot(serial, seat) self.game.sitOut(1) self.game.sitOut(2) self.game.sitOut(4) self.game.beginTurn(1) # (blind, missed, wait) self.check_blinds([(None, None, False), # 1 (None, 'small', False), # 2 ('small', None, False), # 3 (None, 'big', False), # 4 ('big', None, False), # 5 ] ) self.game.sit(1) self.game.sit(2) self.game.autoBlindAnte(3) self.game.noAutoBlindAnte(3) self.game.sit(4) # (blind, missed, wait) self.check_blinds([(None, None, 'first_round'), # 1 (None, 'small', 'first_round'), # 2 (True, None, False), # 3 (None, 'big', 'first_round'), # 4 ('big', None, False), # 5 ] ) self.game.sitOut(5) self.failUnless(self.game.state, "end") self.failUnless(self.game.serial2player[3].money, self.game.bestBuyIn()) def test0(self): """ """ game = self.game # for (serial, seat) in ((1, 0), (2, 1), (3, 2), (4, 3)): for (serial, seat) in ((1, 2), (2, 3)): self.make_new_bot(serial, seat) game.beginTurn(1) self.pay_blinds() self.game.beginTurn(2) # (blind, missed, wait) self.check_blinds([ ('small', None, False), # 1 ('big', None, False), # 2 ] ) game.autoBlindAnte(1) game.noAutoBlindAnte(1) game.autoBlindAnte(2) game.noAutoBlindAnte(2) game.sitOutNextTurn(2) self.failUnless(self.game.state, "end") # # Two players came in and are waiting for # the late blind because they attempted to enter # either on the small blind or the dealer position. # Fake this situation instead of playing hands that # will lead to the same because it introduces an # complexity that is not necessary. This case can happen # indeed although very rarely. # for (serial, seat) in ((3, 0), (4, 7)): self.make_new_player(serial, seat) game.getPlayer(serial).wait_for = 'late' self.game.beginTurn(3) # (blind, missed, wait) self.check_blinds([('big', None, False), # 1 (None, None, False), # 2 (None, 'small', False), # 3 ('small', None, False) # 4 ] ) def run(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestBlinds)) verbosity = int(os.environ.get('VERBOSE_T', 2)) return unittest.TextTestRunner(verbosity=verbosity).run(suite) if __name__ == '__main__': if run().wasSuccessful(): sys.exit(0) else: sys.exit(1) # Interpreted by emacs # Local Variables: # compile-command: "( cd .. ; ./config.status tests/blinds.py ) ; ( cd ../tests ; make TESTS='blinds.py' check )" # End: