icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
branchindvdum
changeset 7 251961e44b5f
parent 6 0dbbc8983926
child 8 ad884dc6877c
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 00:36:43 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 02:07:51 2012 +0400
     1.3 @@ -1,13 +1,23 @@
     1.4  package ru.bosony.model.mine;
     1.5  
     1.6 +import java.util.HashSet;
     1.7 +import java.util.Set;
     1.8 +
     1.9  import ru.bosony.model.cellscontents.CellContent;
    1.10  import ru.bosony.model.io.TextRepresentable;
    1.11 +import ru.bosony.model.moving.Movement;
    1.12  
    1.13 +/**
    1.14 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    1.15 + *         14.07.2012 2:02:18
    1.16 + * 
    1.17 + */
    1.18  public class Mine implements TextRepresentable {
    1.19  
    1.20 -	public Cell[][]	cells;
    1.21 -	protected int	sizeX;
    1.22 -	protected int	sizeY;
    1.23 +	private Cell[][]	cells;
    1.24 +	protected int		sizeX;
    1.25 +	protected int		sizeY;
    1.26 +	protected Cell		robotCell;
    1.27  
    1.28  	public Mine(String text) {
    1.29  		fromText(text);
    1.30 @@ -44,15 +54,65 @@
    1.31  					content = CellContent.fromText("" + row.charAt(curX));
    1.32  				else
    1.33  					content = CellContent.Empty;
    1.34 -				Cell cell = new Cell(curX + 1, rows.length - curY, content);
    1.35 +				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    1.36  				cells[curX][curY] = cell;
    1.37 +				if (content == CellContent.MiningRobot)
    1.38 +					robotCell = cell;
    1.39  			}
    1.40  		}
    1.41  	}
    1.42  
    1.43 +	public Cell getCell(Coordinate coordinate) {
    1.44 +		int x = coordinate.x;
    1.45 +		int y = coordinate.y;
    1.46 +		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
    1.47 +			return null;
    1.48 +		return cells[x - 1][sizeY - y];
    1.49 +	}
    1.50 +
    1.51  	@Override
    1.52  	public String toString() {
    1.53  		return toText();
    1.54  	}
    1.55  
    1.56 +	public int getSizeX() {
    1.57 +		return sizeX;
    1.58 +	}
    1.59 +
    1.60 +	public int getSizeY() {
    1.61 +		return sizeY;
    1.62 +	}
    1.63 +
    1.64 +	public Set<Movement> getAvailableRobotMovements() {
    1.65 +		Set<Movement> movs = new HashSet<Movement>();
    1.66 +		Set<Cell> nCells = getNeighboringCells(robotCell);
    1.67 +		for (Cell nCell : nCells) {
    1.68 +			CellContent nContent = nCell.getContent();
    1.69 +			Coordinate nCoordinate = nCell.getCoordinate();
    1.70 +			Cell rightNCell = getCell(nCoordinate.right());
    1.71 +			Cell leftNCell = getCell(nCoordinate.left());
    1.72 +			if (nContent == CellContent.Empty
    1.73 +					|| nContent == CellContent.Earth
    1.74 +					|| nContent == CellContent.Lambda
    1.75 +					|| nContent == CellContent.OpenLambdaLift
    1.76 +					|| (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate)
    1.77 +							&& rightNCell != null && rightNCell.getContent() == CellContent.Empty)
    1.78 +					|| (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate)
    1.79 +							&& leftNCell != null && leftNCell.getContent() == CellContent.Empty)) {
    1.80 +				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
    1.81 +				movs.add(mov);
    1.82 +			}
    1.83 +		}
    1.84 +		movs.add(Movement.ABORT);
    1.85 +		movs.add(Movement.WAIT);
    1.86 +		return movs;
    1.87 +	}
    1.88 +
    1.89 +	public Set<Cell> getNeighboringCells(Cell cell) {
    1.90 +		Set<Cell> cells = new HashSet<Cell>();
    1.91 +		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
    1.92 +			cells.add(getCell(coordinate));
    1.93 +		return cells;
    1.94 +	}
    1.95 +
    1.96  }