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