Exemplo n.º 1
0
    def testDeckThreeHandsMockDeal2(self):
        """
        Shuffle a clean deck twice, deal 3 hands,
        validate number of cards, score, and winner.

        Tests run with a mocked random number generator
        """

        deck = Deck.initialize_deck(num_cards=20, colors=[RED])
        self.assertEqual(20, len(deck))

        deck.shuffler = Shuffler(0.3723)
        deck.shuffle()
        self.assertEqual(20, len(deck))

        hands = deck.deal_hands(num_hands=3, num_cards=5)
        self.assertEqual(3, len(hands))

        ordered_hands = Hand.sort_hands(hands)
        self.assertEqual(3, len(ordered_hands))

        scores = [hand.score for hand in ordered_hands]
        self.assertTrue(scores[0] > scores[1])
        self.assertTrue(scores[1] > scores[2])
        self.assertEqual([210, 174, 114], scores)

        n, winners = Hand.determine_winner(hands)
        self.assertEqual(1, n)
        self.assertEqual(1, len(winners))
        self.assertEqual(210, winners[0].score)
Exemplo n.º 2
0
    def testDeckThreeHandsMockDeal1(self):
        """
        Shuffle a clean deck, deal 3 hands,
        validate number of cards, score, and winner.

        Tests run with a mocked random number generator
        """

        deck = Deck.initialize_deck()
        deck.shuffler = self.shuffler
        deck.shuffle()

        ## regular deck
        self.assertEqual(39, len(deck))

        ## Deal 3 hands, 3 cards each
        hand1, hand2, hand3 = deck.deal_hands(3, 3)

        self.assertEqual(30, len(deck))

        self.assertEqual(3, len(hand1))
        self.assertEqual(3, len(hand2))
        self.assertEqual(3, len(hand3))

        self.assertEqual(49, hand1.score)
        self.assertEqual(26, hand2.score)
        self.assertEqual(47, hand3.score)

        n, hands = Hand.determine_winner([hand1, hand2, hand3])
        self.assertEqual(1, n)
        self.assertEqual(1, len(hands))
        self.assertEqual(49, hands[0].score)
Exemplo n.º 3
0
    def testHandsEqual(self):
        cards = [
            Card(RED, 2),
            Card(YELLOW, 4),
            Card(GREEN, 2),
        ]
        hand1 = Hand()
        hand1.add_cards(cards)

        self.assertEqual(16, hand1.score)
        self.assertEqual(3, len(hand1))

        self.assertNotEqual(99, hand1.score)
        self.assertNotEqual(1, len(hand1))

        cards = [
            Card(RED, 3),
            Card(YELLOW, 3),
            Card(GREEN, 1),
        ]
        hand2 = Hand()
        hand2.add_cards(cards)

        self.assertEqual(16, hand2.score)
        self.assertEqual(3, len(hand2))

        self.assertNotEqual(99, hand2.score)
        self.assertNotEqual(1, len(hand2))

        ##################################################
        self.assertEqual(hand1, hand2)
        self.assertTrue(hand1 == hand2)
        self.assertFalse(hand1 != hand2)
        self.assertTrue(hand1.is_tie(hand2))
        self.assertFalse(hand1.is_winner(hand2))
        self.assertFalse(hand1.is_loser(hand2))
        self.assertTrue(hand1.same_number_of_cards(hand2))
        self.assertTrue(hand2.same_number_of_cards(hand1))

        ## hands are a tie
        hands = [hand1, hand2]
        n, winners = Hand.determine_winner(hands)
        self.assertEqual(2, n)
        self.assertEqual(hands, winners)
Exemplo n.º 4
0
    def testDeckTwoHandsMockDeal3(self):
        """
        Shuffle a clean deck twice, deal 2 hands,
        validate number of cards, score, and winner.

        Tests run with a mocked random number generator
        """

        deck = Deck.initialize_deck(num_cards=20, colors=[RED])
        deck.shuffler = Shuffler(0.3972)

        deck.shuffle()
        self.assertEqual(20, len(deck))

        deck.shuffle()
        self.assertEqual(20, len(deck))

        hand1 = Hand()
        hand2 = Hand()

        hand1.add_card(deck.deal_card())
        hand2.add_card(deck.deal_card())
        hand1.add_card(deck.deal_card())
        hand2.add_card(deck.deal_card())
        hand1.add_card(deck.deal_card())
        hand2.add_card(deck.deal_card())

        self.assertEqual(3, len(hand1))
        self.assertEqual(3, len(hand2))

        self.assertEqual(111, hand1.score)
        self.assertEqual(135, hand2.score)

        self.assertFalse(hand1.is_winner(hand2))
        self.assertTrue(hand2.is_winner(hand1))

        n, winners = Hand.determine_winner([hand1, hand2])
        self.assertEqual(n, len(winners))
        self.assertEqual(1, len(winners))
        self.assertEqual(135, winners[0].score)
Exemplo n.º 5
0
    def testDeckTwoHandsMockDeal1(self):
        """
        Shuffle a clean deck twice, deal 3 hands,
        validate number of cards, score, and winner.

        Tests run with a mocked random number generator
        """

        deck = Deck.initialize_deck()
        deck.shuffler = self.shuffler
        deck.shuffle()

        ## regular deck
        self.assertEqual(39, len(deck))

        ## Deal 2 hands, 3 cards each
        hand1 = Hand("Player 1")
        hand2 = Hand("Player 2")

        hand1.add_card(deck.deal_card())
        hand2.add_card(deck.deal_card())
        hand1.add_card(deck.deal_card())
        hand2.add_card(deck.deal_card())
        hand1.add_card(deck.deal_card())
        hand2.add_card(deck.deal_card())

        self.assertEqual(33, len(deck))

        self.assertEqual(3, len(hand1))
        self.assertEqual(3, len(hand2))

        self.assertEqual(27, hand1.score)
        self.assertEqual(57, hand2.score)

        n, hands = Hand.determine_winner([hand1, hand2])
        self.assertEqual(1, n)
        self.assertEqual(1, len(hands))
        self.assertEqual(57, hands[0].score)
Exemplo n.º 6
0
    def testHandsThreeTieThree(self):

        cards1 = [
            Card(RED, 2),
            Card(YELLOW, 4),
            Card(GREEN, 5),
        ]

        cards2 = [
            Card(RED, 4),
            Card(YELLOW, 3),
            Card(GREEN, 1),
        ]

        cards3 = [
            Card(RED, 1),
            Card(YELLOW, 6),
            Card(GREEN, 4),
        ]

        hand1 = Hand()
        hand1.add_cards(cards1)

        self.assertEqual(19, hand1.score)
        self.assertEqual(3, len(hand1))

        hand2 = Hand()
        hand2.add_cards(cards2)

        self.assertEqual(19, hand2.score)
        self.assertEqual(3, len(hand2))

        hand3 = Hand()
        hand3.add_cards(cards3)

        self.assertEqual(19, hand3.score)
        self.assertEqual(3, len(hand3))

        ##################################################
        self.assertTrue(hand1.is_tie(hand2))
        self.assertTrue(hand1.is_tie(hand3))
        self.assertTrue(hand2.is_tie(hand3))

        self.assertTrue(hand1 == hand2)
        self.assertFalse(hand1 != hand2)
        self.assertTrue(hand1 == hand3)
        self.assertFalse(hand1 != hand3)
        self.assertTrue(hand2 == hand3)
        self.assertFalse(hand2 != hand3)

        hands = [hand1, hand2, hand3]
        n, winner = Hand.determine_winner(hands)
        self.assertEqual(3, n)
        self.assertEqual(19, winner[0].score)
        self.assertEqual(19, winner[1].score)
        self.assertEqual(19, winner[2].score)

        hands = Hand.sort_hands(hands)
        self.assertEqual(19, hands[0].score)
        self.assertEqual(19, hands[1].score)
        self.assertEqual(19, hands[2].score)

        n, winner = Hand.determine_winner(hands)
        self.assertEqual(3, n)
        self.assertEqual(19, winner[0].score)
        self.assertEqual(19, winner[1].score)
        self.assertEqual(19, winner[2].score)

        hands = Hand.sort_hands(hands, ascending=True)
        self.assertEqual(19, hands[0].score)
        self.assertEqual(19, hands[1].score)
        self.assertEqual(19, hands[2].score)

        n, winner = Hand.determine_winner(hands)
        self.assertEqual(3, n)
        self.assertEqual(19, winner[0].score)
        self.assertEqual(19, winner[1].score)
        self.assertEqual(19, hands[2].score)
Exemplo n.º 7
0
    def testHandsTwoWinner(self):

        cards = [
            Card(RED, 2),
            Card(YELLOW, 4),
            Card(GREEN, 3),
        ]
        hand1 = Hand()
        hand1.add_cards(cards)

        self.assertEqual(17, hand1.score)
        self.assertEqual(3, len(hand1))

        self.assertNotEqual(99, hand1.score)
        self.assertNotEqual(1, len(hand1))

        cards = [
            Card(RED, 3),
            Card(YELLOW, 3),
            Card(GREEN, 1),
        ]
        hand2 = Hand()
        hand2.add_cards(cards)

        self.assertEqual(16, hand2.score)
        self.assertEqual(3, len(hand2))

        self.assertNotEqual(99, hand2.score)
        self.assertNotEqual(1, len(hand2))

        ##################################################
        self.assertNotEqual(hand1, hand2)
        self.assertFalse(hand1 == hand2)
        self.assertTrue(hand1 != hand2)
        self.assertTrue(hand1 > hand2)
        self.assertFalse(hand1 < hand2)

        self.assertFalse(hand1.is_tie(hand2))

        self.assertTrue(hand1.same_number_of_cards(hand2))
        self.assertTrue(hand2.same_number_of_cards(hand1))

        n, winner = Hand.determine_winner([hand1, hand2])
        self.assertEqual(1, n)
        self.assertEqual(17, winner[0].score)

        hands = Hand.sort_hands([hand1, hand2])
        self.assertEqual(17, hands[0].score)
        self.assertEqual(16, hands[1].score)

        n, winner = Hand.determine_winner(hands)
        self.assertEqual(1, n)
        self.assertEqual(17, winner[0].score)

        hands = Hand.sort_hands([hand1, hand2], ascending=True)
        self.assertEqual(16, hands[0].score)
        self.assertEqual(17, hands[1].score)

        n, winner = Hand.determine_winner(hands)
        self.assertEqual(1, n)
        self.assertEqual(17, winner[0].score)