icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 14 Jul 2012 23:21:08 +0400
branchindvdum
changeset 9 336dcbecb53c
parent 8 ad884dc6877c
child 15 fd89eea0ea15
permissions -rw-r--r--
Some refactoring
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@7
    16
import ru.bosony.model.moving.Movement;
indvdum@6
    17
indvdum@7
    18
/**
indvdum@7
    19
 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
indvdum@7
    20
 *         14.07.2012 2:02:18
indvdum@7
    21
 * 
indvdum@7
    22
 */
indvdum@6
    23
public class Mine implements TextRepresentable {
indvdum@6
    24
indvdum@9
    25
	protected Cell[][]	cells;
indvdum@7
    26
	protected int		sizeX;
indvdum@7
    27
	protected int		sizeY;
indvdum@7
    28
	protected Cell		robotCell;
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@9
    38
		for (Cell cell : findCells(MiningRobot))
indvdum@9
    39
			robotCell = cell;
indvdum@6
    40
	}
indvdum@6
    41
indvdum@6
    42
	@Override
indvdum@6
    43
	public String toText() {
indvdum@6
    44
		String text = "";
indvdum@6
    45
		for (int y = 0; y < sizeY; y++) {
indvdum@6
    46
			for (int x = 0; x < sizeX; x++) {
indvdum@6
    47
				text += cells[x][y].getContent().toText();
indvdum@6
    48
			}
indvdum@6
    49
			text += "\n";
indvdum@6
    50
		}
indvdum@6
    51
		return text.replaceAll("\n*$", "");
indvdum@6
    52
	}
indvdum@6
    53
indvdum@9
    54
	protected void fromText(String text) {
indvdum@6
    55
		String[] rows = text.split("\n+");
indvdum@6
    56
		int y = rows.length;
indvdum@6
    57
		int x = 0;
indvdum@6
    58
		for (String row : rows) {
indvdum@6
    59
			if (x < row.length())
indvdum@6
    60
				x = row.length();
indvdum@6
    61
		}
indvdum@6
    62
		sizeX = x;
indvdum@6
    63
		sizeY = y;
indvdum@6
    64
		cells = new Cell[x][y];
indvdum@6
    65
		for (int curY = 0; curY < rows.length; curY++) {
indvdum@6
    66
			String row = rows[curY];
indvdum@6
    67
			for (int curX = 0; curX < row.length() || curX < x; curX++) {
indvdum@6
    68
				CellContent content = null;
indvdum@6
    69
				if (curX < row.length())
indvdum@6
    70
					content = CellContent.fromText("" + row.charAt(curX));
indvdum@6
    71
				else
indvdum@8
    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@7
   104
		Set<Cell> nCells = getNeighboringCells(robotCell);
indvdum@7
   105
		for (Cell nCell : nCells) {
indvdum@7
   106
			CellContent nContent = nCell.getContent();
indvdum@7
   107
			Coordinate nCoordinate = nCell.getCoordinate();
indvdum@7
   108
			Cell rightNCell = getCell(nCoordinate.right());
indvdum@7
   109
			Cell leftNCell = getCell(nCoordinate.left());
indvdum@8
   110
			if (nContent == Empty
indvdum@8
   111
					|| nContent == Earth
indvdum@8
   112
					|| nContent == Lambda
indvdum@8
   113
					|| nContent == OpenLambdaLift
indvdum@8
   114
					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
indvdum@8
   115
							.getContent() == Empty)
indvdum@8
   116
					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
indvdum@8
   117
							.getContent() == Empty)) {
indvdum@7
   118
				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
indvdum@7
   119
				movs.add(mov);
indvdum@7
   120
			}
indvdum@7
   121
		}
indvdum@7
   122
		movs.add(Movement.ABORT);
indvdum@7
   123
		movs.add(Movement.WAIT);
indvdum@7
   124
		return movs;
indvdum@7
   125
	}
indvdum@7
   126
indvdum@7
   127
	public Set<Cell> getNeighboringCells(Cell cell) {
indvdum@7
   128
		Set<Cell> cells = new HashSet<Cell>();
indvdum@7
   129
		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
indvdum@7
   130
			cells.add(getCell(coordinate));
indvdum@7
   131
		return cells;
indvdum@7
   132
	}
indvdum@7
   133
indvdum@9
   134
	public Set<Cell> findCells(CellContent content) {
indvdum@9
   135
		Set<Cell> finded = new HashSet<Cell>();
indvdum@8
   136
		for (int y = sizeY - 1; y >= 0; y--) {
indvdum@8
   137
			for (int x = 0; x < sizeX - 1; x++) {
indvdum@8
   138
				if (cells[x][y].getContent() == content)
indvdum@8
   139
					finded.add(cells[x][y]);
indvdum@8
   140
			}
indvdum@8
   141
		}
indvdum@8
   142
		return finded;
indvdum@8
   143
	}
indvdum@8
   144
indvdum@9
   145
	public Cell[][] getCells() {
indvdum@9
   146
		return cells;
indvdum@9
   147
	}
indvdum@9
   148
indvdum@9
   149
	public void setCells(Cell[][] cells) {
indvdum@9
   150
		this.cells = cells;
indvdum@9
   151
	}
indvdum@9
   152
indvdum@9
   153
	public Cell getRobotCell() {
indvdum@9
   154
		return robotCell;
indvdum@9
   155
	}
indvdum@9
   156
indvdum@9
   157
	public void setRobotCell(Cell robotCell) {
indvdum@9
   158
		this.robotCell = robotCell;
indvdum@8
   159
	}
indvdum@8
   160
indvdum@6
   161
}