def test_wild_cards_with_same_wildness_are_equal(self): card1 = Card(wild="WILD") card2 = Card(wild="WILD") self.assertEqual(card1, card2) card3 = Card(wild="WILD_DRAW_FOUR", color="RED") card4 = Card(wild="WILD_DRAW_FOUR", color="RED") self.assertEqual(card3, card4)
def test_player_has_an_extra_card_if_there_Are_no_valid_cards(self): players = [Player(name="DSP", cards=[]), Player(name="SPD", cards=[])] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", 4) game.discard_pile = [Card("BLUE", 4)] players[1].cards = [Card("RED", 5)] * 7 game.start() self.assertEqual(len(players[1].cards), 8)
def test_winning_player_score_calculation(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) players[0].cards = [Card("RED", 2), Card("BLUE", action="SKIP")] players[2].cards = [ Card(wild="WILD_DRAW_FOUR"), Card("YELLOW", 8), Card("GREEN", action="REVERSE") ] self.assertEqual(game.get_current_player_score(), 100)
def test_winner_is_printed_when_a_game_ends(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [Card("BLUE", 5)] game.play_turn() self.assertEqual(game.winner, 1)
def test_wild_cards_played_should_have_a_color_associated_with_them(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [ Card("RED", 5), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", action="SKIP"), Card(wild="WILD") ] # Here the wild card is the only valid play game.play_turn() self.assertTrue(game.top_card.is_wild_card) self.assertTrue(game.top_card.color) # Shouldn't be None
def test_player_should_play_draw_four_only_if_no_other_valid_cards(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [ Card("YELLOW", action="REVERSE"), Card("RED", 2), Card("GREEN", action="SKIP"), Card(wild="WILD_DRAW_FOUR"), Card("BLUE", action="DRAW_TWO") ] # Blue Draw two -> value = 20 # Yellow Reverse -> Value = 20 # Wild Draw Four -> value = 50 game.play_turn() # ALtghough Draw four is the card with the highest value but since we # can still play yellow reverse or blue draw two, draw four shouldn't be # played self.assertFalse(game.top_card.is_wild_card) self.assertFalse(game.top_card.is_d4)
def test_player_plays_the_card_with_the_highest_value(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [ Card("YELLOW", action="REVERSE"), Card("RED", 2), Card("GREEN", action="SKIP"), Card(wild="WILD"), Card("BLUE", action="DRAW_TWO") ] # Blue Draw two -> value = 20 # Yellow Reverse -> Value = 20 # Wild Draw Four -> value = 50l game.play_turn() self.assertTrue(game.top_card.is_wild_card) self.assertTrue(game.top_card.wild == "WILD")
def test_on_playing_draw_4_next_player_takes_4_card(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [ Card("RED", 5), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", action="SKIP"), Card(wild="WILD_DRAW_FOUR") ] # Here the draw 4 card is the only valid play game.play_turn() self.assertTrue(game.top_card.is_d4) self.assertEqual(game.last_player_decision, "PLAY") game.current_player = 2 # Need to manually set it here for the tests # Next turn. This player should pick two cards game.play_turn() self.assertEqual(len(players[2].cards), 11) # 7+4
def test_pile_has_one_extra_card_after_players_turn(self): players = [Player(name="DSP", cards=[]), Player(name="SPD", cards=[])] game = Game(players, deck, disable_output=True) game.top_card = Card("BLUE", 4) game.discard_pile = [Card("BLUE", 4)] players[1].cards = [ Card("RED", action="REVERSE"), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", 8), Card("BLUE", 8), Card("GREEN", action="DRAW_TWO"), Card("GREEN", action="DRAW_TWO") ] game.start() # Start the game after setup self.assertEqual(len(game.discard_pile), 2)
def test_reverse_should_flip_direction_and_change_next_player(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [ Card("RED", action="REVERSE"), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", action="SKIP") ] # Here the reverse card is the only valid play game.play_turn() self.assertFalse(game.is_clockwise) self.assertEqual(game.next_player, 0)
def test_top_card_should_be_updated_when_a_player_plays(self): players = [Player(name="DSP", cards=[]), Player(name="SPD", cards=[])] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", 4) game.discard_pile = [Card("BLUE", 4)] players[1].cards = [ Card("RED", action="REVERSE"), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", 8), Card("BLUE", 8), Card("GREEN", action="DRAW_TWO"), Card("GREEN", action="DRAW_TWO") ] game.start() self.assertEqual(game.top_card, game.discard_pile[-1])
def generate_deck(self): colors = ["RED", "BLUE", "GREEN", "YELLOW"] # No Pink Ranger :( numbers = list(range(10)) + list(range(1, 10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Generate all combinations of colors and numbers numbered_cards = [ Card(col, num) for col, num in itertools.product(colors, numbers) ] # Twice because each action is present twice in a color actions = ["SKIP", "DRAW_TWO", "REVERSE"] * 2 action_cards = [ Card(col, action=action) for col, action in itertools.product(colors, actions) ] regular_wild_cards = [Card(wild="WILD")] * 4 wild_draw_4_cards = [Card(wild="WILD_DRAW_FOUR")] * 4 return numbered_cards + action_cards + regular_wild_cards + wild_draw_4_cards
def test_last_player_decision_is_play_if_player_plays(self): players = [Player(name="DSP", cards=[]), Player(name="SPD", cards=[])] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", 4) game.discard_pile = [Card("BLUE", 4)] players[1].cards = [ Card("RED", action="REVERSE"), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", 8), Card("BLUE", 8), Card("GREEN", action="DRAW_TWO"), Card("GREEN", action="DRAW_TWO") ] game.start() self.assertEqual(game.last_player_decision, "PLAY")
def test_winner_name_is_printed(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] captured_out = io.StringIO() sys.stdout = captured_out game = Game(players, deck, disable_output=False) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[1].cards = [Card("BLUE", 5)] game.play_turn() sys.stdout = sys.__stdout__ self.assertTrue("Winner: Sasuke" in captured_out.getvalue()) self.assertTrue("GAME OVER" in captured_out.getvalue())
def test_player_played_a_valid_card(self): players = [Player(name="DSP", cards=[]), Player(name="SPD", cards=[])] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", 4) game.discard_pile = [Card("BLUE", 4)] players[1].cards = [ Card("RED", action="REVERSE"), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", 8), Card("BLUE", 8), Card("GREEN", action="DRAW_TWO"), Card("GREEN", action="DRAW_TWO") ] game.start() # Start the game after setup self.assertTrue( is_played_card_valid(game.discard_pile[1], game.discard_pile[0]))
def test_players_points_after_win(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="REVERSE") game.discard_pile = [Card("BLUE", action="REVERSE")] players[0].cards = [Card("RED", 2), Card("BLUE", action="SKIP")] players[2].cards = [ Card(wild="WILD_DRAW_FOUR"), Card("YELLOW", 8), Card("GREEN", action="REVERSE") ] players[1].cards = [Card("BLUE", 5)] game.play_turn() self.assertEqual(players[1].score, 100)
def test_skip_should_skip_the_next_player(self): players = [ Player(name="Naruto", cards=[]), Player(name="Sasuke", cards=[]), Player(name="Sakura", cards=[]) ] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", action="SKIP") game.discard_pile = [Card("BLUE", action="SKIP")] players[1].cards = [ Card("RED", action="REVERSE"), Card("YELLOW", 4), Card("RED", 2), Card("GREEN", action="SKIP") ] # The player has to play skip here as that is the only valid card game.play_turn() self.assertEqual(game.next_player, 0)
def test_players_total_value_is_sum_of_values_of_his_cards(self): cards = [Card("RED", 2), Card("BLUE", 8), Card("RED", action="SKIP"), Card("YELLOW", action="REVERSE"), Card(wild="WILD"), Card(wild="WILD_DRAW_FOUR")] player = Player(name="DSP", cards=cards) self.assertEqual(player.get_total_value(), 150)
def test_action_card_has_value_20(self): self.assertEqual(Card(color="RED", action="SKIP").value, 20) self.assertEqual(Card(color="BLUE", action="REVERSE").value, 20) self.assertEqual(Card(color="YELLOW", action="DRAW_TWO").value, 20)
def test_wild_card_has_value_50(self): self.assertEqual(Card(wild="WILD").value, 50) self.assertEqual(Card(wild="WILD_DRAW_FOUR").value, 50)
def test_number_cards_with_same_color_and_number_are_equal(self): card1 = Card("BLUE", 5) card2 = Card("BLUE", 5) self.assertEqual(card1, card2)
def test_action_cards_with_same_color_and_action_are_equal(self): card1 = Card("BLUE", action="SKIP") card2 = Card("BLUE", action="SKIP") self.assertEqual(card1, card2)
def test_that_is_skip_is_true_for_skip_cards(self): card1 = Card("BLUE", action="SKIP") card2 = Card("RED", action="SKIP") self.assertTrue(card1.is_skip) self.assertTrue(card2.is_skip)
def test_that_is_d2_is_true_for_draw_two_cards(self): card1 = Card("BLUE", action="DRAW_TWO") card2 = Card("GREEN", action="DRAW_TWO") self.assertTrue(card1.is_d2) self.assertTrue(card2.is_d2)
def test_that_is_rev_on_reverse_card_is_true(self): card1 = Card("BLUE", action="REVERSE") card2 = Card("RED", action="REVERSE") self.assertTrue(card1.is_rev) self.assertTrue(card2.is_rev)
def test_draw_four_card_should_have_is_d4_flag_set(self): card1 = Card(wild="WILD_DRAW_FOUR") self.assertTrue(card1.is_d4)
def test_numbered_card_has_same_value_as_number(self): self.assertEqual(Card("RED", 5).value, 5) self.assertEqual(Card("BLUE", 2).value, 2) self.assertEqual(Card("GREEN", 3).value, 3)
def test_wild_cards_string_format_has_color_in_it(self): card = Card(wild="WILD", color="RED") self.assertEqual(card.__repr__(), "Card(WILD, RED)")
def test_player_decides_to_draw_when_there_Are_no_valid_cards(self): player = Player("S", [Card("RED", 1)] * 7) top_card = Card("BLUE", 4) self.assertEqual(player.decide_action(top_card), "TAKE")
def test_last_player_decision_is_take_if_player_takes(self): players = [Player(name="DSP", cards=[]), Player(name="SPD", cards=[])] game = Game(players, deck, disable_output=True) # Overwriting variables for mocking game.top_card = Card("BLUE", 4) game.discard_pile = [Card("BLUE", 4)] players[1].cards = [Card("RED", 5)] * 7 game.start() self.assertEqual(game.last_player_decision, "TAKE")