icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 19:26:28 +0400
changeset 26 964aa8102804
parent 25 d6a75f29ba9b
child 27 505c25412b16
permissions -rw-r--r--
Flooding parse fix
     1 package ru.bosony.model.mine;
     2 
     3 import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
     4 import static ru.bosony.model.cellscontents.CellContent.Earth;
     5 import static ru.bosony.model.cellscontents.CellContent.Empty;
     6 import static ru.bosony.model.cellscontents.CellContent.Lambda;
     7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
     8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     9 import static ru.bosony.model.cellscontents.CellContent.Rock;
    10 
    11 import java.util.HashSet;
    12 import java.util.Set;
    13 import java.util.regex.Matcher;
    14 import java.util.regex.Pattern;
    15 
    16 import ru.bosony.model.cellscontents.CellContent;
    17 import ru.bosony.model.io.TextRepresentable;
    18 import ru.bosony.model.moving.Coordinate;
    19 import ru.bosony.model.moving.Movement;
    20 
    21 /**
    22  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    23  *         14.07.2012 2:02:18
    24  * 
    25  */
    26 public class Mine implements TextRepresentable {
    27 
    28 	protected Cell[][]			cells;
    29 	protected int				sizeX;
    30 	protected int				sizeY;
    31 	protected int				waterLevel			= 0;
    32 	protected int				flooding			= 0;
    33 	protected int				robotWaterproof		= 10;
    34 	protected static Pattern	minePattern			= Pattern
    35 															.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)");
    36 	protected static Pattern	waterPattern		= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    37 	protected static Pattern	floodingPattern		= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    38 	protected static Pattern	waterproofPattern	= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    39 
    40 	public Mine(String text) {
    41 		fromText(text);
    42 		if (findCells(OpenLambdaLift).size() != 0)
    43 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    44 		if (findCells(MiningRobot).size() != 1)
    45 			throw new RuntimeException("New map does not contain a robot!");
    46 		if (findCells(ClosedLambdaLift).size() != 1)
    47 			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    48 	}
    49 
    50 	@Override
    51 	public String toText() {
    52 		String text = "";
    53 		for (int y = 0; y < sizeY; y++) {
    54 			for (int x = 0; x < sizeX; x++) {
    55 				text += cells[x][y].getContent().toText();
    56 			}
    57 			text += "\n";
    58 		}
    59 		text = text.replaceAll("\n*$", "");
    60 		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    61 			text += "\n\n";
    62 			text += "Water " + waterLevel + "\n";
    63 			text += "Flooding " + flooding + "\n";
    64 			text += "Waterproof " + robotWaterproof;
    65 		}
    66 		return text;
    67 	}
    68 
    69 	protected void fromText(String str) {
    70 		Matcher matcher = minePattern.matcher(str);
    71 		if (!matcher.matches())
    72 			throw new RuntimeException("Can't parse mine from string");
    73 		String body = matcher.group(1);
    74 		String[] rows = body.split("\n+");
    75 		int y = rows.length;
    76 		int x = 0;
    77 		for (String row : rows) {
    78 			if (x < row.length())
    79 				x = row.length();
    80 		}
    81 		sizeX = x;
    82 		sizeY = y;
    83 		cells = new Cell[x][y];
    84 		for (int curY = 0; curY < rows.length; curY++) {
    85 			String row = rows[curY];
    86 			for (int curX = 0; curX < row.length() || curX < x; curX++) {
    87 				CellContent content = null;
    88 				if (curX < row.length())
    89 					content = CellContent.fromText("" + row.charAt(curX));
    90 				else
    91 					content = Empty;
    92 				if (content == null)
    93 					content = Empty;
    94 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    95 				cells[curX][curY] = cell;
    96 			}
    97 		}
    98 
    99 		String metadata = matcher.group(3);
   100 		String sWaterLevel = "";
   101 		String sFlooding = "";
   102 		String sRobotWaterproof = "";
   103 		Matcher waterMatcher = waterPattern.matcher(metadata);
   104 		if (waterMatcher.matches()) {
   105 			sWaterLevel = waterMatcher.group(1);
   106 		}
   107 		Matcher floodingMatcher = floodingPattern.matcher(metadata);
   108 		if (floodingMatcher.matches()) {
   109 			sFlooding = floodingMatcher.group(1);
   110 		}
   111 		Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
   112 		if (waterproofMatcher.matches()) {
   113 			sRobotWaterproof = waterproofMatcher.group(1);
   114 		}
   115 		if (sWaterLevel.length() > 0)
   116 			waterLevel = Integer.parseInt(sWaterLevel);
   117 		if (sFlooding.length() > 0)
   118 			flooding = Integer.parseInt(sFlooding);
   119 		if (sRobotWaterproof.length() > 0)
   120 			robotWaterproof = Integer.parseInt(sRobotWaterproof);
   121 	}
   122 
   123 	public Cell getCell(Coordinate coordinate) {
   124 		if (coordinate == null)
   125 			return null;
   126 		int x = coordinate.x;
   127 		int y = coordinate.y;
   128 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
   129 			return null;
   130 		return cells[x - 1][sizeY - y];
   131 	}
   132 
   133 	@Override
   134 	public String toString() {
   135 		return toText();
   136 	}
   137 
   138 	public int getSizeX() {
   139 		return sizeX;
   140 	}
   141 
   142 	public int getSizeY() {
   143 		return sizeY;
   144 	}
   145 
   146 	public Set<Movement> getAvailableRobotMovements() {
   147 		Set<Movement> movs = new HashSet<Movement>();
   148 		Cell robotCell = getRobotCell();
   149 		Set<Cell> nCells = getNeighboringCells(robotCell);
   150 		for (Cell nCell : nCells) {
   151 			CellContent nContent = nCell.getContent();
   152 			Coordinate nCoordinate = nCell.getCoordinate();
   153 			Cell rightNCell = getCell(nCoordinate.right());
   154 			Cell leftNCell = getCell(nCoordinate.left());
   155 			if (nContent == Empty
   156 					|| nContent == Earth
   157 					|| nContent == Lambda
   158 					|| nContent == OpenLambdaLift
   159 					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
   160 							.getContent() == Empty)
   161 					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
   162 							.getContent() == Empty)) {
   163 				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
   164 				movs.add(mov);
   165 			}
   166 		}
   167 		movs.add(Movement.ABORT);
   168 		movs.add(Movement.WAIT);
   169 		return movs;
   170 	}
   171 
   172 	public Set<Cell> getNeighboringCells(Cell cell) {
   173 		Set<Cell> cells = new HashSet<Cell>();
   174 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
   175 			cells.add(getCell(coordinate));
   176 		return cells;
   177 	}
   178 
   179 	public Set<Cell> findCells(CellContent content) {
   180 		Set<Cell> finded = new HashSet<Cell>();
   181 		for (int y = sizeY - 1; y >= 0; y--) {
   182 			for (int x = 0; x < sizeX; x++) {
   183 				if (cells[x][y].getContent() == content)
   184 					finded.add(cells[x][y]);
   185 			}
   186 		}
   187 		return finded;
   188 	}
   189 
   190 	public void setCells(Cell[][] cells) {
   191 		this.cells = cells;
   192 	}
   193 
   194 	public Cell getRobotCell() {
   195 		for (Cell cell : findCells(MiningRobot)) {
   196 			return cell;
   197 		}
   198 		throw new RuntimeException("Where is robot!?");
   199 	}
   200 
   201 	public Cell[][] cloneCells() {
   202 		Cell[][] clone = new Cell[sizeX][sizeY];
   203 		for (int y = sizeY - 1; y >= 0; y--) {
   204 			for (int x = 0; x < sizeX; x++) {
   205 				try {
   206 					clone[x][y] = cells[x][y].clone();
   207 				} catch (CloneNotSupportedException e) {
   208 					throw new RuntimeException(e);
   209 				}
   210 			}
   211 		}
   212 		return clone;
   213 	}
   214 
   215 	public int getWaterLevel() {
   216 		return waterLevel;
   217 	}
   218 
   219 	public void setWaterLevel(int waterLevel) {
   220 		this.waterLevel = waterLevel;
   221 	}
   222 
   223 	public int getFlooding() {
   224 		return flooding;
   225 	}
   226 
   227 	public int getRobotWaterproof() {
   228 		return robotWaterproof;
   229 	}
   230 
   231 }