repl.it
@18cyrj/

Finished maze project

Python (with Turtle)

Generates mazes using Kruskal's algorithm then allows the user to solve the maze, it also has a variable gridsize and cellsize

fork
loading
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from turtle import *
import random, time

######################################################################################
#Creating box

#Prepare to draw box then grid and creates list of lines that cannot be passed
speed(0)
gridsize = 5 #changable, the higher the number the longer it takes
cellsize = 50 #must be even number
pensize(2)
hideturtle()
canthit = []

#making list of all points
points = []
xnum = -cellsize/2
for x in range(gridsize):
  ynum = -cellsize/2
  xnum += cellsize
  for y in range(gridsize):
    ynum += cellsize
    points.append((xnum,ynum))

#draw box
for i in range(4):
  a = pos()
  if a[0] % cellsize != 0:
    a = a[0]-1,a[1]
  a = (int(a[0]),int(a[1]))
  forward(gridsize*cellsize)
  canthit.append((a,(int(pos()[0]),int(pos()[1]))))
  left(90)

#draw and define each line in list "edges"
edges = []
for x in range(gridsize-1):
  forward(cellsize)
  color("red")
  seth(90)
  pendown()
  for i in range(gridsize):
    seth(90)
    a = pos()
    forward(cellsize)
    edges.append((a,pos(),90))
  penup()
  forward(-gridsize*cellsize)
  seth(0)
goto(0,0)
seth(90)
for x in range(gridsize-1):
  forward(cellsize)
  seth(0)
  pendown()
  for i in range(gridsize):
    seth(0)
    a = pos()
    forward(cellsize)
    edges.append((a,pos(),0))
  penup()
  forward(-gridsize*cellsize)
  seth(90)

#####################################################################################
#Defining the functions for solving

#function that takes list of lines and returns whether the position is on any of them
def online(positon,linelist):
  for line in linelist:
    first = line[0]
    second = line[1]
    firstx = first[0]
    firsty = first[1]
    secondx = second[0]
    secondy = second[1]
    if firstx <= positon[0] <= secondx and firsty >= positon[1] >= secondy:
      return(True)
    elif firstx <= positon[0] <= secondx and firsty <= positon[1] <= secondy:
      return(True)
    elif firstx >= positon[0] >= secondx and firsty >= positon[1] >= secondy:
      return(True)
  return(False)

#function that accepts of a position and the lines it can go across, then returns all the possible moves
def checkpossiblemoves(pos,allowed):
  #resets possible move directions to false for checking
  right = False
  left = False
  up = False
  down = False
  possiblemoves = []
  
  #Makes pos() easier to manipulate and makes sure it's correct
  xval = pos[0]
  if pos[0] % 5 != 0:
    xval = pos[0] + (5 - pos[0] % 5)
  yval = round(int(pos[1]),0)
  for line in allowed:
    line1x = round(int(line[0][0]),0)
    if line1x % 5 != 0:
      line1x += 1
    line1y = round(int(line[0][1]),0)
    line2x = round(int(line[1][0]),0)
    if line2x % 5 != 0:
      line2x += 1
    line2y = round(int(line[1][1]),0)
    line = [((line1x,line1y),(line2x,line2y))]
    spot1 = ((xval + cellsize/2),(yval))
    spot2 = ((xval - cellsize/2),(yval))
    spot3 = (xval,yval + cellsize/2)
    spot4 = (xval,yval - cellsize/2)
    if online(spot1,line) == True and 0 not in possiblemoves:
      possiblemoves.append(0)
    if online(spot2,line) == True and 180 not in possiblemoves:
      possiblemoves.append(180)
    if online(spot3,line) == True and 90 not in possiblemoves:
      possiblemoves.append(90)
    if online(spot4,line) == True and 270 not in possiblemoves:
      possiblemoves.append(270)
  return(possiblemoves)

#checks if all points are accessible
def donecheck():
  penup()
  goto(25,25)
  needtocheck = [(25,25)]
  checkerbeento = []
  allpoints = points
  while True:
    if len(needtocheck) != 0:
      checkingnow = random.choice(needtocheck)
    else:
      break
    needtocheck.remove(checkingnow)
    goto(checkingnow)
    checkerbeento.append(pos())
    possibledirections = checkpossiblemoves(pos(),allowedlines)
    for head in possibledirections:
      penup()
      seth(head)
      forward(cellsize)
      goingtocheck = (int(pos()[0]),int(pos()[1]))
      if goingtocheck[0] % 5 != 0:
        goingtocheck = (goingtocheck[0]+1,goingtocheck[1])
      if goingtocheck not in checkerbeento:
        needtocheck.append(goingtocheck)
      forward(-cellsize)
  for x in checkerbeento:
    if x in allpoints:
      allpoints.remove(x)
  if len(allpoints) == 0:
    return("done")

######################################################################################
#Generating actual maze

#counts by cellsize/2's and creates list of 1 less than each
spots = []
xvalue = -cellsize/2
yvalue = -cellsize/2
for t in range(gridsize):
  xvalue += cellsize
  for b in range(gridsize):
    yvalue += cellsize
    spots.append((xvalue,yvalue))
  yvalue = -cellsize/2

#chooses random edge and defines one side of it as the beginning and one as the end
allowedlines = []
for x in range(len(edges)):
  if len(edges) < ((gridsize+1)**2-1)*.333:
    if donecheck() == "done":
      for edge in edges:
        pensize(2)
        goto(edge[0])
        seth(edge[2])
        pendown()
        color("black")
        forward(cellsize)
        penup()
        l1x = int(edge[0][0])
        l1y = int(edge[0][1])
        l2x = int(edge[1][0])
        l2y = int(edge[1][1])
        if l1x % 5 != 0:
          l1x += 1
        if l2x % 5 != 0:
          l2x += 1
        if l1x % cellsize != 0:
          l1x -= 1
        if l2x % cellsize != 0:
          l2x -= 1
        line = ((l1x,l1y), (l2x,l2y))
        canthit.append(line)
      break
  edge = random.choice(edges)
  edges.remove(edge)
  penup()
  goto(edge[0][0],edge[0][1])
  seth(edge[2])
  color("white")
  forward(cellsize)

  #creates start and end points based on direction the line is going
  if edge[2] == 90:
    seth(90)
    forward(-cellsize/2)
    seth(0)
    forward(cellsize/2)
    begin = pos()
    forward(-cellsize)
    end = int(pos()[0]),int(pos()[1])
  elif edge[2] == 0:
    penup()
    seth(0)
    forward(-cellsize/2)
    seth(90)
    forward(cellsize/2)
    begin = pos()
    forward(-cellsize)
    end = int(pos()[0]),int(pos()[1])
  if end[0] % 5 != 0:
    end = (end[0]+1,end[1])
  goto(begin)
  
  #resets edge to not solved
  gotthere = False
  goto(begin)
  havetocheck = [(int(pos()[0]),int(pos()[1]))]
  beento = []
  
  #goes to all possible spots then the spots from that and so on and defines each spot as reachable
  while True:
    penup()
    if len(havetocheck) != 0:
      checking = random.choice(havetocheck)
    else:
      break
    havetocheck.remove(checking)
    goto(checking)
    start = pos()
    if start[0] % 5 != 0:
      start = (start[0]+1,start[1])
    possible = checkpossiblemoves(start,allowedlines)
    penup()
    if checking[0] % 5 != 0:
      checking = (checking[0]+1,checking[1])
    beento.append(checking)
    if checking == end:
      break
    goto(start)
    for direction in possible:
      penup()
      seth(direction)
      forward(cellsize)
      tocheck = (int(pos()[0]),int(pos()[1]))
      if tocheck[0] % 5 != 0:
        tocheck = (tocheck[0]+1,tocheck[1])
      if tocheck not in beento:
        havetocheck.append(tocheck)
      forward(-cellsize)

  #if not solvable draws in line as white
  if end not in beento:
    pensize(4)
    goto(edge[0])
    seth(edge[2])
    pendown()
    color("white")
    forward(cellsize)
    allowedlines.append(edge)
    penup()

  #if solvable draws line in black
  else:
    pensize(2)
    goto(edge[0])
    seth(edge[2])
    pendown()
    color("black")
    forward(cellsize)
    penup()
    l1x = int(edge[0][0])
    l1y = int(edge[0][1])
    l2x = int(edge[1][0])
    l2y = int(edge[1][1])
    if l1x % 5 != 0:
      l1x += 1
    if l2x % 5 != 0:
      l2x += 1
    if l1x % cellsize != 0:
      l1x -= 1
    if l2x % cellsize != 0:
      l2x -= 1
    line = ((l1x,l1y), (l2x,l2y))
    canthit.append(line)

#fill box
goto(0,0)
pendown()
color("black")
seth(0)
pensize(2)
for i in range(4):
  forward(gridsize*cellsize)
  left(90)

#make white spaces
goto(0,gridsize*cellsize-cellsize)
seth(90)
pendown()
pensize(3)
color("white")
forward(cellsize)
penup()
goto(gridsize*cellsize-cellsize,0)
seth(0)
pendown()
forward(cellsize)

#####################################################################################
#Making the maze a game with user inputs
showturtle()
shape("turtle")
color("red")
penup()
goto(cellsize/2,gridsize*cellsize-cellsize/2)
pendown()
speed(5)

#functions for moving in each direction
def goright():
  seth(0)
  forward(cellsize/2)
  hit = False
  posx = pos()[0]
  posy = pos()[1]
  if posx % 5 != 0:
    posx += 1
  position = (posx,posy)
  if online(position,canthit) == True:
    hit = True
  if hit == False:
    forward(cellsize/2)
    time.sleep(.3)
  else:
    forward(-cellsize/2)

def goleft():
  seth(180)
  forward(cellsize/2)
  hit = False
  posx = pos()[0]
  posy = pos()[1]
  if posx % 5 != 0:
    posx += 1
  position = (posx,posy)
  if online(position,canthit) == True:
    hit = True
  if hit == False:
    forward(cellsize/2)
    time.sleep(.3)
  else:
    forward(-cellsize/2)

def goup():
  seth(90)
  forward(cellsize/2)
  hit = False
  posx = pos()[0]
  posy = pos()[1]
  if posx % 5 != 0:
    posx += 1
  position = (posx,posy)
  if online(position,canthit) == True:
    hit = True
  if hit == False:
    forward(cellsize/2)
    time.sleep(.3)
  else:
    forward(-cellsize/2)

def godown():
  seth(270)
  forward(cellsize/2)
  hit = False
  posx = pos()[0]
  posy = pos()[1]
  if posx % 5 != 0:
    posx += 1
  position = (posx,posy)
  if online(position,canthit) == True:
    hit = True
  if hit == False:
    forward(cellsize/2)
    time.sleep(.3)
  else:
    forward(-cellsize/2)

#listening for button presses/ checking if the turtle is at the finish
s = Screen()
while True:
  if pos() == (cellsize*gridsize-cellsize/2,cellsize/2):
    color("lightgreen")
    break
  s.listen()
  s.onkey('w',goup)
  s.onkey('a',goleft)
  s.onkey('d',goright)
  s.onkey('s',godown)
result
console
?