indvdum@6: package ru.bosony.model.mine; indvdum@6: indvdum@9: import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift; indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Earth; indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Empty; indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Lambda; indvdum@9: import static ru.bosony.model.cellscontents.CellContent.MiningRobot; indvdum@9: import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Rock; indvdum@9: indvdum@7: import java.util.HashSet; indvdum@7: import java.util.Set; indvdum@25: import java.util.regex.Matcher; indvdum@25: import java.util.regex.Pattern; indvdum@7: indvdum@6: import ru.bosony.model.cellscontents.CellContent; indvdum@6: import ru.bosony.model.io.TextRepresentable; indvdum@22: import ru.bosony.model.moving.Coordinate; 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@25: protected Cell[][] cells; indvdum@25: protected int sizeX; indvdum@25: protected int sizeY; indvdum@25: protected int waterLevel = 0; indvdum@25: protected int flooding = 0; indvdum@25: protected int robotWaterproof = 10; indvdum@25: protected static Pattern pattern = Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)"); indvdum@6: indvdum@6: public Mine(String text) { indvdum@6: fromText(text); indvdum@8: if (findCells(OpenLambdaLift).size() != 0) indvdum@8: throw new RuntimeException("Detected OpenLambdaLift in new map!"); indvdum@8: if (findCells(MiningRobot).size() != 1) indvdum@8: throw new RuntimeException("New map does not contain a robot!"); indvdum@8: if (findCells(ClosedLambdaLift).size() != 1) indvdum@8: throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); 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@25: text = text.replaceAll("\n*$", ""); indvdum@25: if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) { indvdum@25: text += "\n\n"; indvdum@25: text += "Water " + waterLevel + "\n"; indvdum@25: text += "Flooding " + flooding + "\n"; indvdum@25: text += "Waterproof " + robotWaterproof; indvdum@25: } indvdum@25: return text; indvdum@6: } indvdum@6: indvdum@25: protected void fromText(String str) { indvdum@25: Matcher matcher = pattern.matcher(str); indvdum@25: if (!matcher.matches()) indvdum@25: throw new RuntimeException("Can't parse mine from string"); indvdum@25: String body = matcher.group(1); indvdum@25: String[] rows = body.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@8: content = Empty; indvdum@22: if (content == null) indvdum@22: content = Empty; indvdum@7: Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); indvdum@6: cells[curX][curY] = cell; indvdum@6: } indvdum@6: } indvdum@25: indvdum@25: String sWaterLevel = matcher.group(4); indvdum@25: String sFlooding = matcher.group(6); indvdum@25: String sRobotWaterproof = matcher.group(8); indvdum@25: if (sWaterLevel.length() > 0) indvdum@25: waterLevel = Integer.parseInt(sWaterLevel); indvdum@25: if (sFlooding.length() > 0) indvdum@25: flooding = Integer.parseInt(sFlooding); indvdum@25: if (sRobotWaterproof.length() > 0) indvdum@25: robotWaterproof = Integer.parseInt(sRobotWaterproof); indvdum@6: } indvdum@6: indvdum@7: public Cell getCell(Coordinate coordinate) { indvdum@8: if (coordinate == null) indvdum@8: return null; 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@22: Cell robotCell = getRobotCell(); 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@8: if (nContent == Empty indvdum@8: || nContent == Earth indvdum@8: || nContent == Lambda indvdum@8: || nContent == OpenLambdaLift indvdum@8: || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell indvdum@8: .getContent() == Empty) indvdum@8: || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell indvdum@8: .getContent() == 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@9: public Set findCells(CellContent content) { indvdum@9: Set finded = new HashSet(); indvdum@8: for (int y = sizeY - 1; y >= 0; y--) { indvdum@15: for (int x = 0; x < sizeX; x++) { indvdum@8: if (cells[x][y].getContent() == content) indvdum@8: finded.add(cells[x][y]); indvdum@8: } indvdum@8: } indvdum@8: return finded; indvdum@8: } indvdum@8: indvdum@9: public void setCells(Cell[][] cells) { indvdum@9: this.cells = cells; indvdum@9: } indvdum@9: indvdum@9: public Cell getRobotCell() { indvdum@22: for (Cell cell : findCells(MiningRobot)) { indvdum@22: return cell; indvdum@22: } indvdum@22: throw new RuntimeException("Where is robot!?"); indvdum@9: } indvdum@9: indvdum@22: public Cell[][] cloneCells() { indvdum@22: Cell[][] clone = new Cell[sizeX][sizeY]; indvdum@22: for (int y = sizeY - 1; y >= 0; y--) { indvdum@22: for (int x = 0; x < sizeX; x++) { indvdum@22: try { indvdum@22: clone[x][y] = cells[x][y].clone(); indvdum@22: } catch (CloneNotSupportedException e) { indvdum@22: throw new RuntimeException(e); indvdum@22: } indvdum@22: } indvdum@22: } indvdum@22: return clone; indvdum@8: } indvdum@8: indvdum@25: public int getWaterLevel() { indvdum@25: return waterLevel; indvdum@25: } indvdum@25: indvdum@25: public void setWaterLevel(int waterLevel) { indvdum@25: this.waterLevel = waterLevel; indvdum@25: } indvdum@25: indvdum@25: public int getFlooding() { indvdum@25: return flooding; indvdum@25: } indvdum@25: indvdum@25: public int getRobotWaterproof() { indvdum@25: return robotWaterproof; indvdum@25: } indvdum@25: indvdum@6: }