icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 17:58:43 +0400
changeset 25 d6a75f29ba9b
parent 22 a46d0179df7a
child 26 964aa8102804
permissions -rw-r--r--
Add water flooding
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@25
    31
	protected int				waterLevel		= 0;
indvdum@25
    32
	protected int				flooding		= 0;
indvdum@25
    33
	protected int				robotWaterproof	= 10;
indvdum@25
    34
	protected static Pattern	pattern			= Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)");
indvdum@6
    35
indvdum@6
    36
	public Mine(String text) {
indvdum@6
    37
		fromText(text);
indvdum@8
    38
		if (findCells(OpenLambdaLift).size() != 0)
indvdum@8
    39
			throw new RuntimeException("Detected OpenLambdaLift in new map!");
indvdum@8
    40
		if (findCells(MiningRobot).size() != 1)
indvdum@8
    41
			throw new RuntimeException("New map does not contain a robot!");
indvdum@8
    42
		if (findCells(ClosedLambdaLift).size() != 1)
indvdum@8
    43
			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
indvdum@6
    44
	}
indvdum@6
    45
indvdum@6
    46
	@Override
indvdum@6
    47
	public String toText() {
indvdum@6
    48
		String text = "";
indvdum@6
    49
		for (int y = 0; y < sizeY; y++) {
indvdum@6
    50
			for (int x = 0; x < sizeX; x++) {
indvdum@6
    51
				text += cells[x][y].getContent().toText();
indvdum@6
    52
			}
indvdum@6
    53
			text += "\n";
indvdum@6
    54
		}
indvdum@25
    55
		text = text.replaceAll("\n*$", "");
indvdum@25
    56
		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
indvdum@25
    57
			text += "\n\n";
indvdum@25
    58
			text += "Water " + waterLevel + "\n";
indvdum@25
    59
			text += "Flooding " + flooding + "\n";
indvdum@25
    60
			text += "Waterproof " + robotWaterproof;
indvdum@25
    61
		}
indvdum@25
    62
		return text;
indvdum@6
    63
	}
indvdum@6
    64
indvdum@25
    65
	protected void fromText(String str) {
indvdum@25
    66
		Matcher matcher = pattern.matcher(str);
indvdum@25
    67
		if (!matcher.matches())
indvdum@25
    68
			throw new RuntimeException("Can't parse mine from string");
indvdum@25
    69
		String body = matcher.group(1);
indvdum@25
    70
		String[] rows = body.split("\n+");
indvdum@6
    71
		int y = rows.length;
indvdum@6
    72
		int x = 0;
indvdum@6
    73
		for (String row : rows) {
indvdum@6
    74
			if (x < row.length())
indvdum@6
    75
				x = row.length();
indvdum@6
    76
		}
indvdum@6
    77
		sizeX = x;
indvdum@6
    78
		sizeY = y;
indvdum@6
    79
		cells = new Cell[x][y];
indvdum@6
    80
		for (int curY = 0; curY < rows.length; curY++) {
indvdum@6
    81
			String row = rows[curY];
indvdum@6
    82
			for (int curX = 0; curX < row.length() || curX < x; curX++) {
indvdum@6
    83
				CellContent content = null;
indvdum@6
    84
				if (curX < row.length())
indvdum@6
    85
					content = CellContent.fromText("" + row.charAt(curX));
indvdum@6
    86
				else
indvdum@8
    87
					content = Empty;
indvdum@22
    88
				if (content == null)
indvdum@22
    89
					content = Empty;
indvdum@7
    90
				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
indvdum@6
    91
				cells[curX][curY] = cell;
indvdum@6
    92
			}
indvdum@6
    93
		}
indvdum@25
    94
		
indvdum@25
    95
		String sWaterLevel = matcher.group(4);
indvdum@25
    96
		String sFlooding = matcher.group(6);
indvdum@25
    97
		String sRobotWaterproof = matcher.group(8);
indvdum@25
    98
		if (sWaterLevel.length() > 0)
indvdum@25
    99
			waterLevel = Integer.parseInt(sWaterLevel);
indvdum@25
   100
		if (sFlooding.length() > 0)
indvdum@25
   101
			flooding = Integer.parseInt(sFlooding);
indvdum@25
   102
		if (sRobotWaterproof.length() > 0)
indvdum@25
   103
			robotWaterproof = Integer.parseInt(sRobotWaterproof);
indvdum@6
   104
	}
indvdum@6
   105
indvdum@7
   106
	public Cell getCell(Coordinate coordinate) {
indvdum@8
   107
		if (coordinate == null)
indvdum@8
   108
			return null;
indvdum@7
   109
		int x = coordinate.x;
indvdum@7
   110
		int y = coordinate.y;
indvdum@7
   111
		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
indvdum@7
   112
			return null;
indvdum@7
   113
		return cells[x - 1][sizeY - y];
indvdum@7
   114
	}
indvdum@7
   115
indvdum@6
   116
	@Override
indvdum@6
   117
	public String toString() {
indvdum@6
   118
		return toText();
indvdum@6
   119
	}
indvdum@6
   120
indvdum@7
   121
	public int getSizeX() {
indvdum@7
   122
		return sizeX;
indvdum@7
   123
	}
indvdum@7
   124
indvdum@7
   125
	public int getSizeY() {
indvdum@7
   126
		return sizeY;
indvdum@7
   127
	}
indvdum@7
   128
indvdum@7
   129
	public Set<Movement> getAvailableRobotMovements() {
indvdum@7
   130
		Set<Movement> movs = new HashSet<Movement>();
indvdum@22
   131
		Cell robotCell = getRobotCell();
indvdum@7
   132
		Set<Cell> nCells = getNeighboringCells(robotCell);
indvdum@7
   133
		for (Cell nCell : nCells) {
indvdum@7
   134
			CellContent nContent = nCell.getContent();
indvdum@7
   135
			Coordinate nCoordinate = nCell.getCoordinate();
indvdum@7
   136
			Cell rightNCell = getCell(nCoordinate.right());
indvdum@7
   137
			Cell leftNCell = getCell(nCoordinate.left());
indvdum@8
   138
			if (nContent == Empty
indvdum@8
   139
					|| nContent == Earth
indvdum@8
   140
					|| nContent == Lambda
indvdum@8
   141
					|| nContent == OpenLambdaLift
indvdum@8
   142
					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
indvdum@8
   143
							.getContent() == Empty)
indvdum@8
   144
					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
indvdum@8
   145
							.getContent() == Empty)) {
indvdum@7
   146
				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
indvdum@7
   147
				movs.add(mov);
indvdum@7
   148
			}
indvdum@7
   149
		}
indvdum@7
   150
		movs.add(Movement.ABORT);
indvdum@7
   151
		movs.add(Movement.WAIT);
indvdum@7
   152
		return movs;
indvdum@7
   153
	}
indvdum@7
   154
indvdum@7
   155
	public Set<Cell> getNeighboringCells(Cell cell) {
indvdum@7
   156
		Set<Cell> cells = new HashSet<Cell>();
indvdum@7
   157
		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
indvdum@7
   158
			cells.add(getCell(coordinate));
indvdum@7
   159
		return cells;
indvdum@7
   160
	}
indvdum@7
   161
indvdum@9
   162
	public Set<Cell> findCells(CellContent content) {
indvdum@9
   163
		Set<Cell> finded = new HashSet<Cell>();
indvdum@8
   164
		for (int y = sizeY - 1; y >= 0; y--) {
indvdum@15
   165
			for (int x = 0; x < sizeX; x++) {
indvdum@8
   166
				if (cells[x][y].getContent() == content)
indvdum@8
   167
					finded.add(cells[x][y]);
indvdum@8
   168
			}
indvdum@8
   169
		}
indvdum@8
   170
		return finded;
indvdum@8
   171
	}
indvdum@8
   172
indvdum@9
   173
	public void setCells(Cell[][] cells) {
indvdum@9
   174
		this.cells = cells;
indvdum@9
   175
	}
indvdum@9
   176
indvdum@9
   177
	public Cell getRobotCell() {
indvdum@22
   178
		for (Cell cell : findCells(MiningRobot)) {
indvdum@22
   179
			return cell;
indvdum@22
   180
		}
indvdum@22
   181
		throw new RuntimeException("Where is robot!?");
indvdum@9
   182
	}
indvdum@9
   183
indvdum@22
   184
	public Cell[][] cloneCells() {
indvdum@22
   185
		Cell[][] clone = new Cell[sizeX][sizeY];
indvdum@22
   186
		for (int y = sizeY - 1; y >= 0; y--) {
indvdum@22
   187
			for (int x = 0; x < sizeX; x++) {
indvdum@22
   188
				try {
indvdum@22
   189
					clone[x][y] = cells[x][y].clone();
indvdum@22
   190
				} catch (CloneNotSupportedException e) {
indvdum@22
   191
					throw new RuntimeException(e);
indvdum@22
   192
				}
indvdum@22
   193
			}
indvdum@22
   194
		}
indvdum@22
   195
		return clone;
indvdum@8
   196
	}
indvdum@8
   197
indvdum@25
   198
	public int getWaterLevel() {
indvdum@25
   199
		return waterLevel;
indvdum@25
   200
	}
indvdum@25
   201
indvdum@25
   202
	public void setWaterLevel(int waterLevel) {
indvdum@25
   203
		this.waterLevel = waterLevel;
indvdum@25
   204
	}
indvdum@25
   205
indvdum@25
   206
	public int getFlooding() {
indvdum@25
   207
		return flooding;
indvdum@25
   208
	}
indvdum@25
   209
indvdum@25
   210
	public int getRobotWaterproof() {
indvdum@25
   211
		return robotWaterproof;
indvdum@25
   212
	}
indvdum@25
   213
indvdum@6
   214
}