summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryuzu-eva <stevenhu@web.de>2024-11-11 15:52:22 +0100
committeryuzu-eva <stevenhu@web.de>2024-11-11 15:52:22 +0100
commit696199a0227cfccb2247978b1ba8c1b03f1ae26d (patch)
tree493c3f65fdb65634c1577529bcfbf76356d9ec4f
initial commit
-rw-r--r--.gitignore2
-rw-r--r--cell.py34
-rw-r--r--graphics.py43
-rwxr-xr-xmain.py21
-rw-r--r--maze.py51
-rwxr-xr-xtests.py22
6 files changed, 173 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0540009
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+__pycache__/
+venv/ \ No newline at end of file
diff --git a/cell.py b/cell.py
new file mode 100644
index 0000000..c2f763b
--- /dev/null
+++ b/cell.py
@@ -0,0 +1,34 @@
+from graphics import Point, Line
+
+class Cell:
+ def __init__(self, window=None, lwall=True, twall=True, rwall=True, bwall=True):
+ self._x1 = None
+ self._y1 = None
+ self._x2 = None
+ self._y2 = None
+ self._win = window
+ self.lwall = lwall
+ self.twall = twall
+ self.rwall = rwall
+ self.bwall = bwall
+
+ def draw(self, x1, y1, x2, y2):
+ if self._win is None:
+ return
+ self._x1 = x1
+ self._y1 = y1
+ self._x2 = x2
+ self._y2 = y2
+
+ self._win.draw_line(Line(Point(x1, y1), Point(x1, y2))) if self.lwall else self._win.draw_line(Line(Point(x1, y1), Point(x1, y2)), "white")
+ self._win.draw_line(Line(Point(x1, y1), Point(x2, y1))) if self.twall else self._win.draw_line(Line(Point(x1, y1), Point(x2, y1)), "white")
+ self._win.draw_line(Line(Point(x2, y1), Point(x2, y2))) if self.rwall else self._win.draw_line(Line(Point(x2, y1), Point(x2, y2)), "white")
+ self._win.draw_line(Line(Point(x1, y2), Point(x2, y2))) if self.bwall else self._win.draw_line(Line(Point(x1, y2), Point(x2, y2)), "white")
+
+ def draw_move(self, to_cell, undo=False):
+ center_start = Point((self._x1+self._x2)>>1, (self._y1+self._y2)>>1)
+ center_end = Point((to_cell._x1+to_cell._x2)>>1, (to_cell._y1+to_cell._y2)>>1)
+ fill_color = "red"
+ if undo:
+ fill_color = "gray"
+ self._win.draw_line(Line(center_start, center_end), fill_color)
diff --git a/graphics.py b/graphics.py
new file mode 100644
index 0000000..3e4a745
--- /dev/null
+++ b/graphics.py
@@ -0,0 +1,43 @@
+from tkinter import Tk, BOTH, Canvas, Frame, Button
+
+class Window:
+ def __init__(self, width, height):
+ self.__root = Tk()
+ self.__root.title("Maze Solver")
+ self.__canvas = Canvas(self.__root, bg="white", height=height, width=width)
+ self.__canvas.pack(fill=BOTH, expand=1)
+ self.__is_running = False
+ self.__root.protocol("WM_DELETE_WINDOW", self.close)
+ self.__button = Button(self.__root, text="Close", command=self.close)
+ self.__button.pack()
+
+ def redraw(self):
+ self.__root.update_idletasks()
+ self.__root.update()
+
+ def wait_for_close(self):
+ self.__is_running = True
+ while(self.__is_running):
+ self.redraw()
+
+ def close(self):
+ self.__is_running = False
+
+ def draw_line(self, line, fill_color="black"):
+ line.draw(self.__canvas, fill_color)
+
+class Point:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+class Line:
+ def __init__(self, start, end):
+ self.start = start
+ self.end = end
+
+ def draw(self, canvas, fill_color):
+ canvas.create_line(
+ self.start.x, self.start.y, self.end.x, self.end.y, fill=fill_color, width=2
+ )
+
diff --git a/main.py b/main.py
new file mode 100755
index 0000000..56f0817
--- /dev/null
+++ b/main.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+from graphics import Window
+from maze import Maze
+
+def main():
+ num_rows = 12
+ num_cols = 16
+ margin = 50
+ screen_x = 800
+ screen_y = 600
+ cell_size_x = (screen_x - 2 * margin) / num_cols
+ cell_size_y = (screen_y - 2 * margin) / num_rows
+ win = Window(screen_x, screen_y)
+
+ maze = Maze(margin, margin, num_rows, num_cols, cell_size_x, cell_size_y, win)
+
+ win.wait_for_close()
+
+if __name__ == "__main__":
+ main()
diff --git a/maze.py b/maze.py
new file mode 100644
index 0000000..71086cb
--- /dev/null
+++ b/maze.py
@@ -0,0 +1,51 @@
+from cell import Cell
+import time
+
+class Maze:
+ def __init__(
+ self,
+ x1,
+ y1,
+ num_rows,
+ num_cols,
+ cell_size_x,
+ cell_size_y,
+ win=None
+ ):
+ self._cells = []
+ self._x1 = x1
+ self._y1 = y1
+ self._num_rows = num_rows
+ self._num_cols = num_cols
+ self._cell_size_x = cell_size_x
+ self._cell_size_y = cell_size_y
+ self._win = win
+
+ self._create_cells()
+ self._break_entrance_and_exit()
+
+ def _create_cells(self):
+ self._cells = [[Cell(self._win)]*self._num_rows for i in range(self._num_cols)]
+ for i in range(self._num_cols):
+ for j in range(self._num_rows):
+ self._draw_cell(i, j)
+
+ def _draw_cell(self, i, j):
+ if self._win is None:
+ return
+ x1 = self._x1 + i * self._cell_size_x
+ y1 = self._y1 + j * self._cell_size_y
+ x2 = x1 + self._cell_size_x
+ y2 = y1 + self._cell_size_x
+ self._cells[i][j].draw(x1, y1, x2, y2)
+ self._animate()
+
+ def _animate(self):
+ self._win.redraw()
+ time.sleep(0.01)
+
+ def _break_entrance_and_exit(self):
+ self._cells[0][0].twall = False
+ self._draw_cell(0, 0)
+ self._cells[-1][-1].bwall = False
+ self._draw_cell(self._num_cols-1, self._num_rows-1)
diff --git a/tests.py b/tests.py
new file mode 100755
index 0000000..f233699
--- /dev/null
+++ b/tests.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+
+import unittest
+
+from maze import Maze
+
+class Tests(unittest.TestCase):
+ def test_maze_create_cells(self):
+ num_cols = 12
+ num_rows = 10
+ m1 = Maze(0, 0, num_rows, num_cols, 10, 10)
+ self.assertEqual(
+ len(m1._cells),
+ num_cols
+ )
+ self.assertEqual(
+ len(m1._cells[0]),
+ num_rows
+ )
+
+if __name__ == "__main__":
+ unittest.main()