Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions loveletter/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ def is_action_valid(self, action):
if player.hand_card == Card.noCard:
return PlayerActionTools.is_blank(action)

# cannot target an invalid player
if not self._is_valid_player_target(action.player_target):
return False

target_player = self._players[action.player_target]
player_hand = [player.hand_card, self._deck[0]]

Expand All @@ -282,10 +286,6 @@ def is_action_valid(self, action):
(action.discard == Card.prince or action.discard == Card.king):
return False

# cannot target an invalid player
if not self._is_valid_player_target(action.player_target):
return False

# cannot mis-target a card
if self.player_turn() == action.player_target and action.discard in Card.only_other:
return False
Expand All @@ -304,6 +304,9 @@ def is_action_valid(self, action):

def _is_valid_player_target(self, player_target):
"""True iff the player can be targeted by an action"""
if player_target < 0 or player_target >= len(self._players):
return False

return PlayerTools.is_playing(self._players[player_target])

def _invalid_input(self, throw):
Expand Down