diff -r 0dbbc8983926 -r 251961e44b5f icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 00:36:43 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 02:07:51 2012 +0400 @@ -1,13 +1,23 @@ package ru.bosony.model.mine; +import java.util.HashSet; +import java.util.Set; + import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.io.TextRepresentable; +import ru.bosony.model.moving.Movement; +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 2:02:18 + * + */ public class Mine implements TextRepresentable { - public Cell[][] cells; - protected int sizeX; - protected int sizeY; + private Cell[][] cells; + protected int sizeX; + protected int sizeY; + protected Cell robotCell; public Mine(String text) { fromText(text); @@ -44,15 +54,65 @@ content = CellContent.fromText("" + row.charAt(curX)); else content = CellContent.Empty; - Cell cell = new Cell(curX + 1, rows.length - curY, content); + Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); cells[curX][curY] = cell; + if (content == CellContent.MiningRobot) + robotCell = cell; } } } + public Cell getCell(Coordinate coordinate) { + int x = coordinate.x; + int y = coordinate.y; + if (x < 1 || x > sizeX || y < 1 || y > sizeY) + return null; + return cells[x - 1][sizeY - y]; + } + @Override public String toString() { return toText(); } + public int getSizeX() { + return sizeX; + } + + public int getSizeY() { + return sizeY; + } + + public Set getAvailableRobotMovements() { + Set movs = new HashSet(); + Set nCells = getNeighboringCells(robotCell); + for (Cell nCell : nCells) { + CellContent nContent = nCell.getContent(); + Coordinate nCoordinate = nCell.getCoordinate(); + Cell rightNCell = getCell(nCoordinate.right()); + Cell leftNCell = getCell(nCoordinate.left()); + if (nContent == CellContent.Empty + || nContent == CellContent.Earth + || nContent == CellContent.Lambda + || nContent == CellContent.OpenLambdaLift + || (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate) + && rightNCell != null && rightNCell.getContent() == CellContent.Empty) + || (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate) + && leftNCell != null && leftNCell.getContent() == CellContent.Empty)) { + Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); + movs.add(mov); + } + } + movs.add(Movement.ABORT); + movs.add(Movement.WAIT); + return movs; + } + + public Set getNeighboringCells(Cell cell) { + Set cells = new HashSet(); + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) + cells.add(getCell(coordinate)); + return cells; + } + }