1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 23:27:11 2012 +0400
1.3 @@ -0,0 +1,161 @@
1.4 +package ru.bosony.model.mine;
1.5 +
1.6 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
1.7 +import static ru.bosony.model.cellscontents.CellContent.Earth;
1.8 +import static ru.bosony.model.cellscontents.CellContent.Empty;
1.9 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
1.10 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
1.11 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
1.12 +import static ru.bosony.model.cellscontents.CellContent.Rock;
1.13 +
1.14 +import java.util.HashSet;
1.15 +import java.util.Set;
1.16 +
1.17 +import ru.bosony.model.cellscontents.CellContent;
1.18 +import ru.bosony.model.io.TextRepresentable;
1.19 +import ru.bosony.model.moving.Movement;
1.20 +
1.21 +/**
1.22 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
1.23 + * 14.07.2012 2:02:18
1.24 + *
1.25 + */
1.26 +public class Mine implements TextRepresentable {
1.27 +
1.28 + protected Cell[][] cells;
1.29 + protected int sizeX;
1.30 + protected int sizeY;
1.31 + protected Cell robotCell;
1.32 +
1.33 + public Mine(String text) {
1.34 + fromText(text);
1.35 + if (findCells(OpenLambdaLift).size() != 0)
1.36 + throw new RuntimeException("Detected OpenLambdaLift in new map!");
1.37 + if (findCells(MiningRobot).size() != 1)
1.38 + throw new RuntimeException("New map does not contain a robot!");
1.39 + if (findCells(ClosedLambdaLift).size() != 1)
1.40 + throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
1.41 + for (Cell cell : findCells(MiningRobot))
1.42 + robotCell = cell;
1.43 + }
1.44 +
1.45 + @Override
1.46 + public String toText() {
1.47 + String text = "";
1.48 + for (int y = 0; y < sizeY; y++) {
1.49 + for (int x = 0; x < sizeX; x++) {
1.50 + text += cells[x][y].getContent().toText();
1.51 + }
1.52 + text += "\n";
1.53 + }
1.54 + return text.replaceAll("\n*$", "");
1.55 + }
1.56 +
1.57 + protected void fromText(String text) {
1.58 + String[] rows = text.split("\n+");
1.59 + int y = rows.length;
1.60 + int x = 0;
1.61 + for (String row : rows) {
1.62 + if (x < row.length())
1.63 + x = row.length();
1.64 + }
1.65 + sizeX = x;
1.66 + sizeY = y;
1.67 + cells = new Cell[x][y];
1.68 + for (int curY = 0; curY < rows.length; curY++) {
1.69 + String row = rows[curY];
1.70 + for (int curX = 0; curX < row.length() || curX < x; curX++) {
1.71 + CellContent content = null;
1.72 + if (curX < row.length())
1.73 + content = CellContent.fromText("" + row.charAt(curX));
1.74 + else
1.75 + content = Empty;
1.76 + Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
1.77 + cells[curX][curY] = cell;
1.78 + }
1.79 + }
1.80 + }
1.81 +
1.82 + public Cell getCell(Coordinate coordinate) {
1.83 + if (coordinate == null)
1.84 + return null;
1.85 + int x = coordinate.x;
1.86 + int y = coordinate.y;
1.87 + if (x < 1 || x > sizeX || y < 1 || y > sizeY)
1.88 + return null;
1.89 + return cells[x - 1][sizeY - y];
1.90 + }
1.91 +
1.92 + @Override
1.93 + public String toString() {
1.94 + return toText();
1.95 + }
1.96 +
1.97 + public int getSizeX() {
1.98 + return sizeX;
1.99 + }
1.100 +
1.101 + public int getSizeY() {
1.102 + return sizeY;
1.103 + }
1.104 +
1.105 + public Set<Movement> getAvailableRobotMovements() {
1.106 + Set<Movement> movs = new HashSet<Movement>();
1.107 + Set<Cell> nCells = getNeighboringCells(robotCell);
1.108 + for (Cell nCell : nCells) {
1.109 + CellContent nContent = nCell.getContent();
1.110 + Coordinate nCoordinate = nCell.getCoordinate();
1.111 + Cell rightNCell = getCell(nCoordinate.right());
1.112 + Cell leftNCell = getCell(nCoordinate.left());
1.113 + if (nContent == Empty
1.114 + || nContent == Earth
1.115 + || nContent == Lambda
1.116 + || nContent == OpenLambdaLift
1.117 + || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
1.118 + .getContent() == Empty)
1.119 + || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
1.120 + .getContent() == Empty)) {
1.121 + Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
1.122 + movs.add(mov);
1.123 + }
1.124 + }
1.125 + movs.add(Movement.ABORT);
1.126 + movs.add(Movement.WAIT);
1.127 + return movs;
1.128 + }
1.129 +
1.130 + public Set<Cell> getNeighboringCells(Cell cell) {
1.131 + Set<Cell> cells = new HashSet<Cell>();
1.132 + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
1.133 + cells.add(getCell(coordinate));
1.134 + return cells;
1.135 + }
1.136 +
1.137 + public Set<Cell> findCells(CellContent content) {
1.138 + Set<Cell> finded = new HashSet<Cell>();
1.139 + for (int y = sizeY - 1; y >= 0; y--) {
1.140 + for (int x = 0; x < sizeX - 1; x++) {
1.141 + if (cells[x][y].getContent() == content)
1.142 + finded.add(cells[x][y]);
1.143 + }
1.144 + }
1.145 + return finded;
1.146 + }
1.147 +
1.148 + public Cell[][] getCells() {
1.149 + return cells;
1.150 + }
1.151 +
1.152 + public void setCells(Cell[][] cells) {
1.153 + this.cells = cells;
1.154 + }
1.155 +
1.156 + public Cell getRobotCell() {
1.157 + return robotCell;
1.158 + }
1.159 +
1.160 + public void setRobotCell(Cell robotCell) {
1.161 + this.robotCell = robotCell;
1.162 + }
1.163 +
1.164 +}