fix some bug with my unit testing on the DFS

This commit is contained in:
Maoake Teriierooiterai
2026-03-23 19:24:32 +01:00
parent e75e14110d
commit f8f0e31598
2 changed files with 24 additions and 8 deletions
+5 -5
View File
@@ -129,19 +129,19 @@ class DepthFirstSearch:
x, y = coord x, y = coord
width, height = w_h width, height = w_h
# NORTH # NORTH
if y - 1 >= 0 and coord not in visited: if y - 1 >= 0 and (x, y - 1) not in visited:
rand_cell.append("N") rand_cell.append("N")
# SOUTH # SOUTH
if y + 1 < height and coord not in visited: if y + 1 < height and (x, y + 1) not in visited:
rand_cell.append("S") rand_cell.append("S")
# WEST # WEST
if x - 1 >= 0 and coord not in visited: if x - 1 >= 0 and (x - 1, y) not in visited:
rand_cell.append("W") rand_cell.append("W")
# EAST # EAST
if x + 1 < width and coord not in visited: if x + 1 < width and (x + 1, y) not in visited:
rand_cell.append("E") rand_cell.append("E")
return rand_cell return rand_cell
@@ -183,7 +183,7 @@ class DepthFirstSearch:
@staticmethod @staticmethod
def last(path: list): def last(path: list):
return path(len(path) - 1) return path[len(path) - 1]
def back_on_step(path: list, w_h: tuple) -> list: def back_on_step(path: list, w_h: tuple) -> list:
last = DepthFirstSearch.last(path) last = DepthFirstSearch.last(path)
+19 -3
View File
@@ -11,7 +11,23 @@ class TestDepth:
assert maze[0][0].value == cell.value assert maze[0][0].value == cell.value
def test_rand_cells(self) -> None: def test_rand_cells(self) -> None:
maze = DepthFirstSearch.init_maze(10, 10) w_h = (10, 10)
lst = DepthFirstSearch.add_cell_visited(maze[0][0]) lst = DepthFirstSearch.add_cell_visited((0, 0))
rand_cells = DepthFirstSearch.random_cells(lst, maze, 0, 1) rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h)
assert len(rand_cells) == 2 assert len(rand_cells) == 2
def test_next_cell(self) -> None:
coord = (5, 4)
x, y = coord
assert DepthFirstSearch.next_cell(x, y, "N") == (5, 3)
def test_reverse_path(self) -> None:
assert DepthFirstSearch.reverse_path("N") == "S"
def test_last(self) -> None:
lst = [(0, 0), (1, 1)]
assert DepthFirstSearch.last(lst) == (1, 1)
def test_BOS(self) -> None:
path = [(0, 0), (0, 2), (1, 1)]
assert len(DepthFirstSearch.random_cells(path, (0, 1), (10, 10))) == 0