icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
changeset 27 505c25412b16
parent 26 964aa8102804
child 29 66aa96e8fd92
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 19:26:28 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 21:20:04 2012 +0400
     1.3 @@ -28,14 +28,17 @@
     1.4  	protected Cell[][]			cells;
     1.5  	protected int				sizeX;
     1.6  	protected int				sizeY;
     1.7 -	protected int				waterLevel			= 0;
     1.8 -	protected int				flooding			= 0;
     1.9 -	protected int				robotWaterproof		= 10;
    1.10 -	protected static Pattern	minePattern			= Pattern
    1.11 -															.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)");
    1.12 -	protected static Pattern	waterPattern		= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    1.13 -	protected static Pattern	floodingPattern		= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    1.14 -	protected static Pattern	waterproofPattern	= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    1.15 +	protected int				waterLevel				= 0;
    1.16 +	protected int				flooding				= 0;
    1.17 +	protected int				robotWaterproof			= 10;
    1.18 +	protected static Pattern	minePattern				= Pattern
    1.19 +																.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)");
    1.20 +	protected static Pattern	waterPattern			= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    1.21 +	protected static Pattern	floodingPattern			= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    1.22 +	protected static Pattern	waterproofPattern		= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    1.23 +	protected static Pattern	trampolinePattern		= Pattern
    1.24 +																.compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
    1.25 +	protected static String		firstTrampolineRegexp	= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
    1.26  
    1.27  	public Mine(String text) {
    1.28  		fromText(text);
    1.29 @@ -57,12 +60,18 @@
    1.30  			text += "\n";
    1.31  		}
    1.32  		text = text.replaceAll("\n*$", "");
    1.33 +		text += "\n\n";
    1.34  		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    1.35 -			text += "\n\n";
    1.36  			text += "Water " + waterLevel + "\n";
    1.37  			text += "Flooding " + flooding + "\n";
    1.38 -			text += "Waterproof " + robotWaterproof;
    1.39 +			text += "Waterproof " + robotWaterproof + "\n";
    1.40  		}
    1.41 +		for (CellContent trampoline : CellContent.values()) {
    1.42 +			if (trampoline.getTrampolineTarget() != null)
    1.43 +				text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
    1.44 +						+ "\n";
    1.45 +		}
    1.46 +		text = text.replaceAll("\n*$", "");
    1.47  		return text;
    1.48  	}
    1.49  
    1.50 @@ -97,27 +106,46 @@
    1.51  		}
    1.52  
    1.53  		String metadata = matcher.group(3);
    1.54 -		String sWaterLevel = "";
    1.55 -		String sFlooding = "";
    1.56 -		String sRobotWaterproof = "";
    1.57 -		Matcher waterMatcher = waterPattern.matcher(metadata);
    1.58 -		if (waterMatcher.matches()) {
    1.59 -			sWaterLevel = waterMatcher.group(1);
    1.60 +		if (metadata != null) {
    1.61 +			// Water flooding
    1.62 +			String sWaterLevel = "";
    1.63 +			String sFlooding = "";
    1.64 +			String sRobotWaterproof = "";
    1.65 +			Matcher waterMatcher = waterPattern.matcher(metadata);
    1.66 +			if (waterMatcher.matches()) {
    1.67 +				sWaterLevel = waterMatcher.group(1);
    1.68 +			}
    1.69 +			Matcher floodingMatcher = floodingPattern.matcher(metadata);
    1.70 +			if (floodingMatcher.matches()) {
    1.71 +				sFlooding = floodingMatcher.group(1);
    1.72 +			}
    1.73 +			Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
    1.74 +			if (waterproofMatcher.matches()) {
    1.75 +				sRobotWaterproof = waterproofMatcher.group(1);
    1.76 +			}
    1.77 +			if (sWaterLevel.length() > 0)
    1.78 +				waterLevel = Integer.parseInt(sWaterLevel);
    1.79 +			if (sFlooding.length() > 0)
    1.80 +				flooding = Integer.parseInt(sFlooding);
    1.81 +			if (sRobotWaterproof.length() > 0)
    1.82 +				robotWaterproof = Integer.parseInt(sRobotWaterproof);
    1.83 +
    1.84 +			// Trampolines and targets
    1.85 +			String trampolines = metadata;
    1.86 +			while (true) {
    1.87 +				Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
    1.88 +				if (trampolineMatcher.matches()) {
    1.89 +					String sTrampoline = trampolineMatcher.group(1);
    1.90 +					String sTarget = trampolineMatcher.group(2);
    1.91 +					CellContent trampoline = CellContent.fromText(sTrampoline);
    1.92 +					CellContent target = CellContent.fromText(sTarget);
    1.93 +					if (trampoline != null && target != null)
    1.94 +						trampoline.setTrampolineTarget(target);
    1.95 +				} else
    1.96 +					break;
    1.97 +				trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
    1.98 +			}
    1.99  		}
   1.100 -		Matcher floodingMatcher = floodingPattern.matcher(metadata);
   1.101 -		if (floodingMatcher.matches()) {
   1.102 -			sFlooding = floodingMatcher.group(1);
   1.103 -		}
   1.104 -		Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
   1.105 -		if (waterproofMatcher.matches()) {
   1.106 -			sRobotWaterproof = waterproofMatcher.group(1);
   1.107 -		}
   1.108 -		if (sWaterLevel.length() > 0)
   1.109 -			waterLevel = Integer.parseInt(sWaterLevel);
   1.110 -		if (sFlooding.length() > 0)
   1.111 -			flooding = Integer.parseInt(sFlooding);
   1.112 -		if (sRobotWaterproof.length() > 0)
   1.113 -			robotWaterproof = Integer.parseInt(sRobotWaterproof);
   1.114  	}
   1.115  
   1.116  	public Cell getCell(Coordinate coordinate) {