diff --git a/loveletter/game.py b/loveletter/game.py index e318805..838909e 100644 --- a/loveletter/game.py +++ b/loveletter/game.py @@ -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]] @@ -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 @@ -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):