def test_play_trick_talon_closed_opponent_cannot_follow_suit_or_trump(self):
   game_state = get_game_state_for_tests()
   with GameStateValidator(game_state):
     trick = game_state.won_tricks.one.pop(0)
     game_state.talon.append(trick.one)
     game_state.talon.append(trick.two)
     game_state.trick_points.one -= trick.one.card_value
     game_state.trick_points.one -= trick.two.card_value
     trick = game_state.won_tricks.two.pop(-1)
     game_state.talon.append(trick.one)
     game_state.talon.append(trick.two)
     game_state.trick_points.two -= trick.one.card_value
     game_state.trick_points.two -= trick.two.card_value
     game_state.next_player = PlayerId.TWO
   game_state.close_talon()
   self.assertEqual([False, False, False, False, False],
                    [card.public for card in game_state.talon])
   action = PlayCardAction(PlayerId.TWO, Card(Suit.DIAMONDS, CardValue.QUEEN))
   self.assertTrue(action.can_execute_on(game_state))
   game_state = action.execute(game_state)
   self.assertEqual([False, False, False, False, False],
                    [card.public for card in game_state.talon])
   action = PlayCardAction(PlayerId.ONE, Card(Suit.HEARTS, CardValue.QUEEN))
   self.assertTrue(action.can_execute_on(game_state))
   game_state = action.execute(game_state)
   self.assertEqual([True, False, False, True, True],
                    [card.public for card in game_state.talon])
Example #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)
Example #3
0
    def test_reset_withoutDealer(self):
        # given
        self.game.bid_value = 24
        self.game.dealer = 2
        self.game.skat.append(Card(Card.Suit.DIAMOND, Card.Face.SEVEN))
        self.game.players[0].cards.append(
            Card(Card.Suit.DIAMOND, Card.Face.EIGHT))
        self.game.players[1].cards.append(
            Card(Card.Suit.DIAMOND, Card.Face.NINE))
        self.game.players[2].cards.append(
            Card(Card.Suit.DIAMOND, Card.Face.TEN))
        self.game.passed_bid_players.append(self.game.players[0])

        # when
        self.game.reset()

        # then
        # clear cards had to be called
        self.assertEquals(len(self.game.skat), 0)
        self.assertEquals(len(self.game.players[0].cards), 0)
        self.assertEquals(len(self.game.players[1].cards), 0)
        self.assertEquals(len(self.game.players[2].cards), 0)
        # reset bid value
        self.assertEquals(self.game.bid_value, -1)
        # reset game variant
        self.assertEquals(self.game.game_variant, None)
        # reset passed bid player list
        self.assertEquals(len(self.game.passed_bid_players), 0)
        # untouched dealer
        self.assertEquals(self.game.dealer, 2)
Example #4
0
 def test_init_args_order_and_type(self):
   # Swaps the order of the arguments.
   with self.assertRaisesRegex(TypeError, "suit must be an instance of Suit"):
     Card(CardValue.ACE, Suit.DIAMONDS)
   with self.assertRaisesRegex(TypeError,
                               "card_value must be an instance of CardValue"):
     Card(Suit.DIAMONDS, Suit.DIAMONDS)
  def test_actions_when_player_is_to_lead_talon_is_closed(self):
    game_state = get_game_state_for_tests()
    game_state = CloseTheTalonAction(PlayerId.ONE).execute(game_state)

    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)),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))

    game_state = get_game_state_for_tests()
    with GameStateValidator(game_state):
      game_state.next_player = PlayerId.TWO
    game_state = CloseTheTalonAction(PlayerId.TWO).execute(game_state)

    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)),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))
Example #6
0
    def test_reset_witDealer(self):
        # given
        self.game.bid_value = 24
        self.game.dealer = 2
        self.game.skat.append(Card(Card.Suit.DIAMOND, Card.Face.SEVEN))
        self.game.players[0].cards.append(
            Card(Card.Suit.DIAMOND, Card.Face.EIGHT))
        self.game.players[1].cards.append(
            Card(Card.Suit.DIAMOND, Card.Face.NINE))
        self.game.players[2].cards.append(
            Card(Card.Suit.DIAMOND, Card.Face.TEN))

        # when
        self.game.reset(True)

        # then
        # cleared cards
        self.assertEquals(len(self.game.skat), 0)
        self.assertEquals(len(self.game.players[0].cards), 0)
        self.assertEquals(len(self.game.players[1].cards), 0)
        self.assertEquals(len(self.game.players[2].cards), 0)
        # reset bid value
        self.assertEquals(self.game.bid_value, -1)
        # reset game variant
        self.assertEquals(self.game.game_variant, None)
        # reset dealer
        self.assertEquals(self.game.dealer, -1)
  def test_actions_after_the_opponent_played_one_card_talon_is_closed(self):
    game_state = get_game_state_for_tests()
    game_state = CloseTheTalonAction(PlayerId.ONE).execute(game_state)
    action = PlayCardAction(PlayerId.ONE, Card(Suit.SPADES, CardValue.TEN))
    game_state = action.execute(game_state)

    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.SPADES, CardValue.JACK)),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))

    game_state = get_game_state_for_tests()
    with GameStateValidator(game_state):
      game_state.next_player = PlayerId.TWO
    game_state = CloseTheTalonAction(PlayerId.TWO).execute(game_state)
    action = PlayCardAction(PlayerId.TWO, Card(Suit.SPADES, CardValue.JACK))
    game_state = action.execute(game_state)

    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.SPADES, CardValue.TEN)),
      PlayCardAction(PlayerId.ONE, Card(Suit.SPADES, CardValue.ACE)),
    ]
    self.assertSetEqual(set(expected_actions), set(actions))
  def test_must_follow_suit_must_use_higher_trump(self):
    """
    Player.ONE plays the trump Queen. Player.TWO has three trump cards.
    The valid cards are only the trump King and Ace.
    """
    game_state = get_game_state_with_empty_talon_for_tests()

    with GameStateValidator(game_state):
      ace_clubs = game_state.cards_in_hand.one[0]
      queen_clubs = game_state.cards_in_hand.two[3]
      game_state.cards_in_hand.two[3] = ace_clubs
      game_state.cards_in_hand.one[0] = queen_clubs

    action = PlayCardAction(PlayerId.ONE, queen_clubs)
    self.assertTrue(action.can_execute_on(game_state))
    game_state = action.execute(game_state)
    num_legal_cards = 0
    valid_cards = [Card(Suit.CLUBS, CardValue.KING),
                   Card(Suit.CLUBS, CardValue.ACE)]
    for card in game_state.cards_in_hand[PlayerId.TWO]:
      action = PlayCardAction(PlayerId.TWO, card)
      is_legal_card = action.can_execute_on(game_state)
      self.assertEqual(card in valid_cards, is_legal_card, msg=f"{card}")
      self.assertEqual(is_legal_card,
                       action.can_execute_on(game_state.next_player_view()))
      if is_legal_card:
        num_legal_cards += 1
    self.assertEqual(2, num_legal_cards)
Example #9
0
    def test_leaf_node(self):
        game_state = _get_game_state_with_one_card_left()
        play_jack_clubs = PlayCardAction(PlayerId.ONE,
                                         Card(Suit.CLUBS, CardValue.JACK))
        game_state = play_jack_clubs.execute(game_state)
        mcts = Mcts(PlayerId.TWO)
        root_node = mcts.build_tree(game_state)

        self.assertIsNone(root_node.parent)
        self.assertEqual(1, len(root_node.children))
        self.assertEqual([], root_node.untried_actions)
        self.assertTrue(root_node.fully_expanded)
        self.assertFalse(root_node.terminal)
        self.assertEqual(PlayerId.TWO, root_node.player)
        self.assertFalse(root_node.fully_simulated)

        action = list(root_node.children.keys())[0]
        self.assertEqual(
            PlayCardAction(PlayerId.TWO, Card(Suit.SPADES, CardValue.JACK)),
            action)
        leaf: Node = root_node.children[action]
        self.assertIs(root_node, leaf.parent)
        self.assertIsNone(leaf.children)
        self.assertIsNone(leaf.untried_actions)
        self.assertTrue(leaf.fully_expanded)
        self.assertTrue(leaf.terminal)
        self.assertEqual(PlayerId.ONE, leaf.player)
        self.assertAlmostEqual(0.33, leaf.ucb, delta=0.01)
        self.assertTrue(leaf.fully_simulated)
Example #10
0
    def test_equals(self):
        # given
        card_a = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)
        card_b = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)

        # then
        self.assertEquals(card_a, card_b)
Example #11
0
    def test_notEquals_differentFace(self):
        # given
        card_a = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)
        card_b = Card(Card.Suit.DIAMOND, Card.Face.EIGHT)

        # then
        self.assertNotEquals(card_a, card_b)
Example #12
0
 def test_aspect_ratio_is_set_in_constructor(self):
     card_widget = CardWidget(Card(Suit.SPADES, CardValue.ACE),
                              aspect_ratio=0.5)
     self.assertEqual([50, 100], card_widget.children[0].size)
     card_widget = CardWidget(Card(Suit.SPADES, CardValue.ACE),
                              aspect_ratio=0.25)
     self.assertEqual([25, 100], card_widget.children[0].size)
Example #13
0
 def setUp(self):
     """Initializes four Cards of different ranks and suits."""
     self.heart = Card(Suit.HEART, Rank.ACE)
     self.diam = Card(Suit.DIAMOND, 2)    
     self.spade = Card(Suit.SPADE, 3)
     self.club = Card(Suit.CLUB, Rank.KING)
     self.cards = [self.heart, self.diam, self.spade, self.club]
    def test_compareJacks_invalidLowerJackFails(self):
        # given
        higher_jack = Card(Card.Suit.CLUB, Card.Face.JACK)
        no_jack = Card(Card.Suit.CLUB, Card.Face.TEN)

        # when/then
        self.assertRaises(TypeError, self.game_variant.compare_jacks,
                          higher_jack, no_jack)
    def test_compareJacks_invalidJacksFails(self):
        # given
        no_jack_a = Card(Card.Suit.CLUB, Card.Face.TEN)
        no_jack_b = Card(Card.Suit.DIAMOND, Card.Face.NINE)

        # when/then
        self.assertRaises(TypeError, self.game_variant.compare_jacks,
                          no_jack_a, no_jack_b)
Example #16
0
 def test_marriage_pair(self):
   for suit in Suit:
     king = Card(suit, CardValue.KING)
     queen = Card(suit, CardValue.QUEEN)
     self.assertEqual(king, queen.marriage_pair)
     self.assertEqual(queen, king.marriage_pair)
   with self.assertRaises(AssertionError):
     _ = Card(Suit.DIAMONDS, CardValue.TEN).marriage_pair
    def test_isTrump_TrumpTrue(self):
        # when/then
        for suit in Card.Suit:
            self.assertTrue(
                self.game_variant.is_trump(Card(suit, Card.Face.JACK)))

        for face in Card.Face:
            self.assertTrue(
                self.game_variant.is_trump(Card(Card.Suit.CLUB, face)))
 def test_duck_puzzle(self):
     game_view = get_game_view_for_duck_puzzle()
     action = self._mcts_player.request_next_action(game_view)
     print(f"Selected action: {action}")
     expected_actions = {
         PlayCardAction(PlayerId.ONE, Card(Suit.SPADES, CardValue.ACE)),
         PlayCardAction(PlayerId.ONE, Card(Suit.SPADES, CardValue.TEN))
     }
     self.assertIn(action, expected_actions)
Example #19
0
 def test_pushKingEmpty(self):
     """Tests that a King can be pushed to an empty Tableau."""
     print "tableau: test_pushKingEmpty ",
     king = Card(Suit.SPADE, Rank.KING)
     king.flip_up()
     self.pile.push_card(king)
     self.assertEqual(len(self.pile), 1, 
                      "TestTableauPile.test_pushKingEmpty: A King was \
                      rejected from an empty Tableau.")
 def test_pushNonAceEmpty(self):
     """Tests that an empty Foundation rejects a non-Ace."""
     print "foundation: test_pushNonAceEmpty ",
     nonAce = Card(Suit.SPADE, 2)
     nonAce.flip_up()
     self.pile.push_card(nonAce)
     self.assertNotIn(nonAce, self.pile, 
                      "The empty Foundation pile accepted \
                      a non-Ace of its suit.")
  def test_cancel_two_cards_animation_with_callback(self):
    card_widget_1 = CardWidget(Card(Suit.SPADES, CardValue.QUEEN))
    card_widget_1.pos = 0, 0
    card_widget_2 = CardWidget(Card(Suit.SPADES, CardValue.KING))
    card_widget_2.pos = self.window.size[0], 0
    float_layout = FloatLayout()
    float_layout.add_widget(card_widget_1)
    float_layout.add_widget(card_widget_2)
    self.render(float_layout)

    self.assert_pixels_almost_equal([0, 0], card_widget_1.pos)
    self.assert_pixels_almost_equal([self.window.size[0], 0], card_widget_2.pos)
    animation_controller = AnimationController()
    animation_1 = Animation(x=self.window.size[0], y=self.window.size[1],
                            duration=5)
    on_complete_callback_1 = Mock()
    animation_1.bind(on_complete=on_complete_callback_1)
    animation_controller.add_card_animation(card_widget_1, animation_1)
    animation_2 = Animation(x=0, y=self.window.size[1],
                            duration=2 * animation_1.duration)
    on_complete_callback_2 = Mock()
    animation_2.bind(on_complete=on_complete_callback_2)
    animation_controller.add_card_animation(card_widget_2, animation_2)
    on_both_animations_complete_callback = Mock()
    self.assertFalse(animation_controller.is_running)
    animation_controller.start(on_both_animations_complete_callback)
    self.assertTrue(animation_controller.is_running)

    on_complete_callback_1.assert_not_called()
    on_complete_callback_2.assert_not_called()
    on_both_animations_complete_callback.assert_not_called()

    # Advance a couple of frames to move the cards a little bit, but not
    # complete any animation.
    self.advance_frames(5)
    self.assertNotEqual(0, card_widget_1.x)
    self.assertNotEqual(0, card_widget_1.y)
    self.assertNotEqual(self.window.size[0], card_widget_2.x)
    self.assertNotEqual(0, card_widget_2.y)

    # No callback should be called.
    on_complete_callback_1.assert_not_called()
    on_complete_callback_2.assert_not_called()
    on_both_animations_complete_callback.assert_not_called()

    # Cancel the animations.
    self.assertTrue(animation_controller.is_running)
    animation_controller.cancel()
    self.assertFalse(animation_controller.is_running)

    # All callbacks should be called.
    on_complete_callback_1.assert_called_once()
    on_complete_callback_2.assert_called_once()
    on_both_animations_complete_callback.assert_called_once()

    # Verify that the AnimationController can be reused.
    animation_controller.add_card_animation(card_widget_1, animation_1)
Example #22
0
def append_cards(pile):
    """
    Populates the Tableau pile with alternating Spades and Diamonds 
    built top-down from King to five.
    """
    for i in range(5,14):
        card = Card(Suit.SPADE, i) if i % 2 == 0 else Card(Suit.DIAMOND, i)
        card.flip_up()
        pile.enqueue(card)
Example #23
0
 def test_no_marriage_available(self):
   self.assertIsNone(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")),
     ], Suit.SPADES))
    def test_compareCards_sameSuit(self):
        # given
        diamond_seven = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)
        diamond_eight = Card(Card.Suit.DIAMOND, Card.Face.EIGHT)

        # when/then
        self.assertEquals(
            self.game_variant.compare_cards(diamond_eight, diamond_seven), 1)
        self.assertEquals(
            self.game_variant.compare_cards(diamond_seven, diamond_eight), -1)
Example #25
0
    def test_getCurrentTurnPlayer_thirdHand(self):
        # given
        self.trick.add(self.player2, Card(Card.Suit.DIAMOND, Card.Face.SEVEN))
        self.trick.add(self.player3, Card(Card.Suit.DIAMOND, Card.Face.EIGHT))

        # when
        result = self.trick.get_current_player()

        # then
        self.assertEqual(self.player1, result)
 def test_pushValidCard(self):
     """Tests that push() accepts a valid Card."""
     print "foundation: test_pushValidCard ",
     append_some_cards(self.pile)
     for i in range(7,14):
         card = Card(Suit.SPADE, i)
         card.flip_up()
         self.pile.push_card(card)
         self.assertIn(card, self.pile, 
                       "The Foundation pile rejected a valid card.")
    def test_getHighestCard_sameSuit(self):
        # given
        diamonds_ten = Card(Card.Suit.DIAMOND, Card.Face.TEN)
        diamonds_queen = Card(Card.Suit.DIAMOND, Card.Face.QUEEN)
        diamonds_king = Card(Card.Suit.DIAMOND, Card.Face.KING)

        # when/then
        result = self.game_variant.get_highest_card(
            [diamonds_queen, diamonds_king, diamonds_ten])
        self.assertEquals(Card(Card.Suit.DIAMOND, Card.Face.TEN), result)
Example #28
0
 def test_card_value_and_suit_cannot_be_none(self):
   with self.assertRaisesRegex(ValueError,
                               "card_value and suit cannot be None"):
     print(Card(None, CardValue.ACE))
   with self.assertRaisesRegex(ValueError,
                               "card_value and suit cannot be None"):
     print(Card(Suit.DIAMONDS, None))
   with self.assertRaisesRegex(ValueError,
                               "card_value and suit cannot be None"):
     print(Card(None, None))
    def test_getHighestCard_differentFaceAndSuit(self):
        # given
        diamonds_seven = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)
        hearts_eight = Card(Card.Suit.HEARTS, Card.Face.EIGHT)
        spade_nine = Card(Card.Suit.SPADE, Card.Face.NINE)

        # when/then
        result = self.game_variant.get_highest_card(
            [spade_nine, hearts_eight, diamonds_seven])
        self.assertEquals(Card(Card.Suit.SPADE, Card.Face.NINE), result)
    def test_getHighestCard_TrumpAndSuit(self):
        # given
        diamond_ace = Card(Card.Suit.DIAMOND, Card.Face.ACE)
        diamond_king = Card(Card.Suit.DIAMOND, Card.Face.KING)
        club_seven = Card(Card.Suit.CLUB, Card.Face.SEVEN)

        # when/then
        result = self.game_variant.get_highest_card(
            [diamond_king, club_seven, diamond_ace])
        self.assertEquals(Card(Card.Suit.CLUB, Card.Face.SEVEN), result)
    def test_getHighestCard_jacks(self):
        # given
        diamonds_jack = Card(Card.Suit.DIAMOND, Card.Face.JACK)
        hearts_jack = Card(Card.Suit.HEARTS, Card.Face.JACK)
        spade_jack = Card(Card.Suit.SPADE, Card.Face.JACK)

        # when/then
        result = self.game_variant.get_highest_card(
            [spade_jack, hearts_jack, diamonds_jack])
        self.assertEquals(Card(Card.Suit.SPADE, Card.Face.JACK), result)
    def test_getHighestCard_jackAndSuit(self):
        # given
        diamonds_jack = Card(Card.Suit.DIAMOND, Card.Face.JACK)
        diamonds_king = Card(Card.Suit.DIAMOND, Card.Face.KING)
        club_ace = Card(Card.Suit.CLUB, Card.Face.ACE)

        # when/then
        result = self.game_variant.get_highest_card(
            [diamonds_king, club_ace, diamonds_jack])
        self.assertEquals(Card(Card.Suit.DIAMOND, Card.Face.JACK), result)
Example #33
0
    def test_isComplete_False(self):
        # given
        self.trick.add(self.player1, Card(Card.Suit.DIAMOND, Card.Face.SEVEN))
        self.trick.add(self.player2, Card(Card.Suit.DIAMOND, Card.Face.EIGHT))

        # when
        result = self.trick.is_complete()

        # then
        self.assertFalse(result)
    def test_getHighestCard_sameFace(self):
        # given
        diamonds_seven = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)
        hearts_seven = Card(Card.Suit.HEARTS, Card.Face.SEVEN)
        spade_seven = Card(Card.Suit.SPADE, Card.Face.SEVEN)

        # when/then
        result = self.game_variant.get_highest_card(
            [hearts_seven, spade_seven, diamonds_seven])
        self.assertEquals(Card(Card.Suit.HEARTS, Card.Face.SEVEN), result)
    def test_compareCards_sameFace(self):
        # given
        diamonds_seven = Card(Card.Suit.DIAMOND, Card.Face.SEVEN)
        heats_seven = Card(Card.Suit.HEARTS, Card.Face.SEVEN)

        # when/then
        self.assertEquals(
            self.game_variant.compare_cards(heats_seven, diamonds_seven), 0)
        self.assertEquals(
            self.game_variant.compare_cards(diamonds_seven, heats_seven), 0)
 def test_pushInvalidRankCard(self):
     """Tests that push() rejects a Card with an invalid rank."""
     print "foundation: test_pushInvalidRankCard ",
     append_some_cards(self.pile)
     for i in range(8,14):
         card = Card(Suit.SPADE, i)
         card.flip_up()
         self.pile.push_card(card)
         self.assertNotIn(card, self.pile, 
                          "The Foundation pile accepted a card with \
                          invalid rank.")
 def test_pushAceEmpty(self):
     """
     Tests that an empty Foundation pile accepts an Ace of the 
     proper suit.
     """
     print "foundation: test_pushAceEmpty ",
     aceS = Card(Suit.SPADE, 1)
     aceS.flip_up()
     self.pile.push_card(aceS)
     self.assertIn(aceS, self.pile, 
                   "The empty Foundation pile rejected a valid Ace.")
Example #38
0
 def test_pushInvalidEmpty(self):
     """
     Tests that push_card() on an empty Tableau pile rejects 
     non-Kings.
     """
     print "tableau: test_pushInvalidEmpty ",
     nonking = Card(Suit.SPADE, 5)
     nonking.flip_up()
     self.pile.push_card(nonking)
     self.assertEqual(len(self.pile), 0, 
                      "TestTableauPile.test_pushInvalidEmpty: A non-King \
                      was pushed to an empty Tableau pile.")
 def test_enqueueCard(self):
     """
     Tests that enqueue() does not insert a Card at the bottom of a
     Foundation pile.
     """
     print "foundation: test_enqueueCard ",
     for i in range(1,6):
         card = Card(Suit.SPADE, i)
         card.flip_up()
         self.pile.enqueue_card(card)
         self.assertNotIn(card, self.pile, 
                          "A Card was enqueued to a Foundation pile.")
Example #40
0
 def test_pushInvalidRank(self):
     """
     Tests that a Foundation pile rejects a Card of the improper 
     rank.
     """
     print "tableau: test_pushInvalidRank ",
     append_cards(self.pile)
     oldSize = len(self.pile)
     invalidRankCard = Card(Suit.SPADE, 3)
     invalidRankCard.flip_up()
     self.pile.push_card(invalidRankCard)
     self.assertEqual(len(self.pile), oldSize, 
                      "TestTableauPile.test_pushInvalidRank: A Card with \
                      an invalid rank was pushed to a Tableau pile.")
Example #41
0
 def test_pushInvalidColor(self):
     """
     Tests that a Foundation pile rejects a Card of the improper 
     color.
     """
     print "tableau: test_pushInvalidColor ",
     append_cards(self.pile)
     oldSize = len(self.pile)
     invalidColorCard = Card(Suit.HEART, 4)
     invalidColorCard.flip_up()
     self.pile.push_card(invalidColorCard)
     self.assertEqual(len(self.pile), oldSize, 
                      "TestTableauPile.test_pushInvalidColor: A Card with \
                      an invalid color was pushed to a Tableau pile.")
Example #42
0
 def test_pushValidCard(self):
     """
     Tests that a Card of the appropriate rank and suit can be 
     pushed to the pile.
     """
     print "tableau: test_pushValidCard ",
     append_cards(self.pile)
     oldSize = len(self.pile)
     validCard = Card(Suit.SPADE, 4)
     validCard.flip_up()
     self.pile.push_card(validCard)
     self.assertEqual(len(self.pile), oldSize + 1, 
                      "TestTableauPile.test_pushValidCard: A valid Card \
                      was not pushed to the pile.")
Example #43
0
 def test_pushInvalidSuitPile(self):
     """
     Tests that a Foundation pile rejects a pile that is built 
     properly, but the top card is not of the proper color.
     """
     print "tableau: test_pushInvalidSuitPile ",
     append_cards(self.pile)
     oldCards = self.pile
     invalidPile = Pile()
     for i in range(1,5):
         card = Card(Suit.DIAMOND, i) if i % 2 == 0 else Card(Suit.SPADE, i)
         card.flip_up()
         invalidPile.enqueue(card)
     self.pile.push_pile(invalidPile)
     self.assertEqual(self.pile, oldCards, 
                      "TestTableauPile.test_pushInvalidSuitPile: A pile \
                      whose top Card had an invalid color was pushed onto \
                      the Tableau.")
Example #44
0
 def test_pushInvalidAlternatingPile(self):
     """
     Tests that a Foundation pile rejects a pile with valid bottom 
     Card but is not alternating in color.
     """
     print "tableau: test_pushInvalidAlternatingPile ",
     append_cards(self.pile)
     oldCards = self.pile
     invalidPile = Pile()
     for i in range(1,5):
         card = Card(Suit.SPADE, i)
         card.flip_up()
         invalidPile.enqueue(card)
     self.pile.push_pile(invalidPile)
     self.assertEqual(self.pile, oldCards, 
                      "TestTableauPile.test_pushInvalidAlternatingPile: \
                      A pile whose top Card had an invalid rank was pushed\
                      onto the Tableau.")
Example #45
0
 def test_pushValidPile(self):
     """
     Tests that a Foundation pile accepts a properly built pile 
     whose bottom card is the proper color and rank.
     """
     print "tableau: test_pushValidPile ",
     append_cards(self.pile)
     self.pile.flip_top()
     oldCards = Pile(copy.copy(list(self.pile)))
     validPile = Pile()
     for i in range(1,5):
         card = Card(Suit.SPADE, i) if i % 2 == 0 else Card(Suit.HEART, i)
         card.flip_up()
         validPile.enqueue(card)
     newCards = copy.copy(validPile)
     oldCards.extend(newCards)
     self.pile.push_pile(validPile)
     self.assertEqual(self.pile, oldCards, 
                      "TestTableauPile.test_pushValidPile: A valid pile \
                      was rejected by the Tableau pile.")
 def test_pushInvalidSuitCard(self):
     """Tests that push() rejects a Card with an invalid suit."""
     print "foundation: test_pushInvalidSuitCard ",
     invalidSuit = Card(Suit.DIAMOND, Rank.ACE)
     invalidSuit.flip_up()
     self.pile.push_card(invalidSuit)
     self.assertNotIn(invalidSuit, self.pile, 
                      "The Foundation pile accepted a card with \
                      invalid suit.")
     invalidSuit = Card(Suit.HEART, Rank.ACE)
     invalidSuit.flip_up()
     self.pile.push_card(invalidSuit)
     self.assertNotIn(invalidSuit, self.pile, 
                      "The Foundation pile accepted a card with \
                      invalid suit.")
Example #47
0
 def test_pushInvalidBuiltPile(self):
     """
     Tests that a Foundation pile rejects a pile with valid bottom 
     Card but is not built properly.
     """
     print "tableau: test_pushInvalidBuiltPile ",
     append_cards(self.pile)
     oldCards = Pile(copy.copy(list(self.pile)))
     invalidPile = Pile()
     for i in range(1,5):
         card = Card(Suit.SPADE, i) if i % 2 == 0 else Card(Suit.DIAMOND, i)
         card.flip_up()
         invalidPile.enqueue_card(card)
     card = Card(Suit.CLUB, 6)
     card.flip_up()
     invalidPile.push_card(card)
     self.pile.push_pile(invalidPile)
     self.assertEqual(self.pile, oldCards, 
                      "An invalid pile was pushed to the Tableau.")
Example #48
0
class TestCard(unittest.TestCase):
    
    """
    
    Tests a the functionality of a Card.
    
    """

    def setUp(self):
        """Initializes four Cards of different ranks and suits."""
        self.heart = Card(Suit.HEART, Rank.ACE)
        self.diam = Card(Suit.DIAMOND, 2)    
        self.spade = Card(Suit.SPADE, 3)
        self.club = Card(Suit.CLUB, Rank.KING)
        self.cards = [self.heart, self.diam, self.spade, self.club]
    
    def tearDown(self):
        """Does nothing."""
        return
    
    def test_initSuit(self):
        """Tests that the init function properly sets the suit."""
        self.assertEqual(self.heart.suit, Suit.HEART, 
                         "The suit of the Ace of Hearts was incorrect.")
        self.assertEqual(self.diam.suit, Suit.DIAMOND, 
                         "The suit of the 2 of Diamonds was incorrect.")
        self.assertEqual(self.spade.suit, Suit.SPADE, 
                         "The suit of the 3 of Spades was incorrect.")
        self.assertEqual(self.club.suit, Suit.CLUB, 
                         "The suit of the King of Clubs was incorrect.")
    
    def test_initRank(self):
        """Tests that the init function properly sets the rank."""
        self.assertEqual(self.heart.rank, Rank.ACE, 
                         "The rank of the Ace of Hearts was incorrect.")
        self.assertEqual(self.diam.rank, 2, 
                         "The rank of the 2 of Diamonds was incorrect.")
        self.assertEqual(self.spade.rank, 3, 
                         "The rank of the 3 of Spades was incorrect.")
        self.assertEqual(self.club.rank, Rank.KING, 
                         "The rank of the King of Clubs was incorrect.")
    
    def test_initSide(self):
        """Tests that all cards are face-down initially."""
        for c in self.cards:
            self.assertEqual(c.side, Side.FACE_DOWN, 
                             "The Cards were not face-down initially.")
    
    def test_initInvalidSuit(self):
        """
        Tests that creating a Card with an invalid suit raises 
        a ValueError.
        """
        with self.assertRaises(ValueError):
            Card('V', Rank.ACE)
    
    def test_initInvalidRank(self):
        """
        Tests that creating a Card with an invalid rank raises
        a ValueError.
        """
        with self.assertRaises(ValueError):
            Card(Suit.SPADE, -1)
    
    def test_flip(self):
        """Tests the flip function."""
        for c in self.cards:
            c.flip()
            self.assertEqual(c.side, Side.FACE_UP, 
                             "The card is still face-down.")
            c.flip()
            self.assertEqual(c.side, Side.FACE_DOWN,
                             "The card is still face-up.")
    
    def test_flipUp(self):
        """Tests the flip up function."""
        for c in self.cards:
            c.flip_up()
            self.assertEqual(c.side, Side.FACE_UP, "The card is not face-up.")
            c.flip_up()
            self.assertEqual(c.side, Side.FACE_UP, "The card is not face-up.")
    
    def test_flipDown(self):
        """Tests the flip down function."""
        for c in self.cards:
            c.flip_up()
            self.assertEqual(c.side, Side.FACE_UP, 
                             "The card is not face-down.")
            c.flip_down()
            self.assertEqual(c.side, Side.FACE_DOWN, 
                             "The card is not face-down.")
    
    def test_isFaceDown(self):
        """
        Tests that is_face_down returns True for face-down cards and
        False for face-up cards.
        """
        for c in self.cards:
            self.assertTrue(c.is_face_down())
            c.flip_up()
            self.assertFalse(c.is_face_down())
    
    def test_isFaceUp(self):
        """
        Tests that is_face_up returns True for face-up cards and False
        for face-down cards.
        """
        for c in self.cards:
            self.assertFalse(c.is_face_up())
            c.flip_up()
            self.assertTrue(c.is_face_up())
    
    def test_isBlack(self):
        """Tests that is_black returns True only for black cards."""
        self.assertFalse(self.heart.is_black(), "Hearts are not black.")
        self.assertFalse(self.diam.is_black(), "Diamonds are not black.")
        self.assertTrue(self.spade.is_black(), "Spades are black.")
        self.assertTrue(self.club.is_black(), "Clubs are black.")
    
    def test_isRed(self):
        """Tests that is_red returns True only for red cards."""
        self.assertTrue(self.heart.is_red(), "Hearts are red.")
        self.assertTrue(self.diam.is_red(), "Diamonds are red.")
        self.assertFalse(self.spade.is_red(), "Spades are not red.")
        self.assertFalse(self.spade.is_red(), "Clubs are not red.")
    
    def test_color(self):
        """Tests that color() returns the appropriate color."""
        self.assertEqual(self.heart.color(), Color.RED, "Hearts are red.")
        self.assertEqual(self.diam.color(), Color.RED, "Diamonds are red.")
        self.assertEqual(self.spade.color(), Color.BLACK, "Spades are black.")
        self.assertEqual(self.club.color(), Color.BLACK, "Clubs are black.")
def append_some_cards(pile):
    """Appends Spades to the pile bottom-up from Ace to seven."""
    for i in range(1,7):
        card = Card(Suit.SPADE, i)
        card.flip_up()
        pile.append(card)