def test_cannot_exchange_trump_if_not_in_players_hand(self):
   game_state = get_game_state_for_tests()
   trump_jack = Card(suit=game_state.trump, card_value=CardValue.JACK)
   self.assertFalse(trump_jack in game_state.cards_in_hand[PlayerId.ONE])
   self.assertTrue(game_state.is_to_lead(PlayerId.ONE))
   self.assertFalse(game_state.is_talon_closed)
   action = ExchangeTrumpCardAction(PlayerId.ONE)
   self.assertFalse(action.can_execute_on(game_state))
   self.assertFalse(action.can_execute_on(game_state.next_player_view()))
 def test_cannot_exchange_trump_when_the_talon_is_empty(self):
   game_state = get_game_state_with_empty_talon_for_tests()
   game_state.next_player = PlayerId.TWO
   self.assertTrue(game_state.is_to_lead(PlayerId.TWO))
   trump_jack = Card(suit=game_state.trump, card_value=CardValue.JACK)
   self.assertTrue(trump_jack in game_state.cards_in_hand[PlayerId.TWO])
   self.assertFalse(game_state.is_talon_closed)
   action = ExchangeTrumpCardAction(PlayerId.TWO)
   self.assertFalse(action.can_execute_on(game_state))
   self.assertFalse(action.can_execute_on(game_state.next_player_view()))
 def test_execute(self):
   game_state = get_game_state_for_tests()
   action = ExchangeTrumpCardAction(PlayerId.TWO)
   game_state.next_player = PlayerId.TWO
   self.assertTrue(action.can_execute_on(game_state))
   trump_card = game_state.trump_card
   trump_jack = Card(suit=game_state.trump, card_value=CardValue.JACK)
   game_state = action.execute(game_state)
   self.assertEqual(game_state.trump_card, trump_jack)
   self.assertTrue(trump_card in game_state.cards_in_hand[PlayerId.TWO])
   index = game_state.cards_in_hand[PlayerId.TWO].index(trump_card)
   self.assertTrue(game_state.cards_in_hand[PlayerId.TWO][index].public)
   self.assertEqual(PlayerId.TWO, game_state.next_player)
 def test_equality(self):
   self.assertEqual(ExchangeTrumpCardAction(PlayerId.ONE),
                    ExchangeTrumpCardAction(PlayerId.ONE))
   self.assertEqual(ExchangeTrumpCardAction(PlayerId.TWO),
                    ExchangeTrumpCardAction(PlayerId.TWO))
   self.assertNotEqual(ExchangeTrumpCardAction(PlayerId.ONE),
                       ExchangeTrumpCardAction(PlayerId.TWO))
   self.assertNotEqual(ExchangeTrumpCardAction(PlayerId.TWO),
                       ExchangeTrumpCardAction(PlayerId.ONE))
   self.assertNotEqual(ExchangeTrumpCardAction(PlayerId.TWO),
                       CloseTheTalonAction(PlayerId.TWO))
  def test_actions_when_player_is_to_lead(self):
    game_state = get_game_state_for_tests()

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

    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.QUEEN)),
      AnnounceMarriageAction(PlayerId.TWO, Card(Suit.CLUBS, CardValue.KING)),
      PlayCardAction(PlayerId.TWO, Card(Suit.CLUBS, CardValue.JACK)),
      AnnounceMarriageAction(PlayerId.TWO, Card(Suit.CLUBS, CardValue.QUEEN)),
      PlayCardAction(PlayerId.TWO, Card(Suit.SPADES, CardValue.JACK)),
      ExchangeTrumpCardAction(PlayerId.TWO),
      CloseTheTalonAction(PlayerId.TWO),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))
 def test_cannot_exchange_trump_when_talon_is_closed(self):
   game_state = get_game_state_for_tests()
   action = ExchangeTrumpCardAction(PlayerId.TWO)
   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()))
   game_state.close_talon()
   self.assertFalse(action.can_execute_on(game_state))
   self.assertFalse(action.can_execute_on(game_state.next_player_view()))
  def test_can_only_execute_before_leading_a_trick(self):
    # Other player is to lead, cannot exchange trump.
    game_state = get_game_state_for_tests()
    self.assertTrue(game_state.is_to_lead(PlayerId.ONE))
    action = ExchangeTrumpCardAction(PlayerId.TWO)
    self.assertFalse(action.can_execute_on(game_state))
    self.assertFalse(action.can_execute_on(game_state.next_player_view()))

    # PlayerId.TWO is to lead, can exchange trump.
    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()))
Exemple #8
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 test_cannot_execute_illegal_action(self):
   game_state = get_game_state_for_tests()
   action = ExchangeTrumpCardAction(PlayerId.TWO)
   self.assertFalse(action.can_execute_on(game_state))
   with self.assertRaises(AssertionError):
     action.execute(game_state)