icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
changeset 25 d6a75f29ba9b
parent 22 a46d0179df7a
child 26 964aa8102804
equal deleted inserted replaced
24:46c2179854dc 25:d6a75f29ba9b
     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.HashSet;
    11 import java.util.HashSet;
    12 import java.util.Set;
    12 import java.util.Set;
       
    13 import java.util.regex.Matcher;
       
    14 import java.util.regex.Pattern;
    13 
    15 
    14 import ru.bosony.model.cellscontents.CellContent;
    16 import ru.bosony.model.cellscontents.CellContent;
    15 import ru.bosony.model.io.TextRepresentable;
    17 import ru.bosony.model.io.TextRepresentable;
    16 import ru.bosony.model.moving.Coordinate;
    18 import ru.bosony.model.moving.Coordinate;
    17 import ru.bosony.model.moving.Movement;
    19 import ru.bosony.model.moving.Movement;
    21  *         14.07.2012 2:02:18
    23  *         14.07.2012 2:02:18
    22  * 
    24  * 
    23  */
    25  */
    24 public class Mine implements TextRepresentable {
    26 public class Mine implements TextRepresentable {
    25 
    27 
    26 	protected Cell[][]	cells;
    28 	protected Cell[][]			cells;
    27 	protected int		sizeX;
    29 	protected int				sizeX;
    28 	protected int		sizeY;
    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*)");
    29 
    35 
    30 	public Mine(String text) {
    36 	public Mine(String text) {
    31 		fromText(text);
    37 		fromText(text);
    32 		if (findCells(OpenLambdaLift).size() != 0)
    38 		if (findCells(OpenLambdaLift).size() != 0)
    33 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    39 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    44 			for (int x = 0; x < sizeX; x++) {
    50 			for (int x = 0; x < sizeX; x++) {
    45 				text += cells[x][y].getContent().toText();
    51 				text += cells[x][y].getContent().toText();
    46 			}
    52 			}
    47 			text += "\n";
    53 			text += "\n";
    48 		}
    54 		}
    49 		return text.replaceAll("\n*$", "");
    55 		text = text.replaceAll("\n*$", "");
    50 	}
    56 		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    51 
    57 			text += "\n\n";
    52 	protected void fromText(String text) {
    58 			text += "Water " + waterLevel + "\n";
    53 		String[] rows = text.split("\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+");
    54 		int y = rows.length;
    71 		int y = rows.length;
    55 		int x = 0;
    72 		int x = 0;
    56 		for (String row : rows) {
    73 		for (String row : rows) {
    57 			if (x < row.length())
    74 			if (x < row.length())
    58 				x = row.length();
    75 				x = row.length();
    72 					content = Empty;
    89 					content = Empty;
    73 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    90 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    74 				cells[curX][curY] = cell;
    91 				cells[curX][curY] = cell;
    75 			}
    92 			}
    76 		}
    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);
    77 	}
   104 	}
    78 
   105 
    79 	public Cell getCell(Coordinate coordinate) {
   106 	public Cell getCell(Coordinate coordinate) {
    80 		if (coordinate == null)
   107 		if (coordinate == null)
    81 			return null;
   108 			return null;
   166 			}
   193 			}
   167 		}
   194 		}
   168 		return clone;
   195 		return clone;
   169 	}
   196 	}
   170 
   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 
   171 }
   214 }