def test_get_winning_indices(self): h0_short_cards = ['as', 'ad', 'qd', 'qc', 'qs'] h0_hand = poker_hand.PokerHand(cards=[ card.create_card_from_short_name(sn) for sn in h0_short_cards ]) h1_short_cards = ['as', '2s', '3s', '4s', '5s'] h1_hand = poker_hand.PokerHand(cards=[ card.create_card_from_short_name(sn) for sn in h1_short_cards ]) h2_short_cards = ['qs', '2s', '3s', '4s', '5s'] h2_hand = poker_hand.PokerHand(cards=[ card.create_card_from_short_name(sn) for sn in h2_short_cards ]) index_to_hand_dict = { 0: h0_hand, 1: h1_hand, 2: h2_hand, } mcr = monte_carlo_runner.MonteCarloRunner([]) self.assertItemsEqual([1], mcr._get_winning_indices(index_to_hand_dict))
def test_get_base_hand_from_cards(self): short_names = ['2h', '2c', '2s', '9s', 'qh', '4d', '2d'] cards = [card.create_card_from_short_name(s) for s in short_names] expected_short_names = ['2h', '2c', '2s', '2d', 'qh'] expected_cards = [ card.create_card_from_short_name(s) for s in expected_short_names] self.assertItemsEqual( expected_cards, poker_hand.get_best_hand_from_cards(cards).cards)
def test_hand_eq_ne(self): lhs_short = ['as', 'ad'] lhs_cards = [card.create_card_from_short_name(sn) for sn in lhs_short] lhs_he_hand = poker_hand.HoldemHand(cards=lhs_cards) rhs_short = ['ks', 'kd'] rhs_cards = [card.create_card_from_short_name(sn) for sn in rhs_short] rhs_he_hand = poker_hand.HoldemHand(cards=rhs_cards) self.assertNotEqual(lhs_he_hand, rhs_he_hand)
def test_parse_string_into_cards_space_sep(self): card_input = 'ah ad qs qc' cards = poker_hand.parse_string_into_cards(card_input) self.assertEqual(4, len(cards)) self.assertIn(card.create_card_from_short_name('ah'), cards) self.assertIn(card.create_card_from_short_name('ad'), cards) self.assertIn(card.create_card_from_short_name('qs'), cards) self.assertIn(card.create_card_from_short_name('qc'), cards)
def test_hand_eq_ne(self): lhs_short = ['as', 'ad'] lhs_cards = [card.create_card_from_short_name(sn) for sn in lhs_short] lhs_he_hand = poker_hand.HoldemHand(cards=lhs_cards) rhs_short = ['ks', 'kd'] rhs_cards = [card.create_card_from_short_name(sn) for sn in rhs_short] rhs_he_hand = poker_hand.HoldemHand(cards=rhs_cards) self.assertNotEqual(lhs_he_hand, rhs_he_hand)
def test_parse_string_into_cards_space_sep(self): card_input = 'ah ad qs qc' cards = poker_hand.parse_string_into_cards(card_input) self.assertEqual(4, len(cards)) self.assertIn(card.create_card_from_short_name('ah'), cards) self.assertIn(card.create_card_from_short_name('ad'), cards) self.assertIn(card.create_card_from_short_name('qs'), cards) self.assertIn(card.create_card_from_short_name('qc'), cards)
def test_get_base_hand_from_cards(self): short_names = ['2h', '2c', '2s', '9s', 'qh', '4d', '2d'] cards = [card.create_card_from_short_name(s) for s in short_names] expected_short_names = ['2h', '2c', '2s', '2d', 'qh'] expected_cards = [ card.create_card_from_short_name(s) for s in expected_short_names ] self.assertItemsEqual(expected_cards, poker_hand.get_best_hand_from_cards(cards).cards)
def test_remove_cards_from_deck(self): d = deck.Deck() d.reset_and_shuffle() cards_to_remove = [ card.create_card_from_short_name('ah'), card.create_card_from_short_name('5d') ] d.remove_cards_from_deck(cards_to_remove) self.assertEqual(50, len(d.cards)) for c in cards_to_remove: self.assertNotIn(c, d.cards)
def test_get_winning_indices_multiple_winners(self): h0_short_cards = ['as', 'ad', 'qd', 'qc', 'qs'] h0_hand = poker_hand.PokerHand( cards=[card.create_card_from_short_name(sn) for sn in h0_short_cards]) h1_short_cards = ['as', 'ad', 'qd', 'qc', 'qs'] h1_hand = poker_hand.PokerHand( cards=[card.create_card_from_short_name(sn) for sn in h1_short_cards]) index_to_hand_dict = { 0: h0_hand, 1: h1_hand, } mcr = monte_carlo_runner.MonteCarloRunner([]) self.assertItemsEqual( [0, 1], mcr._get_winning_indices(index_to_hand_dict))
def test_parse_hands_into_holdem_hands_impossible_pair(self): hand_input = 'tt' short_names = ['th', 'ts', 'td'] used_cards = [card.create_card_from_short_name(sn) for sn in short_names] with self.assertRaises(poker_hand.InvalidHandSpecification): poker_hand.parse_hands_into_holdem_hands( hand_input, used_cards=used_cards)
def test_parse_hands_into_holdem_hands_impossible_pair(self): hand_input = 'tt' short_names = ['th', 'ts', 'td'] used_cards = [ card.create_card_from_short_name(sn) for sn in short_names ] with self.assertRaises(poker_hand.InvalidHandSpecification): poker_hand.parse_hands_into_holdem_hands(hand_input, used_cards=used_cards)
def parse_string_into_cards(card_input): """Parses a string of characters into Card objects. Args: card_input: str, representing cards. May be space or comma separated. Returns: list of Cards. """ # Strip it down to characters only. card_input = card_input.lower().replace(' ', '').replace(',', '') cards = [] for short_name_tuple in zip(card_input[::2], card_input[1::2]): cards.append( card.create_card_from_short_name(''.join(short_name_tuple))) return cards
def parse_string_into_cards(card_input): """Parses a string of characters into Card objects. Args: card_input: str, representing cards. May be space or comma separated. Returns: list of Cards. """ # Strip it down to characters only. card_input = card_input.lower().replace(' ', '').replace(',', '') cards = [] for short_name_tuple in zip(card_input[::2], card_input[1::2]): cards.append( card.create_card_from_short_name(''.join(short_name_tuple))) return cards
def test_create_card_from_short_name_too_long(self): with self.assertRaisesRegexp(ValueError, 'Invalid short name'): card.create_card_from_short_name('xyz')
def test_create_card_from_short_name_invalid_suit(self): with self.assertRaisesRegexp(ValueError, 'Invalid suit character'): card.create_card_from_short_name('tq')
def assertHandMatchesShortCards(self, short_cards, hand): expected_hand = poker_hand.HoldemHand( cards=[card.create_card_from_short_name(sn) for sn in short_cards]) self.assertEqual(expected_hand, hand)
def test_parse_hands_into_holdem_hands_invalid_explicit(self): hand_input = 'asad' used_cards = [card.create_card_from_short_name('As')] with self.assertRaises(poker_hand.InvalidHandSpecification): poker_hand.parse_hands_into_holdem_hands(hand_input, used_cards=used_cards)
def test_create_card_from_short_name_valid(self): c = card.create_card_from_short_name('th') self.assertEqual('Ten', c.rank) self.assertEqual('Hearts', c.suit)
def test_create_card_from_short_name_valid_uppercase(self): c = card.create_card_from_short_name('As') self.assertEqual('Ace', c.rank) self.assertEqual('Spades', c.suit)
def _create_poker_hand_from_short_name_list(self, short_name_list): return poker_hand.PokerHand(cards=[ card.create_card_from_short_name(sn) for sn in short_name_list ])
def test_short_form(self): c = card.create_card_from_short_name('as') self.assertEqual('As', c.short_form()) c = card.create_card_from_short_name('9s') self.assertEqual('9s', c.short_form())
def _create_poker_hand_from_short_name_list(self, short_name_list): return poker_hand.PokerHand( cards=[card.create_card_from_short_name(sn) for sn in short_name_list])
def test_create_card_from_short_name_invalid_rank(self): with self.assertRaisesRegexp(ValueError, 'Invalid rank character'): card.create_card_from_short_name('zh')
def assertHandMatchesShortCards(self, short_cards, hand): expected_hand = poker_hand.HoldemHand(cards=[ card.create_card_from_short_name(sn) for sn in short_cards]) self.assertEqual(expected_hand, hand)
def test_parse_hands_into_holdem_hands_invalid_explicit(self): hand_input = 'asad' used_cards = [card.create_card_from_short_name('As')] with self.assertRaises(poker_hand.InvalidHandSpecification): poker_hand.parse_hands_into_holdem_hands( hand_input, used_cards=used_cards)