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