Compare commits
No commits in common. "2c76beb7337480a735b663d188e06d864312d465" and "9bb23b6c6d5dc41cf126de93671514a7b83e3c3a" have entirely different histories.
2c76beb733
...
9bb23b6c6d
6 changed files with 29 additions and 38 deletions
11
app.py
11
app.py
|
@ -18,7 +18,6 @@ class App(Tk):
|
||||||
centre_x = int(screen_width/2 - width/2)
|
centre_x = int(screen_width/2 - width/2)
|
||||||
centre_y = int(screen_height/2 - height/2)
|
centre_y = int(screen_height/2 - height/2)
|
||||||
self.geometry(f"{width}x{height}+{centre_x}+{centre_y}")
|
self.geometry(f"{width}x{height}+{centre_x}+{centre_y}")
|
||||||
self.resizable(False, False)
|
|
||||||
|
|
||||||
# Styling
|
# Styling
|
||||||
self.style = ttk.Style()
|
self.style = ttk.Style()
|
||||||
|
@ -44,7 +43,7 @@ class App(Tk):
|
||||||
|
|
||||||
self.solver = Solver(self.maze)
|
self.solver = Solver(self.maze)
|
||||||
|
|
||||||
self.search_algorithms = {
|
self.algorithm_map = {
|
||||||
"Breadth-First Search": self.solver.solve_with_dfs_r,
|
"Breadth-First Search": self.solver.solve_with_dfs_r,
|
||||||
"Depth-First Search": self.solver.solve_with_bfs_r,
|
"Depth-First Search": self.solver.solve_with_bfs_r,
|
||||||
}
|
}
|
||||||
|
@ -63,13 +62,11 @@ class App(Tk):
|
||||||
command=self.maze.generate,
|
command=self.maze.generate,
|
||||||
)
|
)
|
||||||
generate.pack()
|
generate.pack()
|
||||||
tuple_of_algorithms = tuple(self.search_algorithms.keys())
|
|
||||||
algorithm = StringVar()
|
algorithm = StringVar()
|
||||||
algorithm.set(tuple_of_algorithms[0])
|
combobox = ttk.Combobox(frame, textvariable=algorithm)
|
||||||
algorithm_label = ttk.Label(frame, text="Searching algorithm:")
|
algorithm_label = ttk.Label(frame, text="Searching algorithm:")
|
||||||
algorithm_label.pack()
|
algorithm_label.pack()
|
||||||
combobox = ttk.Combobox(frame, textvariable=algorithm)
|
combobox["values"] = ("Breadth-First Search", "Depth-First Search")
|
||||||
combobox["values"] = tuple_of_algorithms
|
|
||||||
combobox["state"] = "readonly"
|
combobox["state"] = "readonly"
|
||||||
combobox.pack()
|
combobox.pack()
|
||||||
randomness = BooleanVar()
|
randomness = BooleanVar()
|
||||||
|
@ -85,7 +82,7 @@ class App(Tk):
|
||||||
frame,
|
frame,
|
||||||
text="Solve the maze",
|
text="Solve the maze",
|
||||||
command=lambda: self.solver.solve(
|
command=lambda: self.solver.solve(
|
||||||
solve_method=self.search_algorithms[algorithm.get()],
|
solve_method=self.algorithm_map[algorithm.get()],
|
||||||
enable_random_direction=randomness.get(),
|
enable_random_direction=randomness.get(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
6
cell.py
6
cell.py
|
@ -161,11 +161,5 @@ class Cell:
|
||||||
raise ValueError(f"This is an unknown visitor ({visitor})")
|
raise ValueError(f"This is an unknown visitor ({visitor})")
|
||||||
self._visited[visitor] = True
|
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]:
|
def get_walls(self) -> Dict[CellWallLabels, CellWall]:
|
||||||
return self._walls
|
return self._walls
|
||||||
|
|
|
@ -65,9 +65,3 @@ class Graphics(Canvas):
|
||||||
fill_colour = "grey"
|
fill_colour = "grey"
|
||||||
self._draw_line(line, fill_colour)
|
self._draw_line(line, fill_colour)
|
||||||
self._redraw()
|
self._redraw()
|
||||||
|
|
||||||
def clear(self) -> None:
|
|
||||||
"""
|
|
||||||
clears the canvas
|
|
||||||
"""
|
|
||||||
self.delete("all")
|
|
||||||
|
|
20
main.py
20
main.py
|
@ -4,6 +4,26 @@ from app import App
|
||||||
def main():
|
def main():
|
||||||
app = App()
|
app = App()
|
||||||
app.mainloop()
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
17
maze.py
17
maze.py
|
@ -108,19 +108,14 @@ class Maze:
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
|
|
||||||
# Create the Maze's cells
|
# 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):
|
def generate(self):
|
||||||
"""
|
"""
|
||||||
randomly generates a new maze.
|
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._draw_cell_grid()
|
||||||
self._open_entrance_and_exit()
|
self._open_entrance_and_exit()
|
||||||
self._break_walls_r(MazePosition(
|
self._break_walls_r(MazePosition(
|
||||||
|
@ -293,8 +288,7 @@ class Maze:
|
||||||
draws a path between two cells in an animated way.
|
draws a path between two cells in an animated way.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._graphics.draw_path(
|
self._graphics.draw_path(current_cell.centre(), next_cell.centre(), undo)
|
||||||
current_cell.centre(), next_cell.centre(), undo)
|
|
||||||
|
|
||||||
def mark_cell_as_visited(self, i: int, j: int, visitor: str) -> None:
|
def mark_cell_as_visited(self, i: int, j: int, visitor: str) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -347,8 +341,3 @@ class Maze:
|
||||||
left=left,
|
left=left,
|
||||||
right=right,
|
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()
|
|
||||||
|
|
|
@ -45,11 +45,7 @@ class Solver:
|
||||||
last_j=self._game.get_last_j(),
|
last_j=self._game.get_last_j(),
|
||||||
)
|
)
|
||||||
|
|
||||||
return solve_method(
|
return solve_method(start_position, end_position, enable_random_direction)
|
||||||
start_position,
|
|
||||||
end_position,
|
|
||||||
enable_random_direction,
|
|
||||||
)
|
|
||||||
|
|
||||||
def solve_with_dfs_r(
|
def solve_with_dfs_r(
|
||||||
self,
|
self,
|
||||||
|
@ -66,6 +62,7 @@ class Solver:
|
||||||
visitor=self._solver,
|
visitor=self._solver,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
possible_directions: List[MazeDirection] = []
|
possible_directions: List[MazeDirection] = []
|
||||||
for direction in MazeDirection:
|
for direction in MazeDirection:
|
||||||
|
|
Loading…
Reference in a new issue