icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
changeset 26 964aa8102804
parent 25 d6a75f29ba9b
child 27 505c25412b16
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 17:58:43 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 19:26:28 2012 +0400
     1.3 @@ -28,10 +28,14 @@
     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	pattern			= Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)");
    1.11 +	protected int				waterLevel			= 0;
    1.12 +	protected int				flooding			= 0;
    1.13 +	protected int				robotWaterproof		= 10;
    1.14 +	protected static Pattern	minePattern			= Pattern
    1.15 +															.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)");
    1.16 +	protected static Pattern	waterPattern		= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    1.17 +	protected static Pattern	floodingPattern		= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    1.18 +	protected static Pattern	waterproofPattern	= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    1.19  
    1.20  	public Mine(String text) {
    1.21  		fromText(text);
    1.22 @@ -63,7 +67,7 @@
    1.23  	}
    1.24  
    1.25  	protected void fromText(String str) {
    1.26 -		Matcher matcher = pattern.matcher(str);
    1.27 +		Matcher matcher = minePattern.matcher(str);
    1.28  		if (!matcher.matches())
    1.29  			throw new RuntimeException("Can't parse mine from string");
    1.30  		String body = matcher.group(1);
    1.31 @@ -91,10 +95,23 @@
    1.32  				cells[curX][curY] = cell;
    1.33  			}
    1.34  		}
    1.35 -		
    1.36 -		String sWaterLevel = matcher.group(4);
    1.37 -		String sFlooding = matcher.group(6);
    1.38 -		String sRobotWaterproof = matcher.group(8);
    1.39 +
    1.40 +		String metadata = matcher.group(3);
    1.41 +		String sWaterLevel = "";
    1.42 +		String sFlooding = "";
    1.43 +		String sRobotWaterproof = "";
    1.44 +		Matcher waterMatcher = waterPattern.matcher(metadata);
    1.45 +		if (waterMatcher.matches()) {
    1.46 +			sWaterLevel = waterMatcher.group(1);
    1.47 +		}
    1.48 +		Matcher floodingMatcher = floodingPattern.matcher(metadata);
    1.49 +		if (floodingMatcher.matches()) {
    1.50 +			sFlooding = floodingMatcher.group(1);
    1.51 +		}
    1.52 +		Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
    1.53 +		if (waterproofMatcher.matches()) {
    1.54 +			sRobotWaterproof = waterproofMatcher.group(1);
    1.55 +		}
    1.56  		if (sWaterLevel.length() > 0)
    1.57  			waterLevel = Integer.parseInt(sWaterLevel);
    1.58  		if (sFlooding.length() > 0)