1 package ru.bosony.model.mine;
3 import java.util.HashSet;
6 import ru.bosony.model.cellscontents.CellContent;
7 import ru.bosony.model.io.TextRepresentable;
8 import ru.bosony.model.moving.Movement;
11 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
15 public class Mine implements TextRepresentable {
17 private Cell[][] cells;
20 protected Cell robotCell;
22 public Mine(String text) {
27 public String toText() {
29 for (int y = 0; y < sizeY; y++) {
30 for (int x = 0; x < sizeX; x++) {
31 text += cells[x][y].getContent().toText();
35 return text.replaceAll("\n*$", "");
38 private void fromText(String text) {
39 String[] rows = text.split("\n+");
42 for (String row : rows) {
48 cells = new Cell[x][y];
49 for (int curY = 0; curY < rows.length; curY++) {
50 String row = rows[curY];
51 for (int curX = 0; curX < row.length() || curX < x; curX++) {
52 CellContent content = null;
53 if (curX < row.length())
54 content = CellContent.fromText("" + row.charAt(curX));
56 content = CellContent.Empty;
57 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
58 cells[curX][curY] = cell;
59 if (content == CellContent.MiningRobot)
65 public Cell getCell(Coordinate coordinate) {
68 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
70 return cells[x - 1][sizeY - y];
74 public String toString() {
78 public int getSizeX() {
82 public int getSizeY() {
86 public Set<Movement> getAvailableRobotMovements() {
87 Set<Movement> movs = new HashSet<Movement>();
88 Set<Cell> nCells = getNeighboringCells(robotCell);
89 for (Cell nCell : nCells) {
90 CellContent nContent = nCell.getContent();
91 Coordinate nCoordinate = nCell.getCoordinate();
92 Cell rightNCell = getCell(nCoordinate.right());
93 Cell leftNCell = getCell(nCoordinate.left());
94 if (nContent == CellContent.Empty
95 || nContent == CellContent.Earth
96 || nContent == CellContent.Lambda
97 || nContent == CellContent.OpenLambdaLift
98 || (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate)
99 && rightNCell != null && rightNCell.getContent() == CellContent.Empty)
100 || (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate)
101 && leftNCell != null && leftNCell.getContent() == CellContent.Empty)) {
102 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
106 movs.add(Movement.ABORT);
107 movs.add(Movement.WAIT);
111 public Set<Cell> getNeighboringCells(Cell cell) {
112 Set<Cell> cells = new HashSet<Cell>();
113 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
114 cells.add(getCell(coordinate));