def run(self, tries): """Calculates the win % for each preflop hand, returns the mapping""" cards = c.full_deck() self.wins = {} self.tries = tries for count, two_cards in enumerate(itertools.combinations(cards, 2)): t1 = time.clock() hand = c.Hand(two_cards[0], two_cards[1]) hand_string = hand.simple() if self.wins.get(hand_string) is not None: print "Skipping {}. Already computed its simple representation ({})" \ .format(hand, hand_string) continue simulator = HandSimulator(hand) percent_pots_won = simulator.simulate(tries) self.wins[hand_string] = percent_pots_won print ' {hand} ({s}) won {percent}% in {tries} tries in {t} seconds' \ .format(hand=hand, s=hand_string, tries=tries, percent=percent_pots_won, t=(time.clock() - t1)) # 52 choose 2 == 1326 # We go through everything even though we only calculate 169 hands percent_done = MathUtils.percentage(count, 1326) print ' Finished hand {c} ({p:.2f}%)' \ .format(c=count, p=percent_done) print '-' * 10
def run(self, tries): """Calculates the win % for each preflop hand, returns the mapping""" cards = c.full_deck() self.wins = {} self.tries = tries for count, two_cards in enumerate(itertools.combinations(cards, 2)): t1 = time.clock() hand = c.Hand(two_cards[0], two_cards[1]) hand_string = hand.simple() if self.wins.get(hand_string) is not None: print "Skipping {}. Already computed its simple representation ({})" \ .format(hand, hand_string) continue simulator = HandSimulator(hand) percent_pots_won = simulator.simulate(tries) self.wins[hand_string] = percent_pots_won print ' {hand} ({s}) won {percent}% in {tries} tries in {t} seconds' \ .format(hand=hand, s=hand_string, tries=tries, percent=percent_pots_won, t=(time.clock() - t1)) # 52 choose 2 == 1326 # We go through everything even though we only calculate 169 hands percent_done = MathUtils.percentage(count, 1326) print ' Finished hand {c} ({p:.2f}%)' \ .format(c=count, p=percent_done) print '-'*10
def run(self, tries): """Calculates the win % for each preflop hand, returns the mapping""" cards = c.full_deck() self.wins = {} self.tries = tries count = 0 for two_cards in itertools.combinations(cards, 2): t1 = time.clock() hand = c.Hand(two_cards[0], two_cards[1]) simulator = HandSimulator(hand) percent_pots_won = simulator.simulate(tries) self.wins[repr(hand)] = percent_pots_won print ' {hand} won {percent}% in {tries} tries in {t} seconds' \ .format(hand=hand, tries=tries, percent=percent_pots_won, t=(time.clock() - t1)) count += 1 # 52 choose 2 == 1326 percent_done = MathUtils.percentage(count, 1326) print ' Finished hand {c} ({p}%)' \ .format(c=count, p=round(percent_done)) print '-'*10
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_constructor_with_table(self): """Checks that adding table cards works, and that 4ok always wins""" 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 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 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)