icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
branchindvdum
changeset 7 251961e44b5f
parent 6 0dbbc8983926
child 8 ad884dc6877c
equal deleted inserted replaced
6:0dbbc8983926 7:251961e44b5f
     1 package ru.bosony.model.mine;
     1 package ru.bosony.model.mine;
       
     2 
       
     3 import java.util.HashSet;
       
     4 import java.util.Set;
     2 
     5 
     3 import ru.bosony.model.cellscontents.CellContent;
     6 import ru.bosony.model.cellscontents.CellContent;
     4 import ru.bosony.model.io.TextRepresentable;
     7 import ru.bosony.model.io.TextRepresentable;
       
     8 import ru.bosony.model.moving.Movement;
     5 
     9 
       
    10 /**
       
    11  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
       
    12  *         14.07.2012 2:02:18
       
    13  * 
       
    14  */
     6 public class Mine implements TextRepresentable {
    15 public class Mine implements TextRepresentable {
     7 
    16 
     8 	public Cell[][]	cells;
    17 	private Cell[][]	cells;
     9 	protected int	sizeX;
    18 	protected int		sizeX;
    10 	protected int	sizeY;
    19 	protected int		sizeY;
       
    20 	protected Cell		robotCell;
    11 
    21 
    12 	public Mine(String text) {
    22 	public Mine(String text) {
    13 		fromText(text);
    23 		fromText(text);
    14 	}
    24 	}
    15 
    25 
    42 				CellContent content = null;
    52 				CellContent content = null;
    43 				if (curX < row.length())
    53 				if (curX < row.length())
    44 					content = CellContent.fromText("" + row.charAt(curX));
    54 					content = CellContent.fromText("" + row.charAt(curX));
    45 				else
    55 				else
    46 					content = CellContent.Empty;
    56 					content = CellContent.Empty;
    47 				Cell cell = new Cell(curX + 1, rows.length - curY, content);
    57 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    48 				cells[curX][curY] = cell;
    58 				cells[curX][curY] = cell;
       
    59 				if (content == CellContent.MiningRobot)
       
    60 					robotCell = cell;
    49 			}
    61 			}
    50 		}
    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];
    51 	}
    71 	}
    52 
    72 
    53 	@Override
    73 	@Override
    54 	public String toString() {
    74 	public String toString() {
    55 		return toText();
    75 		return toText();
    56 	}
    76 	}
    57 
    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 
    58 }
   118 }