Exemplo n.º 1
0
    def test_init_scores(self):
        """
        When the game is first initialized,
        the score for each player should be zero.
        """
        game = HeartsGame()

        for i in range(4):
            self.assertEqual(0, game.get_score(i))
Exemplo n.º 2
0
    def test_finish_round(self):
        """
        Tests that when the round is finished,
        the scores are updated,
        observers are notified
        and a new round starts.
        """

        # we're going to cheat and deal only three cards
        # to each player
        hands = [
            ["c2", "c3", "ck"],
            ["c5", "c6", "h2"],
            ["c7", "c8", "h3"],
            ["c9", "c10", "sq"]
        ]

        game = HeartsGame(deal_func=lambda: hands)
        game.start()
        for i in range(4):
            game.pass_cards(i, hands[i])

        self.assertEqual(1, game.get_current_player())

        game.play_card("c2")
        game.play_card("c5")
        game.play_card("c7")
        game.play_card("c9")

        self.assertEqual(0, game.get_current_player())

        game.play_card("sq")
        game.play_card("c3")
        game.play_card("c6")
        game.play_card("c8")

        # player 0 wins this hand, getting 13 points
        self.assertEqual(13, game.get_round_score(0))
        self.assertEqual(0, game.get_current_player())

        observer = Mock()
        game.add_observer(observer)

        game.play_card("c10")
        game.play_card("ck")
        game.play_card("h2")
        game.play_card("h3")

        # player 1 wins this hand, getting 2 points

        # the round should now be over
        self.assertEqual(0, game.get_current_round_number())
        self.assertEqual(13, game.get_score(0))
        self.assertEqual(2, game.get_score(1))
        self.assertEqual([13, 2, 0, 0], game.get_scores())
        observer.on_finish_round.assert_called_once_with([13, 2, 0, 0])

        # We are required to manually start the next round.
        game.start_next_round()
        self.assertEqual(1, game.get_current_round_number())
        observer.on_start_round.assert_called_once_with(1)