1 package ru.bosony.model.mine;
3 import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
4 import static ru.bosony.model.cellscontents.CellContent.Earth;
5 import static ru.bosony.model.cellscontents.CellContent.Empty;
6 import static ru.bosony.model.cellscontents.CellContent.Lambda;
7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
9 import static ru.bosony.model.cellscontents.CellContent.Rock;
11 import java.util.Collection;
12 import java.util.HashSet;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import ru.bosony.model.cellscontents.CellContent;
18 import ru.bosony.model.io.TextRepresentable;
19 import ru.bosony.model.moving.Coordinate;
20 import ru.bosony.model.moving.Movement;
23 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
27 public class Mine implements TextRepresentable, Cloneable {
29 protected Cell[][] cells;
32 protected int waterLevel = 0;
33 protected int flooding = 0;
34 protected int robotWaterproof = 10;
35 protected int growth = 25;
36 protected int razorsCount = 0;
37 protected int lambdasAndHighOrderRocksStartCount = 0;
38 protected String metadata = "";
39 protected static Pattern minePattern = Pattern
40 .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
41 protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
42 protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
43 protected static Pattern waterproofPattern = Pattern
44 .compile("(?s).*(?<=.*Waterproof )(\\d+).*");
45 protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
46 protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
47 protected static Pattern trampolinePattern = Pattern
48 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
49 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
51 public Mine(String text) {
53 if (findCells(OpenLambdaLift).size() != 0)
54 throw new RuntimeException("Detected OpenLambdaLift in new map!");
55 if (findCells(MiningRobot).size() != 1)
56 throw new RuntimeException("New map does not contain a robot!");
57 if (findCells(ClosedLambdaLift).size() != 1)
58 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
61 protected Mine(String text, Object obj) {
63 lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
67 public String toText() {
69 for (int y = 0; y < sizeY; y++) {
70 for (int x = 0; x < sizeX; x++) {
71 text += cells[x][y].getContent().toText();
75 text = text.replaceAll("\n*$", "");
78 text = text.replaceAll("\n*$", "");
82 public String toTextWithoutRobot() {
83 return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText());
86 protected void fromText(String str) {
87 Matcher matcher = minePattern.matcher(str);
88 if (!matcher.matches())
89 throw new RuntimeException("Can't parse mine from string");
90 String body = matcher.group(1);
91 String[] rows = body.split("\n+");
94 for (String row : rows) {
100 cells = new Cell[x][y];
101 for (int curY = 0; curY < rows.length; curY++) {
102 String row = rows[curY];
103 for (int curX = 0; curX < row.length() || curX < x; curX++) {
104 CellContent content = null;
105 if (curX < row.length())
106 content = CellContent.fromText("" + row.charAt(curX));
111 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
112 cells[curX][curY] = cell;
116 metadata = matcher.group(3);
117 if (metadata != null) {
119 String sWaterLevel = "";
120 String sFlooding = "";
121 String sRobotWaterproof = "";
122 Matcher waterMatcher = waterPattern.matcher(metadata);
123 if (waterMatcher.matches()) {
124 sWaterLevel = waterMatcher.group(1);
126 Matcher floodingMatcher = floodingPattern.matcher(metadata);
127 if (floodingMatcher.matches()) {
128 sFlooding = floodingMatcher.group(1);
130 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
131 if (waterproofMatcher.matches()) {
132 sRobotWaterproof = waterproofMatcher.group(1);
134 if (sWaterLevel.length() > 0)
135 waterLevel = Integer.parseInt(sWaterLevel);
136 if (sFlooding.length() > 0)
137 flooding = Integer.parseInt(sFlooding);
138 if (sRobotWaterproof.length() > 0)
139 robotWaterproof = Integer.parseInt(sRobotWaterproof);
141 // Trampolines and targets
142 String trampolines = metadata;
144 Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
145 if (trampolineMatcher.matches()) {
146 String sTrampoline = trampolineMatcher.group(1);
147 String sTarget = trampolineMatcher.group(2);
148 CellContent trampoline = CellContent.fromText(sTrampoline);
149 CellContent target = CellContent.fromText(sTarget);
150 if (trampoline != null && target != null)
151 trampoline.setTrampolineTarget(target);
154 trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
157 // Growth and razorsCount
160 Matcher growthMatcher = growthPattern.matcher(metadata);
161 if (growthMatcher.matches()) {
162 sGrowth = growthMatcher.group(1);
164 Matcher razorsMatcher = razorsPattern.matcher(metadata);
165 if (razorsMatcher.matches()) {
166 sRazors = razorsMatcher.group(1);
168 if (sGrowth.length() > 0)
169 growth = Integer.parseInt(sGrowth);
170 if (sRazors.length() > 0)
171 razorsCount = Integer.parseInt(sRazors);
176 public Cell getCell(Coordinate coordinate) {
177 if (coordinate == null)
179 int x = coordinate.x;
180 int y = coordinate.y;
181 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
183 return cells[x - 1][sizeY - y];
186 public Set<Cell> getCells(Collection<Coordinate> coordinates) {
187 Set<Cell> cells = new HashSet<Cell>();
188 if (coordinates == null)
190 for (Coordinate coordinate : coordinates) {
191 Cell cell = getCell(coordinate);
199 public String toString() {
203 public int getSizeX() {
207 public int getSizeY() {
211 public Set<Movement> getAvailableRobotMovements() {
212 Set<Movement> movs = new HashSet<Movement>();
213 Cell robotCell = getRobotCell();
214 Set<Cell> nCells = getNeighboringCells(robotCell);
215 for (Cell nCell : nCells) {
216 CellContent nContent = nCell.getContent();
217 Coordinate nCoordinate = nCell.getCoordinate();
218 Cell rightNCell = getCell(nCoordinate.right());
219 Cell leftNCell = getCell(nCoordinate.left());
220 if (nContent == Empty
222 || nContent == Lambda
223 || nContent == OpenLambdaLift
224 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
225 .getContent() == Empty)
226 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
227 .getContent() == Empty)) {
228 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
232 movs.add(Movement.ABORT);
233 movs.add(Movement.WAIT);
237 public Set<Cell> getNeighboringCells(Cell cell) {
238 Set<Cell> cells = new HashSet<Cell>();
239 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) {
240 Cell nextCell = getCell(coordinate);
241 if (nextCell != null)
247 public Set<Cell> getAdjacentCells(Cell cell) {
248 Set<Cell> cells = new HashSet<Cell>();
249 for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) {
250 Cell nextCell = getCell(coordinate);
251 if (nextCell != null)
257 public Set<Cell> findCells(CellContent content) {
258 Set<Cell> finded = new HashSet<Cell>();
259 for (int y = sizeY - 1; y >= 0; y--) {
260 for (int x = 0; x < sizeX; x++) {
261 if (cells[x][y].getContent() == content)
262 finded.add(cells[x][y]);
268 public void setCells(Cell[][] cells) {
272 public Cell getRobotCell() {
273 for (Cell cell : findCells(MiningRobot)) {
276 throw new RuntimeException("Where is robot!?");
279 public Cell[][] cloneCells() {
280 Cell[][] clone = new Cell[sizeX][sizeY];
281 for (int y = sizeY - 1; y >= 0; y--) {
282 for (int x = 0; x < sizeX; x++) {
284 clone[x][y] = cells[x][y].clone();
285 } catch (CloneNotSupportedException e) {
286 throw new RuntimeException(e);
293 public int getWaterLevel() {
297 public void setWaterLevel(int waterLevel) {
298 this.waterLevel = waterLevel;
301 public int getFlooding() {
305 public int getRobotWaterproof() {
306 return robotWaterproof;
309 public int getGrowth() {
313 public int getRazorsCount() {
317 public void setRazorsCount(int razorsCount) {
318 this.razorsCount = razorsCount;
321 public int getLambdasAndHighOrderRocksStartCount() {
322 return lambdasAndHighOrderRocksStartCount;
326 public boolean equals(Object obj) {
327 if (obj instanceof Mine) {
328 Mine otherMine = (Mine) obj;
329 return toText().equals(otherMine.toText());
331 return super.equals(obj);
335 public int hashCode() {
336 return toText().hashCode();
340 public Mine clone() {
341 return new Mine(toText(), null);