-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
113 lines (84 loc) · 2.31 KB
/
maze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import math
from turtle import *
from algo import PrimsRandomized
import time
from tkinter import *
from PIL import Image
class RectMaze:
def __init__(self, n=10, sideLen=10):
self.n = n
self.sideLen = sideLen
def create_square(self):
side = self.sideLen * 5
x = - side / 2
y = side / 2
penup()
goto(x, y)
pendown()
for i in range(4):
forward(side)
right(90)
def create_grid(self):
x = - (self.n * self.sideLen) / 2
y = - x
penup()
goto(x, y)
for row in range(self.n):
for col in range(self.n):
pendown()
forward(self.sideLen)
right(90)
forward(self.sideLen)
right(90)
forward(self.sideLen)
right(90)
forward(self.sideLen)
right(90)
penup()
forward(self.sideLen)
y -= self.sideLen
goto(x, y)
def create_maze(self):
pr = PrimsRandomized(n)
mst = pr.prims_mst()
x = - (self.n / 2) * self.sideLen
y = - x
penup()
goto(x, y)
for row in range(self.n):
for col in range(self.n):
node = row * self.n + col
pendown()
if mst[node][pr.TOP] == 1:
penup()
forward(self.sideLen)
right(90)
pendown()
if mst[node][pr.RIGHT] == 1 or node == self.n ** 2 - 1:
penup()
forward(self.sideLen)
right(90)
pendown()
if mst[node][pr.BOTTOM] == 1:
penup()
forward(self.sideLen)
right(90)
pendown()
if mst[node][pr.LEFT] == 1 or node == 0:
penup()
forward(self.sideLen)
right(90)
penup()
forward(self.sideLen)
y -= self.sideLen
penup()
goto(x, y)
if __name__ == '__main__':
pensize(2)
hideturtle()
speed(30)
n = 25
sideLen = 25
rm = RectMaze(n, sideLen)
rm.create_maze()
done()