icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 04:09:14 +0400
changeset 22 a46d0179df7a
parent 15 fd89eea0ea15
child 25 d6a75f29ba9b
permissions -rw-r--r--
Fixes according to FAQ
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@7
    13
indvdum@6
    14
import ru.bosony.model.cellscontents.CellContent;
indvdum@6
    15
import ru.bosony.model.io.TextRepresentable;
indvdum@22
    16
import ru.bosony.model.moving.Coordinate;
indvdum@7
    17
import ru.bosony.model.moving.Movement;
indvdum@6
    18
indvdum@7
    19
/**
indvdum@7
    20
 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
indvdum@7
    21
 *         14.07.2012 2:02:18
indvdum@7
    22
 * 
indvdum@7
    23
 */
indvdum@6
    24
public class Mine implements TextRepresentable {
indvdum@6
    25
indvdum@9
    26
	protected Cell[][]	cells;
indvdum@7
    27
	protected int		sizeX;
indvdum@7
    28
	protected int		sizeY;
indvdum@6
    29
indvdum@6
    30
	public Mine(String text) {
indvdum@6
    31
		fromText(text);
indvdum@8
    32
		if (findCells(OpenLambdaLift).size() != 0)
indvdum@8
    33
			throw new RuntimeException("Detected OpenLambdaLift in new map!");
indvdum@8
    34
		if (findCells(MiningRobot).size() != 1)
indvdum@8
    35
			throw new RuntimeException("New map does not contain a robot!");
indvdum@8
    36
		if (findCells(ClosedLambdaLift).size() != 1)
indvdum@8
    37
			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
indvdum@6
    38
	}
indvdum@6
    39
indvdum@6
    40
	@Override
indvdum@6
    41
	public String toText() {
indvdum@6
    42
		String text = "";
indvdum@6
    43
		for (int y = 0; y < sizeY; y++) {
indvdum@6
    44
			for (int x = 0; x < sizeX; x++) {
indvdum@6
    45
				text += cells[x][y].getContent().toText();
indvdum@6
    46
			}
indvdum@6
    47
			text += "\n";
indvdum@6
    48
		}
indvdum@6
    49
		return text.replaceAll("\n*$", "");
indvdum@6
    50
	}
indvdum@6
    51
indvdum@9
    52
	protected void fromText(String text) {
indvdum@6
    53
		String[] rows = text.split("\n+");
indvdum@6
    54
		int y = rows.length;
indvdum@6
    55
		int x = 0;
indvdum@6
    56
		for (String row : rows) {
indvdum@6
    57
			if (x < row.length())
indvdum@6
    58
				x = row.length();
indvdum@6
    59
		}
indvdum@6
    60
		sizeX = x;
indvdum@6
    61
		sizeY = y;
indvdum@6
    62
		cells = new Cell[x][y];
indvdum@6
    63
		for (int curY = 0; curY < rows.length; curY++) {
indvdum@6
    64
			String row = rows[curY];
indvdum@6
    65
			for (int curX = 0; curX < row.length() || curX < x; curX++) {
indvdum@6
    66
				CellContent content = null;
indvdum@6
    67
				if (curX < row.length())
indvdum@6
    68
					content = CellContent.fromText("" + row.charAt(curX));
indvdum@6
    69
				else
indvdum@8
    70
					content = Empty;
indvdum@22
    71
				if (content == null)
indvdum@22
    72
					content = Empty;
indvdum@7
    73
				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
indvdum@6
    74
				cells[curX][curY] = cell;
indvdum@6
    75
			}
indvdum@6
    76
		}
indvdum@6
    77
	}
indvdum@6
    78
indvdum@7
    79
	public Cell getCell(Coordinate coordinate) {
indvdum@8
    80
		if (coordinate == null)
indvdum@8
    81
			return null;
indvdum@7
    82
		int x = coordinate.x;
indvdum@7
    83
		int y = coordinate.y;
indvdum@7
    84
		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
indvdum@7
    85
			return null;
indvdum@7
    86
		return cells[x - 1][sizeY - y];
indvdum@7
    87
	}
indvdum@7
    88
indvdum@6
    89
	@Override
indvdum@6
    90
	public String toString() {
indvdum@6
    91
		return toText();
indvdum@6
    92
	}
indvdum@6
    93
indvdum@7
    94
	public int getSizeX() {
indvdum@7
    95
		return sizeX;
indvdum@7
    96
	}
indvdum@7
    97
indvdum@7
    98
	public int getSizeY() {
indvdum@7
    99
		return sizeY;
indvdum@7
   100
	}
indvdum@7
   101
indvdum@7
   102
	public Set<Movement> getAvailableRobotMovements() {
indvdum@7
   103
		Set<Movement> movs = new HashSet<Movement>();
indvdum@22
   104
		Cell robotCell = getRobotCell();
indvdum@7
   105
		Set<Cell> nCells = getNeighboringCells(robotCell);
indvdum@7
   106
		for (Cell nCell : nCells) {
indvdum@7
   107
			CellContent nContent = nCell.getContent();
indvdum@7
   108
			Coordinate nCoordinate = nCell.getCoordinate();
indvdum@7
   109
			Cell rightNCell = getCell(nCoordinate.right());
indvdum@7
   110
			Cell leftNCell = getCell(nCoordinate.left());
indvdum@8
   111
			if (nContent == Empty
indvdum@8
   112
					|| nContent == Earth
indvdum@8
   113
					|| nContent == Lambda
indvdum@8
   114
					|| nContent == OpenLambdaLift
indvdum@8
   115
					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
indvdum@8
   116
							.getContent() == Empty)
indvdum@8
   117
					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
indvdum@8
   118
							.getContent() == Empty)) {
indvdum@7
   119
				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
indvdum@7
   120
				movs.add(mov);
indvdum@7
   121
			}
indvdum@7
   122
		}
indvdum@7
   123
		movs.add(Movement.ABORT);
indvdum@7
   124
		movs.add(Movement.WAIT);
indvdum@7
   125
		return movs;
indvdum@7
   126
	}
indvdum@7
   127
indvdum@7
   128
	public Set<Cell> getNeighboringCells(Cell cell) {
indvdum@7
   129
		Set<Cell> cells = new HashSet<Cell>();
indvdum@7
   130
		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
indvdum@7
   131
			cells.add(getCell(coordinate));
indvdum@7
   132
		return cells;
indvdum@7
   133
	}
indvdum@7
   134
indvdum@9
   135
	public Set<Cell> findCells(CellContent content) {
indvdum@9
   136
		Set<Cell> finded = new HashSet<Cell>();
indvdum@8
   137
		for (int y = sizeY - 1; y >= 0; y--) {
indvdum@15
   138
			for (int x = 0; x < sizeX; x++) {
indvdum@8
   139
				if (cells[x][y].getContent() == content)
indvdum@8
   140
					finded.add(cells[x][y]);
indvdum@8
   141
			}
indvdum@8
   142
		}
indvdum@8
   143
		return finded;
indvdum@8
   144
	}
indvdum@8
   145
indvdum@9
   146
	public void setCells(Cell[][] cells) {
indvdum@9
   147
		this.cells = cells;
indvdum@9
   148
	}
indvdum@9
   149
indvdum@9
   150
	public Cell getRobotCell() {
indvdum@22
   151
		for (Cell cell : findCells(MiningRobot)) {
indvdum@22
   152
			return cell;
indvdum@22
   153
		}
indvdum@22
   154
		throw new RuntimeException("Where is robot!?");
indvdum@9
   155
	}
indvdum@9
   156
indvdum@22
   157
	public Cell[][] cloneCells() {
indvdum@22
   158
		Cell[][] clone = new Cell[sizeX][sizeY];
indvdum@22
   159
		for (int y = sizeY - 1; y >= 0; y--) {
indvdum@22
   160
			for (int x = 0; x < sizeX; x++) {
indvdum@22
   161
				try {
indvdum@22
   162
					clone[x][y] = cells[x][y].clone();
indvdum@22
   163
				} catch (CloneNotSupportedException e) {
indvdum@22
   164
					throw new RuntimeException(e);
indvdum@22
   165
				}
indvdum@22
   166
			}
indvdum@22
   167
		}
indvdum@22
   168
		return clone;
indvdum@8
   169
	}
indvdum@8
   170
indvdum@6
   171
}