Example #1
0
    def test_init_pass_cards(self):
        """
        When the game is first initialized
        passing cards should throw an exception.
        """
        game = HeartsGame()

        try:
            game.pass_cards(0, ["c2", "c3", "c4"])
            self.fail()
        except e.PassingNotInProgressError:
            pass  # test succeeded
Example #2
0
    def test_play_card(self):
        game = HeartsGame(deal_func=lambda: example_hands)
        observer = Mock()
        game.add_observer(observer)

        game.start()

        for i in range(4):
            game.pass_cards(i, example_hands[i][:3])

        game.play_card("c2")

        self.assertEqual([{"player": 1, "card": "c2"}], game.get_trick())
Example #3
0
    def test_finish_passing(self):
        """
        Tests that the state transitions properly
        into playing when passing is finished.
        """
        game = HeartsGame(deal_func=lambda: example_hands)
        observer = Mock()
        game.add_observer(observer)

        game.start()

        for i in range(4):
            game.pass_cards(i, example_hands[i][:3])

        self.assertEqual("playing", game.get_state())
        self.assertEqual(1, game.get_current_player())
        new_hand = example_hands[0][:3] + example_hands[1][3:]
        self.assertEqual(set(new_hand), set(game.get_hand(1)))
Example #4
0
    def test_finish_trick(self):
        """
        Tests that observers are notified
        when a trick is finished.
        """
        game = HeartsGame(deal_func=lambda: example_hands)

        game.start()

        for i in range(4):
            game.pass_cards(i, example_hands[i][:3])

        observer = Mock()
        game.add_observer(observer)

        # player 1 starts
        game.play_card("c2")
        game.play_card("c10")
        game.play_card("c6")
        game.play_card("c4")

        observer.on_finish_trick.assert_called_once_with(2, 0)
Example #5
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)