-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·150 lines (132 loc) · 3.24 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
import maze
import cursesMS
import sdlMS
from random import randrange
def maze_random_algo(m, blind = True):
while m.unvisited() > 0:
m.turn(randrange(4))
if blind or m.frontFree():
m.move()
def maze_tremaux_algo(m):
def step(i):
if i == 1:
m.turn(2)
else:
m.turn(-1)
def findUnVistedDir():
for i in range(3):
if m.frontFree() and m.frontUnVisited():
return True
step(i)
return False
def findOnceVistedDir():
for i in range(3):
if m.frontFree() and m.frontOnceVisited():
return True
step(i)
return False
def findOnlyDir():
n = 0
for i in range(3):
if m.frontFree():
n += 1
step(i)
if n != 1:
return False
for i in range(3):
if m.frontFree():
return True
step(i)
def goDeep():
while findUnVistedDir():
m.move()
def turnAround():
m.turn(2)
m.markTwiceVisited()
def goBack():
while True:
if findUnVistedDir():
return True
if findOnceVistedDir():
m.move()
elif findOnlyDir():
m.move()
else:
return False
while True:
goDeep()
turnAround()
if not goBack():
m.turn()
if m.frontFree():
m.move()
if not goBack():
break
else:
break
def maze_backtrack_algo_rec(m, first=True):
if first:
n = 4
else:
n = 3
for i in range(n):
m.turn(i)
if m.frontFree() and m.frontUnVisited():
m.move()
maze_backtrack_algo_rec(m, False)
if not first:
m.turn(-1)
m.move()
m.turn(2)
def maze_backtrack_algo_it(m):
stack = [[3, 2, 1, 0]]
while stack:
job = stack.pop()
if job:
i = job.pop()
stack.append(job)
m.turn(i)
if m.frontFree() and m.frontUnVisited():
m.move()
stack.append([2, 1, 0])
elif stack:
m.turn(-1)
m.move()
m.turn(2)
try:
screen = sdlMS.sdlMazeScreen()
#screen = cursesMS.cursesMazeScreen()
m = maze.Maze(screen)
m.setPause(0.001)
m.generate()
key=''
while key != 'q':
key = screen.getkey()
if key == 'KEY_LEFT':
m.turn(-1)
continue
if key == 'KEY_RIGHT':
m.turn()
continue
if key == 'KEY_UP':
m.move()
continue
if key == 'i':
maze_backtrack_algo_it(m)
continue
if key == 'r':
maze_backtrack_algo_rec(m)
continue
if key == 't':
maze_tremaux_algo(m)
continue
if key == 'b':
maze_random_algo(m)
continue
if key == 'm':
maze_random_algo(m, False)
continue
finally:
sdlMS.sdlMazeScreen.cleanup()
#cursesMS.cursesMazeScreen.cleanup()