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 pattern = Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)");
36 public Mine(String text) {
38 if (findCells(OpenLambdaLift).size() != 0)
39 throw new RuntimeException("Detected OpenLambdaLift in new map!");
40 if (findCells(MiningRobot).size() != 1)
41 throw new RuntimeException("New map does not contain a robot!");
42 if (findCells(ClosedLambdaLift).size() != 1)
43 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
47 public String toText() {
49 for (int y = 0; y < sizeY; y++) {
50 for (int x = 0; x < sizeX; x++) {
51 text += cells[x][y].getContent().toText();
55 text = text.replaceAll("\n*$", "");
56 if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
58 text += "Water " + waterLevel + "\n";
59 text += "Flooding " + flooding + "\n";
60 text += "Waterproof " + robotWaterproof;
65 protected void fromText(String str) {
66 Matcher matcher = pattern.matcher(str);
67 if (!matcher.matches())
68 throw new RuntimeException("Can't parse mine from string");
69 String body = matcher.group(1);
70 String[] rows = body.split("\n+");
73 for (String row : rows) {
79 cells = new Cell[x][y];
80 for (int curY = 0; curY < rows.length; curY++) {
81 String row = rows[curY];
82 for (int curX = 0; curX < row.length() || curX < x; curX++) {
83 CellContent content = null;
84 if (curX < row.length())
85 content = CellContent.fromText("" + row.charAt(curX));
90 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
91 cells[curX][curY] = cell;
95 String sWaterLevel = matcher.group(4);
96 String sFlooding = matcher.group(6);
97 String sRobotWaterproof = matcher.group(8);
98 if (sWaterLevel.length() > 0)
99 waterLevel = Integer.parseInt(sWaterLevel);
100 if (sFlooding.length() > 0)
101 flooding = Integer.parseInt(sFlooding);
102 if (sRobotWaterproof.length() > 0)
103 robotWaterproof = Integer.parseInt(sRobotWaterproof);
106 public Cell getCell(Coordinate coordinate) {
107 if (coordinate == null)
109 int x = coordinate.x;
110 int y = coordinate.y;
111 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
113 return cells[x - 1][sizeY - y];
117 public String toString() {
121 public int getSizeX() {
125 public int getSizeY() {
129 public Set<Movement> getAvailableRobotMovements() {
130 Set<Movement> movs = new HashSet<Movement>();
131 Cell robotCell = getRobotCell();
132 Set<Cell> nCells = getNeighboringCells(robotCell);
133 for (Cell nCell : nCells) {
134 CellContent nContent = nCell.getContent();
135 Coordinate nCoordinate = nCell.getCoordinate();
136 Cell rightNCell = getCell(nCoordinate.right());
137 Cell leftNCell = getCell(nCoordinate.left());
138 if (nContent == Empty
140 || nContent == Lambda
141 || nContent == OpenLambdaLift
142 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
143 .getContent() == Empty)
144 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
145 .getContent() == Empty)) {
146 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
150 movs.add(Movement.ABORT);
151 movs.add(Movement.WAIT);
155 public Set<Cell> getNeighboringCells(Cell cell) {
156 Set<Cell> cells = new HashSet<Cell>();
157 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
158 cells.add(getCell(coordinate));
162 public Set<Cell> findCells(CellContent content) {
163 Set<Cell> finded = new HashSet<Cell>();
164 for (int y = sizeY - 1; y >= 0; y--) {
165 for (int x = 0; x < sizeX; x++) {
166 if (cells[x][y].getContent() == content)
167 finded.add(cells[x][y]);
173 public void setCells(Cell[][] cells) {
177 public Cell getRobotCell() {
178 for (Cell cell : findCells(MiningRobot)) {
181 throw new RuntimeException("Where is robot!?");
184 public Cell[][] cloneCells() {
185 Cell[][] clone = new Cell[sizeX][sizeY];
186 for (int y = sizeY - 1; y >= 0; y--) {
187 for (int x = 0; x < sizeX; x++) {
189 clone[x][y] = cells[x][y].clone();
190 } catch (CloneNotSupportedException e) {
191 throw new RuntimeException(e);
198 public int getWaterLevel() {
202 public void setWaterLevel(int waterLevel) {
203 this.waterLevel = waterLevel;
206 public int getFlooding() {
210 public int getRobotWaterproof() {
211 return robotWaterproof;