Skip to content

Commit

Permalink
Update to force coup if more than 10 coins
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkbrnd committed Dec 15, 2023
1 parent 819b801 commit ae0422b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions coup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


def main():
# Create game handler with 3 players
handler = ResistanceCoupGameHandler(3)
# Create game handler with 5 players
handler = ResistanceCoupGameHandler(5)
print(f"First player is {handler.current_player}")

# Create AI players
Expand Down
29 changes: 26 additions & 3 deletions src/handler/game_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,42 @@ def _deactivate_player(self) -> Optional[Player]:
def _determine_win_state(self) -> bool:
return sum(player.is_active for player in self._players.values()) == 1

<<<<<<< Updated upstream
def validate_action(self, action: Action, current_player: Player, target_player: Optional[Player]) -> bool:
if action.action_type in [ActionType.coup, ActionType.steal, ActionType.assassinate] and not target_player:
return False

# Can't take coin if the treasury has none
=======
def _validate_action(
self, action: Action, current_player: Player, target_player: Optional[Player]
):
if current_player.coins >= 10 and action.action_type != ActionType.coup:
raise Exception(f"Invalid action: You have more than 10 coins and have to perform {ActionType.coup} action.")

if (
action.action_type in [ActionType.coup, ActionType.steal, ActionType.assassinate] and not target_player
):
raise Exception(f"Invalid action: You need a `target_player` for the action {action.action_type}")

# Can't take coin if the treasury has none
if (
action.action_type in [ActionType.income, ActionType.foreign_aid, ActionType.tax] and self._treasury == 0
):
raise Exception(f"Invalid action: The treasury has no coin to give")
>>>>>>> Stashed changes

# You can only do a coup if you have at least 7 coins.
if action.action_type == ActionType.coup and current_player.coins < 7:
return False
raise Exception(f"Invalid action: You need more coins to be able to perform the {ActionType.coup} action.")

# You can only do an assassination if you have at least 3 coins.
if action.action_type == ActionType.assassinate and current_player.coins < 3:
return False
raise Exception(f"Invalid action: You need more coins to be able to perform the {ActionType.assassinate} action.")

# Can't steal from player with 0 coins
if action.action_type == ActionType.steal and target_player.coins == 0:
return False
raise Exception(f"Invalid action: You cannot steal from a player with no coins.")

return True

Expand All @@ -198,8 +217,12 @@ def perform_action(self, player_name: str, action_name: ActionType, target_playe
if player_name != self.current_player.name:
raise Exception(f"Wrong player, it is currently {self.current_player.name}'s turn.")

<<<<<<< Updated upstream
if not self.validate_action(action, self.current_player, target_player):
raise Exception("Invalid action")
=======
self._validate_action(action, self.current_player, target_player)
>>>>>>> Stashed changes

result_action_str = ""

Expand Down

0 comments on commit ae0422b

Please sign in to comment.