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