Example #1
0
 def testdraw(self):
     deck = hw5.Deck()
     hand = ec1.Hand(deck.cards)
     deck1=hw5.Deck()
     hand.draw(deck1)
     self.assertEqual(len(hand.init_card),53)
     self.assertEqual(len(deck1.cards),51)
Example #2
0
    def testAddAndRemove(self):
        c1 = hw5_cards_ec1.Card(0, 2)
        c2 = hw5_cards_ec1.Card(1, 1)
        init_list = [c1, c2]
        hand1 = hw5_cards_ec1.Hand(init_list)

        #c2 should not be added to the hand
        #c2 is already in the hand
        len_before = len(hand1.init_card)
        hand1.add_card(c2)
        len_after = len(hand1.init_card)
        self.assertEqual(len_after, len_before)

        #c3 should be added successfullt
        c3 = hw5_cards_ec1.Card(suit=1, rank=7)
        len_before = len(hand1.init_card)
        hand1.add_card(c3)
        len_after = len(hand1.init_card)
        self.assertEqual(len_after, len_before + 1)

        #remove one card
        #remove c3
        self.assertEqual(c3, hand1.remove_card(c3))

        #cannot remove c3, c3 is not in the hand
        self.assertEqual(None, hand1.remove_card(c3))
Example #3
0
 def test_q9(self):
     '''
     test that hand is initialized properly
     '''
     new_card = hw5_cards_ec1.Card()
     hand = hw5_cards_ec1.Hand([new_card])
     self.assertEqual(hand.cards, [new_card])
Example #4
0
    def test_construct_Card(self):
        c1 = hw5_cards_ec1.Card(0, 2)
        c2 = hw5_cards_ec1.Card(1, 1)
        init_list = [c1, c2]
        hand1 = hw5_cards_ec1.Hand(init_list)

        self.assertEqual(hand1.init_card, init_list)
        self.assertIsInstance(hand1.init_card[0], hw5_cards_ec1.Card)
Example #5
0
 def test1_init(self):
     d = hw5_cards.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_ec1.Hand(hand) # create hand object
     for i in range(0,hand_size):
         self.assertEqual(h.cards[i].__str__(), hand[i].__str__())
Example #6
0
 def testAddAndRemove(self):
     deck = hw5.Deck()
     card_missing=deck.deal_card()
     hand = ec1.Hand(deck.cards)
     for i in hand.init_card:
         hand.add_card(i)
         self.assertEqual(len(hand.init_card), 51)
     hand.add_card(card_missing)
     self.assertEqual(len(hand.init_card),52)
Example #7
0
 def test_construct_Hand(self):
     cards_already_in_hands = \
         [hw5_cards_ec1.Card(0, 1), hw5_cards_ec1.Card(1, 4), hw5_cards_ec1.Card(3, 11), hw5_cards_ec1.Card(3, 12)]
     hand_1 = hw5_cards_ec1.Hand(cards_already_in_hands)
     self.assertIsInstance(hand_1, hw5_cards_ec1.Hand)
     self.assertIsInstance(hand_1.init_card, list)
     self.assertEqual(hand_1.init_card[0].__str__(), "Ace of Diamonds")
     self.assertEqual(hand_1.init_card[1].__str__(), "4 of Clubs")
     self.assertEqual(hand_1.init_card[2].__str__(), "Jack of Spades")
     self.assertEqual(hand_1.init_card[3].__str__(), "Queen of Spades")
Example #8
0
 def test_add_remove(self):
     '''
     test that add_card and remove_card function functions as inteneded
     '''
     new_card = hw5_cards_ec1.Card()
     hand = hw5_cards_ec1.Hand([])
     hand.add_card(new_card)
     self.assertEqual(hand.cards, [new_card])
     hand.remove_card(new_card)
     self.assertEqual(hand.cards, [])
Example #9
0
    def testAddAndRemove(self):
        '''
        Test that add_card( ) and remove_card( ) behave as specified.
        '''
        c1 = hw5_cards_ec1.Card(0, 2)
        c2 = hw5_cards_ec1.Card(1, 1)
        c3 = hw5_cards_ec1.Card(2, 12)
        c4 = hw5_cards_ec1.Card(3, 6)
        c5 = hw5_cards_ec1.Card(0, 13)
        c6 = hw5_cards_ec1.Card(1, 3)

        list_init_cards = [c1, c2, c3, c4, c5]
        h1 = hw5_cards_ec1.Hand(
            list_init_cards)  # Initialize a hand with five cards
        h2 = hw5_cards_ec1.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
Example #10
0
 def testAddAndRemove(self):
     c1 = hw5_cards_ec1.Card(1,2)
     c2 = hw5_cards_ec1.Card(2,4)
     h2 = hw5_cards_ec1.Hand()
     n0 = len(h2.cards)
     h2.add_card(c1)
     n1 = len(h2.cards)
     h2.remove_card(c1)
     n2 = len(h2.cards)
     self.assertEqual(n0,n2)
     self.assertEqual(n1-1,n2)
Example #11
0
 def test3_draw(self):
     d = hw5_cards.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_ec1.Hand(hand) # create hand object
     num_cards_in_hand = len(h.cards) # how many cards in the hand now
     num_cards_in_dect = len(d.cards) # how many cards in the dect now
     h.draw(d)
     self.assertEqual(len(h.cards), num_cards_in_hand+1)
     self.assertEqual(len(d.cards), num_cards_in_dect-1)
Example #12
0
 def testDraw(self):
     deck1 = hw5_cards_ec1.Deck()
     deck1.shuffle()
     len_deck_before = len(deck1.cards)
     hand1 = hw5_cards_ec1.Hand([])
     len_hand_before = len(hand1.init_card)
     hand1.draw(deck1)
     len_dec_after = len(deck1.cards)
     len_hand_after = len(hand1.init_card)
     self.assertEqual(len_hand_before + 1, len_hand_after)
     self.assertEqual(len_dec_after, len_deck_before - 1)
Example #13
0
 def testDraw(self):
     d1 = hw5_cards_ec1.Deck()
     h1 = hw5_cards_ec1.Hand()
     d1_start_length = len(d1.cards)
     h1_start_length = len(h1.cards)
     h1.draw(d1)
     d1_end_length = len(d1.cards)
     h1_end_length = len(h1.cards)
     start_cards = d1_start_length + h1_start_length
     end_cards = d1_end_length + h1_end_length
     self.assertEqual(start_cards,end_cards)
     self.assertEqual(d1_start_length,d1_end_length+1)
     self.assertEqual(h1_start_length,h1_end_length-1)
Example #14
0
    def test_q10(self):
        '''
        test that draw function functions as intended
        '''
        deck = hw5_cards_ec1.Deck()
        hand = hw5_cards_ec1.Hand([])

        self.assertEqual(hand.cards, [])
        self.assertEqual(len(deck.cards), 52)

        hand.draw(deck)
        self.assertEqual(len(hand.cards), 1)
        self.assertEqual(len(deck.cards), 51)
Example #15
0
 def test_draw(self):
     c1 = hw5_cards_ec1.Card(0, 1)
     c2 = hw5_cards_ec1.Card(0, 2)
     d1 = hw5_cards_ec1.Deck()
     d1.shuffle()
     h1 = hw5_cards_ec1.Hand(d1.deal_hand(5))
     self.assertEqual(len(h1.init_cards), 5)
     self.assertEqual(len(d1.cards), 47)
     h1.draw(d1)
     self.assertEqual(len(h1.init_cards), 6)
     self.assertEqual(len(d1.cards), 46)
     h1.draw(d1)
     self.assertEqual(len(h1.init_cards), 7)
     self.assertEqual(len(d1.cards), 45)
Example #16
0
 def testAddAndRemove(self):
     #test add
     c1 = hw5_cards_ec1.Card(0, 1)
     c2 = hw5_cards_ec1.Card(0, 2)
     c3 = hw5_cards_ec1.Card(2, 13)
     hand1 = [c1, c2]
     hand2 = [c1, c2, c3]
     h1 = hw5_cards_ec1.Hand(hand1)
     #add existing card
     h1.add_card(c1)
     self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand1).init_cards)
     #add other card
     h1.add_card(c3)
     self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand2).init_cards)
     #test remove
     #remove card in hand
     h1.remove_card(c3)
     self.assertEqual(len(h1.init_cards), 2)
     self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand1).init_cards)
     #remove card not in hand
     h1.remove_card(c3)
     self.assertEqual(len(h1.init_cards), 2)
     self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand1).init_cards)
Example #17
0
    def test_draw(self):
        '''
        Test that draw( ) works as specified.
        '''
        d = hw5_cards_ec1.Deck()  # Initialize a deck
        h = hw5_cards_ec1.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
Example #18
0
 def test_init(self):
     init_cards = [
         hw5_cards_ec1.Card(3, 12),
         hw5_cards_ec1.Card(0, 13),
         hw5_cards_ec1.Card(2, 6),
         hw5_cards_ec1.Card(1, 5),
         hw5_cards_ec1.Card(3, 3)
     ]
     hand = hw5_cards_ec1.Hand(init_cards)
     self.assertIsInstance(hand, hw5_cards_ec1.Hand)
     self.assertIsInstance(hand.cards, list)
     self.assertEqual(hand.cards[0].__str__(), "Queen of Spades")
     self.assertEqual(hand.cards[1].__str__(), "King of Diamonds")
     self.assertEqual(hand.cards[2].__str__(), "6 of Hearts")
     self.assertEqual(hand.cards[3].__str__(), "5 of Clubs")
     self.assertEqual(hand.cards[4].__str__(), "3 of Spades")
Example #19
0
 def test_construct_Card(self):
     hand = []
     for i in range(1, 4):
         hand.append(hw5_cards_ec1.Card(0, i))
     h1 = hw5_cards_ec1.Hand(hand)
     self.assertEqual(h1.init_cards[0].suit, 0)
     self.assertEqual(h1.init_cards[0].suit_name, "Diamonds")
     self.assertEqual(h1.init_cards[0].rank, 1)
     self.assertEqual(h1.init_cards[0].rank_name, "Ace")
     self.assertEqual(h1.init_cards[1].suit, 0)
     self.assertEqual(h1.init_cards[1].suit_name, "Diamonds")
     self.assertEqual(h1.init_cards[1].rank, 2)
     self.assertEqual(h1.init_cards[1].rank_name, "2")
     self.assertEqual(h1.init_cards[2].suit, 0)
     self.assertEqual(h1.init_cards[2].suit_name, "Diamonds")
     self.assertEqual(h1.init_cards[2].rank, 3)
     self.assertEqual(h1.init_cards[2].rank_name, "3")
Example #20
0
 def test2_AddAndRemove(self):
     d = hw5_cards.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_ec1.Hand(hand) # create hand object
     num_cards_in_hand = len(h.cards) # how many cards in the hand now
     c_new = d.deal_card() # create a card in the deck but not in the hand
     c_old = h.cards[random.randint(0,len(h.cards)-1)] # create a card in the hand but not in the deck
     h.add_card(c_old)
     self.assertEqual(len(h.cards), num_cards_in_hand)
     h.add_card(c_new)
     self.assertEqual(len(h.cards), num_cards_in_hand+1)
     h.remove_card(c_new)
     self.assertEqual(len(h.cards), num_cards_in_hand)
     h.remove_card(c_old)
     self.assertEqual(len(h.cards), num_cards_in_hand-1)
Example #21
0
    def test_remove_card(self):
        '''
        Test that if you invoke the remove_card method on a Hand, the Hand has one fewer cards in it afterwards.
        Test that if you invoke the remove_card method with a Hand that is not in the Hand, the Hand size is not affected.

        '''

        c1 = hw5_cards.Card(1, 1)
        c2 = hw5_cards.Card(0, 2)
        c3 = hw5_cards.Card(0, 2)
        c4 = hw5_cards.Card(3, 3)
        h1 = hw5_cards_ec1.Hand([c1, c2])
        self.assertEqual(len(h1.init_cards), 2)
        h1.remove_card(c3)
        self.assertEqual(len(h1.init_cards), 1)
        h1.remove_card(c4)
        self.assertEqual(len(h1.init_cards), 1)
Example #22
0
    def test_add_card(self):
        '''
        Test that if you invoke the add_card method on a Hand, the Hand has one more cards in it afterwards.
        Test that if you invoke the add_card method with a card that is already in the Hand, the Hand size is not affected.

        '''

        c1 = hw5_cards.Card(1, 1)
        c2 = hw5_cards.Card(0, 2)
        c3 = hw5_cards.Card(3, 3)
        c4 = hw5_cards.Card(0, 2)
        h1 = hw5_cards_ec1.Hand([c1, c2])
        self.assertEqual(len(h1.init_cards), 2)
        h1.add_card(c3)
        self.assertEqual(len(h1.init_cards), 3)
        h1.add_card(c4)
        self.assertEqual(len(h1.init_cards), 3)
Example #23
0
    def test_draw(self):
        deck = hw5_cards_ec1.Deck()
        init_cards = [
            hw5_cards_ec1.Card(3, 12),
            hw5_cards_ec1.Card(0, 13),
            hw5_cards_ec1.Card(2, 6),
            hw5_cards_ec1.Card(1, 5),
            hw5_cards_ec1.Card(3, 3)
        ]
        hand = hw5_cards_ec1.Hand(init_cards)

        # Remove the cards that are in hand from the deck
        deck_str = []
        for i in deck.cards:
            deck_str.append(i.__str__())

        hand_str = []
        for i in hand.cards:
            hand_str.append(i.__str__())

        for i in hand_str:
            for j in deck_str:
                if j == i:
                    idx = deck_str.index(j)
                    deck.cards.pop(idx)
        # Make sure the cards in hand and the deck cards are compensate with each other.
        initial_deck_length = len(deck.cards)
        initial_hand_length = len(hand.cards)
        self.assertEqual(initial_deck_length + initial_hand_length,
                         52,
                         msg="Compensate pair")

        # Make sure the draw function works
        hand.draw(deck)
        after_deck_length = len(deck.cards)
        after_hand_length = len(hand.cards)
        self.assertEqual(after_deck_length + after_hand_length,
                         52,
                         msg="Compensate pair")
        self.assertEqual(after_deck_length,
                         initial_deck_length - 1,
                         msg="Remove one from deck")
        self.assertEqual(after_hand_length,
                         initial_hand_length + 1,
                         msg="Add one to the hand")
Example #24
0
    def testAddAndRemove(self):
        init_cards = [
            hw5_cards_ec1.Card(3, 12),
            hw5_cards_ec1.Card(0, 13),
            hw5_cards_ec1.Card(2, 6),
            hw5_cards_ec1.Card(1, 5),
            hw5_cards_ec1.Card(3, 3)
        ]
        hand = hw5_cards_ec1.Hand(
            init_cards)  #create a Hand object for convenience
        handlength_0 = len(hand.cards)
        #Add a card (invalid card, not adding anything)
        hand.add_card(hw5_cards_ec1.Card(3, 12))
        handlength_1 = len(hand.cards)
        self.assertEqual(handlength_0,
                         handlength_1,
                         msg="Test adding a card when the card is in hand.")
        #Add a card (valid card)
        hand.add_card(hw5_cards_ec1.Card(0, 1))
        handlength_2 = len(hand.cards)
        self.assertEqual(
            handlength_1 + 1,
            handlength_2,
            msg="Test adding a card when the card is not in hand.")

        #Remove a card (valid card)
        card_to_remove = hand.remove_card(hw5_cards_ec1.Card(0, 1))
        handlength_3 = len(hand.cards)
        self.assertEqual(handlength_2,
                         handlength_3 + 1,
                         msg="Test removing a card that is in hand")
        self.assertEqual(card_to_remove.__str__(),
                         "Ace of Diamonds",
                         msg="Test the card that is removed is as specified")

        #Remove a card (invalid card, not really removing anything)
        card_to_remove2 = hand.remove_card(hw5_cards_ec1.Card(3, 8))
        handlength_4 = len(hand.cards)
        self.assertEqual(handlength_3,
                         handlength_4,
                         msg="Test removing a card that is not in hand.")
        self.assertEqual(card_to_remove2, None)
Example #25
0
    def test_initialize_Hand(self):
        '''
        Test that a hand is initialized properly
        '''
        c1 = hw5_cards_ec1.Card(0, 2)
        c2 = hw5_cards_ec1.Card(1, 1)
        c3 = hw5_cards_ec1.Card(2, 12)
        c4 = hw5_cards_ec1.Card(3, 6)
        c5 = hw5_cards_ec1.Card(0, 13)

        h = hw5_cards_ec1.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_ec1.Card)  # check the type of the card
        self.assertEqual(str(h.init_cards[1]),
                         str(c2))  # check if the card corresponds to the input
Example #26
0
    def testAddAndRemove(self):
        cards_already_in_hands = \
            [hw5_cards_ec1.Card(0, 1), hw5_cards_ec1.Card(1, 4), hw5_cards_ec1.Card(3, 11), hw5_cards_ec1.Card(3, 12)]
        hand_1 = hw5_cards_ec1.Hand(cards_already_in_hands)

        # test a card already in hand
        card_in = hand_1.init_card[2]
        initial_length = len(hand_1.init_card)
        hand_1.add_card(card_in)
        length_after_add = len(hand_1.init_card)
        self.assertEqual(initial_length, length_after_add)
        card_to_remove = hand_1.remove_card(card_in)
        self.assertEqual(card_to_remove.__str__(), "Jack of Spades")
        self.assertEqual(len(hand_1.init_card), length_after_add - 1)

        # test a card not in hand
        card_not_in = card_in  # we've already removed it, so not in
        response = hand_1.remove_card(card_not_in)
        self.assertEqual(response, None)
        current_length = len(hand_1.init_card)
        hand_1.add_card(card_not_in)
        self.assertEqual(len(hand_1.init_card), current_length + 1)
Example #27
0
    def test_draw(self):
        # construct hand_1, instance of Hand
        cards_already_in_hands = \
            [hw5_cards_ec1.Card(0, 1), hw5_cards_ec1.Card(1, 4), hw5_cards_ec1.Card(3, 11), hw5_cards_ec1.Card(3, 12)]
        hand_1 = hw5_cards_ec1.Hand(cards_already_in_hands)

        # construct deck, instance of Deck
        deck = hw5_cards_ec1.Deck()

        # eliminate cards already in hand_1 from deck
        deck_existing_card_str = []
        for deck_card in deck.cards:
            deck_existing_card_str.append(deck_card.__str__())
        hand_existing_card_str = []
        for hand_card in hand_1.init_card:
            hand_existing_card_str.append(hand_card.__str__())
        deck_str_to_use = []
        for deck_card_str in deck_existing_card_str:
            if deck_card_str not in hand_existing_card_str:
                deck_str_to_use.append(deck_card_str)
        deck_to_use = []
        for deck_card in deck.cards:
            if deck_card.__str__() in deck_str_to_use:
                deck_to_use.append(deck_card)
        deck.cards = deck_to_use

        # test starts
        current_len_hand_cards = len(hand_1.init_card)
        current_len_deck_cards = len(deck.cards)

        # make sure they compensate for each other
        self.assertEqual(current_len_hand_cards + current_len_deck_cards, 52)
        hand_1.draw(deck)
        after_len_hand_cards = len(hand_1.init_card)
        after_len_deck_cards = len(deck.cards)
        self.assertEqual(after_len_hand_cards, current_len_hand_cards + 1)
        self.assertEqual(after_len_deck_cards, current_len_deck_cards - 1)
Example #28
0
    def test_draw(self):
        '''
        Test that if you invoke the draw method from a deck and add it to the hand, the Hand has one more cards in it afterwards.
        Test that if you invoke the draw method with a Hand that caused a side effect: the deck will be depleted by one card.

        '''

        c1 = hw5_cards.Card(1, 1)
        c2 = hw5_cards.Card(0, 2)
        d1 = hw5_cards.Deck()
        h1 = hw5_cards_ec1.Hand([c1, c2])
        self.assertEqual(len(h1.init_cards), 2)
        self.assertEqual(len(d1.cards), 52)
        h1.draw(d1)
        self.assertEqual(len(h1.init_cards), 3)
        self.assertEqual(len(d1.cards), 51)

        d2 = hw5_cards.Deck()
        self.assertEqual(len(d2.cards), 52)
        for i in range(len(d2.cards)-1):
            d2.cards.pop(-1)
        h1.draw(d2)
        self.assertEqual(len(h1.init_cards), 4)
        self.assertEqual(len(d2.cards), 0)
Example #29
0
 def test_construct_Hand(self):
     deck=hw5.Deck()
     hand = ec1.Hand(deck.cards)
     self.assertEqual(len(hand.init_card),52)
Example #30
0
    def test_construct_Hand(self):
        c1 = hw5_cards.Card(1, 1)
        c2 = hw5_cards.Card(0, 2)
        h1 = hw5_cards_ec1.Hand([c1, c2])

        self.assertIsInstance(h1.init_cards, list)