-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1650 lines (1299 loc) · 49.4 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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import pygame
from time import sleep
from random import randint
#from min_heap import Min_heap
class Min_heap():
def __init__(self, items=list()):
self.heap = [None]
for i in items:
self.heap.append(i)
self.float_up(len(self.heap) - 1)
def print(self):
print(self.heap[1:])
def push(self, data):
self.heap.append(data)
self.float_up(len(self.heap) - 1)
def peek(self):
if len(self.heap) > 1:
return self.heap[1]
else:
return False
def pop(self):
if len(self.heap) > 1:
self.heap[1], self.heap[len(
self.heap) - 1] = self.heap[len(self.heap) - 1], self.heap[1]
minVal = self.heap.pop()
self.heapify(1)
else:
minVal = False
return minVal
def float_up(self, index):
parent = index // 2
if index <= 1:
return None
elif self.heap[index] < self.heap[parent]:
self.heap[index], self.heap[parent] = self.heap[parent], self.heap[index]
self.float_up(parent)
def heapify(self, index):
left = index * 2
right = left + 1
smallest = index
if len(self.heap) > left and self.heap[smallest] > self.heap[left]:
smallest = left
if len(self.heap) > right and self.heap[smallest] > self.heap[right]:
smallest = right
if smallest != index:
self.heap[index], self.heap[smallest] = self.heap[smallest], self.heap[index]
self.heapify(smallest)
step = 15
w = 810
h = 604
car = pygame.image.load('imgs/car_1.png')
wall = pygame.image.load('imgs/wall_1.jpg')
arr = pygame.image.load('imgs/arr_1.png')
sema = pygame.image.load('imgs/sema_1.jpg')
colors = [(0, 0, 150), (0, 150, 0), (150, 0, 150), (150, 0, 150), (0, 150, 150), (150, 150, 0)]
multi_ends = True
def init():
pygame.init()
global win
win = pygame.display.set_mode((w, h + 50))
global finish_arr
finish_arr = list()
global prev_col
prev_col = (255, 255, 255)
global prev_col_2
prev_col_2 = (255, 255, 255)
global algo
algo = None
global labirinto
labirinto = True
pygame.display.set_caption('Path Finding Algorithms')
class Pixel():
def __init__(self, color, pos):
self.color = color
self.pos = pos
def draw(self):
pygame.draw.rect(win, self.color, self.pos)
def is_over(self, mouse):
return mouse == (self.pos[0], self.pos[1])
class Grid():
def __init__(self):
self.mat = list()
c = 0
for i in range(0, w, step):
self.mat.append(list())
c += 1
for y in range(0, h, step):
self.mat[c - 1].append(Pixel((255, 255, 255),
(i, y, step, step)))
x_1 = 20
y_1 = 20
x_2 = 35
y_2 = 20
self.mat[x_1][y_1].color = (0, 255, 0)
self.mat[x_2][y_2].color = (255, 0, 0)
global ends_list
ends_list = list([(35, 20)])
def draw(self):
for i in range(w // step):
for y in range(h // step):
if self.mat[i][y].color == (255, 0, 0):
continue
self.mat[i][y].draw()
for i in range(w // step):
for y in range(h // step):
pos = (self.mat[i][y].pos[0] - 6, self.mat[i][y].pos[1] - 6)
pos_wall = self.mat[i][y].pos
if self.mat[i][y].color == (0, 255, 0):
win.blit(pygame.transform.scale(car, (25, 25)), pos)
if self.mat[i][y].color == (0, 0, 0):
win.blit(pygame.transform.scale(wall, (13, 13)), pos_wall)
if self.mat[i][y].color == (255, 0, 0):
win.blit(pygame.transform.scale(arr, (25, 25)), pos)
if self.mat[i][y].color == (255, 0, 255):
win.blit(pygame.transform.scale(sema, (18, 18)), pos_wall)
class Button():
def __init__(self, color, pos, text=''):
self.color = color
self.pos = pos
self.text = text
def is_over(self, val):
if val[0] > self.pos[0] and val[0] < self.pos[0] + self.pos[2]:
if val[1] > self.pos[1] and val[1] < self.pos[1] + self.pos[3]:
return True
return False
def draw(self):
self.color = (122, 122, 122)
self.pos = (380, 615, 120, 30)
pygame.draw.rect(win, self.color, self.pos)
if self.text != '':
size = 15
font = pygame.font.SysFont('comicsans', size)
text = font.render(self.text, 1, (255, 255, 255))
win.blit(text, (390, 618))
def drawGrid():
for i in range(0, h, step):
pass
pygame.draw.line(win, (122, 122, 122), (0, i), (w, i))
return None
def random_walls():
global labirinto
if not labirinto:
return
labirinto = False
global grid
for i in range(len(grid.mat)):
for y in range(len(grid.mat[i])):
if grid.mat[i][y].color == (255, 255, 255) and randint(1, 6) <= 2:
grid.mat[i][y].color = (0, 0, 0)
if randint(1, 4) == 1:
redraw(grid, Button(
(0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
def redraw(grid, submit):
win.fill((255, 255, 255))
grid.draw()
drawGrid()
submit.draw()
pygame.display.update()
def main():
init()
global grid
global algo
grid = Grid()
clock = pygame.time.Clock()
global submit
global prev_col_2
global ends_list
submit = Button((0, 0, 0), (180, 515, 113, 20), 'Find Best Path')
while True:
clock.tick(30)
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
random_walls()
continue
if keys[pygame.K_BACKSPACE]:
grid = Grid()
sleep(0.2)
continue
try:
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[2]:
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
if multi_ends and grid.mat[pos[0]][pos[1]].color == (255, 255, 255):
grid.mat[pos[0]][pos[1]].color = (255, 0, 0)
ends_list.append((pos[0], pos[1]))
sleep(0.1)
elif grid.mat[pos[0]][pos[1]].color == (255, 0, 0) and len(get_ends()) > 1:
grid.mat[pos[0]][pos[1]].color = (255, 255, 255)
ends_list.remove((pos[0], pos[1]))
sleep(0.15)
elif grid.mat[pos[0]][pos[1]].color == (0, 0, 0) or grid.mat[pos[0]][pos[1]].color == (255, 0, 255):
grid.mat[pos[0]][pos[1]].color = (255, 255, 255)
redraw(grid, submit)
while True:
val = False
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
if grid.mat[pos[0]][pos[1]].color == (0, 0, 0) or grid.mat[pos[0]][pos[1]].color == (255, 0, 255):
val = True
grid.mat[pos[0]][pos[1]].color = (255, 255, 255)
if val:
redraw(grid, submit)
try:
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONUP:
break
except:
pass
except:
pass
if event.type == pygame.MOUSEBUTTONDOWN and submit.is_over(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
keys = pygame.key.get_pressed()
if keys[pygame.K_u]:
algo = 'Uniform'
exec(algo + '()')
elif keys[pygame.K_b]:
algo = 'BFS'
exec(algo + '()')
elif keys[pygame.K_a]:
algo = 'A_star'
exec(algo + '()')
elif keys[pygame.K_g]:
algo = 'Greedy'
exec(algo + '()')
elif keys[pygame.K_d]:
algo = 'DFS'
exec(algo + '()')
elif keys[pygame.K_r]:
for i, coord in enumerate(ends_list):
if i == 0:
continue
grid.mat[coord[0]][coord[1]].color = (255, 255, 255)
redraw(grid, Button((0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
algo = "Reverse"
exec(algo + '()')
elif keys[pygame.K_w]:
algo = "Weighted"
exec(algo + '()')
continue
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if pos[1] >= 600:
continue
pos = [pos[0] // step, pos[1] // step]
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] and grid.mat[pos[0]][pos[1]].color != (0, 0, 0) and grid.mat[pos[0]][pos[1]].color != (255, 0, 0) and grid.mat[pos[0]][pos[1]].color != (0, 0, 255):
grid.mat[pos[0]][pos[1]].color = (255, 0, 255)
while keys[pygame.K_LSHIFT]:
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
try:
val = grid.mat[pos[0]][pos[1]].color
except:
break
if grid.mat[pos[0]][pos[1]].color != (0, 0, 0) and grid.mat[pos[0]][pos[1]].color != (255, 0, 0) and grid.mat[pos[0]][pos[1]].color != (0, 255, 0):
grid.mat[pos[0]][pos[1]].color = (255, 0, 255)
redraw(grid, Button(
(0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
pygame.event.pump()
keys = pygame.key.get_pressed()
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONUP:
break
continue
if grid.mat[pos[0]][pos[1]].color == (255, 255, 255):
grid.mat[pos[0]][pos[1]].color = (0, 0, 0)
col = grid.mat[pos[0]][pos[1]].color
if col == (255, 0, 0):
try:
index = ends_list.index((pos[0], pos[1]))
ends_list.remove((pos[0], pos[1]))
except:
pass
while True:
if pygame.mouse.get_pos()[1] >= 592:
break
try:
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONUP:
new_pos = pygame.mouse.get_pos()
new_pos = [new_pos[0] // step, new_pos[1] // step]
if col == (255, 0, 0):
ends_list.insert(index, tuple(new_pos))
break
new_pos = pygame.mouse.get_pos()
new_pos = [new_pos[0] // step, new_pos[1] // step]
if new_pos != pos:
if col == (255, 0, 0) or col == (0, 255, 0):
try:
val = grid.mat[new_pos[0]][new_pos[1]].color
if val == (255, 0, 0) or val == (0, 255, 0):
if col == (255, 0, 0):
ends_list.insert(index, tuple(pos))
break
except:
if col == (255, 0, 0):
ends_list.insert(index, tuple(pos))
break
grid.mat[pos[0]][pos[1]].color = prev_col_2
prev_col_2 = grid.mat[new_pos[0]][new_pos[1]].color
grid.mat[new_pos[0]][new_pos[1]].color = col
else:
try:
val = grid.mat[new_pos[0]][new_pos[1]].color
except:
break
if grid.mat[new_pos[0]][new_pos[1]].color != (255, 0, 0) and grid.mat[new_pos[0]][new_pos[1]].color != (0, 255, 0) and grid.mat[new_pos[0]][new_pos[1]].color != (255, 0, 255):
grid.mat[new_pos[0]][new_pos[1]
].color = (0, 0, 0)
pos = new_pos
redraw(grid, submit)
except Exception as e:
raise e
redraw(grid, submit)
def finish(draw):
global grid
global submit
global finish_arr
global weigth
for i in range(1, len(finish_arr) - 1):
try:
if grid.mat[finish_arr[i][0]][finish_arr[i][1]].color != (255, 0, 0):
if grid.mat[finish_arr[i][0]][finish_arr[i][1]].color != (0, 255, 0):
grid.mat[finish_arr[i][0]][finish_arr[i]
[1]].color = (255, 255, 0)
if draw and randint(1, 4) < 4:
redraw(grid, Button(
(0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
except:
pass
redraw(grid, Button((0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
for i in range(len(grid.mat)):
for y in range(len(grid.mat[i])):
if (i, y) in weigth:
grid.mat[i][y].color = (255, 0, 255)
def wait_for_click():
global algo
global grid
global submit
global prev_col
global ends_list
global colors
global labirinto
run = True
while run:
run = True
event = pygame.event.poll()
pygame.event.pump()
if event.type == pygame.QUIT:
pygame.quit()
quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_BACKSPACE]:
grid = Grid()
sleep(0.2)
run = False
labirinto = True
try:
if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[2]:
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
if algo == 'Reverse' and (grid.mat[pos[0]][pos[1]].color in colors or grid.mat[pos[0]][pos[1]].color == (255, 255, 255)):
continue
if multi_ends and grid.mat[pos[0]][pos[1]].color == (255, 255, 255) or grid.mat[pos[0]][pos[1]].color == (0, 0, 255) or grid.mat[pos[0]][pos[1]].color == (255, 255, 0) or grid.mat[pos[0]][pos[1]].color in colors:
grid.mat[pos[0]][pos[1]].color = (255, 0, 0)
ends_list.append((pos[0], pos[1]))
delete_yellow_blue()
exec(algo + '(False)')
elif grid.mat[pos[0]][pos[1]].color == (255, 0, 0) and len(get_ends()) > 1:
grid.mat[pos[0]][pos[1]].color = (255, 255, 255)
ends_list.remove((pos[0], pos[1]))
delete_yellow_blue()
exec(algo + '(False)')
elif grid.mat[pos[0]][pos[1]].color == (0, 0, 0) or grid.mat[pos[0]][pos[1]].color == (255, 0, 255):
grid.mat[pos[0]][pos[1]].color = (255, 255, 255)
delete_yellow_blue()
exec(algo + '(False)')
while True:
if pygame.mouse.get_pos()[1] >= 592:
break
val = False
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
if grid.mat[pos[0]][pos[1]].color == (0, 0, 0) or grid.mat[pos[0]][pos[1]].color == (255, 0, 255):
val = True
if grid.mat[pos[0]][pos[1]].color != (255, 0, 0) and grid.mat[pos[0]][pos[1]].color != (0, 255, 0):
grid.mat[pos[0]][pos[1]].color = (255, 255, 255)
if val:
delete_yellow_blue()
exec(algo + '(False)')
try:
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONUP:
break
except:
pass
#continue
elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
if grid.mat[pos[0]][pos[1]].color != (255, 0, 0) and grid.mat[pos[0]][pos[1]].color != (0, 255, 0):
pos = pygame.mouse.get_pos()
pos = [pos[0] // step, pos[1] // step]
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] and grid.mat[pos[0]][pos[1]].color != (0, 0, 0):
if grid.mat[pos[0]][pos[1]].color == (255, 255, 255) or grid.mat[pos[0]][pos[1]].color == (255, 255, 0) or grid.mat[pos[0]][pos[1]].color in colors:
grid.mat[pos[0]][pos[1]].color = (255, 0, 255)
delete_yellow_blue()
exec(algo + '(False)')
col = grid.mat[pos[0]][pos[1]].color
while keys[pygame.K_LSHIFT]:
event = pygame.event.poll()
pygame.event.pump()
if event.type == pygame.MOUSEBUTTONUP:
break
new_pos = pygame.mouse.get_pos()
new_pos = [new_pos[0] // step, new_pos[1] // step]
if new_pos != pos:
try:
val = grid.mat[new_pos[0]
][new_pos[1]].color
except:
break
if grid.mat[new_pos[0]][new_pos[1]].color == (255, 255, 255) or grid.mat[new_pos[0]][new_pos[1]].color == (255, 255, 0) or grid.mat[new_pos[0]][new_pos[1]].color in colors:
grid.mat[new_pos[0]][new_pos[1]
].color = (255, 0, 255)
delete_yellow_blue()
exec(algo + '(False)')
pos = new_pos
if event.type == pygame.MOUSEBUTTONUP:
break
continue
if grid.mat[pos[0]][pos[1]].color == (255, 255, 255) or grid.mat[pos[0]][pos[1]].color == (255, 255, 0) or grid.mat[pos[0]][pos[1]].color in colors:
grid.mat[pos[0]][pos[1]].color = (0, 0, 0)
delete_yellow_blue()
exec(algo + '(False)')
col = grid.mat[pos[0]][pos[1]].color
while True:
if pygame.mouse.get_pos()[1] >= 592:
break
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONUP:
break
new_pos = pygame.mouse.get_pos()
new_pos = [new_pos[0] // step, new_pos[1] // step]
if new_pos != pos:
try:
val = grid.mat[new_pos[0]][new_pos[1]].color
except:
break
if grid.mat[new_pos[0]][new_pos[1]].color == (255, 255, 255) or grid.mat[new_pos[0]][new_pos[1]].color == (255, 255, 0) or grid.mat[new_pos[0]][new_pos[1]].color in colors:
grid.mat[new_pos[0]][new_pos[1]
].color = (0, 0, 0)
delete_yellow_blue()
exec(algo + '(False)')
pos = new_pos
elif grid.mat[pos[0]][pos[1]].color == (255, 0, 0) or grid.mat[pos[0]][pos[1]].color == (0, 255, 0):
prev_pos = pos
colore = grid.mat[pos[0]][pos[1]].color
while True:
if pygame.mouse.get_pos()[1] >= 592:
break
pos = pygame.mouse.get_pos()
new_pos = [pos[0] // step, pos[1] // step]
if prev_pos != new_pos:
try:
val = grid.mat[new_pos[0]][new_pos[1]].color
if val == (255, 0, 0) or val == (0, 255, 0):
break
except:
break
if colore == (255, 0, 0):
index = ends_list.index(tuple(prev_pos))
try:
ends_list[index] = tuple(new_pos)
except:
ends_list.append(tuple(new_pos))
grid.mat[prev_pos[0]][prev_pos[1]].color = prev_col
prev_col = grid.mat[new_pos[0]][new_pos[1]].color
grid.mat[new_pos[0]][new_pos[1]].color = colore
prev_pos = new_pos
delete_yellow_blue()
exec(algo + '(False)')
try:
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONUP:
prev_col = (255, 255, 255)
break
except:
pass
except:
pass
def get_ends():
global grid
res = list()
for i in range(len(grid.mat)):
for y in range(len(grid.mat[i])):
if grid.mat[i][y].color == (255, 0, 0):
res.append((i, y))
return res
def get_pos_mat():
global grid
for i in range(len(grid.mat)):
for y in range(len(grid.mat[i])):
if grid.mat[i][y].color == (0, 255, 0):
return i, y
def check(data, ends):
mem = set(data)
for i in ends:
if i in mem:
ends.remove(i)
return ends if len(ends) > 0 else 'END'
return False
def get_weight():
global grid
res = set()
for i in range(len(grid.mat)):
for y in range(len(grid.mat[i])):
if grid.mat[i][y].color == (255, 0, 255):
res.add((i, y))
return res
def A_star(draw=True, color=0):
global grid
global finish_arr
global ends_list
global weigth
global colors
weigth = get_weight()
start = get_pos_mat()
ends = ends_list[:]
end = ends.pop(0)
heap = Min_heap()
store = dict()
visited = set()
num = 0
cost = abs(end[0] - start[0]) + abs(end[1] - start[1]) + num
store[cost] = [[start]]
heap.push(cost)
while heap.heap:
num = heap.pop()
try:
path = store[num].pop()
except:
break
if len(store[num]) == 0:
del store[num]
x, y = path[-1]
if (x, y) in visited or y == 40:
continue
if end == (x, y):
color += 1
if color == 5:
color = 0
for item in path:
finish_arr.append(item)
if ends:
start = end
end = ends.pop(0)
heap = Min_heap()
store = dict()
visited = set()
cost = abs(end[0] - start[0]) + abs(end[1] - start[1])
store[cost] = [[start]]
heap.push(cost)
continue
else:
if draw:
finish(draw)
finish_arr = list()
wait_for_click()
return None
else:
finish(draw)
finish_arr = list()
return None
visited.add((x, y))
if grid.mat[x][y].color != (0, 255, 0) and grid.mat[x][y].color != (255, 0, 0) and grid.mat[x][y].pos[1] < 600 and grid.mat[x][y].color != (255, 0, 255):
grid.mat[x][y].color = colors[color]
if draw:
grid.mat[x][y].draw()
pygame.display.update()
if x + 1 < len(grid.mat) and grid.mat[x + 1][y].color != (0, 0, 0):
temp = path[:]
temp.append((x + 1, y))
if (x+1, y) in weigth:
w = 15
else:
w = 1
cost = abs(end[0] - (x + 1)) + abs(end[1] - y) + num + w - (abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
if y + 1 < len(grid.mat[x]) and grid.mat[x][y + 1].color != (0, 0, 0):
temp = path[:]
temp.append((x, y + 1))
if (x, y+1) in weigth:
w = 15
else:
w = 1
cost = abs(end[0] - x) + abs(end[1] - (y + 1)) + num + w - (abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
if x - 1 > -1 and grid.mat[x - 1][y].color != (0, 0, 0):
temp = path[:]
temp.append((x - 1, y))
if (x-1, y) in weigth:
w = 15
else:
w = 1
cost = abs(end[0] - (x - 1)) + abs(end[1] - y) + num + w - (abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
if y - 1 > -1 and grid.mat[x][y - 1].color != (0, 0, 0):
temp = path[:]
temp.append((x, y - 1))
if (x, y-1) in weigth:
w = 15
else:
w = 1
cost = abs(end[0] - x) + abs(end[1] - (y - 1)) + num + w - (abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
redraw(grid, Button((0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
if draw:
wait_for_click()
def Weighted(draw=True, color=0):
global grid
global finish_arr
global ends_list
global weigth
global colors
weigth = get_weight()
start = get_pos_mat()
ends = ends_list[:]
end = ends.pop(0)
heap = Min_heap()
store = dict()
visited = set()
num = 0
cost = 2*abs(end[0] - start[0]) + 2*abs(end[1] - start[1]) + num
store[cost] = [[start]]
heap.push(cost)
while heap.heap:
num = heap.pop()
try:
path = store[num].pop()
except:
break
if len(store[num]) == 0:
del store[num]
x, y = path[-1]
if (x, y) in visited or y == 40:
continue
if end == (x, y):
color += 1
if color == 5:
color = 0
for item in path:
finish_arr.append(item)
if ends:
start = end
end = ends.pop(0)
heap = Min_heap()
store = dict()
visited = set()
cost = 2*abs(end[0] - start[0]) + 2*abs(end[1] - start[1])
store[cost] = [[start]]
heap.push(cost)
continue
else:
if draw:
finish(draw)
finish_arr = list()
wait_for_click()
return None
else:
finish(draw)
finish_arr = list()
return None
visited.add((x, y))
if grid.mat[x][y].color != (0, 255, 0) and grid.mat[x][y].color != (255, 0, 0) and grid.mat[x][y].pos[1] < 600 and grid.mat[x][y].color != (255, 0, 255):
grid.mat[x][y].color = colors[color]
if draw:
grid.mat[x][y].draw()
pygame.display.update()
if x + 1 < len(grid.mat) and grid.mat[x + 1][y].color != (0, 0, 0):
temp = path[:]
temp.append((x + 1, y))
if (x+1, y) in weigth:
w = 15
else:
w = 1
cost = 2*abs(end[0] - (x + 1)) + 2*abs(end[1] - y) + num + w - 2*(abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
if y + 1 < len(grid.mat[x]) and grid.mat[x][y + 1].color != (0, 0, 0):
temp = path[:]
temp.append((x, y + 1))
if (x, y+1) in weigth:
w = 15
else:
w = 1
cost = 2*abs(end[0] - x) + 2*abs(end[1] - (y + 1)) + num + w - 2*(abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
if x - 1 > -1 and grid.mat[x - 1][y].color != (0, 0, 0):
temp = path[:]
temp.append((x - 1, y))
if (x-1, y) in weigth:
w = 15
else:
w = 1
cost = 2*abs(end[0] - (x - 1)) + 2*abs(end[1] - y) + num + w - 2*(abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
if y - 1 > -1 and grid.mat[x][y - 1].color != (0, 0, 0):
temp = path[:]
temp.append((x, y - 1))
if (x, y-1) in weigth:
w = 15
else:
w = 1
cost = 2*abs(end[0] - x) + 2*abs(end[1] - (y - 1)) + num + w - 2*(abs(end[0] - x) + abs(end[1] - y))
if cost not in store:
store[cost] = [temp]
else:
store[cost] += [temp]
heap.push(cost)
redraw(grid, Button((0, 0, 0), (180, 515, 113, 20), 'Find Best Path'))
if draw:
wait_for_click()
def BFS(draw=True, color=0):
global grid
global finish_arr
global ends_list
global weigth
global colors
weigth = get_weight()
start = get_pos_mat()
ends = ends_list[:]
end = ends.pop(0)
heap = Min_heap()
store = dict()
visited = set()
num = 0
cost = -1
store[cost] = [[start]]
heap.push(cost)
while heap.heap:
num = heap.pop()
try:
path = store[num].pop()
except:
break
if len(store[num]) == 0:
del store[num]
x, y = path[-1]
if (x, y) in visited or y == 40:
continue
if end == (x, y):
color += 1
if color == 5:
color = 0
for item in path:
finish_arr.append(item)
if ends:
start = end
end = ends.pop(0)
heap = Min_heap()
store = dict()
visited = set()
cost = 0
store[cost] = [[start]]
heap.push(cost)
continue