1
+ import pygame
2
+
3
+ import sys
4
+ import random
5
+ import time
6
+
7
+
8
+ def baseMaze (maze ):
9
+ maze = [
10
+ ["_" ] * 14 + [" " ] + ["_" ],
11
+ ["|" ] + [" " ] * 2 + ["|" ] * 3 + [" " ] * 3 + ["|" ] * 4 + [" " ] * 2 + ["|" ],
12
+ ["|" ] + [" " ] * 4 + ["|" ] * 1 + [" " ] * 2 + ["|" ] * 3 + [" " ] * 4 + ["|" ],
13
+ ["|" ] + ["|" ] * 2 + [" " ] * 1 + ["|" ] * 2 + [" " ] * 1 + ["|" ] * 2 + [" " ] * 3 + ["|" ] * 2 + [" " ] * 1 + ["|" ],
14
+ ["|" ] + ["|" ] * 1 + [" " ] * 2 + ["|" ] * 1 + [" " ] * 3 + ["|" ] * 1 + [" " ] * 2 + ["|" ] * 2 + [" " ] * 2 + ["|" ],
15
+ ["|" ] + [" " ] * 2 + ["|" ] * 2 + [" " ] * 1 + ["|" ] * 2 + [" " ] * 2 + ["|" ] * 1 + ["|" ] * 2 + [" " ] * 2 + [
16
+ "|" ] * 1 + ["|" ],
17
+ ["|" ] + ["|" ] * 1 + [" " ] * 1 + ["|" ] * 1 + [" " ] * 3 + ["|" ] * 2 + [" " ] * 2 + ["|" ] * 4 + ["|" ],
18
+ ["|" ] + [" " ] * 3 + ["|" ] * 2 + [" " ] * 1 + ["|" ] * 1 + [" " ] * 1 + [" " ] * 2 + ["|" ] * 3 + [" " ] * 1 + ["|" ],
19
+ ["|" ] + [" " ] * 1 + ["|" ] * 4 + [" " ] * 3 + ["|" ] * 2 + ["|" ] * 1 + [" " ] * 3 + ["|" ] * 1 + [" " ] * 1 + ["|" ],
20
+ ["|" ] + [" " ] * 4 + ["|" ] * 3 + [" " ] * 1 + [" " ] * 6 + ["|" ],
21
+ ["|" ] + ["|" ] * 3 + [" " ] * 2 + ["|" ] * 1 + [" " ] * 2 + ["|" ] * 4 + [" " ] * 1 + ["|" ] * 1 + ["|" ],
22
+ ["|" ] + [" " ] * 4 + ["|" ] * 2 + [" " ] * 2 + [" " ] * 1 + ["|" ] * 2 + [" " ] * 2 + ["|" ] * 1 + ["|" ],
23
+ ["|" ] + [" " ] * 1 + ["|" ] * 1 + [" " ] * 1 + ["|" ] * 2 + [" " ] * 5 + ["|" ] * 1 + [" " ] * 3 + ["|" ],
24
+ ["|" ] + [" " ] * 2 + ["|" ] * 1 + [" " ] * 2 + ["|" ] * 2 + [" " ] * 4 + ["|" ] * 2 + [" " ] * 1 + ["|" ],
25
+ ["|" ] + [" " ] * 2 + ["|" ] * 2 + [" " ] * 1 + [" " ] * 3 + ["|" ] * 2 + [" " ] * 3 + [" " ] * 1 + ["|" ],
26
+ ["|" ] + [" " ] * 9 + ["|" ] * 1 + [" " ] * 4 + ["|" ],
27
+ ["|" ] * 7 + ["Ω" ] + ["|" ] * 8 ,
28
+ ["_" ] * 16
29
+ ]
30
+ return maze
31
+
32
+
33
+ def baseMazePrint (printmaze ):
34
+ cell_size = width // len (printmaze [0 ]), height // len (printmaze )
35
+
36
+ for i in range (len (printmaze )):
37
+ for j in range (len (printmaze [0 ])):
38
+ rect = pygame .Rect (j * cell_size [0 ], i * cell_size [1 ], cell_size [0 ], cell_size [1 ])
39
+ if printmaze [i ][j ] == "_" :
40
+ pygame .draw .rect (screen , (255 , 255 , 255 ), rect )
41
+ elif printmaze [i ][j ] == "|" :
42
+ pygame .draw .rect (screen , (255 , 255 , 255 ), rect )
43
+ elif printmaze [i ][j ] == "Ω" :
44
+ pygame .draw .rect (screen , (0 , 0 , 0 ), rect )
45
+ elif printmaze [i ][j ] == " " :
46
+ pygame .draw .rect (screen , (0 , 0 , 0 ), rect )
47
+ pygame .display .update ()
48
+
49
+
50
+ def printMaze (printmaze ):
51
+ cell_size = width // len (printmaze [0 ]), height // len (printmaze )
52
+
53
+ for i in range (len (printmaze )):
54
+ for j in range (len (printmaze [0 ])):
55
+ rect = pygame .Rect (j * cell_size [0 ], i * cell_size [1 ], cell_size [0 ], cell_size [1 ])
56
+ if printmaze [i ][j ] == "_" :
57
+ pygame .draw .rect (screen , (255 , 255 , 255 ), rect )
58
+ elif printmaze [i ][j ] == "|" :
59
+ pygame .draw .rect (screen , (255 , 255 , 255 ), rect )
60
+ elif printmaze [i ][j ] == "Ω" :
61
+ pygame .draw .rect (screen , (0 , 255 , 0 ), rect )
62
+ pygame .display .update ()
63
+
64
+
65
+ def addingMoves (lst ):
66
+ lst .append (random .randint (1 , 4 ))
67
+ lst .append (random .randint (1 , 4 ))
68
+
69
+
70
+ def rowAdder (row , col , underScoreadd , addmaze ):
71
+ if not underScoreadd :
72
+ addmaze [row ][col ] = "_"
73
+ else :
74
+ addmaze [row ][col ] = " "
75
+
76
+
77
+ def moving (guy , rowcol , underScoreChange , amount , playmaze ):
78
+ score = 0
79
+ baseMazePrint (maze )
80
+ time .sleep (0.001 )
81
+ for i in range (amount ):
82
+ row = rowcol [0 ]
83
+ col = rowcol [1 ]
84
+ movement = guy [i ]
85
+
86
+ if (movement == 1 ) and (playmaze [row - 1 ][col ] != "_" ) and (playmaze [row - 1 ][col ] != "|" ):
87
+ # up move
88
+ rowAdder (row , col , underScoreChange , playmaze )
89
+ playmaze [row - 1 ][col ] = "Ω"
90
+ rowcol [0 ] = rowcol [0 ] - 1
91
+ underScoreChange = True
92
+ if (row == 1 ) and (col == 14 ):
93
+ baseMazePrint (maze )
94
+ printMaze (playmaze )
95
+ print ("You win!" )
96
+ global gameOn
97
+ gameOn = False
98
+ else :
99
+ score += 4
100
+ printMaze (maze )
101
+ time .sleep (0.05 )
102
+ elif (movement == 2 ) and (playmaze [row ][col - 1 ] != "|" ):
103
+ # left move
104
+ rowAdder (row , col , underScoreChange , playmaze )
105
+ if playmaze [row ][col - 1 ] == "_" :
106
+ underScoreChange = False
107
+ else :
108
+ underScoreChange = True
109
+ playmaze [row ][col - 1 ] = "Ω"
110
+ rowcol [1 ] = rowcol [1 ] - 1
111
+ score += 2
112
+ printMaze (maze )
113
+ time .sleep (0.05 )
114
+ elif (movement == 3 ) and (playmaze [row ][col + 1 ] != "|" ):
115
+ # right move
116
+ rowAdder (row , col , underScoreChange , playmaze )
117
+ if playmaze [row ][col + 1 ] == "_" :
118
+ underScoreChange = False
119
+ else :
120
+ underScoreChange = True
121
+ playmaze [row ][col + 1 ] = "Ω"
122
+ rowcol [1 ] = rowcol [1 ] + 1
123
+ score += 3
124
+ printMaze (maze )
125
+ time .sleep (0.05 )
126
+ elif (movement == 4 ) and (underScoreChange == True ) and (playmaze [row + 1 ][col ] != "|" ):
127
+ # down move
128
+ if row == 16 :
129
+ score -= 1000
130
+ return score
131
+ elif playmaze [row + 1 ][col ] == "_" :
132
+ underScoreChange = False
133
+ else :
134
+ underScoreChange = True
135
+ playmaze [row + 1 ][col ] = "Ω"
136
+ rowcol [0 ] = rowcol [0 ] + 1
137
+ score += 1
138
+ printMaze (maze )
139
+ time .sleep (0.05 )
140
+ else :
141
+ score -= 1000
142
+ return score
143
+
144
+ return score
145
+
146
+
147
+ # Pygame initialization
148
+ pygame .init ()
149
+ width , height = 800 , 800
150
+ screen = pygame .display .set_mode ((width , height ))
151
+ pygame .display .set_caption ("Maze Solver" )
152
+
153
+ clock = pygame .time .Clock ()
154
+ FPS = 60
155
+
156
+ gameOn = True
157
+ underScorelist = [True ] * 32
158
+ amount = 0
159
+ maze = []
160
+ guylist = [[] for _ in range (32 )]
161
+ scorelist = [0 ] * 32
162
+ generaltrack = 0
163
+ tStart = time .time ()
164
+ while gameOn :
165
+ generaltrack += 2
166
+ amount += 2
167
+
168
+ for _ in range (32 ):
169
+ maze = baseMaze (maze )
170
+ rowcol = [16 , 7 ] # Starting position adjusted to the middle of the bottom row
171
+ addingMoves (guylist [_ ])
172
+ scorelist [_ ] += moving (guylist [_ ], rowcol , underScorelist [_ ], amount , maze )
173
+
174
+ if not gameOn :
175
+ break
176
+
177
+ if not gameOn :
178
+ print (amount )
179
+ break
180
+
181
+ max_index = scorelist .index (max (scorelist ))
182
+
183
+ if max (scorelist ) < 0 :
184
+ generaltrack -= 2
185
+ for i in range (8 ):
186
+ guylist [i ].pop ()
187
+ guylist [i ].pop ()
188
+ else :
189
+ for i in range (len (guylist )):
190
+ if i != max_index :
191
+ for j in range (generaltrack ):
192
+ guylist [i ][j ] = guylist [max_index ][j ]
193
+
194
+ for i in range (int (len (guylist ) / 2 )):
195
+ for j in range (generaltrack ):
196
+ temp = random .randint (1 , int (generaltrack / 2 ))
197
+ if temp == 1 :
198
+ guylist [i ][j ] = random .randint (1 , 4 )
199
+
200
+ for i in range (len (scorelist )):
201
+ if i != max_index :
202
+ scorelist [i ] = scorelist [max_index ]
203
+ tEnd = time .time () - tStart
204
+ print (tEnd )
0 commit comments