# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342372804 -14400 # Node ID 505c25412b16f5a481c7a20011b2a39beda11fb7 # Parent 964aa8102804d76c9d82ea08d131e311301e3ef6 Add trampolines and targets diff -r 964aa8102804 -r 505c25412b16 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 19:26:28 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java Sun Jul 15 21:20:04 2012 +0400 @@ -1,5 +1,8 @@ package ru.bosony.model.cellscontents; +import java.util.HashSet; +import java.util.Set; + import ru.bosony.model.io.TextRepresentable; /** @@ -8,7 +11,7 @@ * */ public enum CellContent implements TextRepresentable { - + ClosedLambdaLift("L"), Earth("."), Empty(" "), @@ -16,9 +19,50 @@ MiningRobot("R"), OpenLambdaLift("O"), Rock("*"), - Wall("#"); + Wall("#"), + TrampolineA("A"), + TrampolineB("B"), + TrampolineC("C"), + TrampolineD("D"), + TrampolineE("E"), + TrampolineF("F"), + TrampolineG("G"), + TrampolineH("H"), + TrampolineI("I"), + Target1("1"), + Target2("2"), + Target3("3"), + Target4("4"), + Target5("5"), + Target6("6"), + Target7("7"), + Target8("8"), + Target9("9"); - private String text; + private String text; + private CellContent trampolineTarget = null; + private static Set trampolines = new HashSet(); + private static Set targets = new HashSet(); + static { + trampolines.add(TrampolineA); + trampolines.add(TrampolineB); + trampolines.add(TrampolineC); + trampolines.add(TrampolineD); + trampolines.add(TrampolineE); + trampolines.add(TrampolineF); + trampolines.add(TrampolineG); + trampolines.add(TrampolineH); + trampolines.add(TrampolineI); + targets.add(Target1); + targets.add(Target2); + targets.add(Target3); + targets.add(Target4); + targets.add(Target5); + targets.add(Target6); + targets.add(Target7); + targets.add(Target8); + targets.add(Target9); + } private CellContent(String text) { this.text = text; @@ -37,4 +81,22 @@ return null; } + public CellContent getTrampolineTarget() { + return trampolineTarget; + } + + public void setTrampolineTarget(CellContent trampolineTarget) { + if (!trampolines.contains(this) || trampolineTarget != null && !targets.contains(trampolineTarget)) + throw new RuntimeException("Only references from Trampolines to Targets allowed"); + this.trampolineTarget = trampolineTarget; + } + + public static Set getTrampolines() { + return trampolines; + } + + public static Set getTargets() { + return targets; + } + } diff -r 964aa8102804 -r 505c25412b16 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 19:26:28 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sun Jul 15 21:20:04 2012 +0400 @@ -114,6 +114,20 @@ && getContent(mine.getCell(nextRobotCoord.left())) == Empty) { moveRobot(nextRobotCell); mine.getCell(nextRobotCoord.left()).setContent(Rock); + } else if (CellContent.getTrampolines().contains(nextRobotCellContent) + && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) { + CellContent trampoline = nextRobotCellContent; + CellContent target = trampoline.getTrampolineTarget(); + for (Cell targetCell : mine.findCells(target)) { + moveRobot(targetCell); + } + for (CellContent tramp : CellContent.getTrampolines()) { + if (tramp.getTrampolineTarget() == target) { + for (Cell trampolineCell : mine.findCells(tramp)) { + trampolineCell.setContent(Empty); + } + } + } } else { mov = Movement.WAIT; } diff -r 964aa8102804 -r 505c25412b16 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 19:26:28 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java Sun Jul 15 21:20:04 2012 +0400 @@ -353,6 +353,32 @@ "#.............#\n" + "#......... .#\n" + "#############L#" + ), + /** + *
+			##L###########
+			#.....R#.**..#
+			#.*A...#..1..#
+			#.*....#.  \.#
+			#.\\\..#...\.#
+			#2.....**B...#
+			##############
+			
+			Trampoline A targets 1
+			Trampoline B targets 2
+	 * 
+ */ + MAP_FROM_TRAMPOLINES_TASK( + "##L###########\n" + + "#.....R#.**..#\n" + + "#.*A...#..1..#\n" + + "#.*....#. \\.#\n" + + "#.\\\\\\..#...\\.#\n" + + "#2.....**B...#\n" + + "##############\n" + + "\n" + + "Trampoline A targets 1\n" + + "Trampoline B targets 2" ); private String map; diff -r 964aa8102804 -r 505c25412b16 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 19:26:28 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 21:20:04 2012 +0400 @@ -28,14 +28,17 @@ protected Cell[][] cells; protected int sizeX; protected int sizeY; - protected int waterLevel = 0; - protected int flooding = 0; - protected int robotWaterproof = 10; - protected static Pattern minePattern = Pattern - .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)"); - 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 int waterLevel = 0; + protected int flooding = 0; + protected int robotWaterproof = 10; + protected static Pattern minePattern = Pattern + .compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)"); + 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 trampolinePattern = Pattern + .compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*"); + protected static String firstTrampolineRegexp = "(?s).*?Trampoline ([A-I]) targets ([1-9])"; public Mine(String text) { fromText(text); @@ -57,12 +60,18 @@ text += "\n"; } text = text.replaceAll("\n*$", ""); + text += "\n\n"; if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) { - text += "\n\n"; text += "Water " + waterLevel + "\n"; text += "Flooding " + flooding + "\n"; - text += "Waterproof " + robotWaterproof; + text += "Waterproof " + robotWaterproof + "\n"; } + for (CellContent trampoline : CellContent.values()) { + if (trampoline.getTrampolineTarget() != null) + text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText() + + "\n"; + } + text = text.replaceAll("\n*$", ""); return text; } @@ -97,27 +106,46 @@ } String metadata = matcher.group(3); - String sWaterLevel = ""; - String sFlooding = ""; - String sRobotWaterproof = ""; - Matcher waterMatcher = waterPattern.matcher(metadata); - if (waterMatcher.matches()) { - sWaterLevel = waterMatcher.group(1); + if (metadata != null) { + // Water flooding + String sWaterLevel = ""; + String sFlooding = ""; + String sRobotWaterproof = ""; + Matcher waterMatcher = waterPattern.matcher(metadata); + if (waterMatcher.matches()) { + sWaterLevel = waterMatcher.group(1); + } + Matcher floodingMatcher = floodingPattern.matcher(metadata); + if (floodingMatcher.matches()) { + sFlooding = floodingMatcher.group(1); + } + Matcher waterproofMatcher = waterproofPattern.matcher(metadata); + if (waterproofMatcher.matches()) { + sRobotWaterproof = waterproofMatcher.group(1); + } + if (sWaterLevel.length() > 0) + waterLevel = Integer.parseInt(sWaterLevel); + if (sFlooding.length() > 0) + flooding = Integer.parseInt(sFlooding); + if (sRobotWaterproof.length() > 0) + robotWaterproof = Integer.parseInt(sRobotWaterproof); + + // Trampolines and targets + String trampolines = metadata; + while (true) { + Matcher trampolineMatcher = trampolinePattern.matcher(trampolines); + if (trampolineMatcher.matches()) { + String sTrampoline = trampolineMatcher.group(1); + String sTarget = trampolineMatcher.group(2); + CellContent trampoline = CellContent.fromText(sTrampoline); + CellContent target = CellContent.fromText(sTarget); + if (trampoline != null && target != null) + trampoline.setTrampolineTarget(target); + } else + break; + trampolines = trampolines.replaceFirst(firstTrampolineRegexp, ""); + } } - Matcher floodingMatcher = floodingPattern.matcher(metadata); - if (floodingMatcher.matches()) { - sFlooding = floodingMatcher.group(1); - } - Matcher waterproofMatcher = waterproofPattern.matcher(metadata); - if (waterproofMatcher.matches()) { - sRobotWaterproof = waterproofMatcher.group(1); - } - if (sWaterLevel.length() > 0) - waterLevel = Integer.parseInt(sWaterLevel); - if (sFlooding.length() > 0) - flooding = Integer.parseInt(sFlooding); - if (sRobotWaterproof.length() > 0) - robotWaterproof = Integer.parseInt(sRobotWaterproof); } public Cell getCell(Coordinate coordinate) { diff -r 964aa8102804 -r 505c25412b16 icfp-model/src/test/java/ru/bosony/tests/TestTrampolines.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestTrampolines.java Sun Jul 15 21:20:04 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 20:51:52 + * + */ +public class TestTrampolines { + + @Test + public void test() { + String map = ExampleMaps.MAP_FROM_TRAMPOLINES_TASK.getMap(); + Mine mine = new Mine(map); + Game game = new Game(mine); + assertEquals(game.move("DLLLRDDDLLURRRUUULLU"), GameState.Win); + } + +}