示例#1
0
    def test_play_reversed(self):
        """
        Tests for a game which reverses the order into negative numbers.
        """
        pa = player.RandomAiPlayer("Player A")
        pb = player.RandomAiPlayer("Player B")
        pc = player.RandomAiPlayer("Player C")

        u = uno.Uno([pa, pb, pc])
        pa.hand = [
            unocard.UnoCard(1, 7),
            unocard.UnoCard(1, 7),
            unocard.UnoCard(1, 7)
        ]  # 7 of Diamonds
        pb.hand = [unocard.ReverseActionCard(1),
                   unocard.ReverseActionCard(1)]  # 10 of Diamonds
        pc.hand = [unocard.UnoCard(1, 9),
                   unocard.UnoCard(1, 9)]  # 9 of Diamonds
        u.play()

        # This game should have resulted in 5 cards played, in order:
        self.assertEqual(5, len(u.pile))
        self.assertEqual(7, u.pile[0].rank)
        self.assertEqual(10, u.pile[1].rank)
        self.assertEqual(7, u.pile[2].rank)
        self.assertEqual(9, u.pile[3].rank)
        self.assertEqual(10, u.pile[4].rank)
示例#2
0
    def test_drawHand(self):
        """
        Tests for drawing hands from full to empty decks.
        """
        u = uno.Uno([
            player.RandomAiPlayer("Player A"),
            player.RandomAiPlayer("Player B"),
            player.RandomAiPlayer("Player C")
        ])

        self.assertEqual(5, uno.Uno.STARTING_HAND_SIZE)
        startDeckLen = len(u.deck)
        self.assertTrue(startDeckLen > 5)

        # Check 5 cards are drawn and removed from deck
        hand = u._drawHand()
        self.assertEqual(5, len(hand))
        for c in hand:
            self.assertTrue(isinstance(c, unocard.UnoCard))
        self.assertEqual(startDeckLen - 5, len(u.deck))

        # Check only 3 cards are drawn when deck only contains 3.
        u.deck = hand[:3]
        hand = u._drawHand()
        self.assertEqual(3, len(hand))
        self.assertEqual(0, len(u.deck))

        # Check hand contains zero cards when deck is empty.
        u.deck = []
        hand = u._drawHand()
        self.assertEqual(0, len(hand))
        self.assertEqual(0, len(u.deck))
示例#3
0
    def test_play_drawExtraCards(self):
        """
        Tests for a game where a player has to draw extra cards.
        """
        pa = player.RandomAiPlayer("Player A")
        pb = player.RandomAiPlayer("Player B")
        pc = player.RandomAiPlayer("Player C")

        u = uno.Uno([pa, pb, pc])
        pa.hand = [unocard.UnoCard(1, 7)]  # 7 of Diamonds
        pb.hand = [unocard.DrawTwoActionCard(1),
                   unocard.DrawTwoActionCard(1)]  # Q of Diamonds
        pc.hand = [unocard.UnoCard(1, 9),
                   unocard.UnoCard(1, 9)]  # 9 of Diamonds

        # The deck will start with certain cards meant to be drawn by certain players.
        u.deck[0] = unocard.UnoCard(1, 7)
        u.deck[1] = unocard.UnoCard(1, 7)
        u.deck[2] = unocard.UnoCard(1, 7)
        u.deck[3] = unocard.UnoCard(1, 7)
        u.deck[4] = unocard.UnoCard(1, 9)
        u.deck[5] = unocard.UnoCard(1, 9)
        u.deck[6] = unocard.UnoCard(0, 2)  # 2 of Clubs should not be drawn

        u.gameState.numExtraCardDraw = 4  # Start with drawing 4 cards.
        u.play()

        # This game should have resulted in 5 cards played, in order:
        self.assertEqual(5, len(u.pile))
        self.assertEqual(7, u.pile[0].rank)
        self.assertEqual(12, u.pile[1].rank)
        self.assertEqual(9, u.pile[2].rank)
        self.assertEqual(7, u.pile[3].rank)
        self.assertEqual(12, u.pile[4].rank)

        # Top card of deck should be 2 of clubs.
        self.assertEqual(2, u.deck[0].rank)

        # Player A and C should both end with 3 cards.
        self.assertEqual(3, len(pa.hand))
        self.assertEqual(3, len(pc.hand))
示例#4
0
    def test_play_normalRounds(self):
        """
        Tests for the play method in a normal game that last 4 rounds.
        """
        pa = player.RandomAiPlayer("Player A")
        pb = player.RandomAiPlayer("Player B")
        pc = player.RandomAiPlayer("Player C")

        u = uno.Uno([pa, pb, pc])
        pa.hand = [unocard.UnoCard(1, 7),
                   unocard.UnoCard(1, 7)]  # 7 of Diamonds
        pb.hand = [unocard.UnoCard(1, 8),
                   unocard.UnoCard(1, 8)]  # 8 of Diamonds
        pc.hand = [unocard.UnoCard(1, 9),
                   unocard.UnoCard(1, 9)]  # 9 of Diamonds
        u.play()

        # This game should have resulted in 4 cards played, in order:
        self.assertEqual(4, len(u.pile))
        self.assertEqual(7, u.pile[0].rank)
        self.assertEqual(8, u.pile[1].rank)
        self.assertEqual(9, u.pile[2].rank)
        self.assertEqual(7, u.pile[3].rank)
示例#5
0
                # Return so that the player does not draw a card.
                return

            # If the card was not playable, we notify the player, and try again.
            else:
                player.notifyNotAcceptableCard(card)

        # We got here because either the player ran out of attempts or chose to draw
        # explicitly. Draw a card, so long as the deck has more cards.
        if len(self.deck) > 0:
            player.addCardToHand(self.deck.pop(0))


"""
This sets up and starts a game of Uno.
"""
if __name__ == '__main__':
    p = []
    p.append(a5_player.HumanConsolePlayer())
    p.append(a5_player.RandomAiPlayer("HAL 9000"))
    p.append(a5_player.RandomAiPlayer("WOPR"))
    p.append(a5_player.RandomAiPlayer("Skynet"))

    # TODO: Students, after you implement the RankThenSuitAiPlayer, add a 5th
    # player to the game using your new RankThenSuitAiPlayer implementation.  This
    # should result in one human player, three random AI players and one "smarter"
    # rank-then-suit AI player.

    u = Uno(p)
    u.play()