def test_constructor_with_table(self): """Checks that adding table cards works, and that quads always win""" hand = Hand(Card(10, C.SPADES), Card(3, C.SPADES)) table_cards = [Card(3, C.CLUBS), Card(3, C.HEARTS), Card(3, C.DIAMONDS)] simulator = HandSimulator(hand, table_cards) self.assertEqual(len(simulator.deck), 47) win_percentage = simulator.simulate(5) self.assertEqual(win_percentage, 100)
def __init__(self): self.stacks = {'bot_0': 1000} self.to_call = 20 self.pot = 140 self.big_blind = 20 self.hand = Hand(Card(C.ACE, C.DIAMONDS), Card(C.ACE, C.HEARTS)) self.table_cards = [] self.time_per_move = 500 self.me = 'bot_0' self.bets = {} self.preflop_fear = -1 self.hand_fear = pokeher.handscore.HandScore()
def test_river_betting(self): """Sanity tests of betting with common cards""" self.data.table_cards = [ Card(C.ACE, C.SPADES), Card(2, C.DIAMONDS), Card(7, C.SPADES) ] bot, brain = self.brain() brain.data = self.data brain.iterations = 100 # smaller for unit tests brain.do_turn('bot_0', 500) self.assertTrue(bot.raise_amount > 0)
def test_constructor(self): """Sanity checks on the HandSimulator initial state""" hand = Hand(Card(10, C.SPADES), Card(4, C.DIAMONDS)) simulator = HandSimulator(hand) self.assertFalse(simulator.table_cards) self.assertEqual(len(simulator.deck), 50) self.assertFalse(Card(10, C.SPADES) in simulator.deck) self.assertFalse(Card(4, C.DIAMONDS) in simulator.deck) win_percentage = simulator.simulate(10) self.assertTrue(isinstance(win_percentage, float)) self.assertTrue(win_percentage > 0)
def test_preflop_sanity(self): """Pull some stuff from the preflop equity and spot check it""" bot = BrainTestBot(self.fake_in, self.fake_out, self.fake_log) bad_hand = Hand(Card(2, C.CLUBS), Card(3, C.DIAMONDS)) sample_key = bad_hand.simple() self.assertTrue(sample_key in bot.brain.preflop_equity.keys()) bad_equity = bot.brain.preflop_equity[sample_key] self.assertTrue(bad_equity > 0.2) good_hand = Hand(Card(C.ACE, C.SPADES), Card(C.ACE, C.DIAMONDS)) good_equity = bot.brain.preflop_equity[good_hand.simple()] self.assertTrue(good_equity > bad_equity)
def test_timing_out(self): """Tests that the brain simulator doesn't run forever""" self.data.table_cards = [ Card(C.ACE, C.SPADES), Card(2, C.DIAMONDS), Card(7, C.SPADES) ] bot, brain = self.brain() brain.data = self.data brain.iterations = 10000 # will time out with Timer() as t: brain.do_turn('bot_0', 250) self.assertTrue(bot.raise_amount > 0) self.assertTrue(t.secs < 0.250)
def test_time_per_move(self): """Tests that the brain uses time_per_move over the max time""" self.data.table_cards = [ Card(C.ACE, C.SPADES), Card(2, C.DIAMONDS), Card(7, C.SPADES) ] bot, brain = self.brain() brain.data = self.data brain.data.time_per_move = 250 brain.iterations = 10000 # will time out with Timer() as t: brain.do_turn('bot_0', 10000) self.assertTrue(bot.raise_amount > 0) self.assertTrue(t.secs < 0.250)
def test_min_hand(self): """Verifies that the minimum score filter works""" ace = Card(C.ACE, C.HEARTS) king = Card(C.KING, C.SPADES) three = Card(3, C.HEARTS) two = Card(2, C.SPADES) seven = Card(7, C.HEARTS) cards = [ace, king, three, two] hand = Hand(king, seven) simulator = HandSimulator(hand, cards) # KK should win pretty often self.assertGreater(simulator.simulate(100), 50) min_hand = HandScore(C.TRIPS) # but not against other trips self.assertLess(simulator.simulate(100, min_hand=min_hand), 5)
def test_preflop_big_raise(self): """Sanity test of a big preflop raise (switches to sim equity)""" bot, brain = self.brain() # preflop A-9 suited => 63.044 win % self.data.hand = Hand(Card(C.ACE, C.SPADES), Card(9, C.SPADES)) # opponent made a huge raise from BB self.data.to_call = 1000 self.data.pot = 1040 self.data.stacks = {'bot_0': 2000} brain.data = self.data # 49% pot odds self.assertAlmostEqual(brain.pot_odds(), 100 * 1000 / 2040) brain.do_turn('bot_0', 200) # should fold even though 63% to win / 49% pot odds is positive return # because the opponent's huge raise indicates they have above average # hand strength self.assertEqual(bot.raise_amount, 0) self.assertEqual(bot.bet_amount, 0)
def calculate_best_mapping(self, num_cards): """Iterates through a full suite """ cards = Card.full_deck() mapping = {} for hand in itertools.combinations(cards, num_cards): (best_hand, score) = HandBuilder(hand).find_hand() mapping[hand] = best_hand outfile = 'best_hand_{num}.pickle'.format(num=num_cards) outf = open(outfile, 'wb') pickle.dump(mapping, outf) outf.close()
def test_hand_filter(self): equity = pokeher.preflop_equity.PreflopEquity() hand = Hand(Card(10, C.SPADES), Card(3, C.SPADES)) simulator = HandSimulator(hand, [], preflop_equity=equity.data) ace = Card(C.ACE, C.HEARTS) ace2 = Card(C.ACE, C.SPADES) king = Card(C.KING, C.SPADES) three = Card(3, C.HEARTS) two = Card(2, C.SPADES) # aces win 84% of the time preflop self.assertTrue(simulator.passes_filter(ace, ace2, 30)) self.assertFalse(simulator.passes_filter(ace, ace2, 95)) self.assertTrue(simulator.passes_filter(ace, king, 65)) self.assertTrue(simulator.passes_filter(king, three, 30)) self.assertFalse(simulator.passes_filter(three, two, 40))