Skip to content

Commit

Permalink
Added Structure class.
Browse files Browse the repository at this point in the history
  • Loading branch information
von committed Oct 31, 2010
1 parent 7266918 commit e2d6b31
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
77 changes: 77 additions & 0 deletions pyPoker/PokerGame.py
Expand Up @@ -818,3 +818,80 @@ def _debug(self, msg):
if self.message_handler is not None:
self.message_handler.debug(msg)

######################################################################
class Structure(object):
"""Structure for a poker game.
This is a set of read-only state regarding betting structure, namely
limit, pot limit or no-limit, ante amount, blinds and minimum bet
sizes for each round."""

LIMIT = 0x00
POT_LIMIT = 0x01
NO_LIMIT = 0x02

def __init__(self,
type,
ante,
blinds,
bet_sizes=None):
"""Create a betting structure for the game.
type should one of TYPE_LIMT, TYPE_POT_LIMIT, TYPE_NO_LIMIT.
ante should be the size of the ante each player pays per round.
blind should be an array of blinds, from small to large.
bet_sizes should be an array of the bet size for each round of betting.
Or None if minimum bet is set by the big blind.
"""
# TODO: Sanity check arguments
self.type = type
self.ante = ante
self.blinds = blinds
if bet_sizes is None:
bet_sizes = max(blinds)
self.bet_sizes = bet_sizes

def is_limit(self):
"""Return True if this is a limit structure"""
return self.type == self.LIMIT

def is_pot_limit(self):
"""Return True if this is a pot limit structure"""
return self.type == self.POT_LIMIT

def is_no_limit(self):
"""Return True if this is a no limit structure"""
return self.type == self.NO_LIMIT

def get_ante(self):
"""Return the amount of the ante"""
return self.ante

def get_blinds(self):
"""Return an array of blinds from smallest to largest.
Returns None if there are no blinds."""
return self.blinds

def get_minimum_bet(self, betting_round):
"""Return the minimum bet for the given betting round"""
if isinstance(self.bet_sizes, list):
return self.bet_sizes[betting_round]
else:
# One minimum for all rounds
return self.bet_sizes

def validate_bet(self, action, game_state):
"""Make sure bet represented by given action is legal.
Raises InvalidAtionException if not legal."""
# XXX This is assuming limit.
# XXX This is ignoring a double bet allowed if pair showing
bet_size = self.bet_sizes[game_state.betting_round]
if action.amount != bet_size:
raise InvalidActionException("Invalid bet size: %d != %d" % \
(action.amount, bet_size))

50 changes: 49 additions & 1 deletion unittests/test-PokerGame.py
Expand Up @@ -14,7 +14,7 @@
from pyPoker.PokerGame import \
Action, InvalidActionException, \
BettingRound, HandState, \
MessageHandler, Pot, Result, Simulator, Stats, \
MessageHandler, Pot, Result, Simulator, Stats, Structure, \
PokerGameStateException
from pyPoker.PokerRank import PokerRank
from pyPoker.Ranker import Ranker
Expand Down Expand Up @@ -481,5 +481,53 @@ def test_HandState(self):
self.assertEqual(len(hand_state.betting_rounds), 2)
self.assertIsNotNone(hand_state.dump_to_string())

def test_Structure_limit(self):
"""Test a Limit Structure class"""
ante = 10
blinds = [50,100]
min_bets = [100,100,200,200]
structure = Structure(Structure.LIMIT, ante=ante,
blinds=blinds, bet_sizes=min_bets)
self.assertIsNotNone(structure)
self.assertTrue(structure.is_limit())
self.assertFalse(structure.is_pot_limit())
self.assertFalse(structure.is_no_limit())
self.assertEqual(structure.get_ante(), ante)
self.assertListEqual(structure.get_blinds(), blinds)
betting_round = 1
self.assertEqual(
structure.get_minimum_bet(betting_round=betting_round),
min_bets[betting_round-1])

def test_Structure_pot_limit(self):
"""Test a Limit Structure class"""
ante = 0
blinds = [10,20]
structure = Structure(Structure.POT_LIMIT, ante=ante, blinds=blinds)
self.assertIsNotNone(structure)
self.assertFalse(structure.is_limit())
self.assertTrue(structure.is_pot_limit())
self.assertFalse(structure.is_no_limit())
self.assertEqual(structure.get_ante(), ante)
self.assertListEqual(structure.get_blinds(), blinds)
# minimum bet should be big blind for all rounds
betting_round = 1
self.assertEqual(structure.get_minimum_bet(betting_round=betting_round),
max(blinds))

def test_Structure_no_limit(self):
"""Test a Limit Structure class"""
ante = 5
blinds = [10]
structure = Structure(Structure.LIMIT, ante=ante, blinds=blinds)
self.assertIsNotNone(structure)
self.assertTrue(structure.is_limit())
self.assertEqual(structure.get_ante(), ante)
self.assertListEqual(structure.get_blinds(), blinds)
# minimum bet should be big blind for all rounds
betting_round = 1
self.assertEqual(structure.get_minimum_bet(betting_round=betting_round),
max(blinds))

if __name__ == "__main__":
unittest.main()

0 comments on commit e2d6b31

Please sign in to comment.