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 {
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 static Pattern minePattern = Pattern
38 .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
39 protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
40 protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
41 protected static Pattern waterproofPattern = Pattern
42 .compile("(?s).*(?<=.*Waterproof )(\\d+).*");
43 protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
44 protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
45 protected static Pattern trampolinePattern = Pattern
46 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
47 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
49 public Mine(String text) {
51 if (findCells(OpenLambdaLift).size() != 0)
52 throw new RuntimeException("Detected OpenLambdaLift in new map!");
53 if (findCells(MiningRobot).size() != 1)
54 throw new RuntimeException("New map does not contain a robot!");
55 if (findCells(ClosedLambdaLift).size() != 1)
56 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
57 lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
61 public String toText() {
63 for (int y = 0; y < sizeY; y++) {
64 for (int x = 0; x < sizeX; x++) {
65 text += cells[x][y].getContent().toText();
69 text = text.replaceAll("\n*$", "");
71 if (waterLevel != 0) {
72 text += "Water " + waterLevel + "\n";
75 text += "Flooding " + flooding + "\n";
77 text += "Waterproof " + robotWaterproof + "\n";
78 for (CellContent trampoline : CellContent.values()) {
79 if (trampoline.getTrampolineTarget() != null)
80 text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
83 text += "Growth " + growth + "\n";
84 if (razorsCount != 0) {
85 text += "Razors " + razorsCount + "\n";
87 text = text.replaceAll("\n*$", "");
91 protected void fromText(String str) {
92 Matcher matcher = minePattern.matcher(str);
93 if (!matcher.matches())
94 throw new RuntimeException("Can't parse mine from string");
95 String body = matcher.group(1);
96 String[] rows = body.split("\n+");
99 for (String row : rows) {
100 if (x < row.length())
105 cells = new Cell[x][y];
106 for (int curY = 0; curY < rows.length; curY++) {
107 String row = rows[curY];
108 for (int curX = 0; curX < row.length() || curX < x; curX++) {
109 CellContent content = null;
110 if (curX < row.length())
111 content = CellContent.fromText("" + row.charAt(curX));
116 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
117 cells[curX][curY] = cell;
121 String metadata = matcher.group(3);
122 if (metadata != null) {
124 String sWaterLevel = "";
125 String sFlooding = "";
126 String sRobotWaterproof = "";
127 Matcher waterMatcher = waterPattern.matcher(metadata);
128 if (waterMatcher.matches()) {
129 sWaterLevel = waterMatcher.group(1);
131 Matcher floodingMatcher = floodingPattern.matcher(metadata);
132 if (floodingMatcher.matches()) {
133 sFlooding = floodingMatcher.group(1);
135 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
136 if (waterproofMatcher.matches()) {
137 sRobotWaterproof = waterproofMatcher.group(1);
139 if (sWaterLevel.length() > 0)
140 waterLevel = Integer.parseInt(sWaterLevel);
141 if (sFlooding.length() > 0)
142 flooding = Integer.parseInt(sFlooding);
143 if (sRobotWaterproof.length() > 0)
144 robotWaterproof = Integer.parseInt(sRobotWaterproof);
146 // Trampolines and targets
147 String trampolines = metadata;
149 Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
150 if (trampolineMatcher.matches()) {
151 String sTrampoline = trampolineMatcher.group(1);
152 String sTarget = trampolineMatcher.group(2);
153 CellContent trampoline = CellContent.fromText(sTrampoline);
154 CellContent target = CellContent.fromText(sTarget);
155 if (trampoline != null && target != null)
156 trampoline.setTrampolineTarget(target);
159 trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
162 // Growth and razorsCount
165 Matcher growthMatcher = growthPattern.matcher(metadata);
166 if (growthMatcher.matches()) {
167 sGrowth = growthMatcher.group(1);
169 Matcher razorsMatcher = razorsPattern.matcher(metadata);
170 if (razorsMatcher.matches()) {
171 sRazors = razorsMatcher.group(1);
173 if (sGrowth.length() > 0)
174 growth = Integer.parseInt(sGrowth);
175 if (sRazors.length() > 0)
176 razorsCount = Integer.parseInt(sRazors);
180 public Cell getCell(Coordinate coordinate) {
181 if (coordinate == null)
183 int x = coordinate.x;
184 int y = coordinate.y;
185 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
187 return cells[x - 1][sizeY - y];
191 public String toString() {
195 public int getSizeX() {
199 public int getSizeY() {
203 public Set<Movement> getAvailableRobotMovements() {
204 Set<Movement> movs = new HashSet<Movement>();
205 Cell robotCell = getRobotCell();
206 Set<Cell> nCells = getNeighboringCells(robotCell);
207 for (Cell nCell : nCells) {
208 CellContent nContent = nCell.getContent();
209 Coordinate nCoordinate = nCell.getCoordinate();
210 Cell rightNCell = getCell(nCoordinate.right());
211 Cell leftNCell = getCell(nCoordinate.left());
212 if (nContent == Empty
214 || nContent == Lambda
215 || nContent == OpenLambdaLift
216 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
217 .getContent() == Empty)
218 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
219 .getContent() == Empty)) {
220 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
224 movs.add(Movement.ABORT);
225 movs.add(Movement.WAIT);
229 public Set<Cell> getNeighboringCells(Cell cell) {
230 Set<Cell> cells = new HashSet<Cell>();
231 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
232 cells.add(getCell(coordinate));
236 public Set<Cell> findCells(CellContent content) {
237 Set<Cell> finded = new HashSet<Cell>();
238 for (int y = sizeY - 1; y >= 0; y--) {
239 for (int x = 0; x < sizeX; x++) {
240 if (cells[x][y].getContent() == content)
241 finded.add(cells[x][y]);
247 public void setCells(Cell[][] cells) {
251 public Cell getRobotCell() {
252 for (Cell cell : findCells(MiningRobot)) {
255 throw new RuntimeException("Where is robot!?");
258 public Cell[][] cloneCells() {
259 Cell[][] clone = new Cell[sizeX][sizeY];
260 for (int y = sizeY - 1; y >= 0; y--) {
261 for (int x = 0; x < sizeX; x++) {
263 clone[x][y] = cells[x][y].clone();
264 } catch (CloneNotSupportedException e) {
265 throw new RuntimeException(e);
272 public int getWaterLevel() {
276 public void setWaterLevel(int waterLevel) {
277 this.waterLevel = waterLevel;
280 public int getFlooding() {
284 public int getRobotWaterproof() {
285 return robotWaterproof;
288 public int getGrowth() {
292 public int getRazorsCount() {
296 public void setRazorsCount(int razorsCount) {
297 this.razorsCount = razorsCount;
300 public int getLambdasAndHighOrderRocksStartCount() {
301 return lambdasAndHighOrderRocksStartCount;