Ejemplo n.º 1
0
 def test_choose_best_hand_single_match(self):
     best = score.choose_best_hand(
         Card.from_str_list('2d, 3d, 4d, 5d, 9c, 9s'), 4)
     self.assertEqual(1, len(best))
     self.assertEqual(8, best[0][0])
     self.assertTrue(
         set(Card.from_str_list('2d, 3d, 4d, 5d')) in
         [set(x[1]) for x in best])
Ejemplo n.º 2
0
 def test_nob(self):
     self.assertEqual(
         1,
         score.score_hand(Card.from_str_list('Jc, 2c, Qc, 6h'),
                          Card.from_str('4c')))
     self.assertEqual(
         0,
         score.score_hand(Card.from_str_list('Jc, 2c, Qc, 6h'),
                          Card.from_str('4s')))
Ejemplo n.º 3
0
 def test_card_constructor_throws_appropriate_exceptions(self):
     with self.assertRaises(ValueError):
         Card('15', 'S')
     with self.assertRaises(ValueError):
         Card('xx', 'S')
     with self.assertRaises(ValueError):
         Card('10', 'NotExist')
     with self.assertRaises(ValueError):
         Card('0', 'S')
Ejemplo n.º 4
0
    def test_cards_from_str_list(self):
        cards = Card.from_str_list('AD,1D,10S')
        self.assertEqual(cards[0], self.ace_of_diamonds)
        self.assertEqual(cards[1], self.ace_of_diamonds)
        self.assertEqual(cards[2], self.ten_of_spades)

        Card.from_str_list('AD, 1D, 10S')

        with self.assertRaises(ValueError):
            Card.from_str_list('AD,1x,10S')
Ejemplo n.º 5
0
 def test_choose_best_hand_multiple_matches(self):
     best = score.choose_best_hand(Card.from_str_list('ac,2c,9c,7c,3s,kd'),
                                   4)
     self.assertEqual(2, len(best))
     self.assertEqual(5, best[0][0])
     self.assertTrue(
         set(Card.from_str_list('AC,2C,9C,3S')) in
         [set(x[1]) for x in best])
     self.assertTrue(
         set(Card.from_str_list('AC, 2C, 3S, KD')) in
         [set(x[1]) for x in best])
Ejemplo n.º 6
0
    def test_pegging(self):
        dealer = Card.from_str_list('AD, 3D, 5D, 7S')
        non_dealer = Card.from_str_list('QH, 10H, 7H, 5C')

        self._game.pegging(self._p1, self._p2, dealer, non_dealer,
                           Card.from_str('KC'), self._board)

        #  QH, AD, 10H, 3D, 7H = 31 scored by non_dealer 2+1 points
        #  5D 5C 7S = pair for non dealer 2, last card for dealer, 1 point

        self.assertEqual((1, 0, 0), self._collector.averages(self._p1))
        self.assertEqual((4, 0, 0), self._collector.averages(self._p2))
Ejemplo n.º 7
0
 def next_pegging_card(self, stack, hand, turn_card):
     if not hand:
         return None
     print(f'\nYour hand is {hand}')
     stack_total = sum([x.value for x in stack])
     go_allowed = (min([x.value for x in hand]) + stack_total) > 31
     while True:
         try:
             user_input = input('\nWhat will you peg next (return for GO)? ')
             if not user_input:
                 if go_allowed:
                     return None
                 else:
                     print('GO not allowed, you can play')
                     continue
             card = Card.from_str(user_input)
             if card in hand:
                 if (card.value + stack_total) > 31:
                     print('Total would be more than 31, try again')
                 else:
                     return card
             else:
                 print('Selection is not a subset of your cards_deprecated, try again')
         except ValueError as e:
             print(f'Input error "{e}"')
Ejemplo n.º 8
0
 def test_flush_box(self):
     self.assertEqual(
         5,
         score.score_hand(Card.from_str_list('ac,2c,9c,7c'),
                          Card.from_str('Jc'),
                          is_box=True))
     self.assertEqual(
         0,
         score.score_hand(Card.from_str_list('ac,2c,9c,7s'),
                          Card.from_str('Jc'),
                          is_box=True))
     self.assertEqual(
         0,
         score.score_hand(Card.from_str_list('ac,2c,9c,7c'),
                          None,
                          is_box=True))
Ejemplo n.º 9
0
    def test_score_pegging_stack_no_score(self):
        stack = []
        sc, desc = score.score_pegging_stack(stack)
        self.assertEqual(0, sc)

        stack = Card.from_str_list('AC')
        sc, desc = score.score_pegging_stack(stack)
        self.assertEqual(0, sc)

        stack = Card.from_str_list('AC, 2S, 5D')
        sc, desc = score.score_pegging_stack(stack)
        self.assertEqual(0, sc)

        stack = Card.from_str_list('AC, 2S, AD')
        sc, desc = score.score_pegging_stack(stack)
        self.assertEqual(0, sc)
Ejemplo n.º 10
0
    def test_complex_hands(self):
        sc, bd = score.score_hand_with_breakdown(
            Card.from_str_list('2d, 3d, 4d, 5d'), Card.from_str('5s'))
        self.assertEqual(1, len(bd['pairs']))
        self.assertEqual(2, len(bd['runs']))
        self.assertEqual(1, len(bd['flushes']))
        self.assertEqual(1, len(bd['fifteens']))
        self.assertEqual(16, sc)

        str_rep = '''Runs     : [2D, 3D, 4D, 5D], [2D, 3D, 4D, 5S]
Flushes  : [2D, 3D, 4D, 5D]
Fifteens : [2D, 3D, 5D, 5S]
Pairs    : [5D, 5S]'''

        self.assertEqual(str_rep, score.breakdown_tostring(bd))

        sc, bd = score.score_hand_with_breakdown(
            Card.from_str_list('jc, qc, kc, jd'), Card.from_str('js'))
        self.assertEqual(3, len(bd['pairs']))
        self.assertEqual(3, len(bd['runs']))
        self.assertEqual(15, sc)

        self.assertEqual(
            29,
            score.score_hand(Card.from_str_list('jc,5d,5h,5s'),
                             Card.from_str('5c')))
Ejemplo n.º 11
0
 def test_pairs(self):
     self.assertEqual(
         4,
         score.score_hand(Card.from_str_list('JS, JD, 6C, 2H'),
                          Card.from_str('2C')))
     self.assertEqual(
         2,
         score.score_hand(Card.from_str_list('QS, JD, 6C, 2H'),
                          Card.from_str('2C')))
     self.assertEqual(
         2,
         score.score_hand(Card.from_str_list('JS, QD, 3C, 3H'),
                          Card.from_str('4C')))
     self.assertEqual(
         4,
         score.score_hand(Card.from_str_list('JS, JD, 3C, 3H'),
                          Card.from_str('AC')))
Ejemplo n.º 12
0
 def choose_discards(self, hand, your_box):
     print(f'\nYour dealt cards_deprecated are: {hand}')
     while True:
         try:
             user_input = None
             while not user_input:
                 user_input = input('\nWhat will you discard? ')
             cards = Card.from_str_list(user_input)
             if set(cards).issubset(set(hand)):
                 if len(set(cards)) == 2:
                     return cards
                 else:
                     print('Select two cards_deprecated please, try again')
             else:
                 print('Please select valid cards_deprecated from your hand, try again')
         except ValueError as e:
             print(f'Input error "{e}"')
Ejemplo n.º 13
0
 def test_flush(self):
     self.assertEqual(
         5,
         score.score_hand(Card.from_str_list('ac, 2c, 9c, 7c'),
                          Card.from_str('Jc')))
     self.assertEqual(
         4,
         score.score_hand(Card.from_str_list('ac, 2c, 9c, 7c'),
                          Card.from_str('Jh')))
     self.assertEqual(
         0,
         score.score_hand(Card.from_str_list('ad, 2c, 9c, 7c'),
                          Card.from_str('Jc')))
Ejemplo n.º 14
0
 def test_runs(self):
     self.assertEqual(
         4,
         score.score_hand(Card.from_str_list('2d,3d,4d'),
                          Card.from_str('5D')))
     self.assertEqual(
         8,
         score.score_hand(Card.from_str_list('2d,3d,4d'),
                          Card.from_str('4c')))
     self.assertEqual(
         17,
         score.score_hand(Card.from_str_list('2d,3d,4d,4c'),
                          Card.from_str('4s')))
Ejemplo n.º 15
0
 def test_fifteens(self):
     self.assertEqual(
         6,
         score.score_hand(Card.from_str_list('10C, QD, KH, 2H'),
                          Card.from_str('3S')))
     self.assertEqual(
         2,
         score.score_hand(Card.from_str_list('JC, 5D, 4H, 9S'),
                          Card.from_str('7S')))
     self.assertEqual(
         4,
         score.score_hand(Card.from_str_list('JC, QD, 4H, 3S'),
                          Card.from_str('AS')))
Ejemplo n.º 16
0
 def test_three_of_a_kind(self):
     self.assertEqual(
         6,
         score.score_hand(Card.from_str_list('JC, JD, JH, 2H'),
                          Card.from_str('6S')))
     self.assertEqual(
         6,
         score.score_hand(Card.from_str_list('3c, 3d, 3s, qh'),
                          Card.from_str('kh')))
     self.assertEqual(
         8,
         score.score_hand(Card.from_str_list('JC, JD, JH, 2H'),
                          Card.from_str('2S')))
Ejemplo n.º 17
0
 def test_score_pegging_stack_run_of_3(self):
     stack = Card.from_str_list('AC, JS, 2D, 3S, 4H')
     sc, desc = score.score_pegging_stack(stack)
     self.assertEqual(3, sc)
Ejemplo n.º 18
0
 def test_flush_no_turn(self):
     self.assertEqual(
         4, score.score_hand(Card.from_str_list('ac, 2c, 9c, 7c'), None))
Ejemplo n.º 19
0
 def test_four_of_a_kind(self):
     self.assertEqual(
         12,
         score.score_hand(Card.from_str_list('QC, QD, QH, QS'),
                          Card.from_str('6S')))
Ejemplo n.º 20
0
 def test_score_pegging_stack_run_of_7(self):
     stack = Card.from_str_list('AD, 2D, 3S, 4H, 5H, 6H, 7C')
     sc, desc = score.score_pegging_stack(stack)
     self.assertEqual(7, sc)
Ejemplo n.º 21
0
 def test_pairs_no_turn(self):
     self.assertEqual(
         2, score.score_hand(Card.from_str_list('JS, JD, 6C, 2H'), None))
     self.assertEqual(
         0, score.score_hand(Card.from_str_list('QS, JD, 6C, 2H'), None))
Ejemplo n.º 22
0
 def setUp(self):
     self.ace_of_diamonds = Card(1, 'D')
     self.ten_of_spades = Card(10, 'S')
     self.five_of_clubs = Card(5, 'C')
     self.jack_of_hearts = Card(11, 'H')
Ejemplo n.º 23
0
 def test_score_pegging_stack_run_of_4(self):
     stack = Card.from_str_list('2D, 3S, 4H, 5H')
     sc, desc = score.score_pegging_stack(stack)
     self.assertEqual(4, sc)
Ejemplo n.º 24
0
 def test_score_pegging_stack_4_of_a_kind(self):
     stack = Card.from_str_list('AC, 2S, 2D, 2S, 2H')
     sc, desc = score.score_pegging_stack(stack)
     self.assertEqual(12, sc)
Ejemplo n.º 25
0
 def test_score_hand_with_breakdown(self):
     sc, bd = score.score_hand_with_breakdown(
         Card.from_str_list('JS, JD, 6C, 2H'), Card.from_str('2C'))
     self.assertEqual(4, sc)
     self.assertEqual(2, len(bd['pairs']))
Ejemplo n.º 26
0
    def test_from_str(self):
        self.assertEqual(Card.from_str('AD'), self.ace_of_diamonds)
        self.assertEqual(Card.from_str('1D'), self.ace_of_diamonds)
        self.assertEqual(Card.from_str('ad'), self.ace_of_diamonds)
        self.assertEqual(Card.from_str('1d'), self.ace_of_diamonds)

        self.assertEqual(Card.from_str('10S'), self.ten_of_spades)
        self.assertEqual(Card.from_str('10s'), self.ten_of_spades)

        self.assertEqual(Card.from_str('5C'), self.five_of_clubs)
        self.assertEqual(Card.from_str('5c'), self.five_of_clubs)

        self.assertEqual(Card.from_str('JH'), self.jack_of_hearts)
        self.assertEqual(Card.from_str('Jh'), self.jack_of_hearts)
        self.assertEqual(Card.from_str('11H'), self.jack_of_hearts)
        self.assertEqual(Card.from_str('11h'), self.jack_of_hearts)
Ejemplo n.º 27
0
 def test_set_equality(self):
     c1 = Card.from_str('JH')
     c2 = Card.from_str('JH')
     self.assertEqual(c1, c2)
     self.assertEqual({c1}, {c2})
Ejemplo n.º 28
0
 def test_sort(self):
     cards = standard_deck()
     random.shuffle(cards)
     cards = sorted(cards)
     self.assertEqual(Card.from_str_list('AC,AD,AH,AS,2C,2D,2H,2S'),
                      cards[0:8])