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@46: public class Mine implements TextRepresentable, Cloneable {
indvdum@6:
indvdum@25: protected Cell[][] cells;
indvdum@25: protected int sizeX;
indvdum@25: protected int sizeY;
indvdum@30: protected int waterLevel = 0;
indvdum@30: protected int flooding = 0;
indvdum@30: protected int robotWaterproof = 10;
indvdum@30: protected int growth = 25;
indvdum@30: protected int razorsCount = 0;
indvdum@30: protected int lambdasAndHighOrderRocksStartCount = 0;
indvdum@31: protected String metadata = "";
indvdum@30: protected static Pattern minePattern = Pattern
indvdum@30: .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
indvdum@30: protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
indvdum@30: protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
indvdum@30: protected static Pattern waterproofPattern = Pattern
indvdum@30: .compile("(?s).*(?<=.*Waterproof )(\\d+).*");
indvdum@30: protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
indvdum@30: protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
indvdum@30: protected static Pattern trampolinePattern = Pattern
indvdum@30: .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
indvdum@30: protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])";
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@30: lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
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@27: text += "\n\n";
indvdum@31: text += metadata;
indvdum@27: text = text.replaceAll("\n*$", "");
indvdum@25: return text;
indvdum@6: }
indvdum@6:
indvdum@25: protected void fromText(String str) {
indvdum@26: Matcher matcher = minePattern.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@26:
indvdum@31: metadata = matcher.group(3);
indvdum@27: if (metadata != null) {
indvdum@27: // Water flooding
indvdum@27: String sWaterLevel = "";
indvdum@27: String sFlooding = "";
indvdum@27: String sRobotWaterproof = "";
indvdum@27: Matcher waterMatcher = waterPattern.matcher(metadata);
indvdum@27: if (waterMatcher.matches()) {
indvdum@27: sWaterLevel = waterMatcher.group(1);
indvdum@27: }
indvdum@27: Matcher floodingMatcher = floodingPattern.matcher(metadata);
indvdum@27: if (floodingMatcher.matches()) {
indvdum@27: sFlooding = floodingMatcher.group(1);
indvdum@27: }
indvdum@27: Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
indvdum@27: if (waterproofMatcher.matches()) {
indvdum@27: sRobotWaterproof = waterproofMatcher.group(1);
indvdum@27: }
indvdum@27: if (sWaterLevel.length() > 0)
indvdum@27: waterLevel = Integer.parseInt(sWaterLevel);
indvdum@27: if (sFlooding.length() > 0)
indvdum@27: flooding = Integer.parseInt(sFlooding);
indvdum@27: if (sRobotWaterproof.length() > 0)
indvdum@27: robotWaterproof = Integer.parseInt(sRobotWaterproof);
indvdum@27:
indvdum@27: // Trampolines and targets
indvdum@27: String trampolines = metadata;
indvdum@27: while (true) {
indvdum@27: Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
indvdum@27: if (trampolineMatcher.matches()) {
indvdum@27: String sTrampoline = trampolineMatcher.group(1);
indvdum@27: String sTarget = trampolineMatcher.group(2);
indvdum@27: CellContent trampoline = CellContent.fromText(sTrampoline);
indvdum@27: CellContent target = CellContent.fromText(sTarget);
indvdum@27: if (trampoline != null && target != null)
indvdum@27: trampoline.setTrampolineTarget(target);
indvdum@27: } else
indvdum@27: break;
indvdum@27: trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
indvdum@27: }
indvdum@29:
indvdum@29: // Growth and razorsCount
indvdum@29: String sGrowth = "";
indvdum@29: String sRazors = "";
indvdum@29: Matcher growthMatcher = growthPattern.matcher(metadata);
indvdum@29: if (growthMatcher.matches()) {
indvdum@29: sGrowth = growthMatcher.group(1);
indvdum@29: }
indvdum@29: Matcher razorsMatcher = razorsPattern.matcher(metadata);
indvdum@29: if (razorsMatcher.matches()) {
indvdum@29: sRazors = razorsMatcher.group(1);
indvdum@29: }
indvdum@29: if (sGrowth.length() > 0)
indvdum@29: growth = Integer.parseInt(sGrowth);
indvdum@29: if (sRazors.length() > 0)
indvdum@29: razorsCount = Integer.parseInt(sRazors);
indvdum@31: } else
indvdum@31: metadata = "";
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@40: public Set getAdjacentCells(Cell cell) {
indvdum@40: Set cells = new HashSet();
indvdum@40: for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates())
indvdum@40: cells.add(getCell(coordinate));
indvdum@40: return cells;
indvdum@40: }
indvdum@40:
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@29: public int getGrowth() {
indvdum@29: return growth;
indvdum@29: }
indvdum@29:
indvdum@29: public int getRazorsCount() {
indvdum@29: return razorsCount;
indvdum@29: }
indvdum@29:
indvdum@29: public void setRazorsCount(int razorsCount) {
indvdum@29: this.razorsCount = razorsCount;
indvdum@29: }
indvdum@29:
indvdum@30: public int getLambdasAndHighOrderRocksStartCount() {
indvdum@30: return lambdasAndHighOrderRocksStartCount;
indvdum@30: }
indvdum@30:
indvdum@43: @Override
indvdum@43: public boolean equals(Object obj) {
indvdum@43: if (obj instanceof Mine) {
indvdum@43: Mine otherMine = (Mine) obj;
indvdum@43: return toText().equals(otherMine.toText());
indvdum@43: }
indvdum@43: return super.equals(obj);
indvdum@43: }
indvdum@43:
indvdum@45: @Override
indvdum@45: public int hashCode() {
indvdum@45: return toText().hashCode();
indvdum@45: }
indvdum@45:
indvdum@46: @Override
indvdum@46: public Mine clone() throws CloneNotSupportedException {
indvdum@46: return new Mine(toText());
indvdum@46: }
indvdum@46:
indvdum@6: }
| | | | | | | | | |