def test_follow_suit(self): player = create_player() card1 = Card(label='4', suit='D') card2 = Card(label='5', suit='H') card = player.play(trump_suit=card1.suit, played_cards=[card1, card2]) self.assertEqual(card.suit, card1.suit)
def test_no_trump_highest_lead_suit_wins(self, mock_play): a = Card(label='A', suit='a', rank=1, value=15) b = Card(label='2', suit='b', rank=2, value=3) c = Card(label='Joker', suit='c', rank=20, value=0) d = Card(label='?', suit='d', rank=50, value=0) cards = [a, b, c, d] mock_play.side_effect = cards game = create_game(cards, n_players=4) game._trump_suit = 'e' points = sum([card.value for card in cards]) game.play_trick() self.assertEqual(game._trick_points[0], points)
def test_multiple_trumps_highest_trump_wins(self, mock_play): high_trump = Card(label='A', suit='D', rank=1, value=15) low_trump = Card(label='2', suit='D', rank=2, value=3) bad_spade = Card(label='Joker', suit='S', rank=20, value=0) bad_hearts = Card(label='?', suit='H', rank=50, value=0) cards = [low_trump, bad_spade, high_trump, bad_hearts] mock_play.side_effect = cards game = create_game(cards, n_players=4) game._trump_suit = 'D' points = sum([card.value for card in cards]) game.play_trick() self.assertEqual(game._trick_points[2], points)
def test_trump_lead_highest_trump_wins(self, mock_play): trump_card = Card(label='A', suit='C', rank=1, value=5) a_diamond = Card(label='2', suit='D', rank=2, value=3) bad_spade = Card(label='Joker', suit='S', rank=20, value=0) bad_hearts = Card(label='?', suit='H', rank=50, value=0) cards = [trump_card, bad_spade, a_diamond, bad_hearts] mock_play.side_effect = cards game = create_game(cards, n_players=4) game._trump_suit = 'C' points = sum([card.value for card in cards]) game.play_trick() self.assertEqual(game._trick_points[0], points)
def create_player(labels=('1', '2', '3'), suits=('H', 'D', 'C')): player = Player() for label in labels: for suit in suits: card = Card(label=label, suit=suit) player.add_card(card) return player
def create_deck(labels, suits): cards = [] for label in labels: for suit in suits: card = Card(label=label, suit=suit) cards.append(card) return Deck(cards)
def create_twentynine_deck() -> Deck: suits = ['H', 'S', 'D', 'C'] labels = ['J', '9', 'A', '10', 'K', 'Q', '8', '7'] # in rank order values = {'J': 3, '9': 2, 'A': 1, '10': 1} cards = [] for suit in suits: for rank, label in enumerate(labels): rank = rank + 1 value = values[label] if label in values else 0 cards.append(Card(label=label, suit=suit, rank=rank, value=value)) return Deck(cards=cards)
def play_trick(self) -> None: starting_player = (self._dealer_index + 1) % self.n_players played_cards: List[Card] = [Card()] * 4 for idx in range(4): player_idx = (starting_player + idx) % self.n_players played_card = self.player(player_idx).play(self._trump_suit, played_cards) played_cards[player_idx] = played_card lead_suit = played_cards[starting_player].suit winning_player = _winning_player_for_hand(played_cards, lead_suit, self._trump_suit) points = sum([card.value for card in played_cards]) self._trick_points[winning_player] += points
def test_no_trump_all_one_suit_highest_wins(self, mock_play): cards = [ Card(label=str(i), suit='C', rank=i, value=10 - i) for i in range(4) ] mock_play.side_effect = cards game = create_game(cards, n_players=4) game._trump_suit = 'D' points = sum([card.value for card in cards]) game.play_trick() self.assertEqual(game._trick_points[0], points)
def test_makes_bid(self, mock_get_trump, mock_play): cards = [ Card(label=str(i), suit='C', rank=i, value=10 - i) for i in range(4) ] mock_play.side_effect = cards * 4 game = create_game(cards, n_players=4) mock_get_trump.return_value = 'D' points_per_player = sum([card.value for card in cards]) game._highest_bid = Bid(player=0, value=points_per_player * 2) game.play_hand() self.assertEqual(game._game_points[0], 1)
def test_doesnt_make_bid(self, mock_get_trump, mock_arbitrate_bidding, mock_play): cards = [ Card(label=str(i), suit='C', rank=i, value=10 - i) for i in range(4) ] points_per_player = sum([card.value for card in cards]) mock_play.side_effect = cards * 4 mock_arbitrate_bidding.return_value = Bid( player=0, value=(points_per_player * 2) + 1) mock_get_trump.return_value = 'D' game = create_game(cards, n_players=4) game.play_hand() self.assertEqual(game._game_points[1], 1)
def test_multiple_tricks_scored_correctly(self, mock_get_trump, mock_play): cards = [ Card(label=str(i), suit='C', rank=i, value=10 - i) for i in range(4) ] * 4 mock_play.side_effect = cards mock_get_trump.return_value = 'D' game = create_game(cards, n_players=4) points = sum([card.value for card in cards]) game.play_hand() # because dealer is rotating each player will win once # as cards are played in same order each time for i in range(4): self.assertEqual(game._trick_points[i], points / 4)
def test_team_loses_every_hand(self, mock_get_trump, mock_arbitrate_bidding, mock_play): cards = [ Card(label=str(i), suit='C', rank=i, value=i) for i in range(4) ] points_per_player = sum([card.value for card in cards]) mock_play.side_effect = cards * 4 * TARGET_SCORE mock_arbitrate_bidding.return_value = Bid(player=0, value=(points_per_player * 20)) mock_get_trump.return_value = 'D' game = create_game(cards, n_players=4) winning_team = game.run() # player 0 wins all the bids but team 0 loses every hand self.assertEqual(winning_team, 1)