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!");
58 lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
62 public String toText() {
64 for (int y = 0; y < sizeY; y++) {
65 for (int x = 0; x < sizeX; x++) {
66 text += cells[x][y].getContent().toText();
70 text = text.replaceAll("\n*$", "");
73 text = text.replaceAll("\n*$", "");
77 protected void fromText(String str) {
78 Matcher matcher = minePattern.matcher(str);
79 if (!matcher.matches())
80 throw new RuntimeException("Can't parse mine from string");
81 String body = matcher.group(1);
82 String[] rows = body.split("\n+");
85 for (String row : rows) {
91 cells = new Cell[x][y];
92 for (int curY = 0; curY < rows.length; curY++) {
93 String row = rows[curY];
94 for (int curX = 0; curX < row.length() || curX < x; curX++) {
95 CellContent content = null;
96 if (curX < row.length())
97 content = CellContent.fromText("" + row.charAt(curX));
102 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
103 cells[curX][curY] = cell;
107 metadata = matcher.group(3);
108 if (metadata != null) {
110 String sWaterLevel = "";
111 String sFlooding = "";
112 String sRobotWaterproof = "";
113 Matcher waterMatcher = waterPattern.matcher(metadata);
114 if (waterMatcher.matches()) {
115 sWaterLevel = waterMatcher.group(1);
117 Matcher floodingMatcher = floodingPattern.matcher(metadata);
118 if (floodingMatcher.matches()) {
119 sFlooding = floodingMatcher.group(1);
121 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
122 if (waterproofMatcher.matches()) {
123 sRobotWaterproof = waterproofMatcher.group(1);
125 if (sWaterLevel.length() > 0)
126 waterLevel = Integer.parseInt(sWaterLevel);
127 if (sFlooding.length() > 0)
128 flooding = Integer.parseInt(sFlooding);
129 if (sRobotWaterproof.length() > 0)
130 robotWaterproof = Integer.parseInt(sRobotWaterproof);
132 // Trampolines and targets
133 String trampolines = metadata;
135 Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
136 if (trampolineMatcher.matches()) {
137 String sTrampoline = trampolineMatcher.group(1);
138 String sTarget = trampolineMatcher.group(2);
139 CellContent trampoline = CellContent.fromText(sTrampoline);
140 CellContent target = CellContent.fromText(sTarget);
141 if (trampoline != null && target != null)
142 trampoline.setTrampolineTarget(target);
145 trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
148 // Growth and razorsCount
151 Matcher growthMatcher = growthPattern.matcher(metadata);
152 if (growthMatcher.matches()) {
153 sGrowth = growthMatcher.group(1);
155 Matcher razorsMatcher = razorsPattern.matcher(metadata);
156 if (razorsMatcher.matches()) {
157 sRazors = razorsMatcher.group(1);
159 if (sGrowth.length() > 0)
160 growth = Integer.parseInt(sGrowth);
161 if (sRazors.length() > 0)
162 razorsCount = Integer.parseInt(sRazors);
167 public Cell getCell(Coordinate coordinate) {
168 if (coordinate == null)
170 int x = coordinate.x;
171 int y = coordinate.y;
172 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
174 return cells[x - 1][sizeY - y];
178 public String toString() {
182 public int getSizeX() {
186 public int getSizeY() {
190 public Set<Movement> getAvailableRobotMovements() {
191 Set<Movement> movs = new HashSet<Movement>();
192 Cell robotCell = getRobotCell();
193 Set<Cell> nCells = getNeighboringCells(robotCell);
194 for (Cell nCell : nCells) {
195 CellContent nContent = nCell.getContent();
196 Coordinate nCoordinate = nCell.getCoordinate();
197 Cell rightNCell = getCell(nCoordinate.right());
198 Cell leftNCell = getCell(nCoordinate.left());
199 if (nContent == Empty
201 || nContent == Lambda
202 || nContent == OpenLambdaLift
203 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
204 .getContent() == Empty)
205 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
206 .getContent() == Empty)) {
207 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
211 movs.add(Movement.ABORT);
212 movs.add(Movement.WAIT);
216 public Set<Cell> getNeighboringCells(Cell cell) {
217 Set<Cell> cells = new HashSet<Cell>();
218 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
219 cells.add(getCell(coordinate));
223 public Set<Cell> getAdjacentCells(Cell cell) {
224 Set<Cell> cells = new HashSet<Cell>();
225 for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates())
226 cells.add(getCell(coordinate));
230 public Set<Cell> findCells(CellContent content) {
231 Set<Cell> finded = new HashSet<Cell>();
232 for (int y = sizeY - 1; y >= 0; y--) {
233 for (int x = 0; x < sizeX; x++) {
234 if (cells[x][y].getContent() == content)
235 finded.add(cells[x][y]);
241 public void setCells(Cell[][] cells) {
245 public Cell getRobotCell() {
246 for (Cell cell : findCells(MiningRobot)) {
249 throw new RuntimeException("Where is robot!?");
252 public Cell[][] cloneCells() {
253 Cell[][] clone = new Cell[sizeX][sizeY];
254 for (int y = sizeY - 1; y >= 0; y--) {
255 for (int x = 0; x < sizeX; x++) {
257 clone[x][y] = cells[x][y].clone();
258 } catch (CloneNotSupportedException e) {
259 throw new RuntimeException(e);
266 public int getWaterLevel() {
270 public void setWaterLevel(int waterLevel) {
271 this.waterLevel = waterLevel;
274 public int getFlooding() {
278 public int getRobotWaterproof() {
279 return robotWaterproof;
282 public int getGrowth() {
286 public int getRazorsCount() {
290 public void setRazorsCount(int razorsCount) {
291 this.razorsCount = razorsCount;
294 public int getLambdasAndHighOrderRocksStartCount() {
295 return lambdasAndHighOrderRocksStartCount;
299 public boolean equals(Object obj) {
300 if (obj instanceof Mine) {
301 Mine otherMine = (Mine) obj;
302 return toText().equals(otherMine.toText());
304 return super.equals(obj);
308 public int hashCode() {
309 return toText().hashCode();
313 public Mine clone() throws CloneNotSupportedException {
314 return new Mine(toText());