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 static Pattern minePattern = Pattern
35 .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)");
36 protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
37 protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
38 protected static Pattern waterproofPattern = Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
39 protected static Pattern trampolinePattern = Pattern
40 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
41 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
43 public Mine(String text) {
45 if (findCells(OpenLambdaLift).size() != 0)
46 throw new RuntimeException("Detected OpenLambdaLift in new map!");
47 if (findCells(MiningRobot).size() != 1)
48 throw new RuntimeException("New map does not contain a robot!");
49 if (findCells(ClosedLambdaLift).size() != 1)
50 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
54 public String toText() {
56 for (int y = 0; y < sizeY; y++) {
57 for (int x = 0; x < sizeX; x++) {
58 text += cells[x][y].getContent().toText();
62 text = text.replaceAll("\n*$", "");
64 if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
65 text += "Water " + waterLevel + "\n";
66 text += "Flooding " + flooding + "\n";
67 text += "Waterproof " + robotWaterproof + "\n";
69 for (CellContent trampoline : CellContent.values()) {
70 if (trampoline.getTrampolineTarget() != null)
71 text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
74 text = text.replaceAll("\n*$", "");
78 protected void fromText(String str) {
79 Matcher matcher = minePattern.matcher(str);
80 if (!matcher.matches())
81 throw new RuntimeException("Can't parse mine from string");
82 String body = matcher.group(1);
83 String[] rows = body.split("\n+");
86 for (String row : rows) {
92 cells = new Cell[x][y];
93 for (int curY = 0; curY < rows.length; curY++) {
94 String row = rows[curY];
95 for (int curX = 0; curX < row.length() || curX < x; curX++) {
96 CellContent content = null;
97 if (curX < row.length())
98 content = CellContent.fromText("" + row.charAt(curX));
103 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
104 cells[curX][curY] = cell;
108 String metadata = matcher.group(3);
109 if (metadata != null) {
111 String sWaterLevel = "";
112 String sFlooding = "";
113 String sRobotWaterproof = "";
114 Matcher waterMatcher = waterPattern.matcher(metadata);
115 if (waterMatcher.matches()) {
116 sWaterLevel = waterMatcher.group(1);
118 Matcher floodingMatcher = floodingPattern.matcher(metadata);
119 if (floodingMatcher.matches()) {
120 sFlooding = floodingMatcher.group(1);
122 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
123 if (waterproofMatcher.matches()) {
124 sRobotWaterproof = waterproofMatcher.group(1);
126 if (sWaterLevel.length() > 0)
127 waterLevel = Integer.parseInt(sWaterLevel);
128 if (sFlooding.length() > 0)
129 flooding = Integer.parseInt(sFlooding);
130 if (sRobotWaterproof.length() > 0)
131 robotWaterproof = Integer.parseInt(sRobotWaterproof);
133 // Trampolines and targets
134 String trampolines = metadata;
136 Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
137 if (trampolineMatcher.matches()) {
138 String sTrampoline = trampolineMatcher.group(1);
139 String sTarget = trampolineMatcher.group(2);
140 CellContent trampoline = CellContent.fromText(sTrampoline);
141 CellContent target = CellContent.fromText(sTarget);
142 if (trampoline != null && target != null)
143 trampoline.setTrampolineTarget(target);
146 trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
151 public Cell getCell(Coordinate coordinate) {
152 if (coordinate == null)
154 int x = coordinate.x;
155 int y = coordinate.y;
156 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
158 return cells[x - 1][sizeY - y];
162 public String toString() {
166 public int getSizeX() {
170 public int getSizeY() {
174 public Set<Movement> getAvailableRobotMovements() {
175 Set<Movement> movs = new HashSet<Movement>();
176 Cell robotCell = getRobotCell();
177 Set<Cell> nCells = getNeighboringCells(robotCell);
178 for (Cell nCell : nCells) {
179 CellContent nContent = nCell.getContent();
180 Coordinate nCoordinate = nCell.getCoordinate();
181 Cell rightNCell = getCell(nCoordinate.right());
182 Cell leftNCell = getCell(nCoordinate.left());
183 if (nContent == Empty
185 || nContent == Lambda
186 || nContent == OpenLambdaLift
187 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
188 .getContent() == Empty)
189 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
190 .getContent() == Empty)) {
191 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
195 movs.add(Movement.ABORT);
196 movs.add(Movement.WAIT);
200 public Set<Cell> getNeighboringCells(Cell cell) {
201 Set<Cell> cells = new HashSet<Cell>();
202 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
203 cells.add(getCell(coordinate));
207 public Set<Cell> findCells(CellContent content) {
208 Set<Cell> finded = new HashSet<Cell>();
209 for (int y = sizeY - 1; y >= 0; y--) {
210 for (int x = 0; x < sizeX; x++) {
211 if (cells[x][y].getContent() == content)
212 finded.add(cells[x][y]);
218 public void setCells(Cell[][] cells) {
222 public Cell getRobotCell() {
223 for (Cell cell : findCells(MiningRobot)) {
226 throw new RuntimeException("Where is robot!?");
229 public Cell[][] cloneCells() {
230 Cell[][] clone = new Cell[sizeX][sizeY];
231 for (int y = sizeY - 1; y >= 0; y--) {
232 for (int x = 0; x < sizeX; x++) {
234 clone[x][y] = cells[x][y].clone();
235 } catch (CloneNotSupportedException e) {
236 throw new RuntimeException(e);
243 public int getWaterLevel() {
247 public void setWaterLevel(int waterLevel) {
248 this.waterLevel = waterLevel;
251 public int getFlooding() {
255 public int getRobotWaterproof() {
256 return robotWaterproof;