Skip to content

Commit

Permalink
Validate input for advanced move
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-delmas committed Jun 19, 2022
1 parent 9a43437 commit 08c9ef7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/codingame/game/CommandManager.java
Expand Up @@ -129,7 +129,16 @@ public void parseCommand(Player player, String command, Game game, GamePhase gam
int dailyRoutinesCount = player.getPermanentDailyRoutineCardsCount();
if (match.matches() && gamePhase == GamePhase.MOVE && dailyRoutinesCount>0) {
int zoneToMoveId = Integer.parseInt(match.group("zoneToMoveId"));
if (zoneToMoveId < 0 || zoneToMoveId>=Config.ZONES_COUNT) {
throw new GameRuleException(command, "you can only move to a zone between 0 and 7");
}
if (zoneToMoveId == player.getZoneId()) {
throw new GameRuleException(command, String.format("you must move to another desk (you are already in desk %d", zoneToMoveId));
}
int zoneToTakeCardId = Integer.parseInt(match.group("zoneToTakeCardId"));
if (zoneToTakeCardId < 0 || zoneToTakeCardId>=Config.ZONES_COUNT) {
throw new GameRuleException(command, "you can only take a card from a zone between 0 and 7");
}
int distance = abs(zoneToMoveId - zoneToTakeCardId);
if (distance > dailyRoutinesCount) {
distance = abs(Config.ZONES_COUNT - distance);
Expand All @@ -144,7 +153,7 @@ public void parseCommand(Player player, String command, Game game, GamePhase gam
match = PLAYER_MOVE_PATTERN.matcher(command);
if (match.matches() && gamePhase == GamePhase.MOVE) {
int zoneId = Integer.parseInt(match.group("zoneId"));
if (zoneId < 0 ||zoneId>=Config.ZONES_COUNT) {
if (zoneId < 0 || zoneId>=Config.ZONES_COUNT) {
throw new GameRuleException(command, "you can only move to a zone between 0 and 7");
}
if (zoneId == player.getZoneId()) {
Expand Down

0 comments on commit 08c9ef7

Please sign in to comment.