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