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 static Pattern minePattern = Pattern
37 .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
38 protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
39 protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
40 protected static Pattern waterproofPattern = Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
41 protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
42 protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
43 protected static Pattern trampolinePattern = Pattern
44 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
45 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
47 public Mine(String text) {
49 if (findCells(OpenLambdaLift).size() != 0)
50 throw new RuntimeException("Detected OpenLambdaLift in new map!");
51 if (findCells(MiningRobot).size() != 1)
52 throw new RuntimeException("New map does not contain a robot!");
53 if (findCells(ClosedLambdaLift).size() != 1)
54 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
58 public String toText() {
60 for (int y = 0; y < sizeY; y++) {
61 for (int x = 0; x < sizeX; x++) {
62 text += cells[x][y].getContent().toText();
66 text = text.replaceAll("\n*$", "");
68 if (waterLevel != 0) {
69 text += "Water " + waterLevel + "\n";
72 text += "Flooding " + flooding + "\n";
74 text += "Waterproof " + robotWaterproof + "\n";
75 for (CellContent trampoline : CellContent.values()) {
76 if (trampoline.getTrampolineTarget() != null)
77 text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
80 text += "Growth " + growth + "\n";
81 if (razorsCount != 0) {
82 text += "Razors " + razorsCount + "\n";
84 text = text.replaceAll("\n*$", "");
88 protected void fromText(String str) {
89 Matcher matcher = minePattern.matcher(str);
90 if (!matcher.matches())
91 throw new RuntimeException("Can't parse mine from string");
92 String body = matcher.group(1);
93 String[] rows = body.split("\n+");
96 for (String row : rows) {
102 cells = new Cell[x][y];
103 for (int curY = 0; curY < rows.length; curY++) {
104 String row = rows[curY];
105 for (int curX = 0; curX < row.length() || curX < x; curX++) {
106 CellContent content = null;
107 if (curX < row.length())
108 content = CellContent.fromText("" + row.charAt(curX));
113 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
114 cells[curX][curY] = cell;
118 String metadata = matcher.group(3);
119 if (metadata != null) {
121 String sWaterLevel = "";
122 String sFlooding = "";
123 String sRobotWaterproof = "";
124 Matcher waterMatcher = waterPattern.matcher(metadata);
125 if (waterMatcher.matches()) {
126 sWaterLevel = waterMatcher.group(1);
128 Matcher floodingMatcher = floodingPattern.matcher(metadata);
129 if (floodingMatcher.matches()) {
130 sFlooding = floodingMatcher.group(1);
132 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
133 if (waterproofMatcher.matches()) {
134 sRobotWaterproof = waterproofMatcher.group(1);
136 if (sWaterLevel.length() > 0)
137 waterLevel = Integer.parseInt(sWaterLevel);
138 if (sFlooding.length() > 0)
139 flooding = Integer.parseInt(sFlooding);
140 if (sRobotWaterproof.length() > 0)
141 robotWaterproof = Integer.parseInt(sRobotWaterproof);
143 // Trampolines and targets
144 String trampolines = metadata;
146 Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
147 if (trampolineMatcher.matches()) {
148 String sTrampoline = trampolineMatcher.group(1);
149 String sTarget = trampolineMatcher.group(2);
150 CellContent trampoline = CellContent.fromText(sTrampoline);
151 CellContent target = CellContent.fromText(sTarget);
152 if (trampoline != null && target != null)
153 trampoline.setTrampolineTarget(target);
156 trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
159 // Growth and razorsCount
162 Matcher growthMatcher = growthPattern.matcher(metadata);
163 if (growthMatcher.matches()) {
164 sGrowth = growthMatcher.group(1);
166 Matcher razorsMatcher = razorsPattern.matcher(metadata);
167 if (razorsMatcher.matches()) {
168 sRazors = razorsMatcher.group(1);
170 if (sGrowth.length() > 0)
171 growth = Integer.parseInt(sGrowth);
172 if (sRazors.length() > 0)
173 razorsCount = Integer.parseInt(sRazors);
177 public Cell getCell(Coordinate coordinate) {
178 if (coordinate == null)
180 int x = coordinate.x;
181 int y = coordinate.y;
182 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
184 return cells[x - 1][sizeY - y];
188 public String toString() {
192 public int getSizeX() {
196 public int getSizeY() {
200 public Set<Movement> getAvailableRobotMovements() {
201 Set<Movement> movs = new HashSet<Movement>();
202 Cell robotCell = getRobotCell();
203 Set<Cell> nCells = getNeighboringCells(robotCell);
204 for (Cell nCell : nCells) {
205 CellContent nContent = nCell.getContent();
206 Coordinate nCoordinate = nCell.getCoordinate();
207 Cell rightNCell = getCell(nCoordinate.right());
208 Cell leftNCell = getCell(nCoordinate.left());
209 if (nContent == Empty
211 || nContent == Lambda
212 || nContent == OpenLambdaLift
213 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
214 .getContent() == Empty)
215 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
216 .getContent() == Empty)) {
217 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
221 movs.add(Movement.ABORT);
222 movs.add(Movement.WAIT);
226 public Set<Cell> getNeighboringCells(Cell cell) {
227 Set<Cell> cells = new HashSet<Cell>();
228 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
229 cells.add(getCell(coordinate));
233 public Set<Cell> findCells(CellContent content) {
234 Set<Cell> finded = new HashSet<Cell>();
235 for (int y = sizeY - 1; y >= 0; y--) {
236 for (int x = 0; x < sizeX; x++) {
237 if (cells[x][y].getContent() == content)
238 finded.add(cells[x][y]);
244 public void setCells(Cell[][] cells) {
248 public Cell getRobotCell() {
249 for (Cell cell : findCells(MiningRobot)) {
252 throw new RuntimeException("Where is robot!?");
255 public Cell[][] cloneCells() {
256 Cell[][] clone = new Cell[sizeX][sizeY];
257 for (int y = sizeY - 1; y >= 0; y--) {
258 for (int x = 0; x < sizeX; x++) {
260 clone[x][y] = cells[x][y].clone();
261 } catch (CloneNotSupportedException e) {
262 throw new RuntimeException(e);
269 public int getWaterLevel() {
273 public void setWaterLevel(int waterLevel) {
274 this.waterLevel = waterLevel;
277 public int getFlooding() {
281 public int getRobotWaterproof() {
282 return robotWaterproof;
285 public int getGrowth() {
289 public int getRazorsCount() {
293 public void setRazorsCount(int razorsCount) {
294 this.razorsCount = razorsCount;