def test_get_game_state_for_know_you_opponent_puzzle():
     """
 Verify that get_game_state_for_know_your_opponent_puzzle() returns a valid
 state.
 """
     game_state = get_game_state_for_know_your_opponent_puzzle()
     validate(game_state)
 def test_get_game_view_for_who_laughs_last_puzzle():
     """
 Verify that get_game_state_for_who_laughs_last_puzzle() returns a valid
 state.
 """
     game_state = get_game_state_for_who_laughs_last_puzzle()
     validate(game_state)
 def test_get_game_state_for_forcing_the_issue_puzzle():
     """
 Verify that get_game_state_for_forcing_the_issue_puzzle() returns a valid
 state.
 """
     game_state = get_game_state_for_forcing_the_issue_puzzle()
     validate(game_state)
 def test_get_game_state_for_playing_to_win_the_last_trick_puzzle():
     """
 Verify that get_game_state_for_playing_to_win_the_last_trick_puzzle()
 returns a valid state.
 """
     game_state = get_game_state_for_playing_to_win_the_last_trick_puzzle()
     validate(game_state)
 def test_get_game_state_for_you_first_no_you_first_puzzle():
     """
 Verify that get_game_state_for_you_first_no_you_first_puzzle() returns a
 valid state.
 """
     game_state = get_game_state_for_you_first_no_you_first_puzzle()
     validate(game_state)
 def test_get_game_state_for_elimination_play_puzzle():
     """
 Verify that get_game_state_for_elimination_play_puzzle() returns a valid
 state.
 """
     game_state = get_game_state_for_elimination_play_puzzle()
     validate(game_state)
 def test_get_game_state_with_all_tricks_played():
     """
 Verify that get_game_state_with_all_tricks_played() returns a valid game
 state.
 """
     game_state = get_game_state_with_all_tricks_played()
     validate(game_state)
 def test_get_game_state_with_empty_talon_for_tests():
     """
 Verify that get_game_state_with_empty_talon_for_tests() returns a valid
 game state.
 """
     game_state = get_game_state_with_empty_talon_for_tests()
     validate(game_state)
    def test_duplicated_cards(self):
        dup_trump_card = copy.deepcopy(self.game_state)
        dup_trump_card.cards_in_hand.one[3] = dup_trump_card.trump_card
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "Duplicated cards: A♣"):
            validate(dup_trump_card)

        dup_in_hands = copy.deepcopy(self.game_state)
        dup_in_hands.cards_in_hand.one[3] = dup_in_hands.cards_in_hand.two[1]
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "Duplicated cards: K♣"):
            validate(dup_in_hands)

        dup_in_hand = copy.deepcopy(self.game_state)
        dup_in_hand.cards_in_hand.one[2] = dup_in_hand.cards_in_hand.one[1]
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "Duplicated cards: K♥"):
            validate(dup_in_hand)

        dup_in_talon = copy.deepcopy(self.game_state)
        dup_in_talon.cards_in_hand.one[1] = dup_in_talon.talon[0]
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "Duplicated cards: J♦"):
            validate(dup_in_talon)

        dup_in_trick = copy.deepcopy(self.game_state)
        dup_in_trick.cards_in_hand.one[1] = dup_in_trick.won_tricks.two[0].one
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "Duplicated cards: J♥"):
            validate(dup_in_trick)
 def test_empty_talon_cannot_be_closed(self):
     self.game_state = get_game_state_with_empty_talon_for_tests()
     self.game_state.player_that_closed_the_talon = self.game_state.next_player
     self.game_state.opponent_points_when_talon_was_closed = 10
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "An empty talon cannot be closed"):
         validate(self.game_state)
 def test_get_game_view_for_the_last_trump_puzzle():
     """
 Verify that get_game_view_for_the_last_trump_puzzle() returns a valid state.
 """
     game_view = get_game_view_for_the_last_trump_puzzle()
     unseen_cards = get_unseen_cards(game_view)
     game_state = populate_game_view(game_view, unseen_cards)
     validate(game_state)
 def test_five_cards_in_hand(self):
     self.game_state.talon.append(self.game_state.cards_in_hand.one.pop())
     self.game_state.talon.append(self.game_state.cards_in_hand.two.pop())
     with self.assertRaisesRegex(
             InvalidGameStateError,
             "The players should have 5 cards in hand: 4"):
         validate(self.game_state)
     self.game_state.close_talon()
     validate(self.game_state)
 def test_marriages_with_no_tricks_won(self):
     # Move won tricks to the talon and subtract their value.
     while len(self.game_state.won_tricks.two) > 0:
         trick = self.game_state.won_tricks.two.pop()
         self.game_state.trick_points.two -= trick.one.card_value
         self.game_state.trick_points.two -= trick.two.card_value
         self.game_state.talon.extend([trick.one, trick.two])
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "Invalid trick points.*two=0.*two=20"):
         validate(self.game_state)
 def test_at_most_five_cards_in_hand(self):
     trick = self.game_state.won_tricks.one.pop()
     self.game_state.cards_in_hand.one.append(trick.one)
     self.game_state.cards_in_hand.two.append(trick.two)
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "more than 5 cards in hand: 6"):
         validate(self.game_state)
     self.game_state.close_talon()
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "more than 5 cards in hand: 6"):
         validate(self.game_state)
 def test_invalid_trick_points(self):
     correct_trick_points = self.game_state.trick_points
     for points_one in range(100):
         for points_two in range(100):
             self.game_state.trick_points = PlayerPair(
                 points_one, points_two)
             if self.game_state.trick_points == correct_trick_points:
                 continue
             with self.assertRaisesRegex(InvalidGameStateError,
                                         "Invalid trick points",
                                         msg=f"{points_one} {points_two}"):
                 validate(self.game_state)
 def test_total_number_of_cards_is_20(self):
     self.assertEqual(1, len(self.game_state.talon))
     self.game_state.talon = []
     with self.assertRaisesRegex(
             InvalidGameStateError,
             "Total number of cards must be 20, not 19"):
         validate(self.game_state)
     self.game_state.talon = [None]
     with self.assertRaisesRegex(
             InvalidGameStateError,
             "Total number of cards must be 20, not 19"):
         validate(self.game_state)
    def test_same_number_of_cards_in_hand(self):
        card = self.game_state.cards_in_hand.one.pop()
        self.game_state.talon.append(card)
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "equal number of cards .* 4 vs 5"):
            validate(self.game_state)

        self.game_state = get_game_state_with_empty_talon_for_tests()
        trick = PlayerPair(one=self.game_state.cards_in_hand.one.pop(),
                           two=self.game_state.cards_in_hand.one.pop())
        self.game_state.won_tricks.one.append(trick)
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "equal number of cards .* 2 vs 4"):
            validate(self.game_state)
    def test_won_tricks(self):
        # Cannot win with the same suit, but smaller card.
        trick = self.game_state.won_tricks.one[0]  # K♠, Q♠
        swapped_trick = PlayerPair(one=trick.two, two=trick.one)
        self.game_state.won_tricks.one[0] = swapped_trick
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "ONE cannot win this trick: Q♠, K♠"):
            validate(self.game_state)

        # Cannot win with non-trump card against a trump card.
        self.game_state = get_game_state_for_tests()
        self.game_state.won_tricks.one.append(
            self.game_state.won_tricks.two.pop())
        self.game_state.trick_points = PlayerPair(42, 33)
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "ONE cannot win this trick: X♦, X♣"):
            validate(self.game_state)

        # Different suits, no trump. Could be valid wins for both players, since we
        # don't know who played the first card.
        self.game_state = get_game_state_for_tests()
        card = self.game_state.won_tricks.two[-1].two
        self.game_state.won_tricks.two[-1].two = \
          self.game_state.cards_in_hand.one[3]
        self.game_state.cards_in_hand.one[3] = card
        validate(self.game_state)
        self.game_state.won_tricks.one.append(
            self.game_state.won_tricks.two.pop())
        self.game_state.trick_points = PlayerPair(42, 33)
        validate(self.game_state)
    def test_minimum_cards_in_hand(self):
        self.game_state.talon.append(self.game_state.cards_in_hand.one.pop())
        self.game_state.talon.append(self.game_state.cards_in_hand.two.pop())
        trick = self.game_state.won_tricks.two.pop()
        self.game_state.talon.extend([trick.one, trick.two])
        self.game_state.trick_points.two -= trick.one.card_value
        self.game_state.trick_points.two -= trick.two.card_value
        self.game_state.close_talon()

        self.assertEqual(4, len(self.game_state.cards_in_hand.one))
        validate(self.game_state)

        self.game_state.talon.append(self.game_state.cards_in_hand.one.pop())
        self.game_state.talon.append(self.game_state.cards_in_hand.two.pop())
        self.assertEqual(3, len(self.game_state.cards_in_hand.one))
        validate(self.game_state)

        self.game_state.talon.append(self.game_state.cards_in_hand.one.pop())
        self.game_state.talon.append(self.game_state.cards_in_hand.two.pop())
        self.assertEqual(2, len(self.game_state.cards_in_hand.one))
        validate(self.game_state)

        self.game_state.talon.append(self.game_state.cards_in_hand.one.pop())
        self.game_state.talon.append(self.game_state.cards_in_hand.two.pop())
        self.assertEqual(1, len(self.game_state.cards_in_hand.one))
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "at least 2 cards in hand, not 1"):
            validate(self.game_state)

        # At the beginning, both players must have 5 cards, even if the talon is
        # closed.
        self.game_state = GameState.new()
        self.game_state.talon.append(self.game_state.cards_in_hand.one.pop())
        self.game_state.talon.append(self.game_state.cards_in_hand.two.pop())
        self.game_state.close_talon()
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "at least 5 cards in hand, not 4"):
            validate(self.game_state)
 def test_duplicated_marriage_suits(self):
     self.game_state.marriage_suits.two.append(Suit.DIAMONDS)
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "Duplicated marriage suits: ♦"):
         validate(self.game_state)
     self.game_state.marriage_suits.two.pop()
     validate(self.game_state)
     self.game_state.marriage_suits.one.append(Suit.DIAMONDS)
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "Duplicated marriage suits: ♦"):
         validate(self.game_state)
    def test_the_player_to_lead_must_have_won_a_trick(self):
        for trick in self.game_state.won_tricks.two:
            self.game_state.talon.extend([trick.one, trick.two])
            self.game_state.trick_points.two -= trick.one.card_value
            self.game_state.trick_points.two -= trick.two.card_value
        self.game_state.won_tricks.two = []
        self.game_state.trick_points.two = 0
        validate(self.game_state)
        self.game_state.next_player = PlayerId.TWO
        with self.assertRaisesRegex(
                InvalidGameStateError,
                "player that is to lead did not win any trick"):
            validate(self.game_state)

        # At the beginning of a game, any player can lead without winning any trick.
        self.game_state = GameState.new(PlayerId.ONE)
        validate(self.game_state)
        self.game_state = GameState.new(PlayerId.TWO)
        validate(self.game_state)
 def test_next_player_did_not_already_play_a_card(self):
     self.game_state.current_trick.one = self.game_state.cards_in_hand.one[
         0]
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "current_trick already contains a card"):
         validate(self.game_state)
     self.game_state.next_player = PlayerId.TWO
     validate(self.game_state)
     self.game_state.current_trick.two = self.game_state.cards_in_hand.two[
         0]
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "current_trick already contains a card"):
         validate(self.game_state)
 def test_if_talon_is_closed_opponents_points_must_be_set(self):
     self.game_state.player_that_closed_the_talon = self.game_state.next_player
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "must be either both set or both None"):
         validate(self.game_state)
     self.game_state.player_that_closed_the_talon = None
     self.game_state.opponent_points_when_talon_was_closed = 10
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "must be either both set or both None"):
         validate(self.game_state)
     self.game_state.player_that_closed_the_talon = self.game_state.next_player
     self.game_state.opponent_points_when_talon_was_closed = \
       self.game_state.trick_points[self.game_state.next_player.opponent()] + 1
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "greater than the current value"):
         validate(self.game_state)
 def test_get_simple_game_state_for_tests():
     """Verify that get_simple_game_state_for_tests() returns a valid state."""
     game_state = get_game_state_for_tests()
     validate(game_state)
 def test_get_game_state_for_tempo_puzzle():
     """Verify that get_game_state_for_tempo_puzzle() returns a valid state."""
     game_state = get_game_state_for_tempo_puzzle()
     validate(game_state)
 def test_trump_card_is_present(self):
     self.game_state.talon.append(self.game_state.trump_card)
     self.game_state.trump_card = None
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "trump_card is missing"):
         validate(self.game_state)
 def test_trump_card_is_public(self):
     self.game_state.trump_card.public = False
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "The trump card should be public"):
         validate(self.game_state)
 def test_trump_and_trump_card_match(self):
     self.game_state.trump = Suit.HEARTS
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "trump and trump_card.suit do not match"):
         validate(self.game_state)
 def test_trump_cannot_be_none(self):
     self.game_state.trump = None
     with self.assertRaisesRegex(InvalidGameStateError,
                                 "Trump suit cannot be None"):
         validate(self.game_state)
    def test_marriage_card_not_played(self):
        # One card is in the talon, one card is in the player's hand.
        card = self.game_state.won_tricks.one[-1].two
        self.game_state.won_tricks.one[-1].two = self.game_state.talon[0]
        self.game_state.talon[0] = card
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "♦ was announced, but no card was played"):
            validate(self.game_state)

        # Both cards are still in the player's hand.
        self.game_state = get_game_state_for_tests()
        self.game_state.marriage_suits.one.append(Suit.HEARTS)
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "♥ was announced, but no card was played"):
            validate(self.game_state)

        # Both cards are still in the player's hand, but they just announced it and
        # played one card.
        self.game_state = get_game_state_for_tests()
        king_hearts = self.game_state.cards_in_hand.one[1]
        self.game_state.current_trick.one = king_hearts
        self.game_state.marriage_suits.one.append(Suit.HEARTS)
        self.game_state.trick_points.one += 20
        self.game_state.next_player = PlayerId.TWO
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "Marriage card should be public: Q♥"):
            validate(self.game_state)
        self.game_state.cards_in_hand.one[0].public = True
        validate(self.game_state)

        # Both cards were played, but by different players.
        self.game_state = get_game_state_for_tests()
        self.game_state.marriage_suits.one.append(Suit.SPADES)
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "ONE announced .* ♠ and played one card"):
            validate(self.game_state)

        # The other player announced the marriage.
        self.game_state = get_game_state_for_tests()
        self.game_state.marriage_suits.one.append(
            self.game_state.marriage_suits.two.pop())
        with self.assertRaisesRegex(InvalidGameStateError,
                                    "♦ was announced, but no card was played"):
            validate(self.game_state)