# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342360723 -14400 # Node ID d6a75f29ba9b8aafe395b5ffb1a6175d13bba209 # Parent 46c2179854dcea1c74fd1ffad6772924787c6ac9 Add water flooding diff -r 46c2179854dc -r d6a75f29ba9b icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java --- a/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java Sun Jul 15 04:45:11 2012 +0400 +++ b/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java Sun Jul 15 17:58:43 2012 +0400 @@ -46,14 +46,16 @@ writer.newLine(); writer.write("********************************************************************************"); writer.newLine(); - writer.write("State: " + game.getState()); + writer.write("State: " + game.getState()); writer.newLine(); - writer.write("Score: " + game.getScore() + " (" + writer.write("Score: " + game.getScore() + " (" + (game.getLastScoreChange() >= 0 ? "+" + game.getLastScoreChange() : game.getLastScoreChange()) + ")"); writer.newLine(); - writer.write("Steps: " + game.getRoute().size()); + writer.write("Steps: " + game.getRoute().size()); writer.newLine(); - writer.write("Route: " + game.getStringRoute()); + writer.write("Route: " + game.getStringRoute()); + writer.newLine(); + writer.write("Underwater: " + game.getUnderwater() + " of " + mine.getRobotWaterproof()); writer.newLine(); writer.newLine(); writer.write(mine.toText()); @@ -67,7 +69,7 @@ movs = movs.replaceAll(",\\s*$", ""); writer.write(movs); writer.newLine(); - writer.write("Print action(s): "); + writer.write("Enter action(s): "); writer.flush(); } diff -r 46c2179854dc -r d6a75f29ba9b icfp-model/src/main/java/ru/bosony/model/game/Game.java --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sun Jul 15 04:45:11 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sun Jul 15 17:58:43 2012 +0400 @@ -30,6 +30,7 @@ protected GameState state = GameState.Game; protected int lambdaCollectedCount = 0; protected List route = new ArrayList(); + protected int underwater = 0; public Game(Mine mine) { this.mine = mine; @@ -166,6 +167,7 @@ robotCoord = mine.getRobotCell().getCoordinate(); if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) { mine.setCells(newCells); + lastScoreChange = score - prevScore; state = GameState.Lose; return state; @@ -173,6 +175,22 @@ mine.setCells(newCells); + // Water flooding + robotCoord = mine.getRobotCell().getCoordinate(); + if (robotCoord.y <= mine.getWaterLevel()) { + underwater++; + } else { + underwater = 0; + } + if (underwater > mine.getRobotWaterproof()) { + lastScoreChange = score - prevScore; + state = GameState.Lose; + return state; + } + if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) { + mine.setWaterLevel(mine.getWaterLevel() + 1); + } + lastScoreChange = score - prevScore; state = GameState.Game; return state; @@ -233,4 +251,8 @@ } return movs; } + + public int getUnderwater() { + return underwater; + } } diff -r 46c2179854dc -r d6a75f29ba9b icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 04:45:11 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 17:58:43 2012 +0400 @@ -10,6 +10,8 @@ import java.util.HashSet; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.io.TextRepresentable; @@ -23,9 +25,13 @@ */ public class Mine implements TextRepresentable { - protected Cell[][] cells; - protected int sizeX; - protected int sizeY; + protected Cell[][] cells; + protected int sizeX; + protected int sizeY; + protected int waterLevel = 0; + protected int flooding = 0; + protected int robotWaterproof = 10; + protected static Pattern pattern = Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)"); public Mine(String text) { fromText(text); @@ -46,11 +52,22 @@ } text += "\n"; } - return text.replaceAll("\n*$", ""); + text = text.replaceAll("\n*$", ""); + if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) { + text += "\n\n"; + text += "Water " + waterLevel + "\n"; + text += "Flooding " + flooding + "\n"; + text += "Waterproof " + robotWaterproof; + } + return text; } - protected void fromText(String text) { - String[] rows = text.split("\n+"); + protected void fromText(String str) { + Matcher matcher = pattern.matcher(str); + if (!matcher.matches()) + throw new RuntimeException("Can't parse mine from string"); + String body = matcher.group(1); + String[] rows = body.split("\n+"); int y = rows.length; int x = 0; for (String row : rows) { @@ -74,6 +91,16 @@ cells[curX][curY] = cell; } } + + String sWaterLevel = matcher.group(4); + String sFlooding = matcher.group(6); + String sRobotWaterproof = matcher.group(8); + if (sWaterLevel.length() > 0) + waterLevel = Integer.parseInt(sWaterLevel); + if (sFlooding.length() > 0) + flooding = Integer.parseInt(sFlooding); + if (sRobotWaterproof.length() > 0) + robotWaterproof = Integer.parseInt(sRobotWaterproof); } public Cell getCell(Coordinate coordinate) { @@ -168,4 +195,20 @@ return clone; } + public int getWaterLevel() { + return waterLevel; + } + + public void setWaterLevel(int waterLevel) { + this.waterLevel = waterLevel; + } + + public int getFlooding() { + return flooding; + } + + public int getRobotWaterproof() { + return robotWaterproof; + } + }