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