diff -r d357a16ee62d -r 80f5fc954cff icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Mon Jul 16 13:19:10 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Wed Jul 18 09:42:52 2012 +0400 @@ -8,6 +8,7 @@ import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; import static ru.bosony.model.cellscontents.CellContent.Rock; +import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; @@ -23,7 +24,7 @@ * 14.07.2012 2:02:18 * */ -public class Mine implements TextRepresentable { +public class Mine implements TextRepresentable, Cloneable { protected Cell[][] cells; protected int sizeX; @@ -48,13 +49,17 @@ protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])"; public Mine(String text) { - fromText(text); + this(text, null); if (findCells(OpenLambdaLift).size() != 0) throw new RuntimeException("Detected OpenLambdaLift in new map!"); if (findCells(MiningRobot).size() != 1) throw new RuntimeException("New map does not contain a robot!"); if (findCells(ClosedLambdaLift).size() != 1) throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); + } + + protected Mine(String text, Object obj) { + fromText(text); lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size(); } @@ -74,6 +79,10 @@ return text; } + public String toTextWithoutRobot() { + return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText()); + } + protected void fromText(String str) { Matcher matcher = minePattern.matcher(str); if (!matcher.matches()) @@ -174,6 +183,18 @@ return cells[x - 1][sizeY - y]; } + public Set getCells(Collection coordinates) { + Set cells = new HashSet(); + if (coordinates == null) + return cells; + for (Coordinate coordinate : coordinates) { + Cell cell = getCell(coordinate); + if (cell != null) + cells.add(cell); + } + return cells; + } + @Override public String toString() { return toText(); @@ -215,15 +236,21 @@ public Set getNeighboringCells(Cell cell) { Set cells = new HashSet(); - for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) - cells.add(getCell(coordinate)); + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) { + Cell nextCell = getCell(coordinate); + if (nextCell != null) + cells.add(nextCell); + } return cells; } public Set getAdjacentCells(Cell cell) { Set cells = new HashSet(); - for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) - cells.add(getCell(coordinate)); + for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) { + Cell nextCell = getCell(coordinate); + if (nextCell != null) + cells.add(nextCell); + } return cells; } @@ -309,4 +336,9 @@ return toText().hashCode(); } + @Override + public Mine clone() { + return new Mine(toText(), null); + } + }