From 32253d56a1e57e72d152bc3e23be6c0ba581d76a Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 22:46:07 -0700 Subject: [PATCH 1/7] Completed the Fire class, and passed initial test. --- src/Fire.java | 77 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index b968c51..8979dbf 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -1,3 +1,8 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + public class Fire { /** * Returns how long it takes for all vulnerable trees to be set on fire if a @@ -35,9 +40,77 @@ public class Fire { * @param matchC The column the match is lit at * @return the time at which the final tree to be incinerated starts burning */ - public static int timeToBurn(char[][] forest, int matchR, int matchC) { + public static int timeToBurn(char[][] forest, int matchR, int matchC) + { // HINT: when adding to your BFS queue, you can include more information than // just a location. What other information might be useful? - return -1; + boolean[][] visited = new boolean[forest.length][forest[0].length]; + Queue queue = new LinkedList<>(); + queue.add(new int[]{matchR, matchC}); + int count = -1; + + while (!queue.isEmpty()) + { + int[] current = queue.poll(); + int curR = current[0]; + int curC = current[1]; + + if (curR == -1) + { + count++; + continue; + } + + if (visited[curR][curC]) continue; + + visited[curR][curC] = true; + + queue.addAll(neighborTrees(forest, curR, curC, queue)); + + } + + return count; } + + public static List neighborTrees(char[][] forest, int spreadR, int spreadC, Queue queue) + { + int[][] directions = { + {-1, 0}, // North + {1, 0}, // South + {0, 1}, // East + {0, -1} // West + }; + + List possibleMoves = new ArrayList<>(); + + int check = 0; + for (int[] findBurnTurn : queue) + { + if (findBurnTurn[0] == -1) check++; + } + + if (check == 0) possibleMoves.add(new int[]{-1, 0}); + else check = 0; + + for (int[] direction : directions) + { + int changeR = direction[0]; + int changeC = direction[1]; + + int newR = spreadR + changeR; + int newC = spreadC + changeC; + + if (newR >= 0 && + newR < forest.length && + newC >= 0 && + newC < forest[newR].length && + forest[newR][newC] != '.') + { + possibleMoves.add(new int[]{newR, newC}); + } + } + + return possibleMoves; + } + } \ No newline at end of file From c451c891d314839909ad61b7543eea773318267c Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 22:49:34 -0700 Subject: [PATCH 2/7] Added and passed a test for neighborTrees that checks trees on all sides. --- src/FireTest.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index b3b9085..95c5995 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,4 +1,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Set; import org.junit.jupiter.api.Test; @@ -20,4 +27,41 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + + // Neighbors on All Sides + @Test + public void testNeighborTrees_AllSides() + { + char[][] forest = { + {'t', 't', 't'}, + {'t', 't', 't'}, + {'t', 't', 't'} + }; + + int spreadR = 1; + int spreadC = 1; + + int[] startingLocation = {spreadR, spreadC}; + Queue queue = new LinkedList<>(); + queue.add(startingLocation); + + List neighbors = Fire.neighborTrees(forest, spreadR, spreadC, queue); + Set neighborsSet = toSet(neighbors); + + assertTrue(neighborsSet.contains("0,1")); + assertTrue(neighborsSet.contains("1,0")); + assertTrue(neighborsSet.contains("1,2")); + assertTrue(neighborsSet.contains("2,1")); + + } + + + + private Set toSet(List list) + { + Set set = new HashSet<>(); + for (int[] arr : list) set.add(arr[0] + "," + arr[1]); + return set; + } + } From 6b38a9cbe48c63f2f10bfa46e16c062f00bff06c Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 23:04:03 -0700 Subject: [PATCH 3/7] Added and passed a test for neighborTrees that has no trees around. --- src/FireTest.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index 95c5995..04786c3 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,4 +1,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; @@ -55,6 +57,34 @@ public void testNeighborTrees_AllSides() } + // No Neighbors + @Test + public void testNeighborTrees_NoSides() + { + char[][] forest = { + {'.', '.', '.'}, + {'.', 't', '.'}, + {'.', '.', '.'} + }; + + int spreadR = 1; + int spreadC = 1; + + int[] startingLocation = {spreadR, spreadC}; + Queue queue = new LinkedList<>(); + queue.add(startingLocation); + + List neighbors = Fire.neighborTrees(forest, spreadR, spreadC, queue); + Set neighborsSet = toSet(neighbors); + + assertTrue(neighborsSet.contains("-1,0")); + assertFalse(neighborsSet.contains("0,1")); + assertFalse(neighborsSet.contains("1,0")); + assertFalse(neighborsSet.contains("1,2")); + assertFalse(neighborsSet.contains("2,1")); + + } + private Set toSet(List list) From fd6e729eb71b133d3e86c76214a5ebd665edd797 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 23:20:51 -0700 Subject: [PATCH 4/7] Added and passed a test for neighborTrees starting in a corner. --- src/FireTest.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index 04786c3..c763eeb 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -85,6 +85,31 @@ public void testNeighborTrees_NoSides() } + // Corner Neighbors + @Test + public void testNeighborTrees_CornerTrees() + { + char[][] forest = { + {'t', 't', '.'}, + {'t', '.', '.'}, + {'.', '.', '.'} + }; + + int spreadR = 0; + int spreadC = 0; + + int[] startingLocation = {spreadR, spreadC}; + Queue queue = new LinkedList<>(); + queue.add(startingLocation); + + List neighbors = Fire.neighborTrees(forest, spreadR, spreadC, queue); + Set neighborSet = toSet(neighbors); + + assertTrue(neighborSet.contains("0,1")); + assertTrue(neighborSet.contains("1,0")); + + } + private Set toSet(List list) From cedba81b961c0f8e91f4d5f00781eb70133f53ec Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 23:37:51 -0700 Subject: [PATCH 5/7] Added an exception to the timeToBurn method for when matchR and matchC aren't at a tree in the forest. --- src/Fire.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Fire.java b/src/Fire.java index 8979dbf..dd497e9 100644 --- a/src/Fire.java +++ b/src/Fire.java @@ -42,8 +42,8 @@ public class Fire { */ public static int timeToBurn(char[][] forest, int matchR, int matchC) { - // HINT: when adding to your BFS queue, you can include more information than - // just a location. What other information might be useful? + if (forest[matchR][matchC] == '.') throw new IllegalArgumentException("The match isn't lit at a tree."); + boolean[][] visited = new boolean[forest.length][forest[0].length]; Queue queue = new LinkedList<>(); queue.add(new int[]{matchR, matchC}); From fbb037ee371883e2a1d7f6585218df561026a674 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 23:38:54 -0700 Subject: [PATCH 6/7] Added and passed a test to check the exception works in the timeToBurn method. --- src/FireTest.java | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/FireTest.java b/src/FireTest.java index c763eeb..853ac28 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -1,6 +1,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.HashSet; @@ -30,6 +30,27 @@ public void testTimeToBurnExample() { assertEquals(expected, actual); } + // Match set not on a tree + @Test + public void testTimeToBurn_BadArgument() + { + char[][] forest = { + {'.', '.', '.'}, + {'.', '.', '.'}, + {'.', '.', '.'} + }; + + int spreadR = 1; + int spreadC = 1; + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + Fire.timeToBurn(forest, spreadR, spreadC); + }); + + assertEquals("The match isn't lit at a tree.", exception.getMessage()); + + } + // Neighbors on All Sides @Test public void testNeighborTrees_AllSides() From 9f5e263ca16fce9d2516006829c20cae54a6cd5c Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 9 Jun 2025 23:46:25 -0700 Subject: [PATCH 7/7] Added and passed a test where the whole forest burns. --- src/FireTest.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/FireTest.java b/src/FireTest.java index 853ac28..1ff89c7 100644 --- a/src/FireTest.java +++ b/src/FireTest.java @@ -51,6 +51,26 @@ public void testTimeToBurn_BadArgument() } + // Whole forest burns down + @Test + public void testTimeToBurn_EverythingGoes() + { + char[][] forest = { + {'t', 't', 't'}, + {'t', 't', 't'}, + {'t', 't', 't'} + }; + + int spreadR = 1; + int spreadC = 1; + + int expected = 2; + int actual = Fire.timeToBurn(forest, spreadR, spreadC); + + assertEquals(expected, actual); + + } + // Neighbors on All Sides @Test public void testNeighborTrees_AllSides()