6 import static ru.bosony.model.cellscontents.CellContent.Lambda; |
6 import static ru.bosony.model.cellscontents.CellContent.Lambda; |
7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot; |
7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot; |
8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; |
8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; |
9 import static ru.bosony.model.cellscontents.CellContent.Rock; |
9 import static ru.bosony.model.cellscontents.CellContent.Rock; |
10 |
10 |
|
11 import java.util.Collection; |
11 import java.util.HashSet; |
12 import java.util.HashSet; |
12 import java.util.Set; |
13 import java.util.Set; |
13 import java.util.regex.Matcher; |
14 import java.util.regex.Matcher; |
14 import java.util.regex.Pattern; |
15 import java.util.regex.Pattern; |
15 |
16 |
46 protected static Pattern trampolinePattern = Pattern |
47 protected static Pattern trampolinePattern = Pattern |
47 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*"); |
48 .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*"); |
48 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])"; |
49 protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])"; |
49 |
50 |
50 public Mine(String text) { |
51 public Mine(String text) { |
51 fromText(text); |
52 this(text, null); |
52 if (findCells(OpenLambdaLift).size() != 0) |
53 if (findCells(OpenLambdaLift).size() != 0) |
53 throw new RuntimeException("Detected OpenLambdaLift in new map!"); |
54 throw new RuntimeException("Detected OpenLambdaLift in new map!"); |
54 if (findCells(MiningRobot).size() != 1) |
55 if (findCells(MiningRobot).size() != 1) |
55 throw new RuntimeException("New map does not contain a robot!"); |
56 throw new RuntimeException("New map does not contain a robot!"); |
56 if (findCells(ClosedLambdaLift).size() != 1) |
57 if (findCells(ClosedLambdaLift).size() != 1) |
57 throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); |
58 throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); |
|
59 } |
|
60 |
|
61 protected Mine(String text, Object obj) { |
|
62 fromText(text); |
58 lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size(); |
63 lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size(); |
59 } |
64 } |
60 |
65 |
61 @Override |
66 @Override |
62 public String toText() { |
67 public String toText() { |
70 text = text.replaceAll("\n*$", ""); |
75 text = text.replaceAll("\n*$", ""); |
71 text += "\n\n"; |
76 text += "\n\n"; |
72 text += metadata; |
77 text += metadata; |
73 text = text.replaceAll("\n*$", ""); |
78 text = text.replaceAll("\n*$", ""); |
74 return text; |
79 return text; |
|
80 } |
|
81 |
|
82 public String toTextWithoutRobot() { |
|
83 return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText()); |
75 } |
84 } |
76 |
85 |
77 protected void fromText(String str) { |
86 protected void fromText(String str) { |
78 Matcher matcher = minePattern.matcher(str); |
87 Matcher matcher = minePattern.matcher(str); |
79 if (!matcher.matches()) |
88 if (!matcher.matches()) |
170 int x = coordinate.x; |
179 int x = coordinate.x; |
171 int y = coordinate.y; |
180 int y = coordinate.y; |
172 if (x < 1 || x > sizeX || y < 1 || y > sizeY) |
181 if (x < 1 || x > sizeX || y < 1 || y > sizeY) |
173 return null; |
182 return null; |
174 return cells[x - 1][sizeY - y]; |
183 return cells[x - 1][sizeY - y]; |
|
184 } |
|
185 |
|
186 public Set<Cell> getCells(Collection<Coordinate> coordinates) { |
|
187 Set<Cell> cells = new HashSet<Cell>(); |
|
188 if (coordinates == null) |
|
189 return cells; |
|
190 for (Coordinate coordinate : coordinates) { |
|
191 Cell cell = getCell(coordinate); |
|
192 if (cell != null) |
|
193 cells.add(cell); |
|
194 } |
|
195 return cells; |
175 } |
196 } |
176 |
197 |
177 @Override |
198 @Override |
178 public String toString() { |
199 public String toString() { |
179 return toText(); |
200 return toText(); |
213 return movs; |
234 return movs; |
214 } |
235 } |
215 |
236 |
216 public Set<Cell> getNeighboringCells(Cell cell) { |
237 public Set<Cell> getNeighboringCells(Cell cell) { |
217 Set<Cell> cells = new HashSet<Cell>(); |
238 Set<Cell> cells = new HashSet<Cell>(); |
218 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
239 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) { |
219 cells.add(getCell(coordinate)); |
240 Cell nextCell = getCell(coordinate); |
|
241 if (nextCell != null) |
|
242 cells.add(nextCell); |
|
243 } |
220 return cells; |
244 return cells; |
221 } |
245 } |
222 |
246 |
223 public Set<Cell> getAdjacentCells(Cell cell) { |
247 public Set<Cell> getAdjacentCells(Cell cell) { |
224 Set<Cell> cells = new HashSet<Cell>(); |
248 Set<Cell> cells = new HashSet<Cell>(); |
225 for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) |
249 for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) { |
226 cells.add(getCell(coordinate)); |
250 Cell nextCell = getCell(coordinate); |
|
251 if (nextCell != null) |
|
252 cells.add(nextCell); |
|
253 } |
227 return cells; |
254 return cells; |
228 } |
255 } |
229 |
256 |
230 public Set<Cell> findCells(CellContent content) { |
257 public Set<Cell> findCells(CellContent content) { |
231 Set<Cell> finded = new HashSet<Cell>(); |
258 Set<Cell> finded = new HashSet<Cell>(); |