# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342382750 -14400 # Node ID 66aa96e8fd92d007d58022232c5cb69f033e3443 # Parent 9c68562715ed989d0da486da5b87eac7cb3b45a9 Add beards and razors diff -r 9c68562715ed -r 66aa96e8fd92 icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java --- a/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java Sun Jul 15 21:36:42 2012 +0400 +++ b/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java Mon Jul 16 00:05:50 2012 +0400 @@ -57,6 +57,8 @@ writer.newLine(); writer.write("Underwater: " + game.getUnderwater() + " of " + mine.getRobotWaterproof()); writer.newLine(); + writer.write("Razors: " + mine.getRazorsCount()); + writer.newLine(); writer.newLine(); writer.write(mine.toText()); writer.newLine(); diff -r 9c68562715ed -r 66aa96e8fd92 icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java Sun Jul 15 21:36:42 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java Mon Jul 16 00:05:50 2012 +0400 @@ -37,7 +37,9 @@ Target6("6"), Target7("7"), Target8("8"), - Target9("9"); + Target9("9"), + WadlersBeard("W"), + HuttonsRazor("!"); private String text; private CellContent trampolineTarget = null; diff -r 9c68562715ed -r 66aa96e8fd92 icfp-model/src/main/java/ru/bosony/model/game/Game.java --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sun Jul 15 21:36:42 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Mon Jul 16 00:05:50 2012 +0400 @@ -7,6 +7,8 @@ import static ru.bosony.model.cellscontents.CellContent.MiningRobot; import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; import static ru.bosony.model.cellscontents.CellContent.Rock; +import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor; +import static ru.bosony.model.cellscontents.CellContent.WadlersBeard; import java.util.ArrayList; import java.util.List; @@ -66,7 +68,8 @@ // Moving and scoring route.add(mov); - Coordinate robotCoord = mine.getRobotCell().getCoordinate(); + Cell robotCell = mine.getRobotCell(); + Coordinate robotCoord = robotCell.getCoordinate(); Coordinate nextRobotCoord = null; switch (mov) { case ABORT: @@ -77,6 +80,16 @@ return state; case WAIT: break; + case RAZOR: + if (mine.getRazorsCount() > 0) { + for (Cell cell : mine.getNeighboringCells(robotCell)) { + if (cell.getContent() == WadlersBeard) { + cell.setContent(Empty); + } + } + mine.setRazorsCount(mine.getRazorsCount() - 1); + } + break; case LEFT: nextRobotCoord = robotCoord.left(); break; @@ -106,6 +119,9 @@ lastScoreChange = score - prevScore; state = GameState.Win; return state; + } else if (nextRobotCellContent == HuttonsRazor) { + moveRobot(nextRobotCell); + mine.setRazorsCount(mine.getRazorsCount() + 1); } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock && getContent(mine.getCell(nextRobotCoord.right())) == Empty) { moveRobot(nextRobotCell); @@ -173,6 +189,24 @@ getCell(rightAndDownCoord, newCells).setContent(Rock); } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) { getCell(curCoord, newCells).setContent(OpenLambdaLift); + } else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0 + && route.size() % mine.getGrowth() == 0) { + if (getContent(leftCell) == Empty) + getCell(leftCoord, newCells).setContent(WadlersBeard); + if (getContent(upCell) == Empty) + getCell(upCoord, newCells).setContent(WadlersBeard); + if (getContent(rightCell) == Empty) + getCell(rightCoord, newCells).setContent(WadlersBeard); + if (getContent(downCell) == Empty) + getCell(downCoord, newCells).setContent(WadlersBeard); + if (getContent(leftAndUpCell) == Empty) + getCell(leftAndUpCoord, newCells).setContent(WadlersBeard); + if (getContent(rightAndUpCell) == Empty) + getCell(rightAndUpCoord, newCells).setContent(WadlersBeard); + if (getContent(leftAndDownCell) == Empty) + getCell(leftAndDownCoord, newCells).setContent(WadlersBeard); + if (getContent(rightAndDownCell) == Empty) + getCell(rightAndDownCoord, newCells).setContent(WadlersBeard); } } } diff -r 9c68562715ed -r 66aa96e8fd92 icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java --- a/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java Sun Jul 15 21:36:42 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java Mon Jul 16 00:05:50 2012 +0400 @@ -379,6 +379,48 @@ "\n" + "Trampoline A targets 1\n" + "Trampoline B targets 2" + ), + /** + *
+		###############
+		#***...R......#
+		#***... ...*..#
+		#\\\!.. ..\\\.#
+		#...... ...*..#
+		#..     .. ...#
+		#.... .... ...#
+		#.... .... ...#
+		#.. .  !    ..#
+		#..*. .. . ...#
+		#.... .. . ...#
+		#.\.. .... ..*#
+		#......... . .#
+		#!....... W   #
+		#############L#
+		
+		Growth 25
+		Razors 1
+	 * 
+ */ + MAP_FROM_BEARDS_AND_RAZORS_TASK( + "###############\n" + + "#***...R......#\n" + + "#***... ...*..#\n" + + "#\\\\\\!.. ..\\\\\\.#\n" + + "#...... ...*..#\n" + + "#.. .. ...#\n" + + "#.... .... ...#\n" + + "#.... .... ...#\n" + + "#.. . ! ..#\n" + + "#..*. .. . ...#\n" + + "#.... .. . ...#\n" + + "#.\\.. .... ..*#\n" + + "#......... . .#\n" + + "#!....... W #\n" + + "#############L#\n" + + "\n" + + "Growth 25\n" + + "Razors 1" ); private String map; diff -r 9c68562715ed -r 66aa96e8fd92 icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 21:36:42 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Mon Jul 16 00:05:50 2012 +0400 @@ -31,11 +31,15 @@ protected int waterLevel = 0; protected int flooding = 0; protected int robotWaterproof = 10; + protected int growth = 25; + protected int razorsCount = 0; protected static Pattern minePattern = Pattern - .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)"); + .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*|Growth.*|Razors.*)|$)"); protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*"); protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*"); protected static Pattern waterproofPattern = Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*"); + protected static Pattern growthPattern = Pattern.compile("(?s).*(?<=.*Growth )(\\d+).*"); + protected static Pattern razorsPattern = Pattern.compile("(?s).*(?<=.*Razors )(\\d+).*"); protected static Pattern trampolinePattern = Pattern .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*"); protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])"; @@ -61,16 +65,22 @@ } text = text.replaceAll("\n*$", ""); text += "\n\n"; - if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) { + if (waterLevel != 0) { text += "Water " + waterLevel + "\n"; + } + if (flooding != 0) { text += "Flooding " + flooding + "\n"; - text += "Waterproof " + robotWaterproof + "\n"; } + text += "Waterproof " + robotWaterproof + "\n"; for (CellContent trampoline : CellContent.values()) { if (trampoline.getTrampolineTarget() != null) text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText() + "\n"; } + text += "Growth " + growth + "\n"; + if (razorsCount != 0) { + text += "Razors " + razorsCount + "\n"; + } text = text.replaceAll("\n*$", ""); return text; } @@ -145,6 +155,22 @@ break; trampolines = trampolines.replaceFirst(firstTrampolineRegexp, ""); } + + // Growth and razorsCount + String sGrowth = ""; + String sRazors = ""; + Matcher growthMatcher = growthPattern.matcher(metadata); + if (growthMatcher.matches()) { + sGrowth = growthMatcher.group(1); + } + Matcher razorsMatcher = razorsPattern.matcher(metadata); + if (razorsMatcher.matches()) { + sRazors = razorsMatcher.group(1); + } + if (sGrowth.length() > 0) + growth = Integer.parseInt(sGrowth); + if (sRazors.length() > 0) + razorsCount = Integer.parseInt(sRazors); } } @@ -256,4 +282,16 @@ return robotWaterproof; } + public int getGrowth() { + return growth; + } + + public int getRazorsCount() { + return razorsCount; + } + + public void setRazorsCount(int razorsCount) { + this.razorsCount = razorsCount; + } + } diff -r 9c68562715ed -r 66aa96e8fd92 icfp-model/src/main/java/ru/bosony/model/moving/Movement.java --- a/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Sun Jul 15 21:36:42 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Mon Jul 16 00:05:50 2012 +0400 @@ -14,7 +14,8 @@ UP("U"), DOWN("D"), WAIT("W"), - ABORT("A"); + ABORT("A"), + RAZOR("S"); private String text; diff -r 9c68562715ed -r 66aa96e8fd92 icfp-model/src/test/java/ru/bosony/tests/TestBeardsAndRazors.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestBeardsAndRazors.java Mon Jul 16 00:05:50 2012 +0400 @@ -0,0 +1,27 @@ +package ru.bosony.tests; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import ru.bosony.model.game.Game; +import ru.bosony.model.game.GameState; +import ru.bosony.model.maps.ExampleMaps; +import ru.bosony.model.mine.Mine; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 15.07.2012 23:46:26 + * + */ +public class TestBeardsAndRazors { + + @Test + public void test() { + String map = ExampleMaps.MAP_FROM_BEARDS_AND_RAZORS_TASK.getMap(); + Mine mine = new Mine(map); + Game game = new Game(mine); + assertEquals(game.move("DDDLLLLLLLURRRRRRRRRRRRRDDDDDLLLLLLLDDDLLLLLDDRRRRRRRUUURRRRDLDSDRRD"), GameState.Win); + } + +}