Ejemplo n.º 1
0
 def test_get_top_card_suit(self):
     game = CrazyEights(4)
     game.discard.append(Card(Rank.ACE, Suit.SPADES))
     self.assertEqual(game.get_top_card_suit(), Suit.SPADES)
     game.discard.append(Card(Rank.EIGHT, Suit.SPADES))
     game.curr_suit = Suit.HEARTS
     self.assertEqual(game.get_top_card_suit(), Suit.HEARTS)
Ejemplo n.º 2
0
    def test_play(self):
        game = CrazyEights(4)
        tc = Card(Rank.KING, Suit.HEARTS)
        game.discard.append(tc)
        p1 = game.players.get(1)
        p2 = game.players.get(2)

        # Card cannot be played
        card = Card(Rank.TWO, Suit.CLUBS)
        p1.add_to_hand(card)
        self.assertFalse(game.play(1, card))
        self.assertEqual(len(p1.get_cards()), 6)
        self.assertEqual(game.discard[-1], tc)

        # Card matches rank
        card = Card(Rank.KING, Suit.SPADES)
        p1.add_to_hand(card)
        game.play(1, card, Suit.DIAMONDS)
        self.assertEqual(len(p1.get_cards()), 6)
        self.assertEqual(game.discard[-1], card)

        # Card is an eight, matches neither rank nor suit
        card = Card(Rank.EIGHT, Suit.CLUBS)
        p2.add_to_hand(card)
        game.play(2, card, Suit.DIAMONDS)
        self.assertEqual(len(p2.get_cards()), 5)
        self.assertEqual(game.discard[-1], card)
        self.assertEqual(game.curr_suit, Suit.DIAMONDS)
Ejemplo n.º 3
0
 def test_playable(self):
     game = CrazyEights(4)
     card = Card(Rank.NINE, Suit.HEARTS)
     game.discard.append(card)
     self.assertFalse(game.playable(Card(Rank.ACE, Suit.SPADES)))
     self.assertTrue(game.playable(Card(Rank.NINE, Suit.SPADES)))
     self.assertTrue(game.playable(Card(Rank.ACE, Suit.HEARTS)))
     self.assertTrue(game.playable(Card(Rank.EIGHT, Suit.SPADES)))
Ejemplo n.º 4
0
 def test_shuffle_deck(self):
     deck = Deck()
     deck.shuffle()
     card1 = Card(Rank.ACE, Suit.SPADES)
     card2 = Card(Rank.TWO, Suit.SPADES)
     card1_shuffled = deck._cards[0] is not card1
     card2_shuffled = deck._cards[1] is not card2
     # Extremely unlikely to fail, but can happen
     self.assertTrue(card1_shuffled or card2_shuffled)
Ejemplo n.º 5
0
 def test_reset_round(self):
     game = CrazyEights(2)
     p1 = game.players.get(1)
     p2 = game.players.get(2)
     p1.clear_hand()
     p2.clear_hand()
     p1.add_to_hand(Card(Rank.ACE, Suit.CLUBS))
     p2.add_to_hand(Card(Rank.EIGHT, Suit.DIAMONDS))
     game.reset_round()
     self.assertEqual(p1.get_score(), 49)
Ejemplo n.º 6
0
 def test_get_cards(self):
     cards = [
         Card(Rank.ACE, Suit.SPADES),
         Card(Rank.ACE, Suit.HEARTS),
         Card(Rank.KING, Suit.SPADES),
         Card(Rank.QUEEN, Suit.DIAMONDS)
     ]
     p1 = Player(cards)
     self.assertEqual(len(p1.get_cards()), 4)
     self.assertEqual(len(p1.get_cards(Rank.ACE)), 2)
     self.assertEqual(len(p1.get_cards(Suit.SPADES)), 2)
     self.assertEqual(len(p1.get_cards(Suit.CLUBS)), 0)
Ejemplo n.º 7
0
    def test_play_options(self):
        game = CrazyEights(4)
        p1 = game.players.get(1)

        p1.clear_hand()
        cards = [
            Card(Rank.THREE, Suit.SPADES),
            Card(Rank.FOUR, Suit.DIAMONDS),
            Card(Rank.JACK, Suit.SPADES)
        ]
        [p1.add_to_hand(card) for card in cards]

        game.discard.append(Card(Rank.TEN, Suit.SPADES))
        card_ops = game.play_options(1)
        self.assertEqual(len(card_ops), 2)
        self.assertTrue(all(card in p1.get_cards() for card in card_ops))
Ejemplo n.º 8
0
    def playable(self, card_to_play: Card) -> bool:
        """Checks whether a card can be played.

        Args:
            card_to_play (Card): card to try to play

        Returns:
            bool: whether the card can be played or not
        """
        top_card = self.discard[-1]
        tc_suit = self.get_top_card_suit()  # suit might be set differently

        # Check the conditions to be met for the card to be playable.
        return (card_to_play.get_rank() == top_card.get_rank() or
                card_to_play.get_suit() == tc_suit or
                card_to_play.get_rank() == Rank.EIGHT)
Ejemplo n.º 9
0
    def play(self, player_num: int, card_to_play: Card,
             set_suit: Optional[Suit] = Suit.SPADES) -> bool:
        """Play a specific card, if possible.

        Args:
            player_num (int): number of the player playing the card. Defaults
            to player 1, the user.
            card_to_play (Card): desired card to play
            set_suit (Optional[Suit], optional): suit to change the play to
            if the card being played is an eight. Defaults to spades.

        Returns:
            bool: True if the card was played, and False otherwise
        """
        player = self.players.get(player_num)
        if player.has(card_to_play) and self.playable(card_to_play):
            player.remove_from_hand(card_to_play)
            self.discard.append(card_to_play)

            if not player.has_cards():
                self.game_state = "Round {}".format(str(len(self.round_hist) + 1))
                self.reset_round()

            if card_to_play.get_rank() == Rank.EIGHT:
                self.curr_suit = set_suit

            return True
        return False
Ejemplo n.º 10
0
 def __init__(self, num_decks: Optional[int] = 1):
     # Create the deck.
     self._cards = deque([])
     for suit in Suit:
         for rank in Rank:
             new_card = Card(rank, suit)
             for i in range(num_decks):
                 self._cards.append(new_card)
Ejemplo n.º 11
0
    def test_new_double_deck(self):
        deck = Deck(2)
        self.assertEqual(deck.size(), 2 * _DECK_SZ)

        as_card = Card(Rank.ACE, Suit.SPADES)
        as_count = 0
        for card in deck._cards:
            if card == as_card:
                as_count += 1
        self.assertEqual(as_count, 2)
Ejemplo n.º 12
0
    def handle_card(user_card: str):
        """ Finds a corresponding card based on user input and returns that card of type card. If none exist
        it returns None
        Args: 
            user_card (str): string representation of Users card played

        Return:
            Card: card with rank and suit specified
            None
        """
        user_card = user_card.split(",")
        if len(user_card) != 2:
            return None

        for rank in Rank:
            card_rank = "rank.{}".format(user_card[0].lower())
            for suit in Suit:
                card_suit = "suit.{}".format(user_card[1].lower())

                if str(rank).lower() == card_rank and str(
                        suit).lower() == card_suit:
                    return Card(rank, suit)
        return None
Ejemplo n.º 13
0
 def test_eq(self):
     card1 = Card(Rank.ACE, Suit.SPADES)
     card2 = Card(Rank.ACE, Suit.SPADES)
     card3 = Card(Rank.ACE, Suit.CLUBS)
     self.assertEqual(card1, card2)
     self.assertNotEqual(card1, card3)
Ejemplo n.º 14
0
 def test_get_suit(self):
     card1 = Card(Rank.ACE, Suit.SPADES)
     self.assertEqual(card1.get_suit(), Suit.SPADES)
Ejemplo n.º 15
0
 def test_str(self):
     card1 = Card(Rank.ACE, Suit.SPADES)
     self.assertEqual(card1.__str__(), 'ace of spades')