icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
changeset 27 505c25412b16
parent 26 964aa8102804
child 29 66aa96e8fd92
equal deleted inserted replaced
26:964aa8102804 27:505c25412b16
    26 public class Mine implements TextRepresentable {
    26 public class Mine implements TextRepresentable {
    27 
    27 
    28 	protected Cell[][]			cells;
    28 	protected Cell[][]			cells;
    29 	protected int				sizeX;
    29 	protected int				sizeX;
    30 	protected int				sizeY;
    30 	protected int				sizeY;
    31 	protected int				waterLevel			= 0;
    31 	protected int				waterLevel				= 0;
    32 	protected int				flooding			= 0;
    32 	protected int				flooding				= 0;
    33 	protected int				robotWaterproof		= 10;
    33 	protected int				robotWaterproof			= 10;
    34 	protected static Pattern	minePattern			= Pattern
    34 	protected static Pattern	minePattern				= Pattern
    35 															.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)");
    35 																.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)");
    36 	protected static Pattern	waterPattern		= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    36 	protected static Pattern	waterPattern			= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    37 	protected static Pattern	floodingPattern		= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    37 	protected static Pattern	floodingPattern			= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    38 	protected static Pattern	waterproofPattern	= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    38 	protected static Pattern	waterproofPattern		= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
       
    39 	protected static Pattern	trampolinePattern		= Pattern
       
    40 																.compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
       
    41 	protected static String		firstTrampolineRegexp	= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
    39 
    42 
    40 	public Mine(String text) {
    43 	public Mine(String text) {
    41 		fromText(text);
    44 		fromText(text);
    42 		if (findCells(OpenLambdaLift).size() != 0)
    45 		if (findCells(OpenLambdaLift).size() != 0)
    43 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    46 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    55 				text += cells[x][y].getContent().toText();
    58 				text += cells[x][y].getContent().toText();
    56 			}
    59 			}
    57 			text += "\n";
    60 			text += "\n";
    58 		}
    61 		}
    59 		text = text.replaceAll("\n*$", "");
    62 		text = text.replaceAll("\n*$", "");
       
    63 		text += "\n\n";
    60 		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    64 		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    61 			text += "\n\n";
       
    62 			text += "Water " + waterLevel + "\n";
    65 			text += "Water " + waterLevel + "\n";
    63 			text += "Flooding " + flooding + "\n";
    66 			text += "Flooding " + flooding + "\n";
    64 			text += "Waterproof " + robotWaterproof;
    67 			text += "Waterproof " + robotWaterproof + "\n";
    65 		}
    68 		}
       
    69 		for (CellContent trampoline : CellContent.values()) {
       
    70 			if (trampoline.getTrampolineTarget() != null)
       
    71 				text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
       
    72 						+ "\n";
       
    73 		}
       
    74 		text = text.replaceAll("\n*$", "");
    66 		return text;
    75 		return text;
    67 	}
    76 	}
    68 
    77 
    69 	protected void fromText(String str) {
    78 	protected void fromText(String str) {
    70 		Matcher matcher = minePattern.matcher(str);
    79 		Matcher matcher = minePattern.matcher(str);
    95 				cells[curX][curY] = cell;
   104 				cells[curX][curY] = cell;
    96 			}
   105 			}
    97 		}
   106 		}
    98 
   107 
    99 		String metadata = matcher.group(3);
   108 		String metadata = matcher.group(3);
   100 		String sWaterLevel = "";
   109 		if (metadata != null) {
   101 		String sFlooding = "";
   110 			// Water flooding
   102 		String sRobotWaterproof = "";
   111 			String sWaterLevel = "";
   103 		Matcher waterMatcher = waterPattern.matcher(metadata);
   112 			String sFlooding = "";
   104 		if (waterMatcher.matches()) {
   113 			String sRobotWaterproof = "";
   105 			sWaterLevel = waterMatcher.group(1);
   114 			Matcher waterMatcher = waterPattern.matcher(metadata);
   106 		}
   115 			if (waterMatcher.matches()) {
   107 		Matcher floodingMatcher = floodingPattern.matcher(metadata);
   116 				sWaterLevel = waterMatcher.group(1);
   108 		if (floodingMatcher.matches()) {
   117 			}
   109 			sFlooding = floodingMatcher.group(1);
   118 			Matcher floodingMatcher = floodingPattern.matcher(metadata);
   110 		}
   119 			if (floodingMatcher.matches()) {
   111 		Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
   120 				sFlooding = floodingMatcher.group(1);
   112 		if (waterproofMatcher.matches()) {
   121 			}
   113 			sRobotWaterproof = waterproofMatcher.group(1);
   122 			Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
   114 		}
   123 			if (waterproofMatcher.matches()) {
   115 		if (sWaterLevel.length() > 0)
   124 				sRobotWaterproof = waterproofMatcher.group(1);
   116 			waterLevel = Integer.parseInt(sWaterLevel);
   125 			}
   117 		if (sFlooding.length() > 0)
   126 			if (sWaterLevel.length() > 0)
   118 			flooding = Integer.parseInt(sFlooding);
   127 				waterLevel = Integer.parseInt(sWaterLevel);
   119 		if (sRobotWaterproof.length() > 0)
   128 			if (sFlooding.length() > 0)
   120 			robotWaterproof = Integer.parseInt(sRobotWaterproof);
   129 				flooding = Integer.parseInt(sFlooding);
       
   130 			if (sRobotWaterproof.length() > 0)
       
   131 				robotWaterproof = Integer.parseInt(sRobotWaterproof);
       
   132 
       
   133 			// Trampolines and targets
       
   134 			String trampolines = metadata;
       
   135 			while (true) {
       
   136 				Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
       
   137 				if (trampolineMatcher.matches()) {
       
   138 					String sTrampoline = trampolineMatcher.group(1);
       
   139 					String sTarget = trampolineMatcher.group(2);
       
   140 					CellContent trampoline = CellContent.fromText(sTrampoline);
       
   141 					CellContent target = CellContent.fromText(sTarget);
       
   142 					if (trampoline != null && target != null)
       
   143 						trampoline.setTrampolineTarget(target);
       
   144 				} else
       
   145 					break;
       
   146 				trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
       
   147 			}
       
   148 		}
   121 	}
   149 	}
   122 
   150 
   123 	public Cell getCell(Coordinate coordinate) {
   151 	public Cell getCell(Coordinate coordinate) {
   124 		if (coordinate == null)
   152 		if (coordinate == null)
   125 			return null;
   153 			return null;