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