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