Add trampolines and targets
authorindvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 21:20:04 +0400
changeset 27505c25412b16
parent 26 964aa8102804
child 28 9c68562715ed
Add trampolines and targets
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/test/java/ru/bosony/tests/TestTrampolines.java
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java	Sun Jul 15 19:26:28 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java	Sun Jul 15 21:20:04 2012 +0400
     1.3 @@ -1,5 +1,8 @@
     1.4  package ru.bosony.model.cellscontents;
     1.5  
     1.6 +import java.util.HashSet;
     1.7 +import java.util.Set;
     1.8 +
     1.9  import ru.bosony.model.io.TextRepresentable;
    1.10  
    1.11  /**
    1.12 @@ -8,7 +11,7 @@
    1.13   * 
    1.14   */
    1.15  public enum CellContent implements TextRepresentable {
    1.16 -	
    1.17 +
    1.18  	ClosedLambdaLift("L"),
    1.19  	Earth("."),
    1.20  	Empty(" "),
    1.21 @@ -16,9 +19,50 @@
    1.22  	MiningRobot("R"),
    1.23  	OpenLambdaLift("O"),
    1.24  	Rock("*"),
    1.25 -	Wall("#");
    1.26 +	Wall("#"),
    1.27 +	TrampolineA("A"),
    1.28 +	TrampolineB("B"),
    1.29 +	TrampolineC("C"),
    1.30 +	TrampolineD("D"),
    1.31 +	TrampolineE("E"),
    1.32 +	TrampolineF("F"),
    1.33 +	TrampolineG("G"),
    1.34 +	TrampolineH("H"),
    1.35 +	TrampolineI("I"),
    1.36 +	Target1("1"),
    1.37 +	Target2("2"),
    1.38 +	Target3("3"),
    1.39 +	Target4("4"),
    1.40 +	Target5("5"),
    1.41 +	Target6("6"),
    1.42 +	Target7("7"),
    1.43 +	Target8("8"),
    1.44 +	Target9("9");
    1.45  
    1.46 -	private String	text;
    1.47 +	private String					text;
    1.48 +	private CellContent				trampolineTarget	= null;
    1.49 +	private static Set<CellContent>	trampolines			= new HashSet<CellContent>();
    1.50 +	private static Set<CellContent>	targets				= new HashSet<CellContent>();
    1.51 +	static {
    1.52 +		trampolines.add(TrampolineA);
    1.53 +		trampolines.add(TrampolineB);
    1.54 +		trampolines.add(TrampolineC);
    1.55 +		trampolines.add(TrampolineD);
    1.56 +		trampolines.add(TrampolineE);
    1.57 +		trampolines.add(TrampolineF);
    1.58 +		trampolines.add(TrampolineG);
    1.59 +		trampolines.add(TrampolineH);
    1.60 +		trampolines.add(TrampolineI);
    1.61 +		targets.add(Target1);
    1.62 +		targets.add(Target2);
    1.63 +		targets.add(Target3);
    1.64 +		targets.add(Target4);
    1.65 +		targets.add(Target5);
    1.66 +		targets.add(Target6);
    1.67 +		targets.add(Target7);
    1.68 +		targets.add(Target8);
    1.69 +		targets.add(Target9);
    1.70 +	}
    1.71  
    1.72  	private CellContent(String text) {
    1.73  		this.text = text;
    1.74 @@ -37,4 +81,22 @@
    1.75  		return null;
    1.76  	}
    1.77  
    1.78 +	public CellContent getTrampolineTarget() {
    1.79 +		return trampolineTarget;
    1.80 +	}
    1.81 +
    1.82 +	public void setTrampolineTarget(CellContent trampolineTarget) {
    1.83 +		if (!trampolines.contains(this) || trampolineTarget != null && !targets.contains(trampolineTarget))
    1.84 +			throw new RuntimeException("Only references from Trampolines to Targets allowed");
    1.85 +		this.trampolineTarget = trampolineTarget;
    1.86 +	}
    1.87 +
    1.88 +	public static Set<CellContent> getTrampolines() {
    1.89 +		return trampolines;
    1.90 +	}
    1.91 +
    1.92 +	public static Set<CellContent> getTargets() {
    1.93 +		return targets;
    1.94 +	}
    1.95 +
    1.96  }
     2.1 --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 19:26:28 2012 +0400
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 21:20:04 2012 +0400
     2.3 @@ -114,6 +114,20 @@
     2.4  				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
     2.5  			moveRobot(nextRobotCell);
     2.6  			mine.getCell(nextRobotCoord.left()).setContent(Rock);
     2.7 +		} else if (CellContent.getTrampolines().contains(nextRobotCellContent)
     2.8 +				&& CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
     2.9 +			CellContent trampoline = nextRobotCellContent;
    2.10 +			CellContent target = trampoline.getTrampolineTarget();
    2.11 +			for (Cell targetCell : mine.findCells(target)) {
    2.12 +				moveRobot(targetCell);
    2.13 +			}
    2.14 +			for (CellContent tramp : CellContent.getTrampolines()) {
    2.15 +				if (tramp.getTrampolineTarget() == target) {
    2.16 +					for (Cell trampolineCell : mine.findCells(tramp)) {
    2.17 +						trampolineCell.setContent(Empty);
    2.18 +					}
    2.19 +				}
    2.20 +			}
    2.21  		} else {
    2.22  			mov = Movement.WAIT;
    2.23  		}
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java	Sun Jul 15 19:26:28 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/maps/ExampleMaps.java	Sun Jul 15 21:20:04 2012 +0400
     3.3 @@ -353,6 +353,32 @@
     3.4  			"#.............#\n" + 
     3.5  			"#.........   .#\n" + 
     3.6  			"#############L#"
     3.7 +			),
     3.8 +	/**
     3.9 +	 * <pre>
    3.10 +			##L###########
    3.11 +			#.....R#.**..#
    3.12 +			#.*A...#..1..#
    3.13 +			#.*....#.  \.#
    3.14 +			#.\\\..#...\.#
    3.15 +			#2.....**B...#
    3.16 +			##############
    3.17 +			
    3.18 +			Trampoline A targets 1
    3.19 +			Trampoline B targets 2
    3.20 +	 * </pre>
    3.21 +	 */
    3.22 +	MAP_FROM_TRAMPOLINES_TASK(
    3.23 +			"##L###########\n" + 
    3.24 +			"#.....R#.**..#\n" + 
    3.25 +			"#.*A...#..1..#\n" + 
    3.26 +			"#.*....#.  \\.#\n" + 
    3.27 +			"#.\\\\\\..#...\\.#\n" + 
    3.28 +			"#2.....**B...#\n" + 
    3.29 +			"##############\n" + 
    3.30 +			"\n" + 
    3.31 +			"Trampoline A targets 1\n" + 
    3.32 +			"Trampoline B targets 2"
    3.33  			);
    3.34  
    3.35  	private String	map;
     4.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 19:26:28 2012 +0400
     4.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 21:20:04 2012 +0400
     4.3 @@ -28,14 +28,17 @@
     4.4  	protected Cell[][]			cells;
     4.5  	protected int				sizeX;
     4.6  	protected int				sizeY;
     4.7 -	protected int				waterLevel			= 0;
     4.8 -	protected int				flooding			= 0;
     4.9 -	protected int				robotWaterproof		= 10;
    4.10 -	protected static Pattern	minePattern			= Pattern
    4.11 -															.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)");
    4.12 -	protected static Pattern	waterPattern		= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    4.13 -	protected static Pattern	floodingPattern		= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    4.14 -	protected static Pattern	waterproofPattern	= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    4.15 +	protected int				waterLevel				= 0;
    4.16 +	protected int				flooding				= 0;
    4.17 +	protected int				robotWaterproof			= 10;
    4.18 +	protected static Pattern	minePattern				= Pattern
    4.19 +																.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*|Trampoline.*)|$)");
    4.20 +	protected static Pattern	waterPattern			= Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
    4.21 +	protected static Pattern	floodingPattern			= Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
    4.22 +	protected static Pattern	waterproofPattern		= Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
    4.23 +	protected static Pattern	trampolinePattern		= Pattern
    4.24 +																.compile("(?s).*?Trampoline ([A-I]) targets ([1-9]).*");
    4.25 +	protected static String		firstTrampolineRegexp	= "(?s).*?Trampoline ([A-I]) targets ([1-9])";
    4.26  
    4.27  	public Mine(String text) {
    4.28  		fromText(text);
    4.29 @@ -57,12 +60,18 @@
    4.30  			text += "\n";
    4.31  		}
    4.32  		text = text.replaceAll("\n*$", "");
    4.33 +		text += "\n\n";
    4.34  		if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
    4.35 -			text += "\n\n";
    4.36  			text += "Water " + waterLevel + "\n";
    4.37  			text += "Flooding " + flooding + "\n";
    4.38 -			text += "Waterproof " + robotWaterproof;
    4.39 +			text += "Waterproof " + robotWaterproof + "\n";
    4.40  		}
    4.41 +		for (CellContent trampoline : CellContent.values()) {
    4.42 +			if (trampoline.getTrampolineTarget() != null)
    4.43 +				text += "Trampoline " + trampoline.toText() + " targets " + trampoline.getTrampolineTarget().toText()
    4.44 +						+ "\n";
    4.45 +		}
    4.46 +		text = text.replaceAll("\n*$", "");
    4.47  		return text;
    4.48  	}
    4.49  
    4.50 @@ -97,27 +106,46 @@
    4.51  		}
    4.52  
    4.53  		String metadata = matcher.group(3);
    4.54 -		String sWaterLevel = "";
    4.55 -		String sFlooding = "";
    4.56 -		String sRobotWaterproof = "";
    4.57 -		Matcher waterMatcher = waterPattern.matcher(metadata);
    4.58 -		if (waterMatcher.matches()) {
    4.59 -			sWaterLevel = waterMatcher.group(1);
    4.60 +		if (metadata != null) {
    4.61 +			// Water flooding
    4.62 +			String sWaterLevel = "";
    4.63 +			String sFlooding = "";
    4.64 +			String sRobotWaterproof = "";
    4.65 +			Matcher waterMatcher = waterPattern.matcher(metadata);
    4.66 +			if (waterMatcher.matches()) {
    4.67 +				sWaterLevel = waterMatcher.group(1);
    4.68 +			}
    4.69 +			Matcher floodingMatcher = floodingPattern.matcher(metadata);
    4.70 +			if (floodingMatcher.matches()) {
    4.71 +				sFlooding = floodingMatcher.group(1);
    4.72 +			}
    4.73 +			Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
    4.74 +			if (waterproofMatcher.matches()) {
    4.75 +				sRobotWaterproof = waterproofMatcher.group(1);
    4.76 +			}
    4.77 +			if (sWaterLevel.length() > 0)
    4.78 +				waterLevel = Integer.parseInt(sWaterLevel);
    4.79 +			if (sFlooding.length() > 0)
    4.80 +				flooding = Integer.parseInt(sFlooding);
    4.81 +			if (sRobotWaterproof.length() > 0)
    4.82 +				robotWaterproof = Integer.parseInt(sRobotWaterproof);
    4.83 +
    4.84 +			// Trampolines and targets
    4.85 +			String trampolines = metadata;
    4.86 +			while (true) {
    4.87 +				Matcher trampolineMatcher = trampolinePattern.matcher(trampolines);
    4.88 +				if (trampolineMatcher.matches()) {
    4.89 +					String sTrampoline = trampolineMatcher.group(1);
    4.90 +					String sTarget = trampolineMatcher.group(2);
    4.91 +					CellContent trampoline = CellContent.fromText(sTrampoline);
    4.92 +					CellContent target = CellContent.fromText(sTarget);
    4.93 +					if (trampoline != null && target != null)
    4.94 +						trampoline.setTrampolineTarget(target);
    4.95 +				} else
    4.96 +					break;
    4.97 +				trampolines = trampolines.replaceFirst(firstTrampolineRegexp, "");
    4.98 +			}
    4.99  		}
   4.100 -		Matcher floodingMatcher = floodingPattern.matcher(metadata);
   4.101 -		if (floodingMatcher.matches()) {
   4.102 -			sFlooding = floodingMatcher.group(1);
   4.103 -		}
   4.104 -		Matcher waterproofMatcher = waterproofPattern.matcher(metadata);
   4.105 -		if (waterproofMatcher.matches()) {
   4.106 -			sRobotWaterproof = waterproofMatcher.group(1);
   4.107 -		}
   4.108 -		if (sWaterLevel.length() > 0)
   4.109 -			waterLevel = Integer.parseInt(sWaterLevel);
   4.110 -		if (sFlooding.length() > 0)
   4.111 -			flooding = Integer.parseInt(sFlooding);
   4.112 -		if (sRobotWaterproof.length() > 0)
   4.113 -			robotWaterproof = Integer.parseInt(sRobotWaterproof);
   4.114  	}
   4.115  
   4.116  	public Cell getCell(Coordinate coordinate) {
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestTrampolines.java	Sun Jul 15 21:20:04 2012 +0400
     5.3 @@ -0,0 +1,27 @@
     5.4 +package ru.bosony.tests;
     5.5 +
     5.6 +import static org.junit.Assert.assertEquals;
     5.7 +
     5.8 +import org.junit.Test;
     5.9 +
    5.10 +import ru.bosony.model.game.Game;
    5.11 +import ru.bosony.model.game.GameState;
    5.12 +import ru.bosony.model.maps.ExampleMaps;
    5.13 +import ru.bosony.model.mine.Mine;
    5.14 +
    5.15 +/**
    5.16 + * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
    5.17 + * 			15.07.2012 20:51:52
    5.18 + *
    5.19 + */
    5.20 +public class TestTrampolines {
    5.21 +
    5.22 +	@Test
    5.23 +	public void test() {
    5.24 +		String map = ExampleMaps.MAP_FROM_TRAMPOLINES_TASK.getMap();
    5.25 +		Mine mine = new Mine(map);
    5.26 +		Game game = new Game(mine);
    5.27 +		assertEquals(game.move("DLLLRDDDLLURRRUUULLU"), GameState.Win);
    5.28 +	}
    5.29 +
    5.30 +}