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.*)|$)");
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+).*");
40 public Mine(String text) {
42 if (findCells(OpenLambdaLift).size() != 0)
43 throw new RuntimeException("Detected OpenLambdaLift in new map!");
44 if (findCells(MiningRobot).size() != 1)
45 throw new RuntimeException("New map does not contain a robot!");
46 if (findCells(ClosedLambdaLift).size() != 1)
47 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
51 public String toText() {
53 for (int y = 0; y < sizeY; y++) {
54 for (int x = 0; x < sizeX; x++) {
55 text += cells[x][y].getContent().toText();
59 text = text.replaceAll("\n*$", "");
60 if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
62 text += "Water " + waterLevel + "\n";
63 text += "Flooding " + flooding + "\n";
64 text += "Waterproof " + robotWaterproof;
69 protected void fromText(String str) {
70 Matcher matcher = minePattern.matcher(str);
71 if (!matcher.matches())
72 throw new RuntimeException("Can't parse mine from string");
73 String body = matcher.group(1);
74 String[] rows = body.split("\n+");
77 for (String row : rows) {
83 cells = new Cell[x][y];
84 for (int curY = 0; curY < rows.length; curY++) {
85 String row = rows[curY];
86 for (int curX = 0; curX < row.length() || curX < x; curX++) {
87 CellContent content = null;
88 if (curX < row.length())
89 content = CellContent.fromText("" + row.charAt(curX));
94 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
95 cells[curX][curY] = cell;
99 String metadata = matcher.group(3);
100 String sWaterLevel = "";
101 String sFlooding = "";
102 String sRobotWaterproof = "";
103 Matcher waterMatcher = waterPattern.matcher(metadata);
104 if (waterMatcher.matches()) {
105 sWaterLevel = waterMatcher.group(1);
107 Matcher floodingMatcher = floodingPattern.matcher(metadata);
108 if (floodingMatcher.matches()) {
109 sFlooding = floodingMatcher.group(1);
111 Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
112 if (waterproofMatcher.matches()) {
113 sRobotWaterproof = waterproofMatcher.group(1);
115 if (sWaterLevel.length() > 0)
116 waterLevel = Integer.parseInt(sWaterLevel);
117 if (sFlooding.length() > 0)
118 flooding = Integer.parseInt(sFlooding);
119 if (sRobotWaterproof.length() > 0)
120 robotWaterproof = Integer.parseInt(sRobotWaterproof);
123 public Cell getCell(Coordinate coordinate) {
124 if (coordinate == null)
126 int x = coordinate.x;
127 int y = coordinate.y;
128 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
130 return cells[x - 1][sizeY - y];
134 public String toString() {
138 public int getSizeX() {
142 public int getSizeY() {
146 public Set<Movement> getAvailableRobotMovements() {
147 Set<Movement> movs = new HashSet<Movement>();
148 Cell robotCell = getRobotCell();
149 Set<Cell> nCells = getNeighboringCells(robotCell);
150 for (Cell nCell : nCells) {
151 CellContent nContent = nCell.getContent();
152 Coordinate nCoordinate = nCell.getCoordinate();
153 Cell rightNCell = getCell(nCoordinate.right());
154 Cell leftNCell = getCell(nCoordinate.left());
155 if (nContent == Empty
157 || nContent == Lambda
158 || nContent == OpenLambdaLift
159 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
160 .getContent() == Empty)
161 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
162 .getContent() == Empty)) {
163 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
167 movs.add(Movement.ABORT);
168 movs.add(Movement.WAIT);
172 public Set<Cell> getNeighboringCells(Cell cell) {
173 Set<Cell> cells = new HashSet<Cell>();
174 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
175 cells.add(getCell(coordinate));
179 public Set<Cell> findCells(CellContent content) {
180 Set<Cell> finded = new HashSet<Cell>();
181 for (int y = sizeY - 1; y >= 0; y--) {
182 for (int x = 0; x < sizeX; x++) {
183 if (cells[x][y].getContent() == content)
184 finded.add(cells[x][y]);
190 public void setCells(Cell[][] cells) {
194 public Cell getRobotCell() {
195 for (Cell cell : findCells(MiningRobot)) {
198 throw new RuntimeException("Where is robot!?");
201 public Cell[][] cloneCells() {
202 Cell[][] clone = new Cell[sizeX][sizeY];
203 for (int y = sizeY - 1; y >= 0; y--) {
204 for (int x = 0; x < sizeX; x++) {
206 clone[x][y] = cells[x][y].clone();
207 } catch (CloneNotSupportedException e) {
208 throw new RuntimeException(e);
215 public int getWaterLevel() {
219 public void setWaterLevel(int waterLevel) {
220 this.waterLevel = waterLevel;
223 public int getFlooding() {
227 public int getRobotWaterproof() {
228 return robotWaterproof;