-
Notifications
You must be signed in to change notification settings - Fork 11
/
game.py
377 lines (324 loc) · 11.3 KB
/
game.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
import time
import csv
import re
from termcolor import colored
maze = []
menuList = ["Exit",
"Read and load maze from file",
"View maze",
"Play maze game",
"Configure current maze"]
configMenuList = [
"Exit to Main Menu",
"Create Wall",
"Create Passageway",
"Create start point",
"Create end point",
]
bars = "="*35
completed = False
# Display Menu
def printMenu(menu):
if menu == True:
print(colored("\nMain Menu\n{}".format(bars), 'green'))
for menu in range(1, 5):
print(colored("[{}]{}{}".format(
menu, '\t', menuList[menu]), 'green'))
print('\n'*0)
for m in range(0, 1):
print(colored("[{}]{}{}".format(m, '\t', menuList[m]), 'green'))
return "Menu displayed."
else:
return "Menu display error."
def mainMenu(option, cut=""):
if option is 1:
print("Reading and loading maze from file...")
if cut == "cut":
return "Reading maze."
filename = str(
input(colored("Enter filename (w/o .csv):", 'red')))+'.csv'
try:
checkFile(filename)
except FileNotFoundError:
print(colored("File wasn't found.", 'magenta'))
return "Reading maze."
elif option is 2:
print("Viewing maze...")
printMaze()
return "Viewing maze."
elif option is 3:
print("Playing maze game...")
if cut == "cut":
return "Playing maze game."
if len(maze) == 0:
print(colored("The maze is empty. Please load one in from the menu.", 'blue'))
else:
playGame()
return "Playing maze game."
elif option is 4:
print("Configuring current maze...")
if cut == "cut":
return "Configuring current maze."
if len(maze) == 0:
print(colored("The maze is empty. Please load one in from the menu.", 'blue'))
else:
ConfigureMenu()
return "Configuring current maze."
elif option is 0:
print("Shutting down...")
print("Goodbye.")
return False
else:
print("You have entered an invalid option. Please re-enter your option.")
return "Invalid option selected."
def checkFile(filename):
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
maze.insert(0, row)
line_count += 1
elif line_count == 1:
maze.insert(1, row)
line_count += 1
elif line_count == 2:
maze.insert(2, row)
line_count += 1
elif line_count == 3:
maze.insert(3, row)
line_count += 1
elif line_count == 4:
maze.insert(4, row)
line_count += 1
elif line_count == 5:
maze.insert(5, row)
line_count += 1
elif line_count == 6:
maze.insert(6, row)
line_count += 1
elif line_count == 7:
maze.insert(7, row)
line_count += 1
print(f'Read {line_count} lines.')
return line_count
def printConfigMenu():
print("Configuration Menu\n{}".format(bars))
for menu in range(1, 5):
print("[{}]{}{}".format(menu, '\t', configMenuList[menu]))
print('\n'*0)
for m in range(0, 1):
print("[{}]{}{}".format(m, '\t', configMenuList[m]))
return(int(input(colored("Enter your option: ", 'red'))))
def ConfigureMenu():
printMaze()
configOption = printConfigMenu()
if configOption is 0:
pass
elif configOption is 1:
wallopt = str(input(colored(
"Enter Coords Row,Column to add/replace with wall, or B(configure menu) or M(main menu) to return there:")))
if wallopt is "B":
ConfigureMenu()
elif wallopt is "M":
pass
else:
coords = [int(i) for i in wallopt.split(',')]
print(coords)
maze[coords[0]][coords[1]] = 'X'
elif configOption is 2:
passopt = str(input(colored(
"Enter Coords Row,Column to add/replace with passageway, or B(configure menu) or M(main menu) to return there:", 'red')))
if passopt is "B":
ConfigureMenu()
elif passopt is "M":
pass
else:
coords = [int(i) for i in passopt.split(',')]
print(coords)
maze[coords[0]][coords[1]] = 'O'
elif configOption is 3:
startptopt = str(input(colored(
"Enter Coords Row,Column to add/replace with start point, or B(configure menu) or M(main menu) to return there:", 'red')))
if startptopt is "B":
ConfigureMenu()
elif startptopt is "M":
pass
else:
coords = [int(i) for i in startptopt.split(',')]
print(coords)
maze[coords[0]][coords[1]] = 'A'
elif configOption is 4:
endptopt = str(input(colored(
"Enter Coords Row,Column to add/replace with end point, or B(configure menu) or M(main menu) to return there:", 'red')))
if endptopt is "B":
ConfigureMenu()
elif endptopt is "M":
pass
else:
coords = [int(i) for i in endptopt.split(',')]
print(coords)
maze[coords[0]][coords[1]] = 'B'
# Returns start point of maze if it is found.
def searchStart():
result = [(i, el.index("A"))
for i, el in enumerate(maze) if "A" in el]
if result == []:
print("Maze doesn't have a start point!")
else:
print("Returning this value: %s", str(result[0]))
return str(result[0]) # Converts to string so it is easier to pull
def validMove(coords, move):
if move == 'up':
if maze[coords[0]-1][coords[1]] == 'O' or maze[coords[0]-1][coords[1]] == 'B':
return True
else:
return False
if move == 'left':
if maze[coords[0]][coords[1]-1] == 'O' or maze[coords[0]][coords[1]-1] == 'B':
return True
else:
return False
if move == 'down':
if maze[coords[0]+1][coords[1]] == 'O' or maze[coords[0]+1][coords[1]] == 'B':
return True
else:
return False
if move == 'right':
if maze[coords[0]][coords[1]+1] == 'O' or maze[coords[0]][coords[1]+1] == 'B':
return True
else:
return False
def validCompletetion(coords, move):
global completed
if move == 'up':
if maze[coords[0]-1][coords[1]] == 'B':
completed = True
return True
else:
return False
if move == 'left':
if maze[coords[0]][coords[1]-1] == 'B':
completed = True
return True
else:
return False
if move == 'down':
if maze[coords[0]+1][coords[1]] == 'B':
completed = True
return True
else:
return False
if move == 'right':
if maze[coords[0]][coords[1]+1] == 'B':
completed = True
return True
else:
return False
def movePlayer(direction):
direction.upper()
start_coords = searchStart()
start_coords_formatted = re.split('[()]', start_coords)[1]
if direction == 'W':
# W - [-1, same index]
coords = [int(i)
for i in start_coords_formatted.split(',')]
if validCompletetion(coords, 'up'):
print(colored("You've completed the maze. Good job!", 'magenta'))
if validMove(coords, 'up'):
maze[coords[0]][coords[1]] = 'O'
maze[coords[0]-1][coords[1]] = 'A'
printMaze()
print(colored("Successfully moved up", 'green'))
else:
printInvalidOpt()
elif direction == 'A':
# A - [same index, -1]
coords = [int(i)
for i in start_coords_formatted.split(',')]
if validCompletetion(coords, 'left'):
print(colored("You've completed the maze. Good job!", 'magenta'))
if validMove(coords, 'left'):
maze[coords[0]][coords[1]] = 'O'
maze[coords[0]][coords[1]-1] = 'A'
printMaze()
print(colored("Successfully moved left", 'green'))
else:
printInvalidOpt()
elif direction == 'S':
# S - [+1, same index]
coords = [int(i)
for i in start_coords_formatted.split(',')]
if validCompletetion(coords, 'down'):
print(colored("You've completed the maze. Good job!", 'magenta'))
if validMove(coords, 'down'):
maze[coords[0]][coords[1]] = 'O'
maze[coords[0]+1][coords[1]] = 'A'
printMaze()
print(colored("Successfully moved down", 'green'))
else:
printInvalidOpt()
elif direction == 'D':
# D - [same, +1]
coords = [int(i)
for i in start_coords_formatted.split(',')]
if validCompletetion(coords, 'right'):
print(colored("You've completed the maze. Good job!", 'magenta'))
if validMove(coords, 'right'):
maze[coords[0]][coords[1]] = 'O'
maze[coords[0]][coords[1]+1] = 'A'
printMaze()
print(colored("Successfully moved right", 'green'))
else:
printInvalidOpt()
else:
print(colored(
"That option is not a valid option. Please select another option", 'magenta'))
def printMaze():
if len(maze) == 0:
print(colored("The maze is empty. Please load one in from the menu.", 'blue'))
else:
print(colored(bars, 'blue'))
for row in maze:
for element in row:
if element is "X":
print(colored(element, 'cyan'), end='')
print(" ", end="")
if element is "A":
print(colored(element, 'red'), end='')
print(" ", end="")
if element is "B":
print(colored(element, 'magenta'), end='')
print(" ", end="")
if element is "O":
print(element, end='')
print(" ", end="")
print("\n")
# print(*maze, sep="\n")
print(colored(bars, 'blue'))
def printInvalidOpt():
print(colored(
"That option is not a valid move. Please select another move", 'magenta'))
def playGame():
global completed
printMaze()
while not completed:
direction = str(input(
colored("Enter move (WASD) or (M) to return. :", 'red'))).upper()
if direction == "M":
break
elif direction in ["W", "A", "S", "D"]:
movePlayer(direction)
else:
print("Invalid option.")
# Menu while loop
if __name__ == "__main__":
playing = True
while (playing != False):
try:
printMenu(True)
playing = mainMenu(
int(input(colored("Enter your option: ", 'red'))))
except ValueError:
print("You have entered a digit range of 0 - 4. Please re-enter your option.")
continue