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