|
indvdum@6
|
1 |
package ru.bosony.model.mine;
|
|
indvdum@6
|
2 |
|
|
indvdum@9
|
3 |
import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
|
|
indvdum@9
|
4 |
import static ru.bosony.model.cellscontents.CellContent.Earth;
|
|
indvdum@9
|
5 |
import static ru.bosony.model.cellscontents.CellContent.Empty;
|
|
indvdum@9
|
6 |
import static ru.bosony.model.cellscontents.CellContent.Lambda;
|
|
indvdum@9
|
7 |
import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
|
|
indvdum@9
|
8 |
import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
|
|
indvdum@9
|
9 |
import static ru.bosony.model.cellscontents.CellContent.Rock;
|
|
indvdum@9
|
10 |
|
|
indvdum@51
|
11 |
import java.util.Collection;
|
|
indvdum@7
|
12 |
import java.util.HashSet;
|
|
indvdum@7
|
13 |
import java.util.Set;
|
|
indvdum@25
|
14 |
import java.util.regex.Matcher;
|
|
indvdum@25
|
15 |
import java.util.regex.Pattern;
|
|
indvdum@7
|
16 |
|
|
indvdum@6
|
17 |
import ru.bosony.model.cellscontents.CellContent;
|
|
indvdum@6
|
18 |
import ru.bosony.model.io.TextRepresentable;
|
|
indvdum@22
|
19 |
import ru.bosony.model.moving.Coordinate;
|
|
indvdum@7
|
20 |
import ru.bosony.model.moving.Movement;
|
|
indvdum@6
|
21 |
|
|
indvdum@7
|
22 |
/**
|
|
indvdum@7
|
23 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
|
|
indvdum@7
|
24 |
* 14.07.2012 2:02:18
|
|
indvdum@7
|
25 |
*
|
|
indvdum@7
|
26 |
*/
|
|
indvdum@46
|
27 |
public class Mine implements TextRepresentable, Cloneable {
|
|
indvdum@6
|
28 |
|
|
indvdum@25
|
29 |
protected Cell[][] cells;
|
|
indvdum@25
|
30 |
protected int sizeX;
|
|
indvdum@25
|
31 |
protected int sizeY;
|
|
indvdum@30
|
32 |
protected int waterLevel = 0;
|
|
indvdum@30
|
33 |
protected int flooding = 0;
|
|
indvdum@30
|
34 |
protected int robotWaterproof = 10;
|
|
indvdum@30
|
35 |
protected int growth = 25;
|
|
indvdum@30
|
36 |
protected int razorsCount = 0;
|
|
indvdum@30
|
37 |
protected int lambdasAndHighOrderRocksStartCount = 0;
|
|
indvdum@31
|
38 |
protected String metadata = "";
|
|
indvdum@30
|
39 |
protected static Pattern minePattern = Pattern
|
|
indvdum@30
|
40 |
.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
|
|
indvdum@30
|
41 |
protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
|
|
indvdum@30
|
42 |
protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
|
|
indvdum@30
|
43 |
protected static Pattern waterproofPattern = Pattern
|
|
indvdum@30
|
44 |
.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
|
|
indvdum@30
|
45 |
protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
|
|
indvdum@30
|
46 |
protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
|
|
indvdum@30
|
47 |
protected static Pattern trampolinePattern = Pattern
|
|
indvdum@30
|
48 |
.compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
|
|
indvdum@30
|
49 |
protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
|
|
indvdum@6
|
50 |
|
|
indvdum@6
|
51 |
public Mine(String text) {
|
|
indvdum@50
|
52 |
this(text, null);
|
|
indvdum@8
|
53 |
if (findCells(OpenLambdaLift).size() != 0)
|
|
indvdum@8
|
54 |
throw new RuntimeException("Detected OpenLambdaLift in new map!");
|
|
indvdum@8
|
55 |
if (findCells(MiningRobot).size() != 1)
|
|
indvdum@8
|
56 |
throw new RuntimeException("New map does not contain a robot!");
|
|
indvdum@8
|
57 |
if (findCells(ClosedLambdaLift).size() != 1)
|
|
indvdum@8
|
58 |
throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
|
|
indvdum@50
|
59 |
}
|
|
indvdum@50
|
60 |
|
|
indvdum@50
|
61 |
protected Mine(String text, Object obj) {
|
|
indvdum@50
|
62 |
fromText(text);
|
|
indvdum@30
|
63 |
lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
|
|
indvdum@6
|
64 |
}
|
|
indvdum@6
|
65 |
|
|
indvdum@6
|
66 |
@Override
|
|
indvdum@6
|
67 |
public String toText() {
|
|
indvdum@6
|
68 |
String text = "";
|
|
indvdum@6
|
69 |
for (int y = 0; y < sizeY; y++) {
|
|
indvdum@6
|
70 |
for (int x = 0; x < sizeX; x++) {
|
|
indvdum@6
|
71 |
text += cells[x][y].getContent().toText();
|
|
indvdum@6
|
72 |
}
|
|
indvdum@6
|
73 |
text += "\n";
|
|
indvdum@6
|
74 |
}
|
|
indvdum@25
|
75 |
text = text.replaceAll("\n*$", "");
|
|
indvdum@27
|
76 |
text += "\n\n";
|
|
indvdum@31
|
77 |
text += metadata;
|
|
indvdum@27
|
78 |
text = text.replaceAll("\n*$", "");
|
|
indvdum@25
|
79 |
return text;
|
|
indvdum@6
|
80 |
}
|
|
indvdum@6
|
81 |
|
|
indvdum@51
|
82 |
public String toTextWithoutRobot() {
|
|
indvdum@51
|
83 |
return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText());
|
|
indvdum@51
|
84 |
}
|
|
indvdum@51
|
85 |
|
|
indvdum@25
|
86 |
protected void fromText(String str) {
|
|
indvdum@26
|
87 |
Matcher matcher = minePattern.matcher(str);
|
|
indvdum@25
|
88 |
if (!matcher.matches())
|
|
indvdum@25
|
89 |
throw new RuntimeException("Can't parse mine from string");
|
|
indvdum@25
|
90 |
String body = matcher.group(1);
|
|
indvdum@25
|
91 |
String[] rows = body.split("\n+");
|
|
indvdum@6
|
92 |
int y = rows.length;
|
|
indvdum@6
|
93 |
int x = 0;
|
|
indvdum@6
|
94 |
for (String row : rows) {
|
|
indvdum@6
|
95 |
if (x < row.length())
|
|
indvdum@6
|
96 |
x = row.length();
|
|
indvdum@6
|
97 |
}
|
|
indvdum@6
|
98 |
sizeX = x;
|
|
indvdum@6
|
99 |
sizeY = y;
|
|
indvdum@6
|
100 |
cells = new Cell[x][y];
|
|
indvdum@6
|
101 |
for (int curY = 0; curY < rows.length; curY++) {
|
|
indvdum@6
|
102 |
String row = rows[curY];
|
|
indvdum@6
|
103 |
for (int curX = 0; curX < row.length() || curX < x; curX++) {
|
|
indvdum@6
|
104 |
CellContent content = null;
|
|
indvdum@6
|
105 |
if (curX < row.length())
|
|
indvdum@6
|
106 |
content = CellContent.fromText("" + row.charAt(curX));
|
|
indvdum@6
|
107 |
else
|
|
indvdum@8
|
108 |
content = Empty;
|
|
indvdum@22
|
109 |
if (content == null)
|
|
indvdum@22
|
110 |
content = Empty;
|
|
indvdum@7
|
111 |
Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
|
|
indvdum@6
|
112 |
cells[curX][curY] = cell;
|
|
indvdum@6
|
113 |
}
|
|
indvdum@6
|
114 |
}
|
|
indvdum@26
|
115 |
|
|
indvdum@31
|
116 |
metadata = matcher.group(3);
|
|
indvdum@27
|
117 |
if (metadata != null) {
|
|
indvdum@27
|
118 |
// Water flooding
|
|
indvdum@27
|
119 |
String sWaterLevel = "";
|
|
indvdum@27
|
120 |
String sFlooding = "";
|
|
indvdum@27
|
121 |
String sRobotWaterproof = "";
|
|
indvdum@27
|
122 |
Matcher waterMatcher = waterPattern.matcher(metadata);
|
|
indvdum@27
|
123 |
if (waterMatcher.matches()) {
|
|
indvdum@27
|
124 |
sWaterLevel = waterMatcher.group(1);
|
|
indvdum@27
|
125 |
}
|
|
indvdum@27
|
126 |
Matcher floodingMatcher = floodingPattern.matcher(metadata);
|
|
indvdum@27
|
127 |
if (floodingMatcher.matches()) {
|
|
indvdum@27
|
128 |
sFlooding = floodingMatcher.group(1);
|
|
indvdum@27
|
129 |
}
|
|
indvdum@27
|
130 |
Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
|
|
indvdum@27
|
131 |
if (waterproofMatcher.matches()) {
|
|
indvdum@27
|
132 |
sRobotWaterproof = waterproofMatcher.group(1);
|
|
indvdum@27
|
133 |
}
|
|
indvdum@27
|
134 |
if (sWaterLevel.length() > 0)
|
|
indvdum@27
|
135 |
waterLevel = Integer.parseInt(sWaterLevel);
|
|
indvdum@27
|
136 |
if (sFlooding.length() > 0)
|
|
indvdum@27
|
137 |
flooding = Integer.parseInt(sFlooding);
|
|
indvdum@27
|
138 |
if (sRobotWaterproof.length() > 0)
|
|
indvdum@27
|
139 |
robotWaterproof = Integer.parseInt(sRobotWaterproof);
|
|
indvdum@27
|
140 |
|
|
indvdum@27
|
141 |
// Trampolines and targets
|
|
indvdum@27
|
142 |
String trampolines = metadata;
|
|
indvdum@27
|
143 |
while (true) {
|
|
indvdum@27
|
144 |
Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
|
|
indvdum@27
|
145 |
if (trampolineMatcher.matches()) {
|
|
indvdum@27
|
146 |
String sTrampoline = trampolineMatcher.group(1);
|
|
indvdum@27
|
147 |
String sTarget = trampolineMatcher.group(2);
|
|
indvdum@27
|
148 |
CellContent trampoline = CellContent.fromText(sTrampoline);
|
|
indvdum@27
|
149 |
CellContent target = CellContent.fromText(sTarget);
|
|
indvdum@27
|
150 |
if (trampoline != null && target != null)
|
|
indvdum@27
|
151 |
trampoline.setTrampolineTarget(target);
|
|
indvdum@27
|
152 |
} else
|
|
indvdum@27
|
153 |
break;
|
|
indvdum@27
|
154 |
trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
|
|
indvdum@27
|
155 |
}
|
|
indvdum@29
|
156 |
|
|
indvdum@29
|
157 |
// Growth and razorsCount
|
|
indvdum@29
|
158 |
String sGrowth = "";
|
|
indvdum@29
|
159 |
String sRazors = "";
|
|
indvdum@29
|
160 |
Matcher growthMatcher = growthPattern.matcher(metadata);
|
|
indvdum@29
|
161 |
if (growthMatcher.matches()) {
|
|
indvdum@29
|
162 |
sGrowth = growthMatcher.group(1);
|
|
indvdum@29
|
163 |
}
|
|
indvdum@29
|
164 |
Matcher razorsMatcher = razorsPattern.matcher(metadata);
|
|
indvdum@29
|
165 |
if (razorsMatcher.matches()) {
|
|
indvdum@29
|
166 |
sRazors = razorsMatcher.group(1);
|
|
indvdum@29
|
167 |
}
|
|
indvdum@29
|
168 |
if (sGrowth.length() > 0)
|
|
indvdum@29
|
169 |
growth = Integer.parseInt(sGrowth);
|
|
indvdum@29
|
170 |
if (sRazors.length() > 0)
|
|
indvdum@29
|
171 |
razorsCount = Integer.parseInt(sRazors);
|
|
indvdum@31
|
172 |
} else
|
|
indvdum@31
|
173 |
metadata = "";
|
|
indvdum@6
|
174 |
}
|
|
indvdum@6
|
175 |
|
|
indvdum@7
|
176 |
public Cell getCell(Coordinate coordinate) {
|
|
indvdum@8
|
177 |
if (coordinate == null)
|
|
indvdum@8
|
178 |
return null;
|
|
indvdum@7
|
179 |
int x = coordinate.x;
|
|
indvdum@7
|
180 |
int y = coordinate.y;
|
|
indvdum@7
|
181 |
if (x < 1 || x > sizeX || y < 1 || y > sizeY)
|
|
indvdum@7
|
182 |
return null;
|
|
indvdum@7
|
183 |
return cells[x - 1][sizeY - y];
|
|
indvdum@7
|
184 |
}
|
|
indvdum@7
|
185 |
|
|
indvdum@51
|
186 |
public Set<Cell> getCells(Collection<Coordinate> coordinates) {
|
|
indvdum@51
|
187 |
Set<Cell> cells = new HashSet<Cell>();
|
|
indvdum@51
|
188 |
if (coordinates == null)
|
|
indvdum@51
|
189 |
return cells;
|
|
indvdum@51
|
190 |
for (Coordinate coordinate : coordinates) {
|
|
indvdum@51
|
191 |
Cell cell = getCell(coordinate);
|
|
indvdum@51
|
192 |
if (cell != null)
|
|
indvdum@51
|
193 |
cells.add(cell);
|
|
indvdum@51
|
194 |
}
|
|
indvdum@51
|
195 |
return cells;
|
|
indvdum@51
|
196 |
}
|
|
indvdum@51
|
197 |
|
|
indvdum@6
|
198 |
@Override
|
|
indvdum@6
|
199 |
public String toString() {
|
|
indvdum@6
|
200 |
return toText();
|
|
indvdum@6
|
201 |
}
|
|
indvdum@6
|
202 |
|
|
indvdum@7
|
203 |
public int getSizeX() {
|
|
indvdum@7
|
204 |
return sizeX;
|
|
indvdum@7
|
205 |
}
|
|
indvdum@7
|
206 |
|
|
indvdum@7
|
207 |
public int getSizeY() {
|
|
indvdum@7
|
208 |
return sizeY;
|
|
indvdum@7
|
209 |
}
|
|
indvdum@7
|
210 |
|
|
indvdum@7
|
211 |
public Set<Movement> getAvailableRobotMovements() {
|
|
indvdum@7
|
212 |
Set<Movement> movs = new HashSet<Movement>();
|
|
indvdum@22
|
213 |
Cell robotCell = getRobotCell();
|
|
indvdum@7
|
214 |
Set<Cell> nCells = getNeighboringCells(robotCell);
|
|
indvdum@7
|
215 |
for (Cell nCell : nCells) {
|
|
indvdum@7
|
216 |
CellContent nContent = nCell.getContent();
|
|
indvdum@7
|
217 |
Coordinate nCoordinate = nCell.getCoordinate();
|
|
indvdum@7
|
218 |
Cell rightNCell = getCell(nCoordinate.right());
|
|
indvdum@7
|
219 |
Cell leftNCell = getCell(nCoordinate.left());
|
|
indvdum@8
|
220 |
if (nContent == Empty
|
|
indvdum@8
|
221 |
|| nContent == Earth
|
|
indvdum@8
|
222 |
|| nContent == Lambda
|
|
indvdum@8
|
223 |
|| nContent == OpenLambdaLift
|
|
indvdum@8
|
224 |
|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
|
|
indvdum@8
|
225 |
.getContent() == Empty)
|
|
indvdum@8
|
226 |
|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
|
|
indvdum@8
|
227 |
.getContent() == Empty)) {
|
|
indvdum@7
|
228 |
Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
|
|
indvdum@7
|
229 |
movs.add(mov);
|
|
indvdum@7
|
230 |
}
|
|
indvdum@7
|
231 |
}
|
|
indvdum@7
|
232 |
movs.add(Movement.ABORT);
|
|
indvdum@7
|
233 |
movs.add(Movement.WAIT);
|
|
indvdum@7
|
234 |
return movs;
|
|
indvdum@7
|
235 |
}
|
|
indvdum@7
|
236 |
|
|
indvdum@7
|
237 |
public Set<Cell> getNeighboringCells(Cell cell) {
|
|
indvdum@7
|
238 |
Set<Cell> cells = new HashSet<Cell>();
|
|
indvdum@51
|
239 |
for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) {
|
|
indvdum@51
|
240 |
Cell nextCell = getCell(coordinate);
|
|
indvdum@51
|
241 |
if (nextCell != null)
|
|
indvdum@51
|
242 |
cells.add(nextCell);
|
|
indvdum@51
|
243 |
}
|
|
indvdum@7
|
244 |
return cells;
|
|
indvdum@7
|
245 |
}
|
|
indvdum@7
|
246 |
|
|
indvdum@40
|
247 |
public Set<Cell> getAdjacentCells(Cell cell) {
|
|
indvdum@40
|
248 |
Set<Cell> cells = new HashSet<Cell>();
|
|
indvdum@51
|
249 |
for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) {
|
|
indvdum@51
|
250 |
Cell nextCell = getCell(coordinate);
|
|
indvdum@51
|
251 |
if (nextCell != null)
|
|
indvdum@51
|
252 |
cells.add(nextCell);
|
|
indvdum@51
|
253 |
}
|
|
indvdum@40
|
254 |
return cells;
|
|
indvdum@40
|
255 |
}
|
|
indvdum@40
|
256 |
|
|
indvdum@9
|
257 |
public Set<Cell> findCells(CellContent content) {
|
|
indvdum@9
|
258 |
Set<Cell> finded = new HashSet<Cell>();
|
|
indvdum@8
|
259 |
for (int y = sizeY - 1; y >= 0; y--) {
|
|
indvdum@15
|
260 |
for (int x = 0; x < sizeX; x++) {
|
|
indvdum@8
|
261 |
if (cells[x][y].getContent() == content)
|
|
indvdum@8
|
262 |
finded.add(cells[x][y]);
|
|
indvdum@8
|
263 |
}
|
|
indvdum@8
|
264 |
}
|
|
indvdum@8
|
265 |
return finded;
|
|
indvdum@8
|
266 |
}
|
|
indvdum@8
|
267 |
|
|
indvdum@9
|
268 |
public void setCells(Cell[][] cells) {
|
|
indvdum@9
|
269 |
this.cells = cells;
|
|
indvdum@9
|
270 |
}
|
|
indvdum@9
|
271 |
|
|
indvdum@9
|
272 |
public Cell getRobotCell() {
|
|
indvdum@22
|
273 |
for (Cell cell : findCells(MiningRobot)) {
|
|
indvdum@22
|
274 |
return cell;
|
|
indvdum@22
|
275 |
}
|
|
indvdum@22
|
276 |
throw new RuntimeException("Where is robot!?");
|
|
indvdum@9
|
277 |
}
|
|
indvdum@9
|
278 |
|
|
indvdum@22
|
279 |
public Cell[][] cloneCells() {
|
|
indvdum@22
|
280 |
Cell[][] clone = new Cell[sizeX][sizeY];
|
|
indvdum@22
|
281 |
for (int y = sizeY - 1; y >= 0; y--) {
|
|
indvdum@22
|
282 |
for (int x = 0; x < sizeX; x++) {
|
|
indvdum@22
|
283 |
try {
|
|
indvdum@22
|
284 |
clone[x][y] = cells[x][y].clone();
|
|
indvdum@22
|
285 |
} catch (CloneNotSupportedException e) {
|
|
indvdum@22
|
286 |
throw new RuntimeException(e);
|
|
indvdum@22
|
287 |
}
|
|
indvdum@22
|
288 |
}
|
|
indvdum@22
|
289 |
}
|
|
indvdum@22
|
290 |
return clone;
|
|
indvdum@8
|
291 |
}
|
|
indvdum@8
|
292 |
|
|
indvdum@25
|
293 |
public int getWaterLevel() {
|
|
indvdum@25
|
294 |
return waterLevel;
|
|
indvdum@25
|
295 |
}
|
|
indvdum@25
|
296 |
|
|
indvdum@25
|
297 |
public void setWaterLevel(int waterLevel) {
|
|
indvdum@25
|
298 |
this.waterLevel = waterLevel;
|
|
indvdum@25
|
299 |
}
|
|
indvdum@25
|
300 |
|
|
indvdum@25
|
301 |
public int getFlooding() {
|
|
indvdum@25
|
302 |
return flooding;
|
|
indvdum@25
|
303 |
}
|
|
indvdum@25
|
304 |
|
|
indvdum@25
|
305 |
public int getRobotWaterproof() {
|
|
indvdum@25
|
306 |
return robotWaterproof;
|
|
indvdum@25
|
307 |
}
|
|
indvdum@25
|
308 |
|
|
indvdum@29
|
309 |
public int getGrowth() {
|
|
indvdum@29
|
310 |
return growth;
|
|
indvdum@29
|
311 |
}
|
|
indvdum@29
|
312 |
|
|
indvdum@29
|
313 |
public int getRazorsCount() {
|
|
indvdum@29
|
314 |
return razorsCount;
|
|
indvdum@29
|
315 |
}
|
|
indvdum@29
|
316 |
|
|
indvdum@29
|
317 |
public void setRazorsCount(int razorsCount) {
|
|
indvdum@29
|
318 |
this.razorsCount = razorsCount;
|
|
indvdum@29
|
319 |
}
|
|
indvdum@29
|
320 |
|
|
indvdum@30
|
321 |
public int getLambdasAndHighOrderRocksStartCount() {
|
|
indvdum@30
|
322 |
return lambdasAndHighOrderRocksStartCount;
|
|
indvdum@30
|
323 |
}
|
|
indvdum@30
|
324 |
|
|
indvdum@43
|
325 |
@Override
|
|
indvdum@43
|
326 |
public boolean equals(Object obj) {
|
|
indvdum@43
|
327 |
if (obj instanceof Mine) {
|
|
indvdum@43
|
328 |
Mine otherMine = (Mine) obj;
|
|
indvdum@43
|
329 |
return toText().equals(otherMine.toText());
|
|
indvdum@43
|
330 |
}
|
|
indvdum@43
|
331 |
return super.equals(obj);
|
|
indvdum@43
|
332 |
}
|
|
indvdum@43
|
333 |
|
|
indvdum@45
|
334 |
@Override
|
|
indvdum@45
|
335 |
public int hashCode() {
|
|
indvdum@45
|
336 |
return toText().hashCode();
|
|
indvdum@45
|
337 |
}
|
|
indvdum@45
|
338 |
|
|
indvdum@46
|
339 |
@Override
|
|
indvdum@50
|
340 |
public Mine clone() {
|
|
indvdum@50
|
341 |
return new Mine(toText(), null);
|
|
indvdum@46
|
342 |
}
|
|
indvdum@46
|
343 |
|
|
indvdum@6
|
344 |
}
|