From b8e9b9b30704a0ff6a9983e9452503f6457c33d0 Mon Sep 17 00:00:00 2001 From: Mason Zarns Date: Tue, 5 Nov 2024 12:43:29 -0600 Subject: [PATCH] Allow playing dev cards before rolling. Remove duplicate logic --- catanatron_core/catanatron/models/actions.py | 44 +++++++------------- tests/test_game.py | 5 +-- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/catanatron_core/catanatron/models/actions.py b/catanatron_core/catanatron/models/actions.py index 7574fc41..1657ccaa 100644 --- a/catanatron_core/catanatron/models/actions.py +++ b/catanatron_core/catanatron/models/actions.py @@ -51,23 +51,26 @@ def generate_playable_actions(state) -> List[Action]: elif action_prompt == ActionPrompt.MOVE_ROBBER: return robber_possibilities(state, color) elif action_prompt == ActionPrompt.PLAY_TURN: + actions = [] + # Allow playing dev cards before and after rolling + if player_can_play_dev(state, color, "YEAR_OF_PLENTY"): + actions.extend(year_of_plenty_possibilities(color, state.resource_freqdeck)) + if player_can_play_dev(state, color, "MONOPOLY"): + actions.extend(monopoly_possibilities(color)) + if player_can_play_dev(state, color, "KNIGHT"): + actions.append(Action(color, ActionType.PLAY_KNIGHT_CARD, None)) + if ( + player_can_play_dev(state, color, "ROAD_BUILDING") + and len(road_building_possibilities(state, color, False)) > 0 + ): + actions.append(Action(color, ActionType.PLAY_ROAD_BUILDING, None)) + if state.is_road_building: actions = road_building_possibilities(state, color, False) elif not player_has_rolled(state, color): - actions = [Action(color, ActionType.ROLL, None)] - # Allow playing any dev card before rolling - if player_can_play_dev(state, color, "KNIGHT"): - actions.append(Action(color, ActionType.PLAY_KNIGHT_CARD, None)) - if player_can_play_dev(state, color, "YEAR_OF_PLENTY"): - actions.extend( - year_of_plenty_possibilities(color, state.resource_freqdeck) - ) - if player_can_play_dev(state, color, "MONOPOLY"): - actions.extend(monopoly_possibilities(color)) - if player_can_play_dev(state, color, "ROAD_BUILDING"): - actions.append(Action(color, ActionType.PLAY_ROAD_BUILDING, None)) + actions.append(Action(color, ActionType.ROLL, None)) else: - actions = [Action(color, ActionType.END_TURN, None)] + actions.append(Action(color, ActionType.END_TURN, None)) actions.extend(road_building_possibilities(state, color)) actions.extend(settlement_possibilities(state, color)) actions.extend(city_possibilities(state, color)) @@ -79,21 +82,6 @@ def generate_playable_actions(state) -> List[Action]: if can_buy_dev_card: actions.append(Action(color, ActionType.BUY_DEVELOPMENT_CARD, None)) - # Play Dev Cards - if player_can_play_dev(state, color, "YEAR_OF_PLENTY"): - actions.extend( - year_of_plenty_possibilities(color, state.resource_freqdeck) - ) - if player_can_play_dev(state, color, "MONOPOLY"): - actions.extend(monopoly_possibilities(color)) - if player_can_play_dev(state, color, "KNIGHT"): - actions.append(Action(color, ActionType.PLAY_KNIGHT_CARD, None)) - if ( - player_can_play_dev(state, color, "ROAD_BUILDING") - and len(road_building_possibilities(state, color, False)) > 0 - ): - actions.append(Action(color, ActionType.PLAY_ROAD_BUILDING, None)) - # Trade actions.extend(maritime_trade_possibilities(state, color)) return actions diff --git a/tests/test_game.py b/tests/test_game.py index c00d7561..80b4f503 100644 --- a/tests/test_game.py +++ b/tests/test_game.py @@ -327,10 +327,7 @@ def test_play_road_building(fake_roll_dice): ): game.play_tick() - # roll not a 7 - fake_roll_dice.return_value = (1, 2) - game.play_tick() # roll - + # Play Road Building before rolling game.execute(Action(p0.color, ActionType.PLAY_ROAD_BUILDING, None)) assert game.state.is_road_building assert game.state.free_roads_available == 2