okpalan

mazy.py

Nov 7th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. import tkinter as tk
  2. import random
  3.  
  4.  
  5. class Maze:
  6.     def __init__(self, root):
  7.         self.root = root
  8.         self.size = 8
  9.         self.w = self.size * 60
  10.         self.h = self.size * 60
  11.         self.canvas = tk.Canvas(self.root, width=self.w,
  12.                                 height=self.h, borderwidth=2, relief="solid")
  13.         self.canvas.pack()
  14.         self.cells = []
  15.         self.current = None
  16.         self.stack = []
  17.         self.visited = 0
  18.         self.done = False
  19.  
  20.     def build(self):
  21.         for i in range(0, self.size):
  22.             self.cells.append([])
  23.             for j in range(0, self.size):
  24.                 self.cells[i].append(Cell(self.canvas, i, j, self.size))
  25.  
  26.     def cell_at(self, x, y):
  27.         if x < 0 or y < 0 or x >= self.size or y >= self.size:
  28.             return None
  29.         return self.cells[x][y]
  30.  
  31.     def shuffle(self):
  32.         import random
  33.         neighbours = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  34.         random.shuffle(neighbours)
  35.         for n in neighbours:
  36.             next = self.cell_at(self.current.x + n[0], self.current.y + n[1])
  37.             if next and not next.visited:
  38.                 next.make_door(self.current)
  39.                 self.stack.append(self.current)
  40.                 self.current = next
  41.                 self.shuffle()
  42.  
  43.     def dfs(self):
  44.         neighbours = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  45.         random.shuffle(neighbours)
  46.         for n in neighbours:
  47.             next = self.cell_at(self.current.x + n[0], self.current.y + n[1])
  48.             if next and not next.visited:
  49.                 next.make_door(self.current)
  50.                 self.stack.append(self.current)
  51.                 self.current = next
  52.                 self.visited += 1
  53.                 if self.visited == self.size * self.size:
  54.                     self.done = True
  55.                 self.dfs()
  56.  
  57.     def bfs(self):
  58.         queue = []
  59.         queue.append(self.current)
  60.         while len(queue) > 0:
  61.             c = queue.pop(0)
  62.             c.visited = True
  63.             neighbours = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  64.             random.shuffle(neighbours)
  65.             for n in neighbours:
  66.                 next = self.cell_at(c.x + n[0], c.y + n[1])
  67.                 if next and not next.visited:
  68.                     next.make_door(c)
  69.                     queue.append(next)
  70.  
  71.     def solve(self, algo):
  72.         self.build()
  73.         if algo == "dfs":
  74.             self.current = self.cells[0][0]
  75.             self.current.visited = True
  76.             self.visited = 1
  77.             self.dfs()
  78.         elif algo == "bfs":
  79.             self.current = self.cells[0][0]
  80.             self.bfs()
  81.         else:
  82.             self.current = self.cells[0][0]
  83.             self.shuffle()
  84.  
  85.     def draw(self):
  86.         for i in range(0, self.size):
  87.             for j in range(0, self.size):
  88.                 self.cells[i][j].draw()
  89.  
  90.  
  91. class Cell:
  92.     def __init__(self, canvas, x, y, size):
  93.         self.canvas = canvas
  94.         self.x = x
  95.         self.y = y
  96.         self.size = size
  97.         self.walls = [True, True, True, True]
  98.         self.visited = False
  99.  
  100.     def show(self):
  101.         x = self.x * self.size
  102.         y = self.y * self.size
  103.         self.canvas.create_rectangle(
  104.             x, y, x + self.size, y + self.size, outline="#fff", fill="#fff", width=2)
  105.  
  106.     def highlight(self):
  107.         x = self.x * self.size
  108.         y = self.y * self.size
  109.         self.canvas.create_rectangle(
  110.             x, y, x + self.size, y + self.size, outline="#000", fill="#000", width=2)
  111.  
  112.     def is_valid(self):
  113.         if self.x >= 0 and self.x < self.size and self.y >= 0 and self.y < self.size:
  114.             return True
  115.         return False
  116.  
  117.     def has_all_walls(self):
  118.         if self.walls[0] == True and self.walls[1] == True and self.walls[2] == True and self.walls[3] == True:
  119.             return True
  120.         return False
  121.  
  122.     def draw(self):
  123.         if self.has_all_walls():
  124.             self.show()
  125.         else:
  126.             self.highlight()
  127.  
  128.     def make_door(self, c):
  129.         x = self.x - c.x
  130.         y = self.y - c.y
  131.         if x == 1:
  132.             self.walls[3] = False
  133.             c.walls[1] = False
  134.         elif x == -1:
  135.             self.walls[1] = False
  136.             c.walls[3] = False
  137.         if y == 1:
  138.             self.walls[0] = False
  139.             c.walls[2] = False
  140.         elif y == -1:
  141.             self.walls[2] = False
  142.             c.walls[0] = False
  143.  
  144.     def get_neighbours(self):
  145.         neighbours = []
  146.         top = self.canvas.cell_at(self.x, self.y - 1)
  147.         right = self.canvas.cell_at(self.x + 1, self.y)
  148.         bottom = self.canvas.cell_at(self.x, self.y + 1)
  149.         left = self.canvas.cell_at(self.x - 1, self.y)
  150.         if top and not top.visited:
  151.             neighbours.append(top)
  152.         if right and not right.visited:
  153.             neighbours.append(right)
  154.         if bottom and not bottom.visited:
  155.             neighbours.append(bottom)
  156.         if left and not left.visited:
  157.             neighbours.append(left)
  158.         return neighbours
  159.  
  160.     def __str__(self):
  161.         return "(" + str(self.x) + "," + str(self.y) + ")"
  162.  
  163.  
  164. if __name__ == "__main__":
  165.     root = tk.Tk()
  166.     root.title("Maze Solver")
  167.     maze = Maze(root)
  168.     maze.solve("dfs")
  169.     maze.draw()
  170.  
Add Comment
Please, Sign In to add comment