diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/src/SalamanderSearch.java b/src/SalamanderSearch.java index 93c9ab5..594606a 100644 --- a/src/SalamanderSearch.java +++ b/src/SalamanderSearch.java @@ -44,6 +44,88 @@ public static void main(String[] args) { * @throws IllegalArgumentException if the enclosure does not contain a salamander */ public static boolean canReach(char[][] enclosure) { + int[] start = salamanderLocation(enclosure); + boolean[][] visited = new boolean[enclosure.length][enclosure[0].length]; + return canReach(enclosure, start, visited); + } + + //questions: + // + // + public static boolean canReach(char[][] enclosure, int[] current, boolean[][] visited) + { + int curR = current[0]; + int curC = current[1]; + if(enclosure[curR][curC] == 'f') return true; //if the food is found + + if(visited[curR][curC]) return false; //if we've already seen that location + + visited[curR][curC] = true; + + List neighbors = possibleMoves(enclosure, current); //getting the possible places you can move using recursion + for(int[] neighbor : neighbors) + { + if(canReach(enclosure, neighbor, visited)) return true; //?? + } return false; } + + public static List possibleMoves(char[][] enclosure, int[] current) + { + List moves = new ArrayList<>(); + + int curR = current[0]; + int curC = current[1]; + + //UP + int newRow = curR-1; + int newCol = curC; + + if(newRow >= 0 && enclosure[newRow][newCol] != 'W') + { + moves.add(new int[]{newRow,newCol}); + } + //DOWN + newRow = curR+1; + newCol = curC; + + if(newRow < enclosure.length && enclosure[newRow][newCol] != 'W') + { + moves.add(new int[]{newRow,newCol}); + } + //LEFT + newRow = curR; + newCol = curC-1; + if(newCol >= 0 && enclosure[newRow][newCol] != 'W') + { + moves.add(new int[]{newRow,newCol}); + } + + //RIGHT + newRow = curR; + newCol = curC+1; + if(newCol < enclosure[newRow].length && enclosure[newRow][newCol] != 'W') + { + moves.add(new int[]{newRow,newCol}); + } + return moves; + } + //O(n*m): + //n = # of cols + //m = # of rows + public static int[] salamanderLocation(char[][] enclosure) + { + for(int row = 0; row < enclosure.length; row++) + { + for(int col = 0; col < enclosure[0].length; col++) + { + if(enclosure[row][col] == 's') + { + int[] location = new int[] {row,col}; + return location; + } + } + } + throw new IllegalArgumentException("No salamander present"); + } } diff --git a/src/SalamanderSearchTest.java b/src/SalamanderSearchTest.java index ddad9de..a8b5af2 100644 --- a/src/SalamanderSearchTest.java +++ b/src/SalamanderSearchTest.java @@ -34,203 +34,203 @@ public void canReach_NoPath() { assertFalse(actual); } - // @Test - // public void testSalamanderLocation_centerOfGrid() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] expected = {1, 1}; - // assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); - // } - - // @Test - // public void testSalamanderLocation_topLeftCorner() { - // char[][] enclosure = { - // {'s', '.', '.'}, - // {'.', '.', '.'}, - // {'.', '.', '.'} - // }; - // int[] expected = {0, 0}; - // assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); - // } - - // @Test - // public void testSalamanderLocation_notFound_throwsException() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', '.', '.'}, - // {'.', '.', '.'} - // }; - // Exception exception = assertThrows(IllegalArgumentException.class, () -> { - // SalamanderSearch.salamanderLocation(enclosure); - // }); - // assertEquals("No salamander present", exception.getMessage()); - // } - - - // @Test - // public void testSalamanderLocation_at_1_2() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', '.', 's'}, - // {'.', '.', '.'} - // }; - // int[] expected = {1, 2}; - // assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); - // } - - // @Test - // public void testPossibleMoves_allDirectionsOpen() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(4, moves.size()); - // assertTrue(moveSet.contains("0,1")); // up - // assertTrue(moveSet.contains("2,1")); // down - // assertTrue(moveSet.contains("1,0")); // left - // assertTrue(moveSet.contains("1,2")); // right - // } - - // @Test - // public void testPossibleMoves_allDirectionsBlockedByWalls() { - // char[][] enclosure = { - // {'W', 'W', 'W'}, - // {'W', 's', 'W'}, - // {'W', 'W', 'W'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // assertTrue(moves.isEmpty()); - // } - - // @Test - // public void testPossibleMoves_partialEdge() { - // char[][] enclosure = { - // {'s', '.', '.'} - // }; - // int[] location = {0, 0}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(1, moves.size()); - // assertTrue(moveSet.contains("0,1")); // only right - // } - - // @Test - // public void testPossibleMoves_oneOpen_twoWalls_oneEdge() { - // // Salamander at (1, 1) - // // Up is W, down is wall, left is edge (0), right is open - // char[][] enclosure = { - // {'W', 'W', 'W'}, - // {'.', 's', '.'}, - // {'W', 'W', 'W'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(2, moves.size()); - // assertTrue(moveSet.contains("1,2")); - // assertTrue(moveSet.contains("1,0")); - // } - - // @Test - // public void testPossibleMoves_blockedAboveByWall() { - // char[][] enclosure = { - // {'.', 'W', '.'}, - // {'.', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertFalse(moveSet.contains("0,1")); // up blocked - // assertTrue(moveSet.contains("2,1")); // down open - // assertTrue(moveSet.contains("1,0")); // left open - // assertTrue(moveSet.contains("1,2")); // right open - // } - - // @Test - // public void testPossibleMoves_blockedBelowByWall() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', '.'}, - // {'.', 'W', '.'} - // }; - // int[] location = {1, 1}; - // Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); - - // assertTrue(moveSet.contains("0,1")); // up open - // assertFalse(moveSet.contains("2,1")); // down blocked - // assertTrue(moveSet.contains("1,0")); // left open - // assertTrue(moveSet.contains("1,2")); // right open - // } - - // @Test - // public void testPossibleMoves_blockedLeftByWall() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'W', 's', '.'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); - - // assertTrue(moveSet.contains("0,1")); // up open - // assertTrue(moveSet.contains("2,1")); // down open - // assertFalse(moveSet.contains("1,0")); // left blocked - // assertTrue(moveSet.contains("1,2")); // right open - // } - - // @Test - // public void testPossibleMoves_blockedRightByWall() { - // char[][] enclosure = { - // {'.', '.', '.'}, - // {'.', 's', 'W'}, - // {'.', '.', '.'} - // }; - // int[] location = {1, 1}; - // Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); - - // assertTrue(moveSet.contains("0,1")); // up open - // assertTrue(moveSet.contains("2,1")); // down open - // assertTrue(moveSet.contains("1,0")); // left open - // assertFalse(moveSet.contains("1,2")); // right blocked - // } - - // @Test - // public void testPossibleMoves_topLeftCornerWithOneOpen() { - // char[][] enclosure = { - // {'s', 'W'}, - // {'W', '.'} - // }; - // int[] location = {0, 0}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - - // assertTrue(moves.isEmpty()); // surrounded by walls/edges - // } - - // @Test - // public void testPossibleMoves_rightEdgeWithOpenLeft() { - // char[][] enclosure = { - // {'.', '.', 's'} - // }; - // int[] location = {0, 2}; - // List moves = SalamanderSearch.possibleMoves(enclosure, location); - // Set moveSet = toSet(moves); - - // assertEquals(1, moves.size()); - // assertTrue(moveSet.contains("0,1")); - // } + @Test + public void testSalamanderLocation_centerOfGrid() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', '.'}, + {'.', '.', '.'} + }; + int[] expected = {1, 1}; + assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); + } + + @Test + public void testSalamanderLocation_topLeftCorner() { + char[][] enclosure = { + {'s', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + int[] expected = {0, 0}; + assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); + } + + @Test + public void testSalamanderLocation_notFound_throwsException() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + SalamanderSearch.salamanderLocation(enclosure); + }); + assertEquals("No salamander present", exception.getMessage()); + } + + + @Test + public void testSalamanderLocation_at_1_2() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', '.', 's'}, + {'.', '.', '.'} + }; + int[] expected = {1, 2}; + assertArrayEquals(expected, SalamanderSearch.salamanderLocation(enclosure)); + } + + @Test + public void testPossibleMoves_allDirectionsOpen() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', '.'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(4, moves.size()); + assertTrue(moveSet.contains("0,1")); // up + assertTrue(moveSet.contains("2,1")); // down + assertTrue(moveSet.contains("1,0")); // left + assertTrue(moveSet.contains("1,2")); // right + } + + @Test + public void testPossibleMoves_allDirectionsBlockedByWalls() { + char[][] enclosure = { + {'W', 'W', 'W'}, + {'W', 's', 'W'}, + {'W', 'W', 'W'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + assertTrue(moves.isEmpty()); + } + + @Test + public void testPossibleMoves_partialEdge() { + char[][] enclosure = { + {'s', '.', '.'} + }; + int[] location = {0, 0}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(1, moves.size()); + assertTrue(moveSet.contains("0,1")); // only right + } + + @Test + public void testPossibleMoves_oneOpen_twoWalls_oneEdge() { + // Salamander at (1, 1) + // Up is W, down is wall, left is edge (0), right is open + char[][] enclosure = { + {'W', 'W', 'W'}, + {'.', 's', '.'}, + {'W', 'W', 'W'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(2, moves.size()); + assertTrue(moveSet.contains("1,2")); + assertTrue(moveSet.contains("1,0")); + } + + @Test + public void testPossibleMoves_blockedAboveByWall() { + char[][] enclosure = { + {'.', 'W', '.'}, + {'.', 's', '.'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertFalse(moveSet.contains("0,1")); // up blocked + assertTrue(moveSet.contains("2,1")); // down open + assertTrue(moveSet.contains("1,0")); // left open + assertTrue(moveSet.contains("1,2")); // right open + } + + @Test + public void testPossibleMoves_blockedBelowByWall() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', '.'}, + {'.', 'W', '.'} + }; + int[] location = {1, 1}; + Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); + + assertTrue(moveSet.contains("0,1")); // up open + assertFalse(moveSet.contains("2,1")); // down blocked + assertTrue(moveSet.contains("1,0")); // left open + assertTrue(moveSet.contains("1,2")); // right open + } + + @Test + public void testPossibleMoves_blockedLeftByWall() { + char[][] enclosure = { + {'.', '.', '.'}, + {'W', 's', '.'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); + + assertTrue(moveSet.contains("0,1")); // up open + assertTrue(moveSet.contains("2,1")); // down open + assertFalse(moveSet.contains("1,0")); // left blocked + assertTrue(moveSet.contains("1,2")); // right open + } + + @Test + public void testPossibleMoves_blockedRightByWall() { + char[][] enclosure = { + {'.', '.', '.'}, + {'.', 's', 'W'}, + {'.', '.', '.'} + }; + int[] location = {1, 1}; + Set moveSet = toSet(SalamanderSearch.possibleMoves(enclosure, location)); + + assertTrue(moveSet.contains("0,1")); // up open + assertTrue(moveSet.contains("2,1")); // down open + assertTrue(moveSet.contains("1,0")); // left open + assertFalse(moveSet.contains("1,2")); // right blocked + } + + @Test + public void testPossibleMoves_topLeftCornerWithOneOpen() { + char[][] enclosure = { + {'s', 'W'}, + {'W', '.'} + }; + int[] location = {0, 0}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + + assertTrue(moves.isEmpty()); // surrounded by walls/edges + } + + @Test + public void testPossibleMoves_rightEdgeWithOpenLeft() { + char[][] enclosure = { + {'.', '.', 's'} + }; + int[] location = {0, 2}; + List moves = SalamanderSearch.possibleMoves(enclosure, location); + Set moveSet = toSet(moves); + + assertEquals(1, moves.size()); + assertTrue(moveSet.contains("0,1")); + } private Set toSet(List list) {