indvdum@6: package ru.bosony.model.mine; indvdum@6: indvdum@7: import java.util.HashSet; indvdum@7: import java.util.Set; indvdum@7: indvdum@6: import ru.bosony.model.cellscontents.CellContent; indvdum@6: import ru.bosony.model.io.TextRepresentable; indvdum@7: import ru.bosony.model.moving.Movement; indvdum@6: indvdum@7: /** indvdum@7: * @author indvdum (gotoindvdum[at]gmail[dot]com)
indvdum@7: * 14.07.2012 2:02:18 indvdum@7: * indvdum@7: */ indvdum@6: public class Mine implements TextRepresentable { indvdum@6: indvdum@7: private Cell[][] cells; indvdum@7: protected int sizeX; indvdum@7: protected int sizeY; indvdum@7: protected Cell robotCell; indvdum@6: indvdum@6: public Mine(String text) { indvdum@6: fromText(text); indvdum@6: } indvdum@6: indvdum@6: @Override indvdum@6: public String toText() { indvdum@6: String text = ""; indvdum@6: for (int y = 0; y < sizeY; y++) { indvdum@6: for (int x = 0; x < sizeX; x++) { indvdum@6: text += cells[x][y].getContent().toText(); indvdum@6: } indvdum@6: text += "\n"; indvdum@6: } indvdum@6: return text.replaceAll("\n*$", ""); indvdum@6: } indvdum@6: indvdum@6: private void fromText(String text) { indvdum@6: String[] rows = text.split("\n+"); indvdum@6: int y = rows.length; indvdum@6: int x = 0; indvdum@6: for (String row : rows) { indvdum@6: if (x < row.length()) indvdum@6: x = row.length(); indvdum@6: } indvdum@6: sizeX = x; indvdum@6: sizeY = y; indvdum@6: cells = new Cell[x][y]; indvdum@6: for (int curY = 0; curY < rows.length; curY++) { indvdum@6: String row = rows[curY]; indvdum@6: for (int curX = 0; curX < row.length() || curX < x; curX++) { indvdum@6: CellContent content = null; indvdum@6: if (curX < row.length()) indvdum@6: content = CellContent.fromText("" + row.charAt(curX)); indvdum@6: else indvdum@6: content = CellContent.Empty; indvdum@7: Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); indvdum@6: cells[curX][curY] = cell; indvdum@7: if (content == CellContent.MiningRobot) indvdum@7: robotCell = cell; indvdum@6: } indvdum@6: } indvdum@6: } indvdum@6: indvdum@7: public Cell getCell(Coordinate coordinate) { indvdum@7: int x = coordinate.x; indvdum@7: int y = coordinate.y; indvdum@7: if (x < 1 || x > sizeX || y < 1 || y > sizeY) indvdum@7: return null; indvdum@7: return cells[x - 1][sizeY - y]; indvdum@7: } indvdum@7: indvdum@6: @Override indvdum@6: public String toString() { indvdum@6: return toText(); indvdum@6: } indvdum@6: indvdum@7: public int getSizeX() { indvdum@7: return sizeX; indvdum@7: } indvdum@7: indvdum@7: public int getSizeY() { indvdum@7: return sizeY; indvdum@7: } indvdum@7: indvdum@7: public Set getAvailableRobotMovements() { indvdum@7: Set movs = new HashSet(); indvdum@7: Set nCells = getNeighboringCells(robotCell); indvdum@7: for (Cell nCell : nCells) { indvdum@7: CellContent nContent = nCell.getContent(); indvdum@7: Coordinate nCoordinate = nCell.getCoordinate(); indvdum@7: Cell rightNCell = getCell(nCoordinate.right()); indvdum@7: Cell leftNCell = getCell(nCoordinate.left()); indvdum@7: if (nContent == CellContent.Empty indvdum@7: || nContent == CellContent.Earth indvdum@7: || nContent == CellContent.Lambda indvdum@7: || nContent == CellContent.OpenLambdaLift indvdum@7: || (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate) indvdum@7: && rightNCell != null && rightNCell.getContent() == CellContent.Empty) indvdum@7: || (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate) indvdum@7: && leftNCell != null && leftNCell.getContent() == CellContent.Empty)) { indvdum@7: Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); indvdum@7: movs.add(mov); indvdum@7: } indvdum@7: } indvdum@7: movs.add(Movement.ABORT); indvdum@7: movs.add(Movement.WAIT); indvdum@7: return movs; indvdum@7: } indvdum@7: indvdum@7: public Set getNeighboringCells(Cell cell) { indvdum@7: Set cells = new HashSet(); indvdum@7: for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) indvdum@7: cells.add(getCell(coordinate)); indvdum@7: return cells; indvdum@7: } indvdum@7: indvdum@6: }