Add beards and razors
authorindvdum (gotoindvdum[at]gmail[dot]com)
Mon, 16 Jul 2012 00:05:50 +0400
changeset 2966aa96e8fd92
parent 28 9c68562715ed
child 30 666821484adc
Add beards and razors
icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java
icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java
icfp-model/src/main/java/ru/bosony/model/game/Game.java
icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java
icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
icfp-model/src/main/java/ru/bosony/model/moving/Movement.java
icfp-model/src/test/java/ru/bosony/tests/TestBeardsAndRazors.java
     1.1 --- a/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java	Sun Jul 15 21:36:42 2012 +0400
     1.2 +++ b/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java	Mon Jul 16 00:05:50 2012 +0400
     1.3 @@ -57,6 +57,8 @@
     1.4  		writer.newLine();
     1.5  		writer.write("Underwater: " + game.getUnderwater() + " of " + mine.getRobotWaterproof());
     1.6  		writer.newLine();
     1.7 +		writer.write("Razors:     " + mine.getRazorsCount());
     1.8 +		writer.newLine();
     1.9  		writer.newLine();
    1.10  		writer.write(mine.toText());
    1.11  		writer.newLine();
     2.1 --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java	Sun Jul 15 21:36:42 2012 +0400
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java	Mon Jul 16 00:05:50 2012 +0400
     2.3 @@ -37,7 +37,9 @@
     2.4  	Target6("6"),
     2.5  	Target7("7"),
     2.6  	Target8("8"),
     2.7 -	Target9("9");
     2.8 +	Target9("9"),
     2.9 +	WadlersBeard("W"),
    2.10 +	HuttonsRazor("!");
    2.11  
    2.12  	private String					text;
    2.13  	private CellContent				trampolineTarget	= null;
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 21:36:42 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Mon Jul 16 00:05:50 2012 +0400
     3.3 @@ -7,6 +7,8 @@
     3.4  import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
     3.5  import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     3.6  import static ru.bosony.model.cellscontents.CellContent.Rock;
     3.7 +import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor;
     3.8 +import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
     3.9  
    3.10  import java.util.ArrayList;
    3.11  import java.util.List;
    3.12 @@ -66,7 +68,8 @@
    3.13  
    3.14  		// Moving and scoring
    3.15  		route.add(mov);
    3.16 -		Coordinate robotCoord = mine.getRobotCell().getCoordinate();
    3.17 +		Cell robotCell = mine.getRobotCell();
    3.18 +		Coordinate robotCoord = robotCell.getCoordinate();
    3.19  		Coordinate nextRobotCoord = null;
    3.20  		switch (mov) {
    3.21  		case ABORT:
    3.22 @@ -77,6 +80,16 @@
    3.23  			return state;
    3.24  		case WAIT:
    3.25  			break;
    3.26 +		case RAZOR:
    3.27 +			if (mine.getRazorsCount() > 0) {
    3.28 +				for (Cell cell : mine.getNeighboringCells(robotCell)) {
    3.29 +					if (cell.getContent() == WadlersBeard) {
    3.30 +						cell.setContent(Empty);
    3.31 +					}
    3.32 +				}
    3.33 +				mine.setRazorsCount(mine.getRazorsCount() - 1);
    3.34 +			}
    3.35 +			break;
    3.36  		case LEFT:
    3.37  			nextRobotCoord = robotCoord.left();
    3.38  			break;
    3.39 @@ -106,6 +119,9 @@
    3.40  			lastScoreChange = score - prevScore;
    3.41  			state = GameState.Win;
    3.42  			return state;
    3.43 +		} else if (nextRobotCellContent == HuttonsRazor) {
    3.44 +			moveRobot(nextRobotCell);
    3.45 +			mine.setRazorsCount(mine.getRazorsCount() + 1);
    3.46  		} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
    3.47  				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
    3.48  			moveRobot(nextRobotCell);
    3.49 @@ -173,6 +189,24 @@
    3.50  					getCell(rightAndDownCoord, newCells).setContent(Rock);
    3.51  				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
    3.52  					getCell(curCoord, newCells).setContent(OpenLambdaLift);
    3.53 +				} else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
    3.54 +						&& route.size() % mine.getGrowth() == 0) {
    3.55 +					if (getContent(leftCell) == Empty)
    3.56 +						getCell(leftCoord, newCells).setContent(WadlersBeard);
    3.57 +					if (getContent(upCell) == Empty)
    3.58 +						getCell(upCoord, newCells).setContent(WadlersBeard);
    3.59 +					if (getContent(rightCell) == Empty)
    3.60 +						getCell(rightCoord, newCells).setContent(WadlersBeard);
    3.61 +					if (getContent(downCell) == Empty)
    3.62 +						getCell(downCoord, newCells).setContent(WadlersBeard);
    3.63 +					if (getContent(leftAndUpCell) == Empty)
    3.64 +						getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
    3.65 +					if (getContent(rightAndUpCell) == Empty)
    3.66 +						getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
    3.67 +					if (getContent(leftAndDownCell) == Empty)
    3.68 +						getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
    3.69 +					if (getContent(rightAndDownCell) == Empty)
    3.70 +						getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
    3.71  				}
    3.72  			}
    3.73  		}
     4.1 --- a/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java	Sun Jul 15 21:36:42 2012 +0400
     4.2 +++ b/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java	Mon Jul 16 00:05:50 2012 +0400
     4.3 @@ -379,6 +379,48 @@
     4.4  			"\n" + 
     4.5  			"Trampoline A targets 1\n" + 
     4.6  			"Trampoline B targets 2"
     4.7 +			),
     4.8 +	/**
     4.9 +	 * <pre>
    4.10 +		###############
    4.11 +		#***...R......#
    4.12 +		#***... ...*..#
    4.13 +		#\\\!.. ..\\\.#
    4.14 +		#...... ...*..#
    4.15 +		#..     .. ...#
    4.16 +		#.... .... ...#
    4.17 +		#.... .... ...#
    4.18 +		#.. .  !    ..#
    4.19 +		#..*. .. . ...#
    4.20 +		#.... .. . ...#
    4.21 +		#.\.. .... ..*#
    4.22 +		#......... . .#
    4.23 +		#!....... W   #
    4.24 +		#############L#
    4.25 +		
    4.26 +		Growth 25
    4.27 +		Razors 1
    4.28 +	 * </pre>
    4.29 +	 */
    4.30 +	MAP_FROM_BEARDS_AND_RAZORS_TASK(
    4.31 +			"###############\n" + 
    4.32 +			"#***...R......#\n" + 
    4.33 +			"#***... ...*..#\n" + 
    4.34 +			"#\\\\\\!.. ..\\\\\\.#\n" + 
    4.35 +			"#...... ...*..#\n" + 
    4.36 +			"#..     .. ...#\n" + 
    4.37 +			"#.... .... ...#\n" + 
    4.38 +			"#.... .... ...#\n" + 
    4.39 +			"#.. .  !    ..#\n" + 
    4.40 +			"#..*. .. . ...#\n" + 
    4.41 +			"#.... .. . ...#\n" + 
    4.42 +			"#.\\.. .... ..*#\n" + 
    4.43 +			"#......... . .#\n" + 
    4.44 +			"#!....... W   #\n" + 
    4.45 +			"#############L#\n" + 
    4.46 +			"\n" + 
    4.47 +			"Growth 25\n" + 
    4.48 +			"Razors 1"
    4.49  			);
    4.50  
    4.51  	private String	map;
     5.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 21:36:42 2012 +0400
     5.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Mon Jul 16 00:05:50 2012 +0400
     5.3 @@ -31,11 +31,15 @@
     5.4  	protected int				waterLevel				= 0;
     5.5  	protected int				flooding				= 0;
     5.6  	protected int				robotWaterproof			= 10;
     5.7 +	protected int				growth					= 25;
     5.8 +	protected int				razorsCount				= 0;
     5.9  	protected static Pattern	minePattern				= Pattern
    5.10 -																.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)");
    5.11 +																.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)");
    5.12  	protected static Pattern	waterPattern			= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    5.13  	protected static Pattern	floodingPattern			= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    5.14  	protected static Pattern	waterproofPattern		= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    5.15 +	protected static Pattern	growthPattern			= Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*");
    5.16 +	protected static Pattern	razorsPattern			= Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*");
    5.17  	protected static Pattern	trampolinePattern		= Pattern
    5.18  																.compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
    5.19  	protected static String		firstTrampolineRegexp	= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
    5.20 @@ -61,16 +65,22 @@
    5.21  		}
    5.22  		text = text.replaceAll("\n*$", "");
    5.23  		text += "\n\n";
    5.24 -		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    5.25 +		if (waterLevel != 0) {
    5.26  			text += "Water " + waterLevel + "\n";
    5.27 +		}
    5.28 +		if (flooding != 0) {
    5.29  			text += "Flooding " + flooding + "\n";
    5.30 -			text += "Waterproof " + robotWaterproof + "\n";
    5.31  		}
    5.32 +		text += "Waterproof " + robotWaterproof + "\n";
    5.33  		for (CellContent trampoline : CellContent.values()) {
    5.34  			if (trampoline.getTrampolineTarget() != null)
    5.35  				text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
    5.36  						+ "\n";
    5.37  		}
    5.38 +		text += "Growth " + growth + "\n";
    5.39 +		if (razorsCount != 0) {
    5.40 +			text += "Razors " + razorsCount + "\n";
    5.41 +		}
    5.42  		text = text.replaceAll("\n*$", "");
    5.43  		return text;
    5.44  	}
    5.45 @@ -145,6 +155,22 @@
    5.46  					break;
    5.47  				trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
    5.48  			}
    5.49 +
    5.50 +			// Growth and razorsCount
    5.51 +			String sGrowth = "";
    5.52 +			String sRazors = "";
    5.53 +			Matcher growthMatcher = growthPattern.matcher(metadata);
    5.54 +			if (growthMatcher.matches()) {
    5.55 +				sGrowth = growthMatcher.group(1);
    5.56 +			}
    5.57 +			Matcher razorsMatcher = razorsPattern.matcher(metadata);
    5.58 +			if (razorsMatcher.matches()) {
    5.59 +				sRazors = razorsMatcher.group(1);
    5.60 +			}
    5.61 +			if (sGrowth.length() > 0)
    5.62 +				growth = Integer.parseInt(sGrowth);
    5.63 +			if (sRazors.length() > 0)
    5.64 +				razorsCount = Integer.parseInt(sRazors);
    5.65  		}
    5.66  	}
    5.67  
    5.68 @@ -256,4 +282,16 @@
    5.69  		return robotWaterproof;
    5.70  	}
    5.71  
    5.72 +	public int getGrowth() {
    5.73 +		return growth;
    5.74 +	}
    5.75 +
    5.76 +	public int getRazorsCount() {
    5.77 +		return razorsCount;
    5.78 +	}
    5.79 +
    5.80 +	public void setRazorsCount(int razorsCount) {
    5.81 +		this.razorsCount = razorsCount;
    5.82 +	}
    5.83 +
    5.84  }
     6.1 --- a/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java	Sun Jul 15 21:36:42 2012 +0400
     6.2 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java	Mon Jul 16 00:05:50 2012 +0400
     6.3 @@ -14,7 +14,8 @@
     6.4  	UP("U"),
     6.5  	DOWN("D"),
     6.6  	WAIT("W"),
     6.7 -	ABORT("A");
     6.8 +	ABORT("A"),
     6.9 +	RAZOR("S");
    6.10  
    6.11  	private String	text;
    6.12  
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestBeardsAndRazors.java	Mon Jul 16 00:05:50 2012 +0400
     7.3 @@ -0,0 +1,27 @@
     7.4 +package ru.bosony.tests;
     7.5 +
     7.6 +import static org.junit.Assert.assertEquals;
     7.7 +
     7.8 +import org.junit.Test;
     7.9 +
    7.10 +import ru.bosony.model.game.Game;
    7.11 +import ru.bosony.model.game.GameState;
    7.12 +import ru.bosony.model.maps.ExampleMaps;
    7.13 +import ru.bosony.model.mine.Mine;
    7.14 +
    7.15 +/**
    7.16 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    7.17 + *         15.07.2012 23:46:26
    7.18 + * 
    7.19 + */
    7.20 +public class TestBeardsAndRazors {
    7.21 +
    7.22 +	@Test
    7.23 +	public void test() {
    7.24 +		String map = ExampleMaps.MAP_FROM_BEARDS_AND_RAZORS_TASK.getMap();
    7.25 +		Mine mine = new Mine(map);
    7.26 +		Game game = new Game(mine);
    7.27 +		assertEquals(game.move("DDDLLLLLLLURRRRRRRRRRRRRDDDDDLLLLLLLDDDLLLLLDDRRRRRRRUUURRRRDLDSDRRD"), GameState.Win);
    7.28 +	}
    7.29 +
    7.30 +}