icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
branchCleverSearch
changeset 51 b431cec961d7
parent 50 9530f55e9639
equal deleted inserted replaced
50:9530f55e9639 51:b431cec961d7
     6 import static ru.bosony.model.cellscontents.CellContent.Lambda;
     6 import static ru.bosony.model.cellscontents.CellContent.Lambda;
     7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
     7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
     8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     9 import static ru.bosony.model.cellscontents.CellContent.Rock;
     9 import static ru.bosony.model.cellscontents.CellContent.Rock;
    10 
    10 
       
    11 import java.util.Collection;
    11 import java.util.HashSet;
    12 import java.util.HashSet;
    12 import java.util.Set;
    13 import java.util.Set;
    13 import java.util.regex.Matcher;
    14 import java.util.regex.Matcher;
    14 import java.util.regex.Pattern;
    15 import java.util.regex.Pattern;
    15 
    16 
    76 		text += metadata;
    77 		text += metadata;
    77 		text = text.replaceAll("\n*$", "");
    78 		text = text.replaceAll("\n*$", "");
    78 		return text;
    79 		return text;
    79 	}
    80 	}
    80 
    81 
       
    82 	public String toTextWithoutRobot() {
       
    83 		return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText());
       
    84 	}
       
    85 
    81 	protected void fromText(String str) {
    86 	protected void fromText(String str) {
    82 		Matcher matcher = minePattern.matcher(str);
    87 		Matcher matcher = minePattern.matcher(str);
    83 		if (!matcher.matches())
    88 		if (!matcher.matches())
    84 			throw new RuntimeException("Can't parse mine from string");
    89 			throw new RuntimeException("Can't parse mine from string");
    85 		String body = matcher.group(1);
    90 		String body = matcher.group(1);
   174 		int x = coordinate.x;
   179 		int x = coordinate.x;
   175 		int y = coordinate.y;
   180 		int y = coordinate.y;
   176 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
   181 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
   177 			return null;
   182 			return null;
   178 		return cells[x - 1][sizeY - y];
   183 		return cells[x - 1][sizeY - y];
       
   184 	}
       
   185 
       
   186 	public Set<Cell> getCells(Collection<Coordinate> coordinates) {
       
   187 		Set<Cell> cells = new HashSet<Cell>();
       
   188 		if (coordinates == null)
       
   189 			return cells;
       
   190 		for (Coordinate coordinate : coordinates) {
       
   191 			Cell cell = getCell(coordinate);
       
   192 			if (cell != null)
       
   193 				cells.add(cell);
       
   194 		}
       
   195 		return cells;
   179 	}
   196 	}
   180 
   197 
   181 	@Override
   198 	@Override
   182 	public String toString() {
   199 	public String toString() {
   183 		return toText();
   200 		return toText();
   217 		return movs;
   234 		return movs;
   218 	}
   235 	}
   219 
   236 
   220 	public Set<Cell> getNeighboringCells(Cell cell) {
   237 	public Set<Cell> getNeighboringCells(Cell cell) {
   221 		Set<Cell> cells = new HashSet<Cell>();
   238 		Set<Cell> cells = new HashSet<Cell>();
   222 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
   239 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) {
   223 			cells.add(getCell(coordinate));
   240 			Cell nextCell = getCell(coordinate);
       
   241 			if (nextCell != null)
       
   242 				cells.add(nextCell);
       
   243 		}
   224 		return cells;
   244 		return cells;
   225 	}
   245 	}
   226 
   246 
   227 	public Set<Cell> getAdjacentCells(Cell cell) {
   247 	public Set<Cell> getAdjacentCells(Cell cell) {
   228 		Set<Cell> cells = new HashSet<Cell>();
   248 		Set<Cell> cells = new HashSet<Cell>();
   229 		for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates())
   249 		for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) {
   230 			cells.add(getCell(coordinate));
   250 			Cell nextCell = getCell(coordinate);
       
   251 			if (nextCell != null)
       
   252 				cells.add(nextCell);
       
   253 		}
   231 		return cells;
   254 		return cells;
   232 	}
   255 	}
   233 
   256 
   234 	public Set<Cell> findCells(CellContent content) {
   257 	public Set<Cell> findCells(CellContent content) {
   235 		Set<Cell> finded = new HashSet<Cell>();
   258 		Set<Cell> finded = new HashSet<Cell>();