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