forked from shresprbn/Garbage_collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamil.py
566 lines (467 loc) · 24.8 KB
/
hamil.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import pygame as pg
import sys
from random import randint
import time
import os
from collections import deque
# Used to modify the window size, values must be a multiple of 40
screen_width = 600
screen_height = 400
# Controls where the window appears on the screen
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30)
class Fruit(object):
def __init__(self):
self.color = pg.Color(139, 0, 0)
self.width = 20
self.height = 20
self.fruit = None
self.radius = 10
# The initial position of the fruit is placed randomly on the screen
self.x = randint(0, screen_width / self.width - 1) * self.width
self.y = randint(0, screen_height / self.height - 1) * self.height
# Prints the fruit on the screen
def draw_fruit(self, surface):
self.fruit = pg.Rect(self.x, self.y, self.width, self.height)
pg.draw.circle(surface, self.color, (self.x + self.radius, self.y + self.radius), self.radius)
# Checks whether the snake's head collides with the fruit
def fruit_collision(self, head):
return self.fruit.colliderect(head)
# Finds a new location for a fruit after a collision occurs
def fruit_position(self):
flag = True
while flag:
# The position of the fruit is chosen randomly
self.x = randint(0, screen_width/self.width - 1) * self.width
self.y = randint(0, screen_height/self.height - 1) * self.height
# Checks whether the new fruit location is already occupied by the snake's body
if snake.empty_space(self.x, self.y):
break
class Snake(object):
def __init__(self):
self.x = screen_width//2
self.y = screen_height//2
self.width = 20
self.height = 20
self.head = None
self.speed = 20
self.direction = None
self.body = deque()
self.segment = deque()
self.head_color = pg.Color(220, 20, 60)
self.body_color = pg.Color(57, 255, 20)
self.outline_color = pg.Color(0, 0, 0)
# Draws the snake's head and body segments on the screen
def draw_snake(self, surface):
if len(self.body) > 0:
for unit in self.segment:
pg.draw.rect(surface, self.body_color, unit)
pg.draw.rect(surface, self.outline_color, unit, 1)
self.head = pg.Rect(self.x, self.y, self.width, self.height)
pg.draw.rect(surface, self.head_color, self.head)
pg.draw.rect(surface, self.outline_color, self.head, 1)
# Adds a segment to the snake if a collision between the head and fruit occurs
def snake_size(self):
if len(self.body) != 0:
index = len(self.body) - 1
x = self.body[index][0]
y = self.body[index][1]
self.body.append([x, y])
self.segment.append(pg.Rect(x, y, self.width, self.height))
# Ends the game in the case where the snake collides with the boundaries or the head collides with a body segment
def boundary_collision(self):
# If the head of the snake collides with a body segment the function returns True
# The head collides with the first 2 body segments, count prevents it from registering as a collision
count = 0
for part in self.segment:
if self.head.colliderect(part) and count > 2:
return True
count += 1
# Checks if the head of the snake lies outside of the boundaries of the window
if self.y < 0 or self.y > screen_height - self.height or self.x < 0 or self.x > screen_width - self.width:
return True
# Allows the snake to move and follow the coordinates of the hamiltonian cycle
def movement(self):
if self.direction == 'up':
self.y -= self.speed
if self.direction == 'down':
self.y += self.speed
if self.direction == 'right':
self.x += self.speed
if self.direction == 'left':
self.x -= self.speed
# Movement is simulated by removing the tail block and adding a block that overlaps with the snake head
if len(self.body) > 0:
self.body.pop()
self.segment.pop()
self.body.appendleft([self.x, self.y])
self.segment.appendleft(pg.Rect(self.x, self.y, self.width, self.height))
# Changes the orientation of movement
# A snake moving in one direction cannot move in the opposite direction as it would collide with its body
def change_direction(self, direction):
if direction == 'up' and self.direction != 'down':
self.direction = 'up'
if direction == 'down' and self.direction != 'up':
self.direction = 'down'
if direction == 'right' and self.direction != 'left':
self.direction = 'right'
if direction == 'left' and self.direction != 'right':
self.direction = 'left'
# Checks whether a new fruit position conflicts with a body segment of the snake
def empty_space(self, x_coordinate, y_coordinate):
if [x_coordinate, y_coordinate] not in self.body:
return True
else:
return False
# Controls the graphics
# Controls the movement of the snake to follow the hamiltonian cycle
def gameplay(fruit, snake, cycle):
# Identifies the starting position of the snake
position = (int(snake.x/20), int(snake.y/20))
# Identifies the position in the hamiltonian cycle at which the snake begins
index = cycle.index(position)
length = len(cycle)
run = True
# Loop simulates the movement of the snake and controls game mechanics
while run:
# Controls the frame rate of the graphics to make movement smooth and modify the speed of the simulation
clock = pg.time.Clock()
clock.tick(50)
# If the user clicks the exit button the program closes
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
# Movement is simulated by making screen black and redrawing the snake and fruit
window.fill(pg.Color(0, 0, 0))
fruit.draw_fruit(window)
snake.draw_snake(window)
# Finds the direction for the snake's next movement according to the calculated hamiltonian cycle
if index + 1 < length and cycle[index+1] == (position[0] + 1, position[1]):
snake.change_direction('right')
position = (position[0] + 1, position[1])
elif index + 1 < length and cycle[index+1] == (position[0] - 1, position[1]):
snake.change_direction('left')
position = (position[0] - 1, position[1])
elif index + 1 < length and cycle[index+1] == (position[0], position[1] + 1):
snake.change_direction('down')
position = (position[0], position[1] + 1)
elif index + 1 < length and cycle[index+1] == (position[0], position[1] - 1):
snake.change_direction('up')
position = (position[0], position[1] - 1)
# Takes care of boundary case where the next index of the cycle does not exist
# The next position is 1st index of the cycle
# Otherwise the index is incremented by 1
if index == length - 1:
if cycle[0] == (position[0] + 1, position[1]):
snake.change_direction('right')
position = (position[0] + 1, position[1])
elif cycle[0] == (position[0] - 1, position[1]):
snake.change_direction('left')
position = (position[0] - 1, position[1])
elif cycle[0] == (position[0], position[1] + 1):
snake.change_direction('down')
position = (position[0], position[1] + 1)
elif cycle[0] == (position[0], position[1] - 1):
snake.change_direction('up')
position = (position[0], position[1] - 1)
index = 0
else:
index += 1
# Changes the coordinates of the snake's position
snake.movement()
# If the snake's head collides with a fruit
if fruit.fruit_collision(snake.head):
# A new fruit is generated and the size of the snake is increased by 1
if len(snake.body) < length:
fruit.fruit_position()
snake.snake_size()
# Once the snake fills up the entire grid there are no more positions for the fruit
# The game ends and closes
else:
time.sleep(3)
pg.quit()
sys.exit()
# Ends the game if the snakes collides with itself or the boundaries
if snake.boundary_collision():
time.sleep(3)
pg.quit()
sys.exit()
# Draws all elements on the window
pg.display.update()
# Uses prim's algorithm to generate a randomized maze using randomized edge weights
def prim_maze_generator(grid_rows, grid_columns):
directions = dict()
vertices = grid_rows * grid_columns
# Creates keys for the directions dictionary
# Note that the maze has half the width and length of the grid for the hamiltonian cycle
for i in range(grid_rows):
for j in range(grid_columns):
directions[j, i] = []
# The initial cell for maze generation is chosen randomly
x = randint(0, grid_columns - 1)
y = randint(0, grid_rows - 1)
initial_cell = (x, y)
current_cell = initial_cell
# Stores all cells that have been visited
visited = [initial_cell]
# Contains all neighbouring cells to cells that have been visited
adjacent_cells = set()
# Generates walls in grid randomly to create a randomized maze
while len(visited) != vertices:
# Stores the position of the current cell in the grid
x_position = current_cell[0]
y_position = current_cell[1]
# Finds adjacent cells when the current cell does not lie on the edge of the grid
if x_position != 0 and y_position != 0 and x_position != grid_columns - 1 and y_position != grid_rows - 1:
adjacent_cells.add((x_position, y_position - 1))
adjacent_cells.add((x_position, y_position + 1))
adjacent_cells.add((x_position - 1, y_position))
adjacent_cells.add((x_position + 1, y_position))
# Finds adjacent cells when the current cell lies in the left top corner of the grid
elif x_position == 0 and y_position == 0:
adjacent_cells.add((x_position + 1, y_position))
adjacent_cells.add((x_position, y_position + 1))
# Finds adjacent cells when the current cell lies in the bottom left corner of the grid
elif x_position == 0 and y_position == grid_rows - 1:
adjacent_cells.add((x_position, y_position - 1))
adjacent_cells.add((x_position + 1, y_position))
# Finds adjacent cells when the current cell lies in the left column of the grid
elif x_position == 0:
adjacent_cells.add((x_position, y_position - 1))
adjacent_cells.add((x_position, y_position + 1))
adjacent_cells.add((x_position + 1, y_position))
# Finds adjacent cells when the current cell lies in the top right corner of the grid
elif x_position == grid_columns - 1 and y_position == 0:
adjacent_cells.add((x_position, y_position + 1))
adjacent_cells.add((x_position - 1, y_position))
# Finds adjacent cells when the current cell lies in the bottom right corner of the grid
elif x_position == grid_columns - 1 and y_position == grid_rows - 1:
adjacent_cells.add((x_position, y_position - 1))
adjacent_cells.add((x_position - 1, y_position))
# Finds adjacent cells when the current cell lies in the right column of the grid
elif x_position == grid_columns - 1:
adjacent_cells.add((x_position, y_position - 1))
adjacent_cells.add((x_position, y_position + 1))
adjacent_cells.add((x_position - 1, y_position))
# Finds adjacent cells when the current cell lies in the top row of the grid
elif y_position == 0:
adjacent_cells.add((x_position, y_position + 1))
adjacent_cells.add((x_position - 1, y_position))
adjacent_cells.add((x_position + 1, y_position))
# Finds adjacent cells when the current cell lies in the bottom row of the grid
else:
adjacent_cells.add((x_position, y_position - 1))
adjacent_cells.add((x_position + 1, y_position))
adjacent_cells.add((x_position - 1, y_position))
# Generates a wall between two cells in the grid
while current_cell:
current_cell = (adjacent_cells.pop())
# The neighbouring cell is disregarded if it is already a wall in the maze
if current_cell not in visited:
# The neighbouring cell is now classified as having been visited
visited.append(current_cell)
x = current_cell[0]
y = current_cell[1]
# To generate a wall, a cell adjacent to the current cell must already have been visited
# The direction of the wall between cells is stored
# The process is simplified by only considering a wall to be to the right or down
if (x + 1, y) in visited:
directions[x, y] += ['right']
elif (x - 1, y) in visited:
directions[x-1, y] += ['right']
elif (x, y + 1) in visited:
directions[x, y] += ['down']
elif (x, y - 1) in visited:
directions[x, y-1] += ['down']
break
# Provides the hamiltonian cycle generating algorithm with the direction of the walls to avoid
return hamiltonian_cycle(grid_rows, grid_columns, directions)
# Finds a hamiltonian cycle for the snake to follow to prevent collisions with its body segments
# Note that the grid for the hamiltonian cycle is double the width and height of the grid for the maze
def hamiltonian_cycle(grid_rows, grid_columns, orientation):
# The path for the snake is stored in a dictionary
# The keys are the (x, y) positions in the grid
# The values are the adjacent (x, y) positions that the snake can travel towards
hamiltonian_graph = dict()
# Uses the coordinates of the walls to generate available adjacent cells for each cell
# Simplified by only considering the right and down directions
for i in range(grid_rows):
for j in range(grid_columns):
# Finds available adjacent cells if current cell does not lie on an edge of the grid
if j != grid_columns - 1 and i != grid_rows - 1 and j != 0 and i != 0:
if 'right' in orientation[j, i]:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 2, i*2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 2, i*2 + 1)]
else:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' in orientation[j, i]:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2, i*2 + 2)]
if (j*2 + 1, i*2 + 1) in hamiltonian_graph:
hamiltonian_graph[j * 2 + 1, i * 2 + 1] += [(j * 2 + 1, i * 2 + 2)]
else:
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 1, i*2 + 2)]
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'down' not in orientation[j, i-1]:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
if 'right' not in orientation[j-1, i]:
if (j*2, i*2) in hamiltonian_graph:
hamiltonian_graph[j * 2, i * 2] += [(j * 2, i * 2 + 1)]
else:
hamiltonian_graph[j*2, i*2] = [(j*2, i*2 + 1)]
# Finds available adjacent cells if current cell is in the bottom right corner
elif j == grid_columns - 1 and i == grid_rows - 1:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' not in orientation[j, i-1]:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
elif 'right' not in orientation[j-1, i]:
hamiltonian_graph[j*2, i*2] = [(j*2, i*2 + 1)]
# Finds available adjacent cells if current cell is in the top right corner
elif j == grid_columns - 1 and i == 0:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' in orientation[j, i]:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2, i*2 + 2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 1, i*2 + 2)]
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'right' not in orientation[j-1, i]:
hamiltonian_graph[j*2, i*2] += [(j*2, i*2 + 1)]
# Finds available adjacent cells if current cell is in the right column
elif j == grid_columns - 1:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' in orientation[j, i]:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2, i*2 + 2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 1, i*2 + 2)]
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'down' not in orientation[j, i-1]:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
if 'right' not in orientation[j-1, i]:
if (j*2, i*2) in hamiltonian_graph:
hamiltonian_graph[j * 2, i * 2] += [(j * 2, i * 2 + 1)]
else:
hamiltonian_graph[j*2, i*2] = [(j*2, i*2 + 1)]
# Finds available adjacent cells if current cell is in the top left corner
elif j == 0 and i == 0:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
hamiltonian_graph[j*2, i*2] += [(j*2, i*2 + 1)]
if 'right' in orientation[j, i]:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 2, i*2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 2, i*2 + 1)]
else:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' in orientation[j, i]:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2, i*2 + 2)]
if (j*2 + 1, i*2 + 1) in hamiltonian_graph:
hamiltonian_graph[j * 2 + 1, i * 2 + 1] += [(j * 2 + 1, i * 2 + 2)]
else:
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 1, i*2 + 2)]
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
# Finds available adjacent cells if current cell is in the bottom left corner
elif j == 0 and i == grid_rows - 1:
hamiltonian_graph[j*2, i*2] = [(j*2, i*2 + 1)]
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'right' in orientation[j, i]:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 2, i*2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 2, i*2 + 1)]
else:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' not in orientation[j, i-1]:
hamiltonian_graph[j * 2, i * 2] += [(j * 2 + 1, i * 2)]
# Finds available adjacent cells if current cell is in the left corner
elif j == 0:
hamiltonian_graph[j*2, i*2] = [(j*2, i*2 + 1)]
if 'right' in orientation[j, i]:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 2, i*2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 2, i*2 + 1)]
else:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' in orientation[j, i]:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2, i*2 + 2)]
if (j*2 + 1, i*2 + 1) in hamiltonian_graph:
hamiltonian_graph[j*2 + 1, i*2 + 1] += [(j*2 + 1, i*2 + 2)]
else:
hamiltonian_graph[j * 2 + 1, i * 2 + 1] = [(j * 2 + 1, i * 2 + 2)]
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'down' not in orientation[j, i-1]:
hamiltonian_graph[j*2, i*2] += [(j*2 + 1, i*2)]
# Finds available adjacent cells if current cell is in the top row
elif i == 0:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
if 'right' in orientation[j, i]:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 2, i*2)]
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 2, i*2 + 1)]
else:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' in orientation[j, i]:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2, i*2 + 2)]
if (j*2 + 1, i*2 + 1) in hamiltonian_graph:
hamiltonian_graph[j * 2 + 1, i * 2 + 1] += [(j * 2 + 1, i * 2 + 2)]
else:
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 1, i*2 + 2)]
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'right' not in orientation[j-1, i]:
hamiltonian_graph[j*2, i*2] += [(j*2, i*2 + 1)]
# Finds available adjacent cells if current cell is in the bottom row
else:
hamiltonian_graph[j*2, i*2 + 1] = [(j*2 + 1, i*2 + 1)]
if 'right' in orientation[j, i]:
hamiltonian_graph[j*2 + 1, i*2 + 1] = [(j*2 + 2, i*2 + 1)]
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 2, i*2)]
else:
hamiltonian_graph[j*2 + 1, i*2] = [(j*2 + 1, i*2 + 1)]
if 'down' not in orientation[j, i-1]:
hamiltonian_graph[j*2, i*2] = [(j*2 + 1, i*2)]
if 'right' not in orientation[j-1, i]:
if (j*2, i*2) in hamiltonian_graph:
hamiltonian_graph[j*2, i*2] += [(j*2, i*2 + 1)]
else:
hamiltonian_graph[j * 2, i * 2] = [(j * 2, i * 2 + 1)]
# Provides the coordinates of available adjacent cells to generate directions for the snake's movement
return path_generator(hamiltonian_graph, grid_rows*grid_columns*4)
# Generates a path composed of coordinates for the snake to travel along
def path_generator(graph, cells):
# The starting position for the path is at cell (0, 0)
path = [(0, 0)]
previous_cell = path[0]
previous_direction = None
# Generates a path that is a hamiltonian cycle by following a set of general laws
# 1. If the right cell is available, travel to the right
# 2. If the cell underneath is available, travel down
# 3. If the left cell is available, travel left
# 4. If the cell above is available, travel up
# 5. The current direction cannot oppose the previous direction (e.g. left --> right)
while len(path) != cells:
if previous_cell in graph and (previous_cell[0] + 1, previous_cell[1]) in graph[previous_cell] \
and previous_direction != 'left':
path.append((previous_cell[0] + 1, previous_cell[1]))
previous_cell = (previous_cell[0] + 1, previous_cell[1])
previous_direction = 'right'
elif previous_cell in graph and (previous_cell[0], previous_cell[1] + 1) in graph[previous_cell] \
and previous_direction != 'up':
path.append((previous_cell[0], previous_cell[1] + 1))
previous_cell = (previous_cell[0], previous_cell[1] + 1)
previous_direction = 'down'
elif (previous_cell[0] - 1, previous_cell[1]) in graph \
and previous_cell in graph[previous_cell[0] - 1, previous_cell[1]] and previous_direction != 'right':
path.append((previous_cell[0] - 1, previous_cell[1]))
previous_cell = (previous_cell[0] - 1, previous_cell[1])
previous_direction = 'left'
else:
path.append((previous_cell[0], previous_cell[1] - 1))
previous_cell = (previous_cell[0], previous_cell[1] - 1)
previous_direction = 'up'
# Returns the coordinates of the hamiltonian cycle path
return path
circuit = prim_maze_generator(int(screen_height/40), int(screen_width/40))
pg.init()
window = pg.display.set_mode((screen_width, screen_height))
pg.display.set_caption('Snake Solver')
fruit = Fruit()
snake = Snake()
gameplay(fruit, snake, circuit)