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;
14 import ru.bosony.model.cellscontents.CellContent;
15 import ru.bosony.model.io.TextRepresentable;
16 import ru.bosony.model.moving.Coordinate;
17 import ru.bosony.model.moving.Movement;
20 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
24 public class Mine implements TextRepresentable {
26 protected Cell[][] cells;
30 public Mine(String text) {
32 if (findCells(OpenLambdaLift).size() != 0)
33 throw new RuntimeException("Detected OpenLambdaLift in new map!");
34 if (findCells(MiningRobot).size() != 1)
35 throw new RuntimeException("New map does not contain a robot!");
36 if (findCells(ClosedLambdaLift).size() != 1)
37 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
41 public String toText() {
43 for (int y = 0; y < sizeY; y++) {
44 for (int x = 0; x < sizeX; x++) {
45 text += cells[x][y].getContent().toText();
49 return text.replaceAll("\n*$", "");
52 protected void fromText(String text) {
53 String[] rows = text.split("\n+");
56 for (String row : rows) {
62 cells = new Cell[x][y];
63 for (int curY = 0; curY < rows.length; curY++) {
64 String row = rows[curY];
65 for (int curX = 0; curX < row.length() || curX < x; curX++) {
66 CellContent content = null;
67 if (curX < row.length())
68 content = CellContent.fromText("" + row.charAt(curX));
73 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
74 cells[curX][curY] = cell;
79 public Cell getCell(Coordinate coordinate) {
80 if (coordinate == null)
84 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
86 return cells[x - 1][sizeY - y];
90 public String toString() {
94 public int getSizeX() {
98 public int getSizeY() {
102 public Set<Movement> getAvailableRobotMovements() {
103 Set<Movement> movs = new HashSet<Movement>();
104 Cell robotCell = getRobotCell();
105 Set<Cell> nCells = getNeighboringCells(robotCell);
106 for (Cell nCell : nCells) {
107 CellContent nContent = nCell.getContent();
108 Coordinate nCoordinate = nCell.getCoordinate();
109 Cell rightNCell = getCell(nCoordinate.right());
110 Cell leftNCell = getCell(nCoordinate.left());
111 if (nContent == Empty
113 || nContent == Lambda
114 || nContent == OpenLambdaLift
115 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
116 .getContent() == Empty)
117 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
118 .getContent() == Empty)) {
119 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
123 movs.add(Movement.ABORT);
124 movs.add(Movement.WAIT);
128 public Set<Cell> getNeighboringCells(Cell cell) {
129 Set<Cell> cells = new HashSet<Cell>();
130 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
131 cells.add(getCell(coordinate));
135 public Set<Cell> findCells(CellContent content) {
136 Set<Cell> finded = new HashSet<Cell>();
137 for (int y = sizeY - 1; y >= 0; y--) {
138 for (int x = 0; x < sizeX; x++) {
139 if (cells[x][y].getContent() == content)
140 finded.add(cells[x][y]);
146 public void setCells(Cell[][] cells) {
150 public Cell getRobotCell() {
151 for (Cell cell : findCells(MiningRobot)) {
154 throw new RuntimeException("Where is robot!?");
157 public Cell[][] cloneCells() {
158 Cell[][] clone = new Cell[sizeX][sizeY];
159 for (int y = sizeY - 1; y >= 0; y--) {
160 for (int x = 0; x < sizeX; x++) {
162 clone[x][y] = cells[x][y].clone();
163 } catch (CloneNotSupportedException e) {
164 throw new RuntimeException(e);