From 5bb199d985cde5cf99821ebd1ddc95e70a24a6a5 Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Thu, 3 Apr 2025 14:47:49 -0700 Subject: [PATCH 1/4] added DFS method to check if salamander can reach the food --- src/SalamanderSearch.java | 80 +++++++ src/SalamanderSearchTest.java | 394 +++++++++++++++++----------------- 2 files changed, 277 insertions(+), 197 deletions(-) diff --git a/src/SalamanderSearch.java b/src/SalamanderSearch.java index 688cda7..051adff 100644 --- a/src/SalamanderSearch.java +++ b/src/SalamanderSearch.java @@ -43,6 +43,86 @@ public static void main(String[] args) { * @return whether the salamander can reach the food */ public static boolean canReach(char[][] enclosure) { + int [] start = salamanderLocation(enclosure); + boolean[][] visited = new boolean[enclosure.length][enclosure[0].length]; + return canReachHelper(enclosure, start, visited); + } + + public static boolean canReachHelper(char[][] enclosure, int[] current, boolean[][] visited) { + int curR = current[0]; + int curC = current[1]; + + if (visited[curR][curC]) { + return false; + } + + if (enclosure[curR][curC] == 'f') { + return true; + } + + visited[curR][curC] = true; + + List moves = possibleMoves(enclosure, current); + + for (int[] move : moves) { + if (canReachHelper (enclosure, move, visited)) { + return true; + } + } + return false; } + + public static List possibleMoves(char[][] enclosure, int[] current) { + int curR = current[0]; + int curC = current[1]; + + List moves = new ArrayList<>(); + + // UP + int newR = curR - 1; + int newC = curC; + + if (newR >= 0 && enclosure[newR][newC] != 'W') { + moves.add(new int[]{newR, newC}); + } + + // DOWN + newR = curR + 1; + newC = curC; + + if (newR < enclosure.length && enclosure[newR][newC] != 'W') { + moves.add(new int[]{newR, newC}); + } + + // LEFT + newR = curR; + newC = curC - 1; + + if (newC >= 0 && enclosure[newR][newC] != 'W') { + moves.add(new int[]{newR, newC}); + } + + // RIGHT + newR = curR; + newC = curC + 1; + + if (newC < enclosure[0].length && enclosure[newR][newC] != 'W') { + moves.add(new int[]{newR, newC}); + } + + return moves; + + } + + public static int[] salamanderLocation(char[][] enclosure) { + for (int r = 0; r < enclosure.length; r++) { + for (int c = 0; c < enclosure[r].length; c++) { + if (enclosure[r][c] == 's') { + return new int[]{r, c}; + } + } + } + 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) { From 8763ed66f72a901b1e4944051dc1d23160cf50b7 Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Sat, 5 Apr 2025 00:35:57 -0700 Subject: [PATCH 2/4] added more tests for canreach --- src/SalamanderSearchTest.java | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/SalamanderSearchTest.java b/src/SalamanderSearchTest.java index a8b5af2..87d1654 100644 --- a/src/SalamanderSearchTest.java +++ b/src/SalamanderSearchTest.java @@ -34,6 +34,45 @@ public void canReach_NoPath() { assertFalse(actual); } + @Test + public void canReach_SalamanderAtFood() { + char[][] enclosure = { + {'.','.','.','.','.','.'}, + {'W','W','W','W','f','.'}, + {'.','.','W','.','.','s'}, + {'W','W','.','.','W','.'}, + {'W','.','W','.','.','.'}, + }; + boolean actual = SalamanderSearch.canReach(enclosure); + assertTrue(actual); + } + + @Test + public void canReach_MultipleFoodLocations() { + char[][] enclosure = { + {'.','.','.','.','.','.'}, + {'W','W','W','W','f','.'}, + {'.','.','W','.','.','s'}, + {'f','W','.','.','W','.'}, + {'W','.','W','.','.','.'}, + }; + boolean actual = SalamanderSearch.canReach(enclosure); + assertTrue(actual); + } + + @Test + public void canReach_NoFood() { + char[][] enclosure = { + {'.','.','.','.','.','.'}, + {'W','W','W','W','s','.'}, + {'.','.','W','.','.','W'}, + {'W','W','.','.','W','.'}, + {'W','.','W','.','.','.'}, + }; + boolean actual = SalamanderSearch.canReach(enclosure); + assertFalse(actual); + } + @Test public void testSalamanderLocation_centerOfGrid() { char[][] enclosure = { From 21ddd3b2927727cfa63701d497667bccd8091c40 Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Tue, 8 Apr 2025 14:25:56 -0700 Subject: [PATCH 3/4] refactored possibleMoves --- src/SalamanderSearch.java | 81 +++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/src/SalamanderSearch.java b/src/SalamanderSearch.java index 051adff..ca19827 100644 --- a/src/SalamanderSearch.java +++ b/src/SalamanderSearch.java @@ -1,5 +1,6 @@ import java.util.ArrayList; import java.util.List; +import java.util.*; public class SalamanderSearch { public static void main(String[] args) { @@ -18,6 +19,17 @@ public static void main(String[] args) { {'f','W','.','.','W','.'}, {'W','.','W','.','.','.'}, }; + + Set coordinateSet = new HashSet<>(); + int[] coord1 = new int[]{1, 5}; + int [] coord2 = new int[]{3, 7}; + int[] coord3 = new int[]{1, 5}; + + coordinateSet.add(coord1); + coordinateSet.add(coord2); + coordinateSet.add(coord3); + + System.out.println(coordinateSet.size()); } /** @@ -78,40 +90,61 @@ public static List possibleMoves(char[][] enclosure, int[] current) { int curC = current[1]; List moves = new ArrayList<>(); + + int[][] directions = new int[][] { + {-1, 0}, // UP + {1, 0}, // DOWN + {0, -1}, // LEFT + {0, 1} // RIGHT + }; - // UP - int newR = curR - 1; - int newC = curC; + for(int[] direction : directions) { + int newR = curR + direction[0]; + int newC = curC + direction[1]; - if (newR >= 0 && enclosure[newR][newC] != 'W') { - moves.add(new int[]{newR, newC}); + if (newR >= 0 && newR < enclosure.length && + newC >= 0 && newC < enclosure[0].length && + enclosure[newR][newC] != 'W') { + + moves.add(new int[]{newR, newC}); + } } - // DOWN - newR = curR + 1; - newC = curC; + return moves; + + // // UP + // int newR = curR - 1; + // int newC = curC; - if (newR < enclosure.length && enclosure[newR][newC] != 'W') { - moves.add(new int[]{newR, newC}); - } + // if (newR >= 0 && enclosure[newR][newC] != 'W') { + // moves.add(new int[]{newR, newC}); + // } - // LEFT - newR = curR; - newC = curC - 1; + // // DOWN + // newR = curR + 1; + // newC = curC; - if (newC >= 0 && enclosure[newR][newC] != 'W') { - moves.add(new int[]{newR, newC}); - } + // if (newR < enclosure.length && enclosure[newR][newC] != 'W') { + // moves.add(new int[]{newR, newC}); + // } - // RIGHT - newR = curR; - newC = curC + 1; + // // LEFT + // newR = curR; + // newC = curC - 1; - if (newC < enclosure[0].length && enclosure[newR][newC] != 'W') { - moves.add(new int[]{newR, newC}); - } + // if (newC >= 0 && enclosure[newR][newC] != 'W') { + // moves.add(new int[]{newR, newC}); + // } - return moves; + // // RIGHT + // newR = curR; + // newC = curC + 1; + + // if (newC < enclosure[0].length && enclosure[newR][newC] != 'W') { + // moves.add(new int[]{newR, newC}); + // } + + // return moves; } From 4caf4645def29ae6d093d8ba1d589c19444d8202 Mon Sep 17 00:00:00 2001 From: alstondsouza1 Date: Tue, 8 Apr 2025 16:52:37 -0700 Subject: [PATCH 4/4] added position to record the grid coordinates --- src/Position.java | 1 + src/SalamanderSearch.java | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 src/Position.java diff --git a/src/Position.java b/src/Position.java new file mode 100644 index 0000000..9e751f6 --- /dev/null +++ b/src/Position.java @@ -0,0 +1 @@ +public record Position (int row, int col) {} diff --git a/src/SalamanderSearch.java b/src/SalamanderSearch.java index ca19827..6deac64 100644 --- a/src/SalamanderSearch.java +++ b/src/SalamanderSearch.java @@ -20,16 +20,22 @@ public static void main(String[] args) { {'W','.','W','.','.','.'}, }; - Set coordinateSet = new HashSet<>(); - int[] coord1 = new int[]{1, 5}; - int [] coord2 = new int[]{3, 7}; - int[] coord3 = new int[]{1, 5}; + // Set coordinateSet = new HashSet<>(); + // int[] coord1 = new int[]{1, 5}; + // int [] coord2 = new int[]{3, 7}; + // int[] coord3 = new int[]{1, 5}; - coordinateSet.add(coord1); - coordinateSet.add(coord2); - coordinateSet.add(coord3); + // coordinateSet.add(coord1); + // coordinateSet.add(coord2); + // coordinateSet.add(coord3); - System.out.println(coordinateSet.size()); + // System.out.println(coordinateSet.size()); + + // canReach(enclosure1); + + Position pos = new Position(3, 5); + System.out.println(pos.row()); + System.out.println(pos.col()); } /** @@ -72,6 +78,8 @@ public static boolean canReachHelper(char[][] enclosure, int[] current, boolean[ return true; } + System.out.println(Arrays.toString(current)); + visited[curR][curC] = true; List moves = possibleMoves(enclosure, current);