Compare commits

..

2 commits

Author SHA1 Message Date
2c76beb733
checkpoint: add ability to regenerate the maze
Some checks failed
test / test (pull_request) Failing after 1m0s
2024-02-18 05:48:07 +00:00
8b55d28060
set combobox values to keys from dictionary 2024-02-18 02:57:32 +00:00
6 changed files with 38 additions and 29 deletions

11
app.py
View file

@ -18,6 +18,7 @@ class App(Tk):
centre_x = int(screen_width/2 - width/2)
centre_y = int(screen_height/2 - height/2)
self.geometry(f"{width}x{height}+{centre_x}+{centre_y}")
self.resizable(False, False)
# Styling
self.style = ttk.Style()
@ -43,7 +44,7 @@ class App(Tk):
self.solver = Solver(self.maze)
self.algorithm_map = {
self.search_algorithms = {
"Breadth-First Search": self.solver.solve_with_dfs_r,
"Depth-First Search": self.solver.solve_with_bfs_r,
}
@ -62,11 +63,13 @@ class App(Tk):
command=self.maze.generate,
)
generate.pack()
tuple_of_algorithms = tuple(self.search_algorithms.keys())
algorithm = StringVar()
combobox = ttk.Combobox(frame, textvariable=algorithm)
algorithm.set(tuple_of_algorithms[0])
algorithm_label = ttk.Label(frame, text="Searching algorithm:")
algorithm_label.pack()
combobox["values"] = ("Breadth-First Search", "Depth-First Search")
combobox = ttk.Combobox(frame, textvariable=algorithm)
combobox["values"] = tuple_of_algorithms
combobox["state"] = "readonly"
combobox.pack()
randomness = BooleanVar()
@ -82,7 +85,7 @@ class App(Tk):
frame,
text="Solve the maze",
command=lambda: self.solver.solve(
solve_method=self.algorithm_map[algorithm.get()],
solve_method=self.search_algorithms[algorithm.get()],
enable_random_direction=randomness.get(),
),
)

View file

@ -161,5 +161,11 @@ class Cell:
raise ValueError(f"This is an unknown visitor ({visitor})")
self._visited[visitor] = True
def reset(self) -> None:
for label in CellWallLabels:
self._walls[label].configure(True)
for k in self._visited:
self._visited[k] = False
def get_walls(self) -> Dict[CellWallLabels, CellWall]:
return self._walls

View file

@ -65,3 +65,9 @@ class Graphics(Canvas):
fill_colour = "grey"
self._draw_line(line, fill_colour)
self._redraw()
def clear(self) -> None:
"""
clears the canvas
"""
self.delete("all")

20
main.py
View file

@ -4,26 +4,6 @@ from app import App
def main():
app = App()
app.mainloop()
#window = Window(800, 800)
#game = Maze(
# x_position=10,
# y_position=10,
# height=19,
# width=19,
# cell_height=40,
# cell_width=40,
# window=window
#)
#solver = Solver(game)
#if solver.solve(solver.solve_with_bfs_r, True):
# print("Maze solved successfully :)")
#else:
# print("I'm unable to solve the maze :(")
#window.mainloop()
if __name__ == "__main__":

17
maze.py
View file

@ -108,14 +108,19 @@ class Maze:
random.seed(seed)
# Create the Maze's cells
self._cell_grid: List[List[Cell]] = []
self._create_cell_grid()
self._cell_grid: List[List[Cell]] = None
def generate(self):
"""
randomly generates a new maze.
"""
if self._cell_grid is None:
self._cell_grid: List[List[Cell]] = None
self._create_cell_grid()
else:
self._graphics.clear()
self._reset_cell_grid()
self._draw_cell_grid()
self._open_entrance_and_exit()
self._break_walls_r(MazePosition(
@ -288,7 +293,8 @@ class Maze:
draws a path between two cells in an animated way.
"""
self._graphics.draw_path(current_cell.centre(), next_cell.centre(), undo)
self._graphics.draw_path(
current_cell.centre(), next_cell.centre(), undo)
def mark_cell_as_visited(self, i: int, j: int, visitor: str) -> None:
"""
@ -341,3 +347,8 @@ class Maze:
left=left,
right=right,
)
def _reset_cell_grid(self) -> None:
for i in range(self._height):
for j in range(self._width):
self._cell_grid[i][j].reset()

View file

@ -45,7 +45,11 @@ class Solver:
last_j=self._game.get_last_j(),
)
return solve_method(start_position, end_position, enable_random_direction)
return solve_method(
start_position,
end_position,
enable_random_direction,
)
def solve_with_dfs_r(
self,
@ -62,7 +66,6 @@ class Solver:
visitor=self._solver,
)
while True:
possible_directions: List[MazeDirection] = []
for direction in MazeDirection: