From 06dbbeb33215d5f7cd8165b716e9ba94ee9dd907 Mon Sep 17 00:00:00 2001 From: Squizard <184549450+BradyLeg@users.noreply.github.com> Date: Thu, 29 May 2025 11:51:32 -0700 Subject: [PATCH] completed --- src/SalamanderSearch.java | 101 ++++++++- src/SalamanderSearchTest.java | 415 +++++++++++++++++----------------- 2 files changed, 298 insertions(+), 218 deletions(-) diff --git a/src/SalamanderSearch.java b/src/SalamanderSearch.java index 93c9ab5..c3c1da1 100644 --- a/src/SalamanderSearch.java +++ b/src/SalamanderSearch.java @@ -4,19 +4,19 @@ public class SalamanderSearch { public static void main(String[] args) { char[][] enclosure1 = { - {'.','.','.','.','.','.'}, - {'W','.','W','W','.','.'}, - {'.','.','W','.','.','W'}, - {'f','W','.','.','W','.'}, - {'W','.','W','s','.','.'}, + { '.', '.', '.', '.', '.', '.' }, + { 'W', '.', 'W', 'W', '.', '.' }, + { '.', '.', 'W', '.', '.', 'W' }, + { 'f', 'W', '.', '.', 'W', '.' }, + { 'W', '.', 'W', 's', '.', '.' }, }; char[][] enclosure2 = { - {'.','.','.','.','.','.'}, - {'W','W','W','W','s','.'}, - {'.','.','W','.','.','W'}, - {'f','W','.','.','W','.'}, - {'W','.','W','.','.','.'}, + { '.', '.', '.', '.', '.', '.' }, + { 'W', 'W', 'W', 'W', 's', '.' }, + { '.', '.', 'W', '.', '.', 'W' }, + { 'f', 'W', '.', '.', 'W', '.' }, + { 'W', '.', 'W', '.', '.', '.' }, }; } @@ -41,9 +41,88 @@ public static void main(String[] args) { * * @param enclosure * @return whether the salamander can reach the food - * @throws IllegalArgumentException if the enclosure does not contain a salamander + * @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); + } + + 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 (visited[curR][curC]) { + return false; + } + + visited[curR][curC] = true; + List neighbors = possibleMoves(enclosure, current); + + 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 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[newR].length && enclosure[newR][newC] != 'W') { + moves.add(new int[] { newR, newC }); + } + + return moves; + } + + // O(n*m) n = # of rows, m = # of cols + public static int[] salamanderLocation(char[][] enclosure) { + // int[] location = new int[2]; + + for (int r = 0; r < enclosure.length; r++) { + for (int c = 0; c < enclosure[r].length; c++) { + if (enclosure[r][c] == 's') { + int[] location = new int[] { r, c }; + return location; + } + } + } + throw new IllegalArgumentException("No salamander present"); + } } diff --git a/src/SalamanderSearchTest.java b/src/SalamanderSearchTest.java index ddad9de..f3a1e7d 100644 --- a/src/SalamanderSearchTest.java +++ b/src/SalamanderSearchTest.java @@ -11,11 +11,11 @@ public class SalamanderSearchTest { @Test public void canReach_PathExists() { char[][] enclosure = { - {'.','.','.','.','.','.'}, - {'W','.','W','W','.','.'}, - {'.','.','W','.','.','W'}, - {'f','W','.','.','W','.'}, - {'W','.','W','s','.','.'}, + { '.', '.', '.', '.', '.', '.' }, + { 'W', '.', 'W', 'W', '.', '.' }, + { '.', '.', 'W', '.', '.', 'W' }, + { 'f', 'W', '.', '.', 'W', '.' }, + { 'W', '.', 'W', 's', '.', '.' }, }; boolean actual = SalamanderSearch.canReach(enclosure); assertTrue(actual); @@ -24,214 +24,215 @@ public void canReach_PathExists() { @Test public void canReach_NoPath() { char[][] enclosure = { - {'.','.','.','.','.','.'}, - {'W','W','W','W','s','.'}, - {'.','.','W','.','.','W'}, - {'f','W','.','.','W','.'}, - {'W','.','W','.','.','.'}, + { '.', '.', '.', '.', '.', '.' }, + { 'W', 'W', 'W', 'W', 's', '.' }, + { '.', '.', 'W', '.', '.', 'W' }, + { 'f', 'W', '.', '.', 'W', '.' }, + { 'W', '.', 'W', '.', '.', '.' }, }; boolean actual = SalamanderSearch.canReach(enclosure); 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) { Set set = new HashSet<>();