icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 17:58:43 +0400
changeset 25 d6a75f29ba9b
parent 22 a46d0179df7a
child 26 964aa8102804
permissions -rw-r--r--
Add water flooding
     1 package ru.bosony.model.mine;
     2 
     3 import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
     4 import static ru.bosony.model.cellscontents.CellContent.Earth;
     5 import static ru.bosony.model.cellscontents.CellContent.Empty;
     6 import static ru.bosony.model.cellscontents.CellContent.Lambda;
     7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
     8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     9 import static ru.bosony.model.cellscontents.CellContent.Rock;
    10 
    11 import java.util.HashSet;
    12 import java.util.Set;
    13 import java.util.regex.Matcher;
    14 import java.util.regex.Pattern;
    15 
    16 import ru.bosony.model.cellscontents.CellContent;
    17 import ru.bosony.model.io.TextRepresentable;
    18 import ru.bosony.model.moving.Coordinate;
    19 import ru.bosony.model.moving.Movement;
    20 
    21 /**
    22  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    23  *         14.07.2012 2:02:18
    24  * 
    25  */
    26 public class Mine implements TextRepresentable {
    27 
    28 	protected Cell[][]			cells;
    29 	protected int				sizeX;
    30 	protected int				sizeY;
    31 	protected int				waterLevel		= 0;
    32 	protected int				flooding		= 0;
    33 	protected int				robotWaterproof	= 10;
    34 	protected static Pattern	pattern			= Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)");
    35 
    36 	public Mine(String text) {
    37 		fromText(text);
    38 		if (findCells(OpenLambdaLift).size() != 0)
    39 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    40 		if (findCells(MiningRobot).size() != 1)
    41 			throw new RuntimeException("New map does not contain a robot!");
    42 		if (findCells(ClosedLambdaLift).size() != 1)
    43 			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    44 	}
    45 
    46 	@Override
    47 	public String toText() {
    48 		String text = "";
    49 		for (int y = 0; y < sizeY; y++) {
    50 			for (int x = 0; x < sizeX; x++) {
    51 				text += cells[x][y].getContent().toText();
    52 			}
    53 			text += "\n";
    54 		}
    55 		text = text.replaceAll("\n*$", "");
    56 		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    57 			text += "\n\n";
    58 			text += "Water " + waterLevel + "\n";
    59 			text += "Flooding " + flooding + "\n";
    60 			text += "Waterproof " + robotWaterproof;
    61 		}
    62 		return text;
    63 	}
    64 
    65 	protected void fromText(String str) {
    66 		Matcher matcher = pattern.matcher(str);
    67 		if (!matcher.matches())
    68 			throw new RuntimeException("Can't parse mine from string");
    69 		String body = matcher.group(1);
    70 		String[] rows = body.split("\n+");
    71 		int y = rows.length;
    72 		int x = 0;
    73 		for (String row : rows) {
    74 			if (x < row.length())
    75 				x = row.length();
    76 		}
    77 		sizeX = x;
    78 		sizeY = y;
    79 		cells = new Cell[x][y];
    80 		for (int curY = 0; curY < rows.length; curY++) {
    81 			String row = rows[curY];
    82 			for (int curX = 0; curX < row.length() || curX < x; curX++) {
    83 				CellContent content = null;
    84 				if (curX < row.length())
    85 					content = CellContent.fromText("" + row.charAt(curX));
    86 				else
    87 					content = Empty;
    88 				if (content == null)
    89 					content = Empty;
    90 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    91 				cells[curX][curY] = cell;
    92 			}
    93 		}
    94 		
    95 		String sWaterLevel = matcher.group(4);
    96 		String sFlooding = matcher.group(6);
    97 		String sRobotWaterproof = matcher.group(8);
    98 		if (sWaterLevel.length() > 0)
    99 			waterLevel = Integer.parseInt(sWaterLevel);
   100 		if (sFlooding.length() > 0)
   101 			flooding = Integer.parseInt(sFlooding);
   102 		if (sRobotWaterproof.length() > 0)
   103 			robotWaterproof = Integer.parseInt(sRobotWaterproof);
   104 	}
   105 
   106 	public Cell getCell(Coordinate coordinate) {
   107 		if (coordinate == null)
   108 			return null;
   109 		int x = coordinate.x;
   110 		int y = coordinate.y;
   111 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
   112 			return null;
   113 		return cells[x - 1][sizeY - y];
   114 	}
   115 
   116 	@Override
   117 	public String toString() {
   118 		return toText();
   119 	}
   120 
   121 	public int getSizeX() {
   122 		return sizeX;
   123 	}
   124 
   125 	public int getSizeY() {
   126 		return sizeY;
   127 	}
   128 
   129 	public Set<Movement> getAvailableRobotMovements() {
   130 		Set<Movement> movs = new HashSet<Movement>();
   131 		Cell robotCell = getRobotCell();
   132 		Set<Cell> nCells = getNeighboringCells(robotCell);
   133 		for (Cell nCell : nCells) {
   134 			CellContent nContent = nCell.getContent();
   135 			Coordinate nCoordinate = nCell.getCoordinate();
   136 			Cell rightNCell = getCell(nCoordinate.right());
   137 			Cell leftNCell = getCell(nCoordinate.left());
   138 			if (nContent == Empty
   139 					|| nContent == Earth
   140 					|| nContent == Lambda
   141 					|| nContent == OpenLambdaLift
   142 					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
   143 							.getContent() == Empty)
   144 					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
   145 							.getContent() == Empty)) {
   146 				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
   147 				movs.add(mov);
   148 			}
   149 		}
   150 		movs.add(Movement.ABORT);
   151 		movs.add(Movement.WAIT);
   152 		return movs;
   153 	}
   154 
   155 	public Set<Cell> getNeighboringCells(Cell cell) {
   156 		Set<Cell> cells = new HashSet<Cell>();
   157 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
   158 			cells.add(getCell(coordinate));
   159 		return cells;
   160 	}
   161 
   162 	public Set<Cell> findCells(CellContent content) {
   163 		Set<Cell> finded = new HashSet<Cell>();
   164 		for (int y = sizeY - 1; y >= 0; y--) {
   165 			for (int x = 0; x < sizeX; x++) {
   166 				if (cells[x][y].getContent() == content)
   167 					finded.add(cells[x][y]);
   168 			}
   169 		}
   170 		return finded;
   171 	}
   172 
   173 	public void setCells(Cell[][] cells) {
   174 		this.cells = cells;
   175 	}
   176 
   177 	public Cell getRobotCell() {
   178 		for (Cell cell : findCells(MiningRobot)) {
   179 			return cell;
   180 		}
   181 		throw new RuntimeException("Where is robot!?");
   182 	}
   183 
   184 	public Cell[][] cloneCells() {
   185 		Cell[][] clone = new Cell[sizeX][sizeY];
   186 		for (int y = sizeY - 1; y >= 0; y--) {
   187 			for (int x = 0; x < sizeX; x++) {
   188 				try {
   189 					clone[x][y] = cells[x][y].clone();
   190 				} catch (CloneNotSupportedException e) {
   191 					throw new RuntimeException(e);
   192 				}
   193 			}
   194 		}
   195 		return clone;
   196 	}
   197 
   198 	public int getWaterLevel() {
   199 		return waterLevel;
   200 	}
   201 
   202 	public void setWaterLevel(int waterLevel) {
   203 		this.waterLevel = waterLevel;
   204 	}
   205 
   206 	public int getFlooding() {
   207 		return flooding;
   208 	}
   209 
   210 	public int getRobotWaterproof() {
   211 		return robotWaterproof;
   212 	}
   213 
   214 }