def test_cannot_execute_illegal_action(self):
   game_state = get_game_state_for_tests()
   queen_clubs = Card(Suit.CLUBS, CardValue.QUEEN)
   action = AnnounceMarriageAction(PlayerId.TWO, queen_clubs)
   self.assertFalse(action.can_execute_on(game_state))
   with self.assertRaises(AssertionError):
     action.execute(game_state)
Beispiel #2
0
    def _on_lead_do_not_follow_suit(self,
                                    game_view: GameState) -> PlayerAction:
        # If the cards that cannot be won by the opponent can get us to the end,
        # start playing them.
        action = self._play_winning_cards(game_view)
        if action is not None:
            return action

        # If we cannot win yet, and we have a marriage, announce it. If the Ace and
        # Ten from that suit cannot be in the opponents hand, play the King.
        if self._marriage_suit is not None:
            logging.debug("HeuristicPlayer: Announcing marriage for %s",
                          self._marriage_suit)
            king = Card(self._marriage_suit, CardValue.KING)
            ten = Card(self._marriage_suit, CardValue.TEN)
            ace = Card(self._marriage_suit, CardValue.ACE)
            if (ten in self._my_cards or ten in self._played_cards) and \
                (ace in self._my_cards or ace in self._played_cards):
                return AnnounceMarriageAction(self.id, king)
            return AnnounceMarriageAction(self.id, king.marriage_pair)

        # If we expect that the opponent has more trumps and we have big cards
        # (i.e., tens or aces), play one of the high card to force the opponent to
        # either play a trump or give up a lot of points.
        card = self._maybe_trump_control(game_view)
        if card is not None:
            return PlayCardAction(self.id, card)

        # Discard one of the small cards.
        card = self._best_discard(game_view)
        logging.debug("HeuristicPlayer: Discarding %s", card)
        return PlayCardAction(self.id, card)
 def test_can_only_instantiate_with_queen_or_king(self):
   for card in Card.get_all_cards():
     if card.card_value not in [CardValue.QUEEN, CardValue.KING]:
       with self.assertRaises(AssertionError):
         AnnounceMarriageAction(PlayerId.ONE, card)
     else:
       AnnounceMarriageAction(PlayerId.ONE, card)
  def test_actions_when_player_is_to_lead_talon_is_empty(self):
    game_state = get_game_state_with_empty_talon_for_tests()

    actions = get_available_actions(game_state)
    self.assertEqual(set(actions),
                     set(get_available_actions(game_state.next_player_view())))
    expected_actions = [
      PlayCardAction(PlayerId.ONE, Card(Suit.CLUBS, CardValue.ACE)),
      PlayCardAction(PlayerId.ONE, Card(Suit.HEARTS, CardValue.KING)),
      PlayCardAction(PlayerId.ONE, Card(Suit.HEARTS, CardValue.TEN)),
      PlayCardAction(PlayerId.ONE, Card(Suit.SPADES, CardValue.TEN)),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))

    game_state = get_game_state_with_empty_talon_for_tests()
    with GameStateValidator(game_state):
      game_state.next_player = PlayerId.TWO

    actions = get_available_actions(game_state)
    self.assertEqual(set(actions),
                     set(get_available_actions(game_state.next_player_view())))
    expected_actions = [
      PlayCardAction(PlayerId.TWO, Card(Suit.DIAMONDS, CardValue.JACK)),
      AnnounceMarriageAction(PlayerId.TWO, Card(Suit.CLUBS, CardValue.KING)),
      PlayCardAction(PlayerId.TWO, Card(Suit.CLUBS, CardValue.JACK)),
      AnnounceMarriageAction(PlayerId.TWO, Card(Suit.CLUBS, CardValue.QUEEN)),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))
 def test_announcing_marriage_is_enough_to_win_the_game(self):
   game_state = get_game_state_for_tests()
   with GameStateValidator(game_state):
     game_state.next_player = PlayerId.TWO
   king_clubs = Card(Suit.CLUBS, CardValue.KING)
   action = AnnounceMarriageAction(PlayerId.TWO, king_clubs)
   self.assertTrue(action.can_execute_on(game_state))
   game_state = action.execute(game_state)
   queen_clubs = game_state.cards_in_hand[PlayerId.TWO][4]
   self.assertTrue(queen_clubs.public)
   self.assertEqual(93, game_state.trick_points[PlayerId.TWO])
   self.assertTrue(game_state.is_game_over)
 def test_announce_non_trump_marriage(self):
   game_state = get_game_state_for_tests()
   queen_hearts = Card(Suit.HEARTS, CardValue.QUEEN)
   action = AnnounceMarriageAction(PlayerId.ONE, queen_hearts)
   self.assertTrue(action.can_execute_on(game_state))
   game_state = action.execute(game_state)
   self.assertEqual(42, game_state.trick_points[PlayerId.ONE])
   self.assertEqual(queen_hearts, game_state.current_trick[PlayerId.ONE])
   self.assertEqual([Suit.HEARTS], game_state.marriage_suits[PlayerId.ONE])
   self.assertEqual(PlayerId.TWO, game_state.next_player)
   king_hearts = game_state.cards_in_hand[PlayerId.ONE][1]
   self.assertTrue(king_hearts.public)
 def test_can_only_execute_before_leading_a_trick(self):
   game_state = get_game_state_for_tests()
   queen_hearts = Card(Suit.HEARTS, CardValue.QUEEN)
   action = AnnounceMarriageAction(PlayerId.ONE, queen_hearts)
   self.assertTrue(action.can_execute_on(game_state))
   self.assertTrue(action.can_execute_on(game_state.next_player_view()))
   king_clubs = Card(Suit.CLUBS, CardValue.KING)
   action = AnnounceMarriageAction(PlayerId.TWO, king_clubs)
   self.assertFalse(action.can_execute_on(game_state))
   self.assertFalse(action.can_execute_on(game_state.next_player_view()))
   game_state.next_player = PlayerId.TWO
   self.assertTrue(action.can_execute_on(game_state))
   self.assertTrue(action.can_execute_on(game_state.next_player_view()))
 def test_announce_trump_marriage(self):
   game_state = get_game_state_for_tests()
   with GameStateValidator(game_state):
     game_state.next_player = PlayerId.TWO
   queen_clubs = Card(Suit.CLUBS, CardValue.QUEEN)
   action = AnnounceMarriageAction(PlayerId.TWO, queen_clubs)
   self.assertTrue(action.can_execute_on(game_state))
   game_state = action.execute(game_state)
   self.assertEqual(93, game_state.trick_points[PlayerId.TWO])
   self.assertEqual(queen_clubs, game_state.current_trick[PlayerId.TWO])
   self.assertEqual([Suit.DIAMONDS, Suit.CLUBS],
                    game_state.marriage_suits[PlayerId.TWO])
   self.assertEqual(PlayerId.ONE, game_state.next_player)
   king_clubs = game_state.cards_in_hand[PlayerId.TWO][1]
   self.assertTrue(king_clubs.public)
Beispiel #9
0
    def _on_lead_follow_suit(self, game_view: GameState) -> PlayerAction:
        # If talon is depleted, the probabilities here would be either 0 or 1. If
        # we have any card with 100% winning prob, play it. If the talon is closed,
        # play the card with the highest probability to win (can be smaller than 1).
        probabilities = self._get_winning_prob(game_view)
        logging.debug("HeuristicPlayer: Card win probabilities:\n%s",
                      _pprint(probabilities))
        max_prob = max(probabilities.values())
        logging.debug(
            "HeuristicPlayer: The maximum winning probability is %.2f",
            max_prob)
        if max_prob > 0:  # TODO: Set a threshold here.
            # If we have a marriage and the king has the same winning chance as the
            # maximum among all the other cards in hand, prefer to announce the
            # marriage.
            if self._marriage_suit is not None:
                king = Card(self._marriage_suit, CardValue.KING)
                king_prob = probabilities.get(king, 0)
                logging.debug("HeuristicPlayer: %s probability: %.2f", king,
                              king_prob)
                if king_prob == max_prob:
                    logging.debug(
                        "HeuristicPlayer: Announcing the marriage for %s",
                        king)
                    return AnnounceMarriageAction(self.id, king)

            # Play a random card among the ones with the highest chance to win the
            # next trick.
            card_to_play = random.choice([
                card for card, prob in probabilities.items()
                if prob == max_prob
            ])
            logging.debug(
                "HeuristicPlayer: Play a card with the maximum win probability %s",
                card_to_play)
            return self._play_card_or_marriage(card_to_play)

        # If there is no chance we win the next trick and we have a marriage,
        # announce it.
        if self._marriage_suit is not None:
            return AnnounceMarriageAction(
                self.id, Card(self._marriage_suit, CardValue.KING))

        # Discard one of the small cards.
        card_to_play = self._best_discard(game_view)
        return PlayCardAction(self.id, card_to_play)
  def test_announce_marriage_without_scoring_any_trick(self):
    game_state = get_game_state_for_tests()
    with GameStateValidator(game_state):
      for trick in game_state.won_tricks[PlayerId.ONE]:
        game_state.talon.extend([trick.one, trick.two])
      for trick in game_state.won_tricks[PlayerId.TWO]:
        game_state.talon.extend([trick.one, trick.two])
      game_state.won_tricks = PlayerPair([], [])
      game_state.trick_points = PlayerPair(0, 0)
      game_state.marriage_suits[PlayerId.TWO] = []

    queen_hearts = Card(Suit.HEARTS, CardValue.QUEEN)
    action = AnnounceMarriageAction(PlayerId.ONE, queen_hearts)
    self.assertTrue(action.can_execute_on(game_state))
    game_state = action.execute(game_state)
    self.assertEqual(0, game_state.trick_points[PlayerId.ONE])
    self.assertEqual(queen_hearts, game_state.current_trick[PlayerId.ONE])
    self.assertEqual([Suit.HEARTS], game_state.marriage_suits[PlayerId.ONE])
    self.assertEqual(PlayerId.TWO, game_state.next_player)
    king_hearts = game_state.cards_in_hand[PlayerId.ONE][1]
    self.assertTrue(king_hearts.public)
 def test_equality(self):
   self.assertEqual(
     AnnounceMarriageAction(PlayerId.ONE, Card(Suit.DIAMONDS, CardValue.KING)),
     AnnounceMarriageAction(PlayerId.ONE, Card(Suit.DIAMONDS, CardValue.KING)))
   self.assertNotEqual(
     AnnounceMarriageAction(PlayerId.ONE, Card(Suit.DIAMONDS, CardValue.KING)),
     AnnounceMarriageAction(PlayerId.TWO, Card(Suit.DIAMONDS, CardValue.KING)))
   self.assertNotEqual(
     AnnounceMarriageAction(PlayerId.ONE, Card(Suit.DIAMONDS, CardValue.KING)),
     AnnounceMarriageAction(PlayerId.ONE,
                            Card(Suit.DIAMONDS, CardValue.QUEEN)))
   self.assertNotEqual(
     AnnounceMarriageAction(PlayerId.TWO, Card(Suit.DIAMONDS, CardValue.KING)),
     AnnounceMarriageAction(PlayerId.ONE,
                            Card(Suit.DIAMONDS, CardValue.QUEEN)))
   self.assertNotEqual(
     AnnounceMarriageAction(PlayerId.TWO, Card(Suit.DIAMONDS, CardValue.KING)),
     PlayCardAction(PlayerId.TWO, Card(Suit.DIAMONDS, CardValue.KING)))
 def test_both_cards_must_be_in_hand(self):
   game_state = get_game_state_for_tests()
   with GameStateValidator(game_state):
     game_state.next_player = PlayerId.TWO
   queen_diamonds = Card(Suit.DIAMONDS, CardValue.QUEEN)
   action = AnnounceMarriageAction(PlayerId.TWO, queen_diamonds)
   self.assertFalse(action.can_execute_on(game_state))
   self.assertFalse(action.can_execute_on(game_state.next_player_view()))
   # Swap the queen of spades with the trump card
   with GameStateValidator(game_state):
     queen_spades = game_state.cards_in_hand[PlayerId.TWO].pop()
     game_state.cards_in_hand[PlayerId.TWO].append(game_state.trump_card)
     game_state.trump_card = queen_spades
     game_state.trump_card.public = True
   action = AnnounceMarriageAction(PlayerId.TWO, queen_spades)
   self.assertFalse(action.can_execute_on(game_state))
   self.assertFalse(action.can_execute_on(game_state.next_player_view()))
Beispiel #13
0
 def test_trump_marriage_is_preferred(self):
   self.assertIn(get_best_marriage(
     [
       PlayCardAction(PlayerId.ONE, Card.from_string("jh")),
       PlayCardAction(PlayerId.ONE, Card.from_string("ad")),
       PlayCardAction(PlayerId.ONE, Card.from_string("js")),
       PlayCardAction(PlayerId.ONE, Card.from_string("ts")),
       PlayCardAction(PlayerId.ONE, Card.from_string("qc")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("kh")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qh")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("ks")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qs")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("kc")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qc")),
     ], Suit.SPADES),
     {
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("ks")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qs")),
     })
Beispiel #14
0
 def test_generic_on_lead_follow_suit(self):
   self._run_test_cases([
     # Play a non-trump card with the maximum win probability.
     {
       "cards_in_hand": (["qd", "qs", "kh", "ah"], [None, None, None, "th"]),
       "trump": Suit.SPADES,
       "trump_card": "ks",
       "talon": [None, None, None, None, None, None, None, None, None],
       "won_tricks": ([("qh", "qc")], []),
       "player_that_closed_the_talon": PlayerId.ONE,
       "opponent_points_when_talon_was_closed": 0,
       "expected_action": PlayCardAction(PlayerId.ONE, Card.from_string("ah")),
     },
     # Play a trump card with the maximum win probability.
     {
       "cards_in_hand": (["qd", "qs", "ks", "th"], [None, None, None, None]),
       "trump": Suit.HEARTS,
       "trump_card": "jh",
       "talon": [None, None, None, None, None, None, None, None, None],
       "won_tricks": ([("qh", "qc")], []),
       "player_that_closed_the_talon": PlayerId.ONE,
       "opponent_points_when_talon_was_closed": 0,
       "expected_action": PlayCardAction(PlayerId.ONE, Card.from_string("th")),
     },
     # If we have a marriage pair among the cards with the max winning
     # probability, announce it with the king.
     {
       "cards_in_hand": (["qd", "qs", "ks", "ts"], [None, None, None, None]),
       "trump": Suit.SPADES,
       "trump_card": "js",
       "talon": [None, None, None, None, None, None, None, None, None],
       "won_tricks": ([("qh", "qc")], []),
       "player_that_closed_the_talon": PlayerId.ONE,
       "opponent_points_when_talon_was_closed": 0,
       "expected_action": AnnounceMarriageAction(PlayerId.ONE,
                                                 Card.from_string("ks")),
     },
     # If we don't have any chance to win the next trick and we have a
     # marriage, announce it.
     {
       "cards_in_hand": (["qd", "qs", "ks", "ts"], ["as", "ad", None, None]),
       "trump": Suit.SPADES,
       "trump_card": "js",
       "talon": [None, None, None, None, None, None, None, None, None],
       "won_tricks": ([("qh", "qc")], []),
       "player_that_closed_the_talon": PlayerId.ONE,
       "opponent_points_when_talon_was_closed": 0,
       "expected_action": AnnounceMarriageAction(PlayerId.ONE,
                                                 Card.from_string("ks")),
     },
     # Discard the smallest non-trump card.
     {
       "cards_in_hand": (["ts", "qs", "th", "jd"], ["as", "ah", "ad", None]),
       "trump": Suit.SPADES,
       "trump_card": "ks",
       "talon": [None, None, None, None, None, None, None, None, None],
       "won_tricks": ([("qh", "qd")], []),
       "player_that_closed_the_talon": PlayerId.ONE,
       "opponent_points_when_talon_was_closed": 0,
       "expected_action": PlayCardAction(PlayerId.ONE, Card.from_string("jd")),
     },
     # Discard the smallest trump card.
     {
       "cards_in_hand": (["ts", "qs"], ["as", None]),
       "trump": Suit.SPADES,
       "trump_card": "ks",
       "talon": [None, None, None, None, None, None, None, None, None],
       "won_tricks": ([("qh", "qd")], [("tc", "ac"), ("jh", "jc")]),
       "player_that_closed_the_talon": PlayerId.ONE,
       "opponent_points_when_talon_was_closed": 0,
       "expected_action": PlayCardAction(PlayerId.ONE, Card.from_string("qs")),
     },
   ])
Beispiel #15
0
 def test_generic_on_lead_do_not_follow_suit(self):
   self._run_test_cases([
     # Exchanges trump when possible.
     {
       "cards_in_hand": (["qd", "kc", "jc", "js", "qc"],
                         [None, None, None, None, None]),
       "trump": Suit.CLUBS,
       "trump_card": "ac",
       "talon": [None, None, None, None, None, None, None, None, None],
       "expected_action": ExchangeTrumpCardAction(PlayerId.ONE)
     },
     # Play a high trump card if it will secure the win.
     {
       "cards_in_hand": (["tc", "ks", "ac", "jh", "ad"],
                         [None, None, None, None, None]),
       "trump": Suit.CLUBS,
       "trump_card": "jc",
       "talon": [None, None, None, None, None, None, None],
       "won_tricks": ([("qc", "ah")], []),
       "marriage_suits": ([Suit.CLUBS], []),
       "expected_action": {
         PlayCardAction(PlayerId.ONE, Card.from_string("ac")),
         PlayCardAction(PlayerId.ONE, Card.from_string("tc")),
       }
     },
     # Play a high non trump card if it will secure the lead.
     {
       "cards_in_hand": (["ks", "qd", "td", "jh", "ad"],
                         [None, None, None, None, None]),
       "trump": Suit.CLUBS,
       "trump_card": "jc",
       "talon": [None, None, None],
       "won_tricks": ([("qc", "ah"), ("kc", "jd")], [("tc", "ac")]),
       "marriage_suits": ([Suit.CLUBS], []),
       "expected_action": {
         PlayCardAction(PlayerId.ONE, Card.from_string("ad")),
         PlayCardAction(PlayerId.ONE, Card.from_string("td")),
       }
     },
     # Play a high trump card if it will secure the lead. Take into account the
     # marriage in hand.
     {
       "cards_in_hand": (["kd", "qd", "ah", "jd", "ad"],
                         [None, None, None, None, None]),
       "trump": Suit.HEARTS,
       "trump_card": "jh",
       "talon": [None, None, None, None, None],
       "won_tricks": ([("qc", "as")], [("kc", "tc")]),
       "marriage_suits": ([Suit.CLUBS], []),
       "expected_action": PlayCardAction(PlayerId.ONE, Card.from_string("ah"))
     },
     # Announce the marriage with queen.
     {
       "cards_in_hand": (["qd", "kc", "jc", "js", "qc"],
                         [None, None, None, None, None]),
       "trump": Suit.DIAMONDS,
       "trump_card": "ac",
       "talon": [None, None, None, None, None, None, None, None, None],
       "expected_action": AnnounceMarriageAction(PlayerId.ONE,
                                                 Card.from_string("qc"))
     },
     # Announce the marriage with king.
     {
       "cards_in_hand": (["qd", "kc", "jc", "ac", "qc"],
                         [None, None, None, None, None]),
       "trump": Suit.DIAMONDS,
       "trump_card": "ad",
       "talon": [None, None, None, None, None, None, None],
       "won_tricks": ([("tc", "js")], []),
       "expected_action": AnnounceMarriageAction(PlayerId.ONE,
                                                 Card.from_string("kc"))
     },
     # Discard the smallest non-trump card.
     {
       "cards_in_hand": (["qh", "qd", "ac", "qs", "tc"],
                         [None, None, None, None, None]),
       "trump": Suit.HEARTS,
       "trump_card": "ah",
       "talon": [None, None, None, None, None, None, None, None, None],
       "expected_action": {
         PlayCardAction(PlayerId.ONE, Card.from_string("qd")),
         PlayCardAction(PlayerId.ONE, Card.from_string("qs")),
       }
     },
   ])
 def test(self):
     action_1 = AnnounceMarriageAction(PlayerId.ONE,
                                       Card(Suit.SPADES, CardValue.QUEEN))
     action_2 = ExchangeTrumpCardAction(PlayerId.ONE)
     action_3 = CloseTheTalonAction(PlayerId.ONE)
     action_4 = PlayCardAction(PlayerId.ONE, Card(Suit.CLUBS,
                                                  CardValue.KING))
     actions_and_scores_list = [
         {
             action_1:
             ScoringInfo(q=5,
                         n=6,
                         score=5 / 6,
                         fully_simulated=False,
                         terminal=False),
             action_2:
             ScoringInfo(q=6,
                         n=6,
                         score=-0.33,
                         fully_simulated=True,
                         terminal=False),
             action_3:
             ScoringInfo(q=5,
                         n=6,
                         score=5 / 6,
                         fully_simulated=False,
                         terminal=False),
         },
         {
             action_1:
             ScoringInfo(q=15,
                         n=20,
                         score=15 / 20,
                         fully_simulated=False,
                         terminal=False),
             action_2:
             ScoringInfo(q=20,
                         n=20,
                         score=20 / 20,
                         fully_simulated=False,
                         terminal=False),
             action_3:
             ScoringInfo(q=5,
                         n=20,
                         score=5 / 20,
                         fully_simulated=False,
                         terminal=False),
         },
         {
             action_1:
             ScoringInfo(q=15,
                         n=15,
                         score=0.66,
                         fully_simulated=True,
                         terminal=False),
             action_2:
             ScoringInfo(q=12,
                         n=20,
                         score=12 / 20,
                         fully_simulated=False,
                         terminal=False),
             action_4:
             ScoringInfo(q=-3,
                         n=10,
                         score=-3 / 10,
                         fully_simulated=False,
                         terminal=False),
         },
     ]
     actions_and_scores = average_score_with_tiebreakers(
         actions_and_scores_list)
     self.assertEqual(
         {
             (action_1, 0.7478),
             (action_2, 0.4233),
             (action_3, 0.5417),
             (action_4, -0.3),
         }, {(action, round(score[0], 4))
             for action, score in actions_and_scores})
     self.assertEqual(
         {
             (action_1, 0.8611),
             (action_2, 0.8667),
             (action_3, 0.5417),
             (action_4, -0.3),
         }, {(action, round(score[1], 4))
             for action, score in actions_and_scores})
     self.assertEqual(
         {
             (action_1, 50),
             (action_2, 100),
             (action_3, -100),
             (action_4, -4),
         }, {(action, score[2])
             for action, score in actions_and_scores})
    def init_from_game_state(
        self,
        game_state: GameState,
        done_callback: Closure,
        game_score: PlayerPair[int] = PlayerPair(0, 0)
    ) -> None:
        """
    Updates this GameWidget such that it represents the game state provided as
    an argument. It does not hold a reference to the game_state object. This
    GameWidget will not update itself automatically if subsequent changes are
    performed on the game_state object.
    :param game_state The initial game_state that this widget should represent.
    :param done_callback The closure that should be called once the GameWidget
    has finished initializing itself.
    :param game_score The Bummerl game score.
    """
        # Init the cards for each player.
        self._sorted_cards = PlayerPair(
            _sort_cards_for_player(game_state.cards_in_hand.one, PlayerId.ONE),
            _sort_cards_for_player(game_state.cards_in_hand.two, PlayerId.TWO))
        self._update_cards_in_hand_after_animation()

        # Init the won tricks for each player.
        for player in PlayerId:
            for trick in game_state.won_tricks[player]:
                self._cards[trick.one].visible = True
                self._tricks_widgets[player].add_card(self._cards[trick.one])
                self._cards[trick.two].visible = True
                self._tricks_widgets[player].add_card(self._cards[trick.two])

        # Init the trump card and the talon.
        if game_state.trump_card is not None:
            self._talon.set_trump_card(self._cards[game_state.trump_card])
        for i, card in enumerate(reversed(game_state.talon)):
            card_widget = self._cards[card]
            card_widget.visible = False
            if i != 0:
                card_widget.shadow = False
            self._talon.push_card(card_widget)

        if game_state.is_talon_closed:
            self._talon.closed = True

        # Init the scores.
        self.on_score_modified(game_state.trick_points)
        self._update_game_score(game_score)

        # Set the trump image source here since we know what the trump suit is, but
        # only make it visible when the talon is empty.
        self._init_trump_suit_image(game_state.trump)

        # If a card is already played check if it was a simple card play or a
        # marriage announcement and execute the corresponding action.
        for player in PlayerId:
            card = game_state.current_trick[player]
            if card is None:
                continue
            if card.suit in game_state.marriage_suits[player] and \
                card.card_value in [CardValue.QUEEN, CardValue.KING] and \
                card.marriage_pair in game_state.cards_in_hand[player]:
                action = AnnounceMarriageAction(player, card)
            else:
                action = PlayCardAction(player, card)
            # pylint: disable=cell-var-from-loop
            Clock.schedule_once(
                lambda *_: self.on_action(action, done_callback), -1)
            # pylint: enable=cell-var-from-loop
            return

        # If we didn't call on_action() above, we are done with the initialization.
        # If animations are enabled, we just flip the cards in the human player's
        # hand.
        Clock.schedule_once(
            lambda *_: self._flip_human_player_cards(game_state, done_callback
                                                     ), -1)
 def test_card_property(self):
   action = AnnounceMarriageAction(PlayerId.ONE,
                                   Card(Suit.DIAMONDS, CardValue.KING))
   self.assertEqual(Card(Suit.DIAMONDS, CardValue.KING), action.card)
Beispiel #19
0
 def test_non_trump_marriages_are_preferred_randomly(self):
   self.assertIn(get_best_marriage(
     [
       PlayCardAction(PlayerId.ONE, Card.from_string("jh")),
       PlayCardAction(PlayerId.ONE, Card.from_string("ad")),
       PlayCardAction(PlayerId.ONE, Card.from_string("js")),
       PlayCardAction(PlayerId.ONE, Card.from_string("ts")),
       PlayCardAction(PlayerId.ONE, Card.from_string("qc")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("kh")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qh")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("ks")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qs")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("kc")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qc")),
     ], Suit.DIAMONDS),
     {
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("kh")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qh")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("ks")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qs")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("kc")),
       AnnounceMarriageAction(PlayerId.ONE, Card.from_string("qc")),
     })