Compare commits

..

No commits in common. "2c76beb7337480a735b663d188e06d864312d465" and "9bb23b6c6d5dc41cf126de93671514a7b83e3c3a" have entirely different histories.

6 changed files with 29 additions and 38 deletions

11
app.py
View file

@ -18,7 +18,6 @@ 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()
@ -44,7 +43,7 @@ class App(Tk):
self.solver = Solver(self.maze)
self.search_algorithms = {
self.algorithm_map = {
"Breadth-First Search": self.solver.solve_with_dfs_r,
"Depth-First Search": self.solver.solve_with_bfs_r,
}
@ -63,13 +62,11 @@ class App(Tk):
command=self.maze.generate,
)
generate.pack()
tuple_of_algorithms = tuple(self.search_algorithms.keys())
algorithm = StringVar()
algorithm.set(tuple_of_algorithms[0])
combobox = ttk.Combobox(frame, textvariable=algorithm)
algorithm_label = ttk.Label(frame, text="Searching algorithm:")
algorithm_label.pack()
combobox = ttk.Combobox(frame, textvariable=algorithm)
combobox["values"] = tuple_of_algorithms
combobox["values"] = ("Breadth-First Search", "Depth-First Search")
combobox["state"] = "readonly"
combobox.pack()
randomness = BooleanVar()
@ -85,7 +82,7 @@ class App(Tk):
frame,
text="Solve the maze",
command=lambda: self.solver.solve(
solve_method=self.search_algorithms[algorithm.get()],
solve_method=self.algorithm_map[algorithm.get()],
enable_random_direction=randomness.get(),
),
)

View file

@ -161,11 +161,5 @@ 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,9 +65,3 @@ 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,6 +4,26 @@ 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,19 +108,14 @@ class Maze:
random.seed(seed)
# Create the Maze's cells
self._cell_grid: List[List[Cell]] = None
self._cell_grid: List[List[Cell]] = []
self._create_cell_grid()
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(
@ -293,8 +288,7 @@ 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:
"""
@ -347,8 +341,3 @@ 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,11 +45,7 @@ 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,
@ -66,6 +62,7 @@ class Solver:
visitor=self._solver,
)
while True:
possible_directions: List[MazeDirection] = []
for direction in MazeDirection: