diff --git a/uil/october-2013/3/java/.idea/workspace.xml b/uil/october-2013/3/java/.idea/workspace.xml index fdfc5a1..68b0d1c 100644 --- a/uil/october-2013/3/java/.idea/workspace.xml +++ b/uil/october-2013/3/java/.idea/workspace.xml @@ -2,15 +2,8 @@ - - - - - - - + + + + + + + + + @@ -218,12 +227,12 @@ - + - - + + @@ -267,12 +276,16 @@ - - + + + + + + diff --git a/uil/october-2013/3/java/input.dat b/uil/october-2013/3/java/input.dat index 12c0be5..908e768 100644 --- a/uil/october-2013/3/java/input.dat +++ b/uil/october-2013/3/java/input.dat @@ -14,7 +14,7 @@ F F | 4 -UUUUULL -UULULUL -UUUUUURUULL -DRDRDD \ No newline at end of file +UOOOULOOLUOORD +UULULULUL +UOOOUORUOLOOOODOLDR +ODDRDDROD \ No newline at end of file diff --git a/uil/october-2013/3/java/out/production/3/Maze.class b/uil/october-2013/3/java/out/production/3/Maze.class index af3752e..0bd7284 100644 Binary files a/uil/october-2013/3/java/out/production/3/Maze.class and b/uil/october-2013/3/java/out/production/3/Maze.class differ diff --git a/uil/october-2013/3/java/out/production/3/Point.class b/uil/october-2013/3/java/out/production/3/Point.class index 206cb6b..36ab2f8 100644 Binary files a/uil/october-2013/3/java/out/production/3/Point.class and b/uil/october-2013/3/java/out/production/3/Point.class differ diff --git a/uil/october-2013/3/java/out/production/3/problem3.class b/uil/october-2013/3/java/out/production/3/problem3.class index de65f6a..52627a1 100644 Binary files a/uil/october-2013/3/java/out/production/3/problem3.class and b/uil/october-2013/3/java/out/production/3/problem3.class differ diff --git a/uil/october-2013/3/java/src/problem3.java b/uil/october-2013/3/java/src/problem3.java index 2677173..3bf1e5f 100644 --- a/uil/october-2013/3/java/src/problem3.java +++ b/uil/october-2013/3/java/src/problem3.java @@ -1,34 +1,55 @@ import static java.lang.System.*; import java.io.File; import java.io.FileNotFoundException; -import java.util.Set; -import java.util.Arrays; -import java.util.List; -import java.util.ArrayList; -import java.util.Scanner; -import java.util.HashSet; +import java.util.*; +// Point representing and (X, Y) coordinate class Point { int x, y; + Point(int x, int y) { this.x = x; this.y = y; } - void merge(Point other) { - this.x += other.x; - this.y += other.y; + + // Returns a point which is the translated end-point based on the other point (offset). + Point merge(Point other) { + return new Point(this.x + other.x, this.y + other.y); } + public String toString() { return String.format("Point(%d, %d)", this.x, this.y); } + + public boolean equals(Point other) { + return this.x == other.x && this.y == other.y; + } + + public static boolean collectionContains(Collection collection, Point point) { + for(Point other : collection) { + if(point.x == other.x && point.y == other.y) { + return true; + } + } + return false; + } + + // Class method that searches a List + public static int listContains(List c, Point p) { + for(Point o : c) { + if(p.x == o.x && p.y == o.y) + return c.indexOf(o); + } + return -1; + } } class Maze { // Offset Constants - List positionOffsets = Arrays.asList(new Point(0, 1), new Point(1, 0), new Point(0, -1), new Point(-1, 0)); - List nameOffsets = Arrays.asList("U R D L".split(" ")); + public List positionOffsets = Arrays.asList(new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1)); + private List nameOffsets = Arrays.asList("D R U L".split(" ")); // Maze Data - private List snake = new ArrayList();; + List snake = new ArrayList();; private List pellets = new ArrayList(); private char[][] rawMatrix; @@ -38,14 +59,18 @@ class Maze { for(int x = 0; x < 15; x++) { for(int y = 0; y < 15; y++) { switch(rawMatrix[x][y]) { + // Empty Space case ' ': break; + // Point Found case 'X': this.snake.add(new Point(x, y)); break; + // Pellet Found case 'F': this.pellets.add(new Point(x, y)); break; + // Should not be found based on input data. Something has gone wrong/input data is invalid. default: out.println("Possibly faulty Maze Input with item \"" + rawMatrix[x][y] + "\" found."); break; @@ -56,27 +81,33 @@ class Maze { // Simulate the snake game using instructions String simulate(String input) { - char curDirection = 'R'; + String curDirection = "R"; int score = 0; for(int i = 0; i < input.length(); i++) { // Calculate the offset based on the current instruction Point offset; - char curOffset = input.charAt(i); - if(curOffset == 'O') + String curOffset = input.substring(i, i + 1); + if(!curDirection.equals(curOffset) && !curOffset.equals("O")) + curDirection = curOffset; + if(curOffset.equals("O")) offset = positionOffsets.get(nameOffsets.indexOf(curDirection)); else - offset = positionOffsets.get(nameOffsets.indexOf(input.substring(i, i+1))); - out.println(offset); + offset = positionOffsets.get(nameOffsets.indexOf(curOffset)); // Calculate the new point, ensure it's real - Point newPoint = snake.get(snake.size() -1); - newPoint.merge(offset); + Point newPoint = snake.get(snake.size() - 1); + newPoint = newPoint.merge(offset); + // New Point was out of bounds, cannot continue if(!this.inBounds(newPoint)) - return "GAME OVER @ " + newPoint; + return "GAME OVER"; // Add new point, discard end of snake to simulate it's movement - snake.add(newPoint); snake.remove(0); - // Check if we're at a pellet - score += pellets.contains(newPoint) ? 1 : 0; + snake.add(newPoint); + // Check if we're at a pellet, add score. + int pelletIndex = Point.listContains(pellets, newPoint); + if(pelletIndex != -1) { + score += 1; + this.pellets.remove(pelletIndex); + } } return Integer.toString(score); } @@ -91,7 +122,9 @@ class Maze { for(int x = 0; x < 15; x++) { String[] temp = new String[15]; for(int y = 0; y < 15; y++) { - temp[y] = Character.toString(rawMatrix[x][y]); + Point point = new Point(x, y); + // Priority: Empty Space < Pellet < Snake + temp[y] = Point.collectionContains(this.snake, point) ? "X" : (Point.collectionContains(this.pellets, point) ? "F" : " "); } lines[x] = String.join(" - ", temp); } @@ -113,8 +146,7 @@ public class problem3 { rawMatrix[x][y] = line.charAt(y); } } - Maze originalMaze = new Maze(rawMatrix); - + // Read each of the inputs int lines = Integer.parseInt(read.nextLine()); String[] inputs = new String[lines]; @@ -123,10 +155,10 @@ public class problem3 { // Simulate inputs for(String input : inputs) { - Maze temp = originalMaze; - out.println(temp.simulate(input)); - out.println(temp); - } + Maze maze = new Maze(rawMatrix); + String pellets = maze.simulate(input); + // Uncomment to display the final position of the maze +// out.println(maze); + out.println(pellets); } } - } \ No newline at end of file