Add water flooding
authorindvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 17:58:43 +0400
changeset 25d6a75f29ba9b
parent 24 46c2179854dc
child 26 964aa8102804
Add water flooding
icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java
icfp-model/src/main/java/ru/bosony/model/game/Game.java
icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
     1.1 --- a/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java	Sun Jul 15 04:45:11 2012 +0400
     1.2 +++ b/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java	Sun Jul 15 17:58:43 2012 +0400
     1.3 @@ -46,14 +46,16 @@
     1.4  		writer.newLine();
     1.5  		writer.write("********************************************************************************");
     1.6  		writer.newLine();
     1.7 -		writer.write("State: " + game.getState());
     1.8 +		writer.write("State:      " + game.getState());
     1.9  		writer.newLine();
    1.10 -		writer.write("Score: " + game.getScore() + " ("
    1.11 +		writer.write("Score:      " + game.getScore() + " ("
    1.12  				+ (game.getLastScoreChange() >= 0 ? "+" + game.getLastScoreChange() : game.getLastScoreChange()) + ")");
    1.13  		writer.newLine();
    1.14 -		writer.write("Steps: " + game.getRoute().size());
    1.15 +		writer.write("Steps:      " + game.getRoute().size());
    1.16  		writer.newLine();
    1.17 -		writer.write("Route: " + game.getStringRoute());
    1.18 +		writer.write("Route:      " + game.getStringRoute());
    1.19 +		writer.newLine();
    1.20 +		writer.write("Underwater: " + game.getUnderwater() + " of " + mine.getRobotWaterproof());
    1.21  		writer.newLine();
    1.22  		writer.newLine();
    1.23  		writer.write(mine.toText());
    1.24 @@ -67,7 +69,7 @@
    1.25  		movs = movs.replaceAll(",\\s*$", "");
    1.26  		writer.write(movs);
    1.27  		writer.newLine();
    1.28 -		writer.write("Print action(s): ");
    1.29 +		writer.write("Enter action(s): ");
    1.30  		writer.flush();
    1.31  	}
    1.32  
     2.1 --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 04:45:11 2012 +0400
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 17:58:43 2012 +0400
     2.3 @@ -30,6 +30,7 @@
     2.4  	protected GameState			state					= GameState.Game;
     2.5  	protected int				lambdaCollectedCount	= 0;
     2.6  	protected List<Movement>	route					= new ArrayList<Movement>();
     2.7 +	protected int				underwater				= 0;
     2.8  
     2.9  	public Game(Mine mine) {
    2.10  		this.mine = mine;
    2.11 @@ -166,6 +167,7 @@
    2.12  		robotCoord = mine.getRobotCell().getCoordinate();
    2.13  		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
    2.14  			mine.setCells(newCells);
    2.15 +
    2.16  			lastScoreChange = score - prevScore;
    2.17  			state = GameState.Lose;
    2.18  			return state;
    2.19 @@ -173,6 +175,22 @@
    2.20  
    2.21  		mine.setCells(newCells);
    2.22  
    2.23 +		// Water flooding
    2.24 +		robotCoord = mine.getRobotCell().getCoordinate();
    2.25 +		if (robotCoord.y <= mine.getWaterLevel()) {
    2.26 +			underwater++;
    2.27 +		} else {
    2.28 +			underwater = 0;
    2.29 +		}
    2.30 +		if (underwater > mine.getRobotWaterproof()) {
    2.31 +			lastScoreChange = score - prevScore;
    2.32 +			state = GameState.Lose;
    2.33 +			return state;
    2.34 +		}
    2.35 +		if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
    2.36 +			mine.setWaterLevel(mine.getWaterLevel() + 1);
    2.37 +		}
    2.38 +
    2.39  		lastScoreChange = score - prevScore;
    2.40  		state = GameState.Game;
    2.41  		return state;
    2.42 @@ -233,4 +251,8 @@
    2.43  		}
    2.44  		return movs;
    2.45  	}
    2.46 +
    2.47 +	public int getUnderwater() {
    2.48 +		return underwater;
    2.49 +	}
    2.50  }
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 04:45:11 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 17:58:43 2012 +0400
     3.3 @@ -10,6 +10,8 @@
     3.4  
     3.5  import java.util.HashSet;
     3.6  import java.util.Set;
     3.7 +import java.util.regex.Matcher;
     3.8 +import java.util.regex.Pattern;
     3.9  
    3.10  import ru.bosony.model.cellscontents.CellContent;
    3.11  import ru.bosony.model.io.TextRepresentable;
    3.12 @@ -23,9 +25,13 @@
    3.13   */
    3.14  public class Mine implements TextRepresentable {
    3.15  
    3.16 -	protected Cell[][]	cells;
    3.17 -	protected int		sizeX;
    3.18 -	protected int		sizeY;
    3.19 +	protected Cell[][]			cells;
    3.20 +	protected int				sizeX;
    3.21 +	protected int				sizeY;
    3.22 +	protected int				waterLevel		= 0;
    3.23 +	protected int				flooding		= 0;
    3.24 +	protected int				robotWaterproof	= 10;
    3.25 +	protected static Pattern	pattern			= Pattern.compile("(?s)(.*?)(\n\n)?(Water )?(\\d*)\n?(Flooding )?(\\d*)\n?(Waterproof )?(\\d*)");
    3.26  
    3.27  	public Mine(String text) {
    3.28  		fromText(text);
    3.29 @@ -46,11 +52,22 @@
    3.30  			}
    3.31  			text += "\n";
    3.32  		}
    3.33 -		return text.replaceAll("\n*$", "");
    3.34 +		text = text.replaceAll("\n*$", "");
    3.35 +		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    3.36 +			text += "\n\n";
    3.37 +			text += "Water " + waterLevel + "\n";
    3.38 +			text += "Flooding " + flooding + "\n";
    3.39 +			text += "Waterproof " + robotWaterproof;
    3.40 +		}
    3.41 +		return text;
    3.42  	}
    3.43  
    3.44 -	protected void fromText(String text) {
    3.45 -		String[] rows = text.split("\n+");
    3.46 +	protected void fromText(String str) {
    3.47 +		Matcher matcher = pattern.matcher(str);
    3.48 +		if (!matcher.matches())
    3.49 +			throw new RuntimeException("Can't parse mine from string");
    3.50 +		String body = matcher.group(1);
    3.51 +		String[] rows = body.split("\n+");
    3.52  		int y = rows.length;
    3.53  		int x = 0;
    3.54  		for (String row : rows) {
    3.55 @@ -74,6 +91,16 @@
    3.56  				cells[curX][curY] = cell;
    3.57  			}
    3.58  		}
    3.59 +		
    3.60 +		String sWaterLevel = matcher.group(4);
    3.61 +		String sFlooding = matcher.group(6);
    3.62 +		String sRobotWaterproof = matcher.group(8);
    3.63 +		if (sWaterLevel.length() > 0)
    3.64 +			waterLevel = Integer.parseInt(sWaterLevel);
    3.65 +		if (sFlooding.length() > 0)
    3.66 +			flooding = Integer.parseInt(sFlooding);
    3.67 +		if (sRobotWaterproof.length() > 0)
    3.68 +			robotWaterproof = Integer.parseInt(sRobotWaterproof);
    3.69  	}
    3.70  
    3.71  	public Cell getCell(Coordinate coordinate) {
    3.72 @@ -168,4 +195,20 @@
    3.73  		return clone;
    3.74  	}
    3.75  
    3.76 +	public int getWaterLevel() {
    3.77 +		return waterLevel;
    3.78 +	}
    3.79 +
    3.80 +	public void setWaterLevel(int waterLevel) {
    3.81 +		this.waterLevel = waterLevel;
    3.82 +	}
    3.83 +
    3.84 +	public int getFlooding() {
    3.85 +		return flooding;
    3.86 +	}
    3.87 +
    3.88 +	public int getRobotWaterproof() {
    3.89 +		return robotWaterproof;
    3.90 +	}
    3.91 +
    3.92  }