icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Tue, 17 Jul 2012 21:38:39 +0400
branchCleverSearch
changeset 50 9530f55e9639
parent 46 4197f736166a
child 51 b431cec961d7
permissions -rw-r--r--
Moar
     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, Cloneable {
    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 int				growth								= 25;
    35 	protected int				razorsCount							= 0;
    36 	protected int				lambdasAndHighOrderRocksStartCount	= 0;
    37 	protected String			metadata							= "";
    38 	protected static Pattern	minePattern							= Pattern
    39 																			.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
    40 	protected static Pattern	waterPattern						= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    41 	protected static Pattern	floodingPattern						= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    42 	protected static Pattern	waterproofPattern					= Pattern
    43 																			.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    44 	protected static Pattern	growthPattern						= Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
    45 	protected static Pattern	razorsPattern						= Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
    46 	protected static Pattern	trampolinePattern					= Pattern
    47 																			.compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
    48 	protected static String		firstTrampolineRegexp				= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
    49 
    50 	public Mine(String text) {
    51 		this(text, null);
    52 		if (findCells(OpenLambdaLift).size() != 0)
    53 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    54 		if (findCells(MiningRobot).size() != 1)
    55 			throw new RuntimeException("New map does not contain a robot!");
    56 		if (findCells(ClosedLambdaLift).size() != 1)
    57 			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    58 	}
    59 
    60 	protected Mine(String text, Object obj) {
    61 		fromText(text);
    62 		lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
    63 	}
    64 
    65 	@Override
    66 	public String toText() {
    67 		String text = "";
    68 		for (int y = 0; y < sizeY; y++) {
    69 			for (int x = 0; x < sizeX; x++) {
    70 				text += cells[x][y].getContent().toText();
    71 			}
    72 			text += "\n";
    73 		}
    74 		text = text.replaceAll("\n*$", "");
    75 		text += "\n\n";
    76 		text += metadata;
    77 		text = text.replaceAll("\n*$", "");
    78 		return text;
    79 	}
    80 
    81 	protected void fromText(String str) {
    82 		Matcher matcher = minePattern.matcher(str);
    83 		if (!matcher.matches())
    84 			throw new RuntimeException("Can't parse mine from string");
    85 		String body = matcher.group(1);
    86 		String[] rows = body.split("\n+");
    87 		int y = rows.length;
    88 		int x = 0;
    89 		for (String row : rows) {
    90 			if (x < row.length())
    91 				x = row.length();
    92 		}
    93 		sizeX = x;
    94 		sizeY = y;
    95 		cells = new Cell[x][y];
    96 		for (int curY = 0; curY < rows.length; curY++) {
    97 			String row = rows[curY];
    98 			for (int curX = 0; curX < row.length() || curX < x; curX++) {
    99 				CellContent content = null;
   100 				if (curX < row.length())
   101 					content = CellContent.fromText("" + row.charAt(curX));
   102 				else
   103 					content = Empty;
   104 				if (content == null)
   105 					content = Empty;
   106 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
   107 				cells[curX][curY] = cell;
   108 			}
   109 		}
   110 
   111 		metadata = matcher.group(3);
   112 		if (metadata != null) {
   113 			// Water flooding
   114 			String sWaterLevel = "";
   115 			String sFlooding = "";
   116 			String sRobotWaterproof = "";
   117 			Matcher waterMatcher = waterPattern.matcher(metadata);
   118 			if (waterMatcher.matches()) {
   119 				sWaterLevel = waterMatcher.group(1);
   120 			}
   121 			Matcher floodingMatcher = floodingPattern.matcher(metadata);
   122 			if (floodingMatcher.matches()) {
   123 				sFlooding = floodingMatcher.group(1);
   124 			}
   125 			Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
   126 			if (waterproofMatcher.matches()) {
   127 				sRobotWaterproof = waterproofMatcher.group(1);
   128 			}
   129 			if (sWaterLevel.length() > 0)
   130 				waterLevel = Integer.parseInt(sWaterLevel);
   131 			if (sFlooding.length() > 0)
   132 				flooding = Integer.parseInt(sFlooding);
   133 			if (sRobotWaterproof.length() > 0)
   134 				robotWaterproof = Integer.parseInt(sRobotWaterproof);
   135 
   136 			// Trampolines and targets
   137 			String trampolines = metadata;
   138 			while (true) {
   139 				Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
   140 				if (trampolineMatcher.matches()) {
   141 					String sTrampoline = trampolineMatcher.group(1);
   142 					String sTarget = trampolineMatcher.group(2);
   143 					CellContent trampoline = CellContent.fromText(sTrampoline);
   144 					CellContent target = CellContent.fromText(sTarget);
   145 					if (trampoline != null && target != null)
   146 						trampoline.setTrampolineTarget(target);
   147 				} else
   148 					break;
   149 				trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
   150 			}
   151 
   152 			// Growth and razorsCount
   153 			String sGrowth = "";
   154 			String sRazors = "";
   155 			Matcher growthMatcher = growthPattern.matcher(metadata);
   156 			if (growthMatcher.matches()) {
   157 				sGrowth = growthMatcher.group(1);
   158 			}
   159 			Matcher razorsMatcher = razorsPattern.matcher(metadata);
   160 			if (razorsMatcher.matches()) {
   161 				sRazors = razorsMatcher.group(1);
   162 			}
   163 			if (sGrowth.length() > 0)
   164 				growth = Integer.parseInt(sGrowth);
   165 			if (sRazors.length() > 0)
   166 				razorsCount = Integer.parseInt(sRazors);
   167 		} else
   168 			metadata = "";
   169 	}
   170 
   171 	public Cell getCell(Coordinate coordinate) {
   172 		if (coordinate == null)
   173 			return null;
   174 		int x = coordinate.x;
   175 		int y = coordinate.y;
   176 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
   177 			return null;
   178 		return cells[x - 1][sizeY - y];
   179 	}
   180 
   181 	@Override
   182 	public String toString() {
   183 		return toText();
   184 	}
   185 
   186 	public int getSizeX() {
   187 		return sizeX;
   188 	}
   189 
   190 	public int getSizeY() {
   191 		return sizeY;
   192 	}
   193 
   194 	public Set<Movement> getAvailableRobotMovements() {
   195 		Set<Movement> movs = new HashSet<Movement>();
   196 		Cell robotCell = getRobotCell();
   197 		Set<Cell> nCells = getNeighboringCells(robotCell);
   198 		for (Cell nCell : nCells) {
   199 			CellContent nContent = nCell.getContent();
   200 			Coordinate nCoordinate = nCell.getCoordinate();
   201 			Cell rightNCell = getCell(nCoordinate.right());
   202 			Cell leftNCell = getCell(nCoordinate.left());
   203 			if (nContent == Empty
   204 					|| nContent == Earth
   205 					|| nContent == Lambda
   206 					|| nContent == OpenLambdaLift
   207 					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
   208 							.getContent() == Empty)
   209 					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
   210 							.getContent() == Empty)) {
   211 				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
   212 				movs.add(mov);
   213 			}
   214 		}
   215 		movs.add(Movement.ABORT);
   216 		movs.add(Movement.WAIT);
   217 		return movs;
   218 	}
   219 
   220 	public Set<Cell> getNeighboringCells(Cell cell) {
   221 		Set<Cell> cells = new HashSet<Cell>();
   222 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
   223 			cells.add(getCell(coordinate));
   224 		return cells;
   225 	}
   226 
   227 	public Set<Cell> getAdjacentCells(Cell cell) {
   228 		Set<Cell> cells = new HashSet<Cell>();
   229 		for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates())
   230 			cells.add(getCell(coordinate));
   231 		return cells;
   232 	}
   233 
   234 	public Set<Cell> findCells(CellContent content) {
   235 		Set<Cell> finded = new HashSet<Cell>();
   236 		for (int y = sizeY - 1; y >= 0; y--) {
   237 			for (int x = 0; x < sizeX; x++) {
   238 				if (cells[x][y].getContent() == content)
   239 					finded.add(cells[x][y]);
   240 			}
   241 		}
   242 		return finded;
   243 	}
   244 
   245 	public void setCells(Cell[][] cells) {
   246 		this.cells = cells;
   247 	}
   248 
   249 	public Cell getRobotCell() {
   250 		for (Cell cell : findCells(MiningRobot)) {
   251 			return cell;
   252 		}
   253 		throw new RuntimeException("Where is robot!?");
   254 	}
   255 
   256 	public Cell[][] cloneCells() {
   257 		Cell[][] clone = new Cell[sizeX][sizeY];
   258 		for (int y = sizeY - 1; y >= 0; y--) {
   259 			for (int x = 0; x < sizeX; x++) {
   260 				try {
   261 					clone[x][y] = cells[x][y].clone();
   262 				} catch (CloneNotSupportedException e) {
   263 					throw new RuntimeException(e);
   264 				}
   265 			}
   266 		}
   267 		return clone;
   268 	}
   269 
   270 	public int getWaterLevel() {
   271 		return waterLevel;
   272 	}
   273 
   274 	public void setWaterLevel(int waterLevel) {
   275 		this.waterLevel = waterLevel;
   276 	}
   277 
   278 	public int getFlooding() {
   279 		return flooding;
   280 	}
   281 
   282 	public int getRobotWaterproof() {
   283 		return robotWaterproof;
   284 	}
   285 
   286 	public int getGrowth() {
   287 		return growth;
   288 	}
   289 
   290 	public int getRazorsCount() {
   291 		return razorsCount;
   292 	}
   293 
   294 	public void setRazorsCount(int razorsCount) {
   295 		this.razorsCount = razorsCount;
   296 	}
   297 
   298 	public int getLambdasAndHighOrderRocksStartCount() {
   299 		return lambdasAndHighOrderRocksStartCount;
   300 	}
   301 
   302 	@Override
   303 	public boolean equals(Object obj) {
   304 		if (obj instanceof Mine) {
   305 			Mine otherMine = (Mine) obj;
   306 			return toText().equals(otherMine.toText());
   307 		}
   308 		return super.equals(obj);
   309 	}
   310 
   311 	@Override
   312 	public int hashCode() {
   313 		return toText().hashCode();
   314 	}
   315 
   316 	@Override
   317 	public Mine clone() {
   318 		return new Mine(toText(), null);
   319 	}
   320 
   321 }