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