def test__basic_game_3(capsys, monkeypatch): monkeypatch.setattr("builtins.input", lambda x: "") rand = Random() rand.seed(1) deck = Deck(empty=True, random_instance=rand) deck.add_card(Card("Clubs", "9")) deck.shuffle() game = Game(PLAYERS_1, deck=deck, message=MESSAGE, max_rounds=2) game.play() captured = capsys.readouterr() assert captured.out == GAME_3_RESULTS
def test_basic_game_5(capsys, monkeypatch): monkeypatch.setattr("builtins.input", lambda x: "") cards = [ Card("Clubs", "6"), Card("Spades", "8"), Card("Diamonds", "3"), Card("Clubs", "9"), Card("Clubs", "Ace"), Card("Spades", "King"), Card("Diamonds", "5"), Card("Diamonds", "3"), Card("Spades", "10"), ] deck = Deck(empty=True) for card in cards: deck.add_card(card) game = Game(PLAYERS_2, deck=deck, message=MESSAGE) game.play() captured = capsys.readouterr() assert captured.out == GAME_5_RESULTS
def test_basic_empty_deck(): deck = Deck(empty=True) assert len(deck) == 0 assert deck == [] assert deck.top_card is None card_1 = Card("Clubs", "9") deck.add_card(card_1) assert len(deck) == 1 assert deck == [card_1] assert deck.top_card == card_1 card_2 = Card("Spades", "King") deck.add_card(card_2) assert len(deck) == 2 assert deck == [card_1, card_2] assert deck.top_card == card_2 deck.new_deck() assert len(deck) == 52 assert deck == UNSHUFFLED_DECK assert deck.top_card == UNSHUFFLED_DECK[-1]
class Player: """The Player class is used to manage a person's hand and score. Player objects are initiated by passing a person's name. Their hand starts out as an empty Deck and is expanded by drawing Cards. Player total score or score after a specific round can be received. A Player can be compared to another Player based on score (< or >) or based on name and hand (==). Attributes: hand: The Player's hand of playing cards they currently hold. name: The Player's name. """ def __init__(self, name: str) -> None: """Initiates Player with a name and an empty hand. Args: name: Name of player. """ self.hand = Deck(empty=True) self.name = name def clear_hand(self) -> None: """Empty the Player's hand.""" self.hand = Deck(empty=True) def draw_card(self, card: Card) -> None: """Have the Player draw a card. Args: card: The Card to add to the Player's hand. """ self.hand.add_card(card) def score(self, round_number: int = 0) -> int: # We are assuming 1-indexed round numbers """Calculate and return the Player's total or round score. Args: card: Optional; The Card to add to the Player's hand. """ if round_number: if round_number > len(self.hand): return 0 return self.hand[round_number - 1].score return sum([card.score for card in self.hand]) def __str__(self): return "{name} holds {hand} totalling {points} points".format( name=self.name, hand=self.hand, points=self.score()) def __eq__(self, other): if not isinstance(other, Player): return False return self.hand == other.hand and self.name == other.name def __gt__(self, other): if not isinstance(other, Player): return False return self.score() > other.score() def __lt__(self, other): if not isinstance(other, Player): return False return self.score() < other.score()
def test_basic_player_1(): with pytest.raises(TypeError): player_1 = Player() deck = Deck(empty=True) player_1 = Player("Berkelly") assert player_1.name == "Berkelly" assert player_1.hand == deck assert str(player_1.hand) == str(deck) assert player_1.score() == 0 assert not player_1.score(round_number=1) assert str(player_1) == STRING_1 card_1 = Card("Clubs", "King") deck.add_card(card_1) player_1.draw_card(card_1) assert player_1.hand == deck assert str(player_1.hand) == str(deck) assert player_1.score() == 52 assert player_1.score(round_number=1) == 52 assert str(player_1) == STRING_2 card_2 = Card("Hearts", "8") deck.add_card(card_2) player_1.draw_card(card_2) assert player_1.hand == deck assert str(player_1.hand) == str(deck) assert player_1.score() == 76 assert player_1.score(round_number=1) == 52 assert player_1.score(round_number=2) == 24 assert str(player_1) == STRING_3 assert player_1.hand == deck assert str(player_1.hand) == str(deck) assert player_1.score() == 76 assert player_1.score(round_number=1) == 52 assert player_1.score(round_number=2) == 24 assert str(player_1) == STRING_3 deck = Deck(empty=True) player_1.clear_hand() assert player_1.name == "Berkelly" assert player_1.hand == deck assert str(player_1.hand) == str(deck) assert player_1.score() == 0 assert not player_1.score(round_number=1) assert str(player_1) == STRING_1 player_1.draw_card(card_1) player_1.draw_card(card_2) player_2 = Player("Cez") assert player_1 > player_2 assert player_2 < player_1 card_1 = Card("Diamonds", "8") card_2 = Card("Clubs", "2") player_2.draw_card(card_1) player_2.draw_card(card_2) assert player_1 > player_2 assert player_2 < player_1 player_1 = Player("Cez") player_1.draw_card(card_1) player_1.draw_card(card_2) assert player_1 == player_2 player_1 = Player("Cez") player_1.draw_card(card_2) assert player_1 != player_2