Skip to content

Commit

Permalink
Discarding Test (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcollazo committed Aug 27, 2022
1 parent 0f08ff2 commit 5c9e15d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions catanatron_core/catanatron/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ def apply_action(state: State, action: Action):
player_num_resource_cards(state, color) > state.discard_limit
for color in state.colors
]
is_discarding = any(discarders)
should_enter_discarding_sequence = any(discarders)

if is_discarding:
if should_enter_discarding_sequence:
state.current_player_index = discarders.index(True)
state.current_prompt = ActionPrompt.DISCARD
state.is_discarding = True
Expand Down
69 changes: 69 additions & 0 deletions tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,75 @@ def test_rolling_a_seven_triggers_default_discard_limit(fake_roll_dice):
assert player_num_resource_cards(game.state, players[1].color) == 5


@patch("catanatron.state.roll_dice")
def test_all_players_discard_as_needed(fake_roll_dice):
"""Tests irrespective of who rolls the 7, all players discard"""
players = [
SimplePlayer(Color.RED),
SimplePlayer(Color.BLUE),
SimplePlayer(Color.WHITE),
SimplePlayer(Color.ORANGE),
]
game = Game(players)
while not any(
a.action_type == ActionType.ROLL for a in game.state.playable_actions
):
game.play_tick()

ordered_players = game.state.players
fake_roll_dice.return_value = (3, 3)
game.play_tick() # should be p0 rolling a 6
game.play_tick() # should be p0 ending turn

# fill everyones hand
until_nine = 9 - player_num_resource_cards(game.state, players[0].color)
player_deck_replenish(game.state, players[0].color, WHEAT, until_nine)
until_nine = 9 - player_num_resource_cards(game.state, players[1].color)
player_deck_replenish(game.state, players[1].color, WHEAT, until_nine)
until_nine = 9 - player_num_resource_cards(game.state, players[2].color)
player_deck_replenish(game.state, players[2].color, WHEAT, until_nine)
until_nine = 9 - player_num_resource_cards(game.state, players[3].color)
player_deck_replenish(game.state, players[3].color, WHEAT, until_nine)
fake_roll_dice.return_value = (1, 6)
game.play_tick() # should be p1 rolling a 7

# the following assumes, no matter who rolled 7, asking players
# to discard, happens in original seating-order.
assert len(game.state.playable_actions) == 1
assert game.state.playable_actions == [
Action(ordered_players[0].color, ActionType.DISCARD, None)
]

game.play_tick() # p0 discards, places p1 in line to discard
assert player_num_resource_cards(game.state, ordered_players[0].color) == 5
assert len(game.state.playable_actions) == 1
assert game.state.playable_actions == [
Action(ordered_players[1].color, ActionType.DISCARD, None)
]

game.play_tick()
assert player_num_resource_cards(game.state, ordered_players[1].color) == 5
assert len(game.state.playable_actions) == 1
assert game.state.playable_actions == [
Action(ordered_players[2].color, ActionType.DISCARD, None)
]

game.play_tick()
assert player_num_resource_cards(game.state, ordered_players[2].color) == 5
assert len(game.state.playable_actions) == 1
assert game.state.playable_actions == [
Action(ordered_players[3].color, ActionType.DISCARD, None)
]

game.play_tick() # p3 discards, game goes back to p1 moving robber
assert player_num_resource_cards(game.state, ordered_players[3].color) == 5
assert game.state.is_moving_knight
assert all(a.color == ordered_players[1].color for a in game.state.playable_actions)
assert all(
a.action_type == ActionType.MOVE_ROBBER for a in game.state.playable_actions
)


@patch("catanatron.state.roll_dice")
def test_discard_is_configurable(fake_roll_dice):
fake_roll_dice.return_value = (1, 6)
Expand Down

0 comments on commit 5c9e15d

Please sign in to comment.