Пример #1
0
    def test_remove_pairs(self):
        '''
        Test that remove_pairs() works as specified
        '''
        # Case 1: 13 pairs & 0 unpaired cards
        d1 = hw5_cards_ec2.Deck()
        h1 = hw5_cards_ec2.Hand(d1.deal_hand(13 * 2))
        h1.remove_pairs()
        self.assertEqual(len(h1.init_cards), 0)

        # Case 2: 13 pairs & 6 unpaired cards (7 pairs & 6 three of a kind)
        d2 = hw5_cards_ec2.Deck()
        h2 = hw5_cards_ec2.Hand(d2.deal_hand(13 * 2 + 6))
        h2.remove_pairs()
        self.assertEqual(len(h2.init_cards), 6)

        # Case 3: no pairs & 7 unpaired cards
        d2 = hw5_cards_ec2.Deck()
        h2 = hw5_cards_ec2.Hand(d2.deal_hand(7))
        h2.remove_pairs()
        self.assertEqual(len(h2.init_cards), 7)

        # Case 4: 7 four of a kind & 6 three of a kind
        d3 = hw5_cards_ec2.Deck()
        h3 = hw5_cards_ec2.Hand(d3.deal_hand(13 * 3 + 7))
        h3.remove_pairs()
        self.assertEqual(len(h3.init_cards), 6)
Пример #2
0
 def testPairs(self):
     """Give a variety of 5-card hands, check the number of cards in each hand
      after pairs are removed
      """
     c1 = hw5_cards_ec2.Card(0, 4)
     c2 = hw5_cards_ec2.Card(1, 4)
     c3 = hw5_cards_ec2.Card(2, 4)
     c4 = hw5_cards_ec2.Card(3, 4)
     c5 = hw5_cards_ec2.Card(0, 5)
     c6 = hw5_cards_ec2.Card(1, 5)
     c7 = hw5_cards_ec2.Card(3, 6)
     c8 = hw5_cards_ec2.Card(2, 7)
     c9 = hw5_cards_ec2.Card(3, 8)
     two_pair = [c1, c2, c7, c6, c5]
     full_house = [c1, c2, c3, c6, c5]  #Check if there is three of a kind
     no_pair = [c1, c5, c7, c8, c9]
     four_of_a_kind = [c1, c2, c3, c4, c5]
     h1 = hw5_cards_ec2.Hand(two_pair)
     h2 = hw5_cards_ec2.Hand(full_house)
     h3 = hw5_cards_ec2.Hand(no_pair)
     h4 = hw5_cards_ec2.Hand(four_of_a_kind)
     h1.remove_pairs()
     h2.remove_pairs()
     h3.remove_pairs()
     h4.remove_pairs()
     self.assertEqual(len(h1.cards), 1)
     self.assertEqual(len(h2.cards), 1)
     self.assertEqual(len(h3.cards), 5)
     self.assertEqual(len(h4.cards), 1)
Пример #3
0
    def testremovePairs(self):
        c1 = hw5_cards_ec2.Card(suit=0, rank=2)
        c2 = hw5_cards_ec2.Card(suit=1, rank=2)
        init_list = [c1, c2]
        #2, 2
        hand1 = hw5_cards_ec2.Hand(init_list)
        hand1.remove_pairs()
        self.assertEqual(len(hand1.init_card), 0)

        c3 = hw5_cards_ec2.Card(suit=2, rank=2)
        init_list = [c1, c2, c3]
        #2, 2, 2
        hand2 = hw5_cards_ec2.Hand(init_list)
        hand2.remove_pairs()
        self.assertEqual(len(hand2.init_card), 1)

        c4 = hw5_cards_ec2.Card(suit=3, rank=2)
        c5 = hw5_cards_ec2.Card(suit=3, rank=5)
        c6 = hw5_cards_ec2.Card(suit=1, rank=5)
        c7 = hw5_cards_ec2.Card(suit=2, rank=5)
        init_list = [c1, c2, c3, c4, c5, c6, c7]
        # 2, 2, 2, 2, 5, 5, 5
        hand2 = hw5_cards_ec2.Hand(init_list)
        hand2.remove_pairs()
        self.assertEqual(len(hand2.init_card), 1)
Пример #4
0
 def test_remove(self):
     card1=hw5.Card(1,5)
     card2=hw5.Card(2,5)
     card3 = hw5.Card(0, 5)
     card_list=[card1,card2,card3]
     hand=ec2.Hand(card_list)
     hand.remove_pairs()
     return self.assertEqual(len(hand.init_card),1)
Пример #5
0
 def test_1_remove_card(self):
     '''basic test on remove only one pair
     '''
     c1 = hw5_cards_ec2.Card(0, 1)
     c2 = hw5_cards_ec2.Card(0, 2)
     c3 = hw5_cards_ec2.Card(1, 2)
     hand1 = [c1, c2, c3]
     h1 = hw5_cards_ec2.Hand(hand1)
     h1.remove_pairs()
     self.assertEqual(h1.init_cards[0], c1)
Пример #6
0
    def testAddAndRemove(self):
        '''
        Test that add_card( ) and remove_card( ) behave as specified.
        '''
        c1 = hw5_cards_ec2.Card(0, 2)
        c2 = hw5_cards_ec2.Card(1, 1)
        c3 = hw5_cards_ec2.Card(2, 12)
        c4 = hw5_cards_ec2.Card(3, 6)
        c5 = hw5_cards_ec2.Card(0, 13)
        c6 = hw5_cards_ec2.Card(1, 3)

        list_init_cards = [c1, c2, c3, c4, c5]
        h1 = hw5_cards_ec2.Hand(
            list_init_cards)  # Initialize a hand with five cards
        h2 = hw5_cards_ec2.Hand(
            list_init_cards)  # Initialize a hand with five cards
        num_init_cards = len(h1.init_cards)

        # Test add_card()
        h1.add_card(c3)  # Add the card that is already in the hand
        self.assertEqual(
            len(h1.init_cards),
            num_init_cards)  # Check if the number of cards does not change
        h1.add_card(c6)  # Add a new card
        self.assertEqual(
            len(h1.init_cards), num_init_cards +
            1)  # Check if the number of cards has increased by one
        self.assertTrue(str(c6) in [str(card) for card in h1.init_cards
                                    ])  # Check if the new card is in the hand

        # Test remove_card()
        h2.remove_card(c6)  # Remove a card that is not in the hand
        self.assertEqual(
            len(h2.init_cards),
            num_init_cards)  # Check if the number of cards does not change
        removed_card = h2.remove_card(
            c4)  # Remove the card that is in the hand
        self.assertEqual(
            len(h2.init_cards), num_init_cards -
            1)  # Check if the number of cards has decreased by one
        self.assertEqual(str(removed_card),
                         str(c4))  # Check if the removed card is correct
Пример #7
0
 def test1(self):
     d = hw5_cards_ec2.Deck() # create a deck
     d.shuffle() # shuffle the deck
     hand_size = random.randint(1,51) # generate a random hand size
     hand = d.deal_hand(hand_size) # create a temp hand list from deck also update the deck
     h = hw5_cards_ec2.Hand(hand) # create hand object 
     h.remove_pairs() # remove pairs in this hand
     card_list = [i.rank for i in h.cards] # create a list of rank
     for rank in card_list:
         self.assertFalse(card_list.count(rank)==2)
         self.assertFalse(card_list.count(rank)==4)
Пример #8
0
 def test_3_remove_card(self):
     '''test on remove two pairs
     '''
     c1 = hw5_cards_ec2.Card(0, 1)
     c2 = hw5_cards_ec2.Card(0, 2)
     c3 = hw5_cards_ec2.Card(1, 2)
     c4 = hw5_cards_ec2.Card(2, 3)
     c5 = hw5_cards_ec2.Card(3, 3)
     hand1 = [c1, c2, c3, c4, c5]
     hand2 = [c1]
     h1 = hw5_cards_ec2.Hand(hand1)
     h1.remove_pairs()
     self.assertEqual(h1.init_cards[0], c1)
Пример #9
0
 def test_2_remove_card(self):
     '''test on remove a pair from 4
     '''
     c1 = hw5_cards_ec2.Card(0, 1)
     c2 = hw5_cards_ec2.Card(0, 2)
     c3 = hw5_cards_ec2.Card(1, 2)
     c4 = hw5_cards_ec2.Card(2, 2)
     c5 = hw5_cards_ec2.Card(3, 2)
     hand1 = [c1, c2, c3, c4, c5]
     hand2 = [c1, c2, c3]
     h1 = hw5_cards_ec2.Hand(hand1)
     h1.remove_pairs()
     self.assertEqual(h1.init_cards[0], c1)
     self.assertEqual(h1.init_cards[1], c2)
     self.assertEqual(h1.init_cards[2], c3)
Пример #10
0
    def test_draw(self):
        '''
        Test that draw( ) works as specified
        '''
        d = hw5_cards_ec2.Deck()  # Initialize a deck
        h = hw5_cards_ec2.Hand([])  # Initialize a hand with no cards

        h.draw(d)  # Draw a card from the deck
        self.assertEqual(len(h.init_cards), 1)  # Check if one card is added
        self.assertEqual(
            len(d.cards), 51
        )  # Check if the number of cards in the deck has decreased by one (side effect)
        self.assertTrue(
            str(h.init_cards[0])
            not in [str(card) for card in d.cards
                    ])  # Check if the drawed card is not in the deck
Пример #11
0
    def test_remove_pairs(self):
        # construct hand_1, instance of Hand
        cards_already_in_hands = \
            [hw5_cards_ec2.Card(0, 1), hw5_cards_ec2.Card(1, 4), hw5_cards_ec2.Card(3, 11), hw5_cards_ec2.Card(3, 12)]
        hand_1 = hw5_cards_ec2.Hand(cards_already_in_hands)

        ## now there are no pairs
        len_before_remove_pairs = len(hand_1.init_card)
        str_list_before = []
        for card in hand_1.init_card:
            str_list_before.append(card.__str__())
        hand_1.remove_pairs()
        len_after_remove_pairs = len(hand_1.init_card)
        str_list_after = []
        for card in hand_1.init_card:
            str_list_after.append(card.__str__())
        self.assertEqual(len_before_remove_pairs, len_after_remove_pairs)  # make sure equal length
        for element in str_list_after:
            self.assertTrue(element in str_list_before)  # make sure str_list_before contains str_list_after
        for element in str_list_after:
            self.assertTrue(element in str_list_before)  # make sure str_list_after contains str_list_before
        # thus the two list are the same

        ## then we add pairs to hand_1
        hand_1.add_card(hw5_cards_ec2.Card(1, 1))
        hand_1.add_card(hw5_cards_ec2.Card(2, 1))  # we have 3 Aces now
        hand_1.add_card(hw5_cards_ec2.Card(0, 12))
        hand_1.add_card(hw5_cards_ec2.Card(1, 12))
        hand_1.add_card(hw5_cards_ec2.Card(2, 12))  # we have 4 Queens now
        hand_1.add_card(hw5_cards_ec2.Card(1, 11))  # we have 2 Jacks now
        # now the cards in hand_1 in 10, with 4 pairs: 4 Q, 2 J and 2 A
        # remove pairs
        random.seed(10)
        hand_1.remove_pairs()
        # now we will only have "4 of Clubs" and one of the Aces in hand
        # test we only have two cards
        self.assertEqual(len(hand_1.init_card), 2)
        # test the two cards are exactly "4 of Club" and one of the Aces
        str_card_in_hand = []
        for card in hand_1.init_card:
            str_card_in_hand.append(card.__str__())
        ace_str_list = ['Ace of Diamonds', 'Ace of Clubs', 'Ace of Hearts']
        random.seed(10)
        ace_st_remains = ace_str_list[random.randint(0,2)]
        self.assertTrue('4 of Clubs' in str_card_in_hand)
        self.assertTrue(ace_st_remains in str_card_in_hand)
Пример #12
0
    def test_initialize_Hand(self):
        '''
        Test that a hand is initialized properly
        '''
        c1 = hw5_cards_ec2.Card(0, 2)
        c2 = hw5_cards_ec2.Card(1, 1)
        c3 = hw5_cards_ec2.Card(2, 12)
        c4 = hw5_cards_ec2.Card(3, 6)
        c5 = hw5_cards_ec2.Card(0, 13)

        h = hw5_cards_ec2.Hand([c1, c2, c3, c4,
                                c5])  # Initialize a hand with five cards

        self.assertIsInstance(h.init_cards,
                              list)  # check the type of the Instance Attribute
        self.assertEqual(len(h.init_cards), 5)  # check the number of cards
        self.assertEqual(type(h.init_cards[0]),
                         hw5_cards_ec2.Card)  # check the type of the card
        self.assertEqual(str(h.init_cards[1]),
                         str(c2))  # check if the card corresponds to the input
Пример #13
0
 def test_remove_pairs(self):
     # Queen of Spades, King of Diamonds, 6 of Hearts, 5 of Clubs, 3 of Spades, 3 of Hearts, 5 of Hearts
     # Card is not
     init_cards = [
         hw5_cards_ec2.Card(3, 12),
         hw5_cards_ec2.Card(0, 13),
         hw5_cards_ec2.Card(2, 6),
         hw5_cards_ec2.Card(1, 5),
         hw5_cards_ec2.Card(3, 3),
         hw5_cards_ec2.Card(2, 3),
         hw5_cards_ec2.Card(2, 5)
     ]
     hand = hw5_cards_ec2.Hand(init_cards)
     length0 = len(hand.cards)
     card_str_0 = [card.__str__() for card in hand.cards]
     hand.remove_pairs()
     length1 = len(hand.cards)
     card_str_1 = [card.__str__() for card in hand.cards]
     # card length is shortened
     self.assertEqual(length0 - 4,
                      length1,
                      msg="Test if the pairs are removed")
     self.assertIn("5 of Clubs", card_str_0)
     self.assertIn("5 of Hearts", card_str_0)
     self.assertIn("3 of Spades", card_str_0)
     self.assertIn("3 of Hearts", card_str_0)
     # remove the correct card
     self.assertTrue("5 of Clubs" not in card_str_1)
     self.assertTrue("5 of Hearts" not in card_str_1)
     self.assertTrue("3 of Spades" not in card_str_1)
     self.assertTrue("3 of Hearts" not in card_str_1)
     # nothing to remove when there are no pairs
     hand.remove_pairs()
     length2 = len(hand.cards)
     self.assertEqual(
         length1,
         length2,
         msg=
         "Test if the length is equal when there is no pairs to be removed")
Пример #14
0
import hw5_cards_ec2

c1 = hw5_cards_ec2.Card(1, 1)
c2 = hw5_cards_ec2.Card(2, 2)

print(c1)
print(c2)

h1 = hw5_cards_ec2.Hand([c1, c2])

d1 = hw5_cards_ec2.Deck()

hands = d1.deal(5, 12)
print(len(d1.cards))
for x in hands:
    print("HanD!")
    for card in x.cards:
        print(card)