icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
changeset 22 a46d0179df7a
parent 15 fd89eea0ea15
child 25 d6a75f29ba9b
equal deleted inserted replaced
21:fade486c8eda 22:a46d0179df7a
    11 import java.util.HashSet;
    11 import java.util.HashSet;
    12 import java.util.Set;
    12 import java.util.Set;
    13 
    13 
    14 import ru.bosony.model.cellscontents.CellContent;
    14 import ru.bosony.model.cellscontents.CellContent;
    15 import ru.bosony.model.io.TextRepresentable;
    15 import ru.bosony.model.io.TextRepresentable;
       
    16 import ru.bosony.model.moving.Coordinate;
    16 import ru.bosony.model.moving.Movement;
    17 import ru.bosony.model.moving.Movement;
    17 
    18 
    18 /**
    19 /**
    19  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    20  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    20  *         14.07.2012 2:02:18
    21  *         14.07.2012 2:02:18
    23 public class Mine implements TextRepresentable {
    24 public class Mine implements TextRepresentable {
    24 
    25 
    25 	protected Cell[][]	cells;
    26 	protected Cell[][]	cells;
    26 	protected int		sizeX;
    27 	protected int		sizeX;
    27 	protected int		sizeY;
    28 	protected int		sizeY;
    28 	protected Cell		robotCell;
       
    29 
    29 
    30 	public Mine(String text) {
    30 	public Mine(String text) {
    31 		fromText(text);
    31 		fromText(text);
    32 		if (findCells(OpenLambdaLift).size() != 0)
    32 		if (findCells(OpenLambdaLift).size() != 0)
    33 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    33 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    34 		if (findCells(MiningRobot).size() != 1)
    34 		if (findCells(MiningRobot).size() != 1)
    35 			throw new RuntimeException("New map does not contain a robot!");
    35 			throw new RuntimeException("New map does not contain a robot!");
    36 		if (findCells(ClosedLambdaLift).size() != 1)
    36 		if (findCells(ClosedLambdaLift).size() != 1)
    37 			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    37 			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    38 		for (Cell cell : findCells(MiningRobot))
       
    39 			robotCell = cell;
       
    40 	}
    38 	}
    41 
    39 
    42 	@Override
    40 	@Override
    43 	public String toText() {
    41 	public String toText() {
    44 		String text = "";
    42 		String text = "";
    68 				CellContent content = null;
    66 				CellContent content = null;
    69 				if (curX < row.length())
    67 				if (curX < row.length())
    70 					content = CellContent.fromText("" + row.charAt(curX));
    68 					content = CellContent.fromText("" + row.charAt(curX));
    71 				else
    69 				else
    72 					content = Empty;
    70 					content = Empty;
       
    71 				if (content == null)
       
    72 					content = Empty;
    73 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    73 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    74 				cells[curX][curY] = cell;
    74 				cells[curX][curY] = cell;
    75 			}
    75 			}
    76 		}
    76 		}
    77 	}
    77 	}
    99 		return sizeY;
    99 		return sizeY;
   100 	}
   100 	}
   101 
   101 
   102 	public Set<Movement> getAvailableRobotMovements() {
   102 	public Set<Movement> getAvailableRobotMovements() {
   103 		Set<Movement> movs = new HashSet<Movement>();
   103 		Set<Movement> movs = new HashSet<Movement>();
       
   104 		Cell robotCell = getRobotCell();
   104 		Set<Cell> nCells = getNeighboringCells(robotCell);
   105 		Set<Cell> nCells = getNeighboringCells(robotCell);
   105 		for (Cell nCell : nCells) {
   106 		for (Cell nCell : nCells) {
   106 			CellContent nContent = nCell.getContent();
   107 			CellContent nContent = nCell.getContent();
   107 			Coordinate nCoordinate = nCell.getCoordinate();
   108 			Coordinate nCoordinate = nCell.getCoordinate();
   108 			Cell rightNCell = getCell(nCoordinate.right());
   109 			Cell rightNCell = getCell(nCoordinate.right());
   140 			}
   141 			}
   141 		}
   142 		}
   142 		return finded;
   143 		return finded;
   143 	}
   144 	}
   144 
   145 
   145 	public Cell[][] getCells() {
       
   146 		return cells;
       
   147 	}
       
   148 
       
   149 	public void setCells(Cell[][] cells) {
   146 	public void setCells(Cell[][] cells) {
   150 		this.cells = cells;
   147 		this.cells = cells;
   151 	}
   148 	}
   152 
   149 
   153 	public Cell getRobotCell() {
   150 	public Cell getRobotCell() {
   154 		return robotCell;
   151 		for (Cell cell : findCells(MiningRobot)) {
       
   152 			return cell;
       
   153 		}
       
   154 		throw new RuntimeException("Where is robot!?");
   155 	}
   155 	}
   156 
   156 
   157 	public void setRobotCell(Cell robotCell) {
   157 	public Cell[][] cloneCells() {
   158 		this.robotCell = robotCell;
   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;
   159 	}
   169 	}
   160 
   170 
   161 }
   171 }