-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGenerator.py
101 lines (84 loc) · 3.98 KB
/
MazeGenerator.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
import random
class MazeGenerator:
def __init__(self, width, height):
self.width = width
self.height = height
self.path = []
self.start_point = []
self.dead_ends = []
self.goal = 0
self.n = 1
def sinir_kontrol(self, point):
if point[0] > self.width - 40 or point[0] < 0 or point[1] > self.height - 40 or point[1] < 0:
return True
else:
return False
def baslangic_noktasi_olustur(self):
right_bound = int((self.width - 80) / 40)
x = random.randint(1, right_bound)
while x % 2 == 0:
x = random.randint(1, right_bound)
top_bound = int((self.height - 80) / 40)
y = random.randint(1, top_bound)
while y % 2 == 0:
y = random.randint(1, top_bound)
self.start_point = [x * 40, y * 40]
def yol_olustur(self, current_point, pathway):
while True:
if pathway.count([current_point[0] + 80, current_point[1]]) == 1 or self.sinir_kontrol(
[current_point[0] + 80, current_point[1]]):
if pathway.count([current_point[0] - 80, current_point[1]]) == 1 or self.sinir_kontrol(
[current_point[0] - 80, current_point[1]]):
if pathway.count([current_point[0], current_point[1] + 80]) == 1 or self.sinir_kontrol(
[current_point[0], current_point[1] + 80]):
if pathway.count([current_point[0], current_point[1] - 80]) == 1 or self.sinir_kontrol(
[current_point[0], current_point[1] - 80]):
self.dead_ends.append(current_point)
if self.n == 1:
self.goal = current_point
self.n = 0
return pathway
x = random.randint(1, 4)
test_point = [current_point[0], current_point[1]]
if x == 1:
test_point[0] += 80
if pathway.count(test_point) == 1 or self.sinir_kontrol(test_point):
continue
else:
current_point = test_point
pathway.append(test_point)
pathway.append([test_point[0] - 40, test_point[1]])
elif x == 2:
test_point[0] -= 80
if pathway.count(test_point) == 1 or self.sinir_kontrol(test_point):
continue
else:
current_point = test_point
pathway.append(test_point)
pathway.append([test_point[0] + 40, test_point[1]])
elif x == 3:
test_point[1] += 80
if pathway.count(test_point) == 1 or self.sinir_kontrol(test_point):
continue
else:
current_point = test_point
pathway.append(test_point)
pathway.append([test_point[0], test_point[1] - 40])
elif x == 4:
test_point[1] -= 80
if pathway.count(test_point) == 1 or self.sinir_kontrol(test_point):
continue
else:
current_point = test_point
pathway.append(test_point)
pathway.append([test_point[0], test_point[1] + 40])
pathway = self.yol_olustur(current_point, pathway)
def hedefi_tekrar_tanımla(self):
p = random.randint(1, len(self.dead_ends) - 1)
return self.dead_ends[p]
def labirent_olustur(self):
"""Returns the coordinates of points for the normal tiles, the starting point, and the goal"""
self.baslangic_noktasi_olustur()
pathway = [self.start_point]
self.path = self.yol_olustur(self.start_point, pathway)
return self.path, self.start_point, self.goal