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