-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze runner
161 lines (122 loc) · 5.31 KB
/
maze runner
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
//
//
//
#Name: Justin Bret Chu
#Email: JUSTIN.CHU73@myhunter.cuny.edu
## This function determines whether or not a given move is legal
def isPositionAvailable(row, col, maze): ## row and col of the place the User wishies to move to
if row < 0 or col < 0: ## Checks if User is attempting to go out of bounds
return False
if maze[row][col] == '1': ## Checks if User is attempting to go thru walls
return False
elif maze[row][col] == '0': ## Checks if User is attempting to make legal move
return True
else:
return True
## Given a string representing a maze, this function will place it in a 2d array
def generateMaze(textMaze):
textMaze = textMaze.split('\n')
maze = []
for row in textMaze:
rowOfMaze = row.split(' ')
maze.append(rowOfMaze)
return maze
## Prints out a visual representation of the maze, and where the current user is located
def printMaze(maze, currRow, currCol): ## currRow and currCol represent the Users current location
rowCount = -1
for row in maze:
rowCount += 1
for stuff in range(3):
colCount = -1
for column in row:
colCount += 1
## Location of User
if rowCount == currRow and colCount == currCol:
for i in range(3):
print('v', end = '')
## Location of wall
elif column == '1':
for i in range(3):
print('*', end = '')
## Location of hall
elif column == '0':
for i in range(3):
print(' ', end = '')
## Location of start
elif column == 'S':
for i in range(3):
print('S', end = '')
## Location of end
elif column == 'X':
for i in range(3):
print('X', end = '')
print()
## This function asks for a string of commands and executes them
def playGame(maze, row, col):## row and col given here is the starting position
commands = input("Enter a string of commands: ")
## Loops through the given set of commands
for command in commands:
if command == "R":
col = col + 1
results = isPositionAvailable(row, col, maze)
print(results)
if not results:
return False
if command == "L":
col = col - 1
results = isPositionAvailable(row, col, maze)
if not results:
return False
if command == "U":
row = row - 1
results = isPositionAvailable(row, col, maze)
if not results:
return False
if command == "D":
row = row + 1
results = isPositionAvailable(row, col, maze)
if not results:
return False
if (maze[row][col]) == "X":
return True
## If the current command is 'R', then move to the Right by
## increasing col by 1.
## Utilize isPositionAvailable() to check if the User can move there.
## If the position is not available, end the game by returning False.
## If the current command is 'L', then move to the Left by
## decreasing col by 1.
## Utilize isPositionAvailable() to check if the User can move there.
## If the position is not available, end the game by returning False.
## If the current command is 'U', then move Up by
## decreasing row by 1.
## Utilize isPositionAvailable() to check if the User can move there.
## If the position is not available, end the game by returning False.
## If the current command is 'D', then move Down by
## increasing row by 1.
## Utilize isPositionAvailable() to check if the User can move there.
## If the position is not available, end the game by returning False.
## If the current location (maze[row][col]) contains an 'X'
## return True indicating that the User has won the game
## If after all the commands have been executed, the game has not finished
## return false indicating that they did not reach the end
return False
def main():
print("Hello Everyone! Welcome to Maze Runner!\n\
To succeed in this Computer Science class, you must go through some trials and tribulations...\n\
So tonight, you must pass through our maze!\n\
The game is not over until you have found your path...\n\
And if you try and fail, you must restart from the beginning and try again!\n\
Good luck!")
## Reads in a text file containing a maze
textMazeFile = input("Enter name of text file containing a maze: ")
textMaze = open(textMazeFile, "r")
maze = generateMaze(textMaze.read())
## The game continues until the User is victorious
result = False
while not result:
print("\nHere is a picture of the maze, provided by some random person down the street ->")
printMaze(maze, 1, 0)
result = playGame(maze, 1, 0)
print("Congrats on escaping the maze! Please do join us again :)")
if __name__ == '__main__':
main()