Esempio n. 1
0
    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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 7
0
    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)
Esempio n. 8
0
    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)
Esempio n. 11
0
    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)
Esempio n. 12
0
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
Esempio n. 13
0
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
Esempio n. 14
0
 def test_create_card_from_short_name_too_long(self):
     with self.assertRaisesRegexp(ValueError, 'Invalid short name'):
         card.create_card_from_short_name('xyz')
Esempio n. 15
0
 def test_create_card_from_short_name_invalid_suit(self):
     with self.assertRaisesRegexp(ValueError, 'Invalid suit character'):
         card.create_card_from_short_name('tq')
Esempio n. 16
0
 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)
Esempio n. 17
0
 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)
Esempio n. 18
0
 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)
Esempio n. 19
0
 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)
Esempio n. 20
0
 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
     ])
Esempio n. 21
0
 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])
Esempio n. 23
0
 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)