-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvemaze.py
70 lines (57 loc) · 2.13 KB
/
solvemaze.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
import turtle
from INPUTS import WIDTH, HEIGHT, SQUARE_SIZE, MAZE, START, END
from utils.square import Square
turtle.tracer(0, 0) # Stop screen refreshes
#Screen + pen
screen = turtle.getscreen()
screen.setworldcoordinates(-1, -1, screen.window_width() - 1, screen.window_height() - 1)
pen = turtle.Turtle()
#Draw grid
squares = {}
for x in range(WIDTH):
squares[x] = {}
for y in range(HEIGHT):
square = Square(pen, squares, x, y, SQUARE_SIZE)
squares[x][y] = square
#Wall?
try: #No better way to check if index exists in dictionary?
if MAZE[x][y]: square.toggleWall()
except KeyError: pass
pen.up()
#Attempt to solve
#From the end, go back along the path with the least weight
def tracebackSquare(square):
leastWeight = square.weight
leastWeightSquare = None
for o in [square.up(), square.down(), square.left(), square.right()]:
if o:
if o.weight != 0 and o.weight < leastWeight:
leastWeight = o.weight
leastWeightSquare = o
if leastWeightSquare is None:
print("SOLVED!", square.x, square.y)
else:
leastWeightSquare.makePath()
leastWeightSquare.writeWeight() #So it doesn't get coloured over
tracebackSquare(leastWeightSquare)
#From the start, go to each surrounding square incrementing weight by 1 each move
def numberSquare(square, weight=1):
if square:
if square.weight == 0:
if not square.isWall:
square.weight = weight
square.writeWeight()
if not (square.x == END[0] and square.y == END[1]):
numberSquare(square.up(), weight+1)
numberSquare(square.down(), weight+1)
numberSquare(square.left(), weight+1)
numberSquare(square.right(), weight+1)
else:
#Reached the end square. Begin traceback
square.makePath()
square.writeWeight()
tracebackSquare(square)
startSquare = squares[START[0]][START[1]]
numberSquare(startSquare)
#Render screen
turtle.done()