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.HashSet;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
16 import ru.bosony.model.cellscontents.CellContent;
17 import ru.bosony.model.io.TextRepresentable;
18 import ru.bosony.model.moving.Coordinate;
19 import ru.bosony.model.moving.Movement;
22 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
26 public class Mine implements TextRepresentable, Cloneable {
28 protected Cell[][] cells;
31 protected int waterLevel = 0;
32 protected int flooding = 0;
33 protected int robotWaterproof = 10;
34 protected int growth = 25;
35 protected int razorsCount = 0;
36 protected int lambdasAndHighOrderRocksStartCount = 0;
37 protected String metadata = "";
38 protected static Pattern minePattern = Pattern
39 .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
40 protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
41 protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
42 protected static Pattern waterproofPattern = Pattern
43 .compile("(?s).*(?<=.*Waterproof )(\\d+).*");
44 protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
45 protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
46 protected static Pattern trampolinePattern = Pattern
47 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
48 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
50 public Mine(String text) {
52 if (findCells(OpenLambdaLift).size() != 0)
53 throw new RuntimeException("Detected OpenLambdaLift in new map!");
54 if (findCells(MiningRobot).size() != 1)
55 throw new RuntimeException("New map does not contain a robot!");
56 if (findCells(ClosedLambdaLift).size() != 1)
57 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
60 protected Mine(String text, Object obj) {
62 lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
66 public String toText() {
68 for (int y = 0; y < sizeY; y++) {
69 for (int x = 0; x < sizeX; x++) {
70 text += cells[x][y].getContent().toText();
74 text = text.replaceAll("\n*$", "");
77 text = text.replaceAll("\n*$", "");
81 protected void fromText(String str) {
82 Matcher matcher = minePattern.matcher(str);
83 if (!matcher.matches())
84 throw new RuntimeException("Can't parse mine from string");
85 String body = matcher.group(1);
86 String[] rows = body.split("\n+");
89 for (String row : rows) {
95 cells = new Cell[x][y];
96 for (int curY = 0; curY < rows.length; curY++) {
97 String row = rows[curY];
98 for (int curX = 0; curX < row.length() || curX < x; curX++) {
99 CellContent content = null;
100 if (curX < row.length())
101 content = CellContent.fromText("" + row.charAt(curX));
106 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
107 cells[curX][curY] = cell;
111 metadata = matcher.group(3);
112 if (metadata != null) {
114 String sWaterLevel = "";
115 String sFlooding = "";
116 String sRobotWaterproof = "";
117 Matcher waterMatcher = waterPattern.matcher(metadata);
118 if (waterMatcher.matches()) {
119 sWaterLevel = waterMatcher.group(1);
121 Matcher floodingMatcher = floodingPattern.matcher(metadata);
122 if (floodingMatcher.matches()) {
123 sFlooding = floodingMatcher.group(1);
125 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
126 if (waterproofMatcher.matches()) {
127 sRobotWaterproof = waterproofMatcher.group(1);
129 if (sWaterLevel.length() > 0)
130 waterLevel = Integer.parseInt(sWaterLevel);
131 if (sFlooding.length() > 0)
132 flooding = Integer.parseInt(sFlooding);
133 if (sRobotWaterproof.length() > 0)
134 robotWaterproof = Integer.parseInt(sRobotWaterproof);
136 // Trampolines and targets
137 String trampolines = metadata;
139 Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
140 if (trampolineMatcher.matches()) {
141 String sTrampoline = trampolineMatcher.group(1);
142 String sTarget = trampolineMatcher.group(2);
143 CellContent trampoline = CellContent.fromText(sTrampoline);
144 CellContent target = CellContent.fromText(sTarget);
145 if (trampoline != null && target != null)
146 trampoline.setTrampolineTarget(target);
149 trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
152 // Growth and razorsCount
155 Matcher growthMatcher = growthPattern.matcher(metadata);
156 if (growthMatcher.matches()) {
157 sGrowth = growthMatcher.group(1);
159 Matcher razorsMatcher = razorsPattern.matcher(metadata);
160 if (razorsMatcher.matches()) {
161 sRazors = razorsMatcher.group(1);
163 if (sGrowth.length() > 0)
164 growth = Integer.parseInt(sGrowth);
165 if (sRazors.length() > 0)
166 razorsCount = Integer.parseInt(sRazors);
171 public Cell getCell(Coordinate coordinate) {
172 if (coordinate == null)
174 int x = coordinate.x;
175 int y = coordinate.y;
176 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
178 return cells[x - 1][sizeY - y];
182 public String toString() {
186 public int getSizeX() {
190 public int getSizeY() {
194 public Set<Movement> getAvailableRobotMovements() {
195 Set<Movement> movs = new HashSet<Movement>();
196 Cell robotCell = getRobotCell();
197 Set<Cell> nCells = getNeighboringCells(robotCell);
198 for (Cell nCell : nCells) {
199 CellContent nContent = nCell.getContent();
200 Coordinate nCoordinate = nCell.getCoordinate();
201 Cell rightNCell = getCell(nCoordinate.right());
202 Cell leftNCell = getCell(nCoordinate.left());
203 if (nContent == Empty
205 || nContent == Lambda
206 || nContent == OpenLambdaLift
207 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
208 .getContent() == Empty)
209 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
210 .getContent() == Empty)) {
211 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
215 movs.add(Movement.ABORT);
216 movs.add(Movement.WAIT);
220 public Set<Cell> getNeighboringCells(Cell cell) {
221 Set<Cell> cells = new HashSet<Cell>();
222 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
223 cells.add(getCell(coordinate));
227 public Set<Cell> getAdjacentCells(Cell cell) {
228 Set<Cell> cells = new HashSet<Cell>();
229 for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates())
230 cells.add(getCell(coordinate));
234 public Set<Cell> findCells(CellContent content) {
235 Set<Cell> finded = new HashSet<Cell>();
236 for (int y = sizeY - 1; y >= 0; y--) {
237 for (int x = 0; x < sizeX; x++) {
238 if (cells[x][y].getContent() == content)
239 finded.add(cells[x][y]);
245 public void setCells(Cell[][] cells) {
249 public Cell getRobotCell() {
250 for (Cell cell : findCells(MiningRobot)) {
253 throw new RuntimeException("Where is robot!?");
256 public Cell[][] cloneCells() {
257 Cell[][] clone = new Cell[sizeX][sizeY];
258 for (int y = sizeY - 1; y >= 0; y--) {
259 for (int x = 0; x < sizeX; x++) {
261 clone[x][y] = cells[x][y].clone();
262 } catch (CloneNotSupportedException e) {
263 throw new RuntimeException(e);
270 public int getWaterLevel() {
274 public void setWaterLevel(int waterLevel) {
275 this.waterLevel = waterLevel;
278 public int getFlooding() {
282 public int getRobotWaterproof() {
283 return robotWaterproof;
286 public int getGrowth() {
290 public int getRazorsCount() {
294 public void setRazorsCount(int razorsCount) {
295 this.razorsCount = razorsCount;
298 public int getLambdasAndHighOrderRocksStartCount() {
299 return lambdasAndHighOrderRocksStartCount;
303 public boolean equals(Object obj) {
304 if (obj instanceof Mine) {
305 Mine otherMine = (Mine) obj;
306 return toText().equals(otherMine.toText());
308 return super.equals(obj);
312 public int hashCode() {
313 return toText().hashCode();
317 public Mine clone() {
318 return new Mine(toText(), null);