icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 14 Jul 2012 02:07:51 +0400
branchindvdum
changeset 7 251961e44b5f
parent 6 0dbbc8983926
child 8 ad884dc6877c
permissions -rw-r--r--
Movements and Coordinates
     1 package ru.bosony.model.mine;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 import ru.bosony.model.cellscontents.CellContent;
     7 import ru.bosony.model.io.TextRepresentable;
     8 import ru.bosony.model.moving.Movement;
     9 
    10 /**
    11  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    12  *         14.07.2012 2:02:18
    13  * 
    14  */
    15 public class Mine implements TextRepresentable {
    16 
    17 	private Cell[][]	cells;
    18 	protected int		sizeX;
    19 	protected int		sizeY;
    20 	protected Cell		robotCell;
    21 
    22 	public Mine(String text) {
    23 		fromText(text);
    24 	}
    25 
    26 	@Override
    27 	public String toText() {
    28 		String text = "";
    29 		for (int y = 0; y < sizeY; y++) {
    30 			for (int x = 0; x < sizeX; x++) {
    31 				text += cells[x][y].getContent().toText();
    32 			}
    33 			text += "\n";
    34 		}
    35 		return text.replaceAll("\n*$", "");
    36 	}
    37 
    38 	private void fromText(String text) {
    39 		String[] rows = text.split("\n+");
    40 		int y = rows.length;
    41 		int x = 0;
    42 		for (String row : rows) {
    43 			if (x < row.length())
    44 				x = row.length();
    45 		}
    46 		sizeX = x;
    47 		sizeY = y;
    48 		cells = new Cell[x][y];
    49 		for (int curY = 0; curY < rows.length; curY++) {
    50 			String row = rows[curY];
    51 			for (int curX = 0; curX < row.length() || curX < x; curX++) {
    52 				CellContent content = null;
    53 				if (curX < row.length())
    54 					content = CellContent.fromText("" + row.charAt(curX));
    55 				else
    56 					content = CellContent.Empty;
    57 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    58 				cells[curX][curY] = cell;
    59 				if (content == CellContent.MiningRobot)
    60 					robotCell = cell;
    61 			}
    62 		}
    63 	}
    64 
    65 	public Cell getCell(Coordinate coordinate) {
    66 		int x = coordinate.x;
    67 		int y = coordinate.y;
    68 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
    69 			return null;
    70 		return cells[x - 1][sizeY - y];
    71 	}
    72 
    73 	@Override
    74 	public String toString() {
    75 		return toText();
    76 	}
    77 
    78 	public int getSizeX() {
    79 		return sizeX;
    80 	}
    81 
    82 	public int getSizeY() {
    83 		return sizeY;
    84 	}
    85 
    86 	public Set<Movement> getAvailableRobotMovements() {
    87 		Set<Movement> movs = new HashSet<Movement>();
    88 		Set<Cell> nCells = getNeighboringCells(robotCell);
    89 		for (Cell nCell : nCells) {
    90 			CellContent nContent = nCell.getContent();
    91 			Coordinate nCoordinate = nCell.getCoordinate();
    92 			Cell rightNCell = getCell(nCoordinate.right());
    93 			Cell leftNCell = getCell(nCoordinate.left());
    94 			if (nContent == CellContent.Empty
    95 					|| nContent == CellContent.Earth
    96 					|| nContent == CellContent.Lambda
    97 					|| nContent == CellContent.OpenLambdaLift
    98 					|| (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate)
    99 							&& rightNCell != null && rightNCell.getContent() == CellContent.Empty)
   100 					|| (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate)
   101 							&& leftNCell != null && leftNCell.getContent() == CellContent.Empty)) {
   102 				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
   103 				movs.add(mov);
   104 			}
   105 		}
   106 		movs.add(Movement.ABORT);
   107 		movs.add(Movement.WAIT);
   108 		return movs;
   109 	}
   110 
   111 	public Set<Cell> getNeighboringCells(Cell cell) {
   112 		Set<Cell> cells = new HashSet<Cell>();
   113 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
   114 			cells.add(getCell(coordinate));
   115 		return cells;
   116 	}
   117 
   118 }