1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 04:45:11 2012 +0400
1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 17:58:43 2012 +0400
1.3 @@ -10,6 +10,8 @@
1.4
1.5 import java.util.HashSet;
1.6 import java.util.Set;
1.7 +import java.util.regex.Matcher;
1.8 +import java.util.regex.Pattern;
1.9
1.10 import ru.bosony.model.cellscontents.CellContent;
1.11 import ru.bosony.model.io.TextRepresentable;
1.12 @@ -23,9 +25,13 @@
1.13 */
1.14 public class Mine implements TextRepresentable {
1.15
1.16 - protected Cell[][] cells;
1.17 - protected int sizeX;
1.18 - protected int sizeY;
1.19 + protected Cell[][] cells;
1.20 + protected int sizeX;
1.21 + protected int sizeY;
1.22 + protected int waterLevel = 0;
1.23 + protected int flooding = 0;
1.24 + protected int robotWaterproof = 10;
1.25 + protected static Pattern pattern = Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)");
1.26
1.27 public Mine(String text) {
1.28 fromText(text);
1.29 @@ -46,11 +52,22 @@
1.30 }
1.31 text += "\n";
1.32 }
1.33 - return text.replaceAll("\n*$", "");
1.34 + text = text.replaceAll("\n*$", "");
1.35 + if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
1.36 + text += "\n\n";
1.37 + text += "Water " + waterLevel + "\n";
1.38 + text += "Flooding " + flooding + "\n";
1.39 + text += "Waterproof " + robotWaterproof;
1.40 + }
1.41 + return text;
1.42 }
1.43
1.44 - protected void fromText(String text) {
1.45 - String[] rows = text.split("\n+");
1.46 + protected void fromText(String str) {
1.47 + Matcher matcher = pattern.matcher(str);
1.48 + if (!matcher.matches())
1.49 + throw new RuntimeException("Can't parse mine from string");
1.50 + String body = matcher.group(1);
1.51 + String[] rows = body.split("\n+");
1.52 int y = rows.length;
1.53 int x = 0;
1.54 for (String row : rows) {
1.55 @@ -74,6 +91,16 @@
1.56 cells[curX][curY] = cell;
1.57 }
1.58 }
1.59 +
1.60 + String sWaterLevel = matcher.group(4);
1.61 + String sFlooding = matcher.group(6);
1.62 + String sRobotWaterproof = matcher.group(8);
1.63 + if (sWaterLevel.length() > 0)
1.64 + waterLevel = Integer.parseInt(sWaterLevel);
1.65 + if (sFlooding.length() > 0)
1.66 + flooding = Integer.parseInt(sFlooding);
1.67 + if (sRobotWaterproof.length() > 0)
1.68 + robotWaterproof = Integer.parseInt(sRobotWaterproof);
1.69 }
1.70
1.71 public Cell getCell(Coordinate coordinate) {
1.72 @@ -168,4 +195,20 @@
1.73 return clone;
1.74 }
1.75
1.76 + public int getWaterLevel() {
1.77 + return waterLevel;
1.78 + }
1.79 +
1.80 + public void setWaterLevel(int waterLevel) {
1.81 + this.waterLevel = waterLevel;
1.82 + }
1.83 +
1.84 + public int getFlooding() {
1.85 + return flooding;
1.86 + }
1.87 +
1.88 + public int getRobotWaterproof() {
1.89 + return robotWaterproof;
1.90 + }
1.91 +
1.92 }