icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
changeset 53 80f5fc954cff
parent 51 b431cec961d7
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Mon Jul 16 13:19:10 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Wed Jul 18 09:42:52 2012 +0400
     1.3 @@ -8,6 +8,7 @@
     1.4  import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     1.5  import static ru.bosony.model.cellscontents.CellContent.Rock;
     1.6  
     1.7 +import java.util.Collection;
     1.8  import java.util.HashSet;
     1.9  import java.util.Set;
    1.10  import java.util.regex.Matcher;
    1.11 @@ -23,7 +24,7 @@
    1.12   *         14.07.2012 2:02:18
    1.13   * 
    1.14   */
    1.15 -public class Mine implements TextRepresentable {
    1.16 +public class Mine implements TextRepresentable, Cloneable {
    1.17  
    1.18  	protected Cell[][]			cells;
    1.19  	protected int				sizeX;
    1.20 @@ -48,13 +49,17 @@
    1.21  	protected static String		firstTrampolineRegexp				= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
    1.22  
    1.23  	public Mine(String text) {
    1.24 -		fromText(text);
    1.25 +		this(text, null);
    1.26  		if (findCells(OpenLambdaLift).size() != 0)
    1.27  			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    1.28  		if (findCells(MiningRobot).size() != 1)
    1.29  			throw new RuntimeException("New map does not contain a robot!");
    1.30  		if (findCells(ClosedLambdaLift).size() != 1)
    1.31  			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    1.32 +	}
    1.33 +
    1.34 +	protected Mine(String text, Object obj) {
    1.35 +		fromText(text);
    1.36  		lambdasAndHighOrderRocksStartCount = findCells(Lambda).size() + findCells(CellContent.HighOrderRock).size();
    1.37  	}
    1.38  
    1.39 @@ -74,6 +79,10 @@
    1.40  		return text;
    1.41  	}
    1.42  
    1.43 +	public String toTextWithoutRobot() {
    1.44 +		return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText());
    1.45 +	}
    1.46 +
    1.47  	protected void fromText(String str) {
    1.48  		Matcher matcher = minePattern.matcher(str);
    1.49  		if (!matcher.matches())
    1.50 @@ -174,6 +183,18 @@
    1.51  		return cells[x - 1][sizeY - y];
    1.52  	}
    1.53  
    1.54 +	public Set<Cell> getCells(Collection<Coordinate> coordinates) {
    1.55 +		Set<Cell> cells = new HashSet<Cell>();
    1.56 +		if (coordinates == null)
    1.57 +			return cells;
    1.58 +		for (Coordinate coordinate : coordinates) {
    1.59 +			Cell cell = getCell(coordinate);
    1.60 +			if (cell != null)
    1.61 +				cells.add(cell);
    1.62 +		}
    1.63 +		return cells;
    1.64 +	}
    1.65 +
    1.66  	@Override
    1.67  	public String toString() {
    1.68  		return toText();
    1.69 @@ -215,15 +236,21 @@
    1.70  
    1.71  	public Set<Cell> getNeighboringCells(Cell cell) {
    1.72  		Set<Cell> cells = new HashSet<Cell>();
    1.73 -		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
    1.74 -			cells.add(getCell(coordinate));
    1.75 +		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) {
    1.76 +			Cell nextCell = getCell(coordinate);
    1.77 +			if (nextCell != null)
    1.78 +				cells.add(nextCell);
    1.79 +		}
    1.80  		return cells;
    1.81  	}
    1.82  
    1.83  	public Set<Cell> getAdjacentCells(Cell cell) {
    1.84  		Set<Cell> cells = new HashSet<Cell>();
    1.85 -		for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates())
    1.86 -			cells.add(getCell(coordinate));
    1.87 +		for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) {
    1.88 +			Cell nextCell = getCell(coordinate);
    1.89 +			if (nextCell != null)
    1.90 +				cells.add(nextCell);
    1.91 +		}
    1.92  		return cells;
    1.93  	}
    1.94  
    1.95 @@ -309,4 +336,9 @@
    1.96  		return toText().hashCode();
    1.97  	}
    1.98  
    1.99 +	@Override
   1.100 +	public Mine clone() {
   1.101 +		return new Mine(toText(), null);
   1.102 +	}
   1.103 +
   1.104  }