-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework3.py
More file actions
338 lines (294 loc) · 12.3 KB
/
homework3.py
File metadata and controls
338 lines (294 loc) · 12.3 KB
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
# Christopher Wang
# christwang
# 110969745
# CSE 101
# Homework #3
# In this part of the file it is very important that you write code inside
# the functions only. If you write code in between the functions, then the
# grading system will not be able to read your code or grade your work!
# Part I
def find_starman(board):
result = []
for row in range (0, 5):
for column in range (0, 5):
if board[row][column] == '*':
result.append(row+1)
result.append(column+1)
if (row == 4 and column == 4) and (board[4][4] != '*') and len(result) == 0:
result.append(-1)
result.append(-1)
return result
# Part II
# // board[row-1][column] == '*'
# // board[row][column] == '.'
def move_up(board):
result = []
# FOR ALL THE ROWS IN THE RANGE 0 1 2 3 4 = 0-5
#(row) = 1 2 3 4 5 [row] 0 1 2 3 4
for row in range (0,5):
for column in range (0,5):
if board[row][column] == '*':
while (len(result) == 0):
if row == 0:
result.append(-1)
result.append(-1)
elif board[row-1][column] == 'O':
board[row-1][column] = 'X'
board[row][column] = '.'
result.append(row)
result.append(column+1)
elif board[row-1][column] != '.' and board[row-1][column] !='F':
result.append(-1)
result.append(-1)
else:
board[row - 1][column] = '*'
board[row][column] = '.'
result.append(row)
result.append(column+1)
return result
# Part III
def move_down(board):
# 1195438101868657375/student_submission.py", line 64, in move_down
# elif board[row + 1][column] == 'O' and len(result) == 0:
# IndexError: list index out of range
result = []
for row in range (0, 5):
for column in range (0, 5):
if board[row][column] == '*':
while (len(result) == 0):
if row == 4: # moves out of board
result.append(-1)
result.append(-1)
elif board[row + 1][column] == 'O':
board[row + 1][column] = 'X'
board[row][column] = '.'
result.append(row + 2)
result.append(column + 1)
elif board[row+1][column] == 'W': # wall
result.append(-1)
result.append(-1)
else:# 'F' or '.'
board[row + 1][column] = '*'
board[row][column] = '.'
result.append(row+2)
result.append(column+1)
return result
# Part IV
def move_left(board):
result=[]
for row in range (0,5):
for column in range (0,5):
if board[row][column] == '*':
while (len(result) == 0):
if column == 0:
result.append(-1)
result.append(-1)
elif board[row][column-1] == 'O':
board[row][column-1] = 'X'
board[row][column] = '.'
result.append(row+1)
result.append(column)
elif board[row][column-1] != '.' and board[row][column-1] !='F':
result.append(-1)
result.append(-1)
else:
board[row ][column-1] = '*'
board[row][column] = '.'
result.append(row+1)
result.append(column)
return result
# Part V
def move_right(board):
result = []
for row in range(0, 5):
for column in range(0, 5):
if board[row][column] == '*':
while (len(result) == 0):
if column == 4:
result.append(-1)
result.append(-1)
elif board[row][column + 1] == 'O':
board[row][column + 1] = 'X'
board[row][column] = '.'
result.append(row + 1)
result.append(column + 2)
elif board[row][column + 1] != '.' and board[row][column + 1] != 'F':
result.append(-1)
result.append(-1)
else:
board[row][column + 1] = '*'
board[row][column] = '.'
result.append(row + 1)
result.append(column + 2)
return result
# Part VI
def move(board, movement):
# recursion, when you call another function from a function and it returns back to original function
result=[]
if movement == 'U':
result = move_up(board) # result = the returned value from the function move_up
if movement == 'D':
result = move_down(board)
if movement == 'R':
result = move_right(board)
if movement == 'L':
result = move_left(board)
return result
def print_board(board):
print("The updated game board:")
row_num = 1
print(" 1 2 3 4 5")
print(" ----------------")
for row in board:
print(str(row_num) + '| ' + ' '.join(row) + ' |')
row_num += 1
print(" ----------------" + "\n")
# Below you will see an if-statement and a few tests. It is REALLY important
# that you not delete this if-statement or the tests inside. You may, however,
# add more tests to the code below. You can format them however you like.
# Upload your homework3.py file to CodeLoad to see how it matches up against other
# test cases!
if __name__ == '__main__':
# Testing Part I
board1 = [['*', '.', '.', '.', '.'],
['.', 'W', '.', '.', 'F'],
['.', '.', '.', 'W', '.'],
['.', '.', '.', 'W', '.'],
['F', '.', 'O', '.', '.']]
board2 = [['.', 'W', 'W', 'W', 'W'],
['F', 'W', '.', '.', 'F'],
['.', 'O', '.', 'W', '.'],
['.', '.', '*', 'W', '.'],
['O', '.', 'O', '.', '.']]
board3 = [['O', '.', '.', '.', '.'],
['.', '*', '.', '.', 'F'],
['.', '.', '.', 'O', '.'],
['.', '.', '.', 'O', '.'],
['F', '.', '.', '.', '.']]
print("##### Part I ##### ")
print("Testing find_starman() with board = board1: " + str(find_starman(board1)))
print("Testing find_starman() with board = board2: " + str(find_starman(board2)))
print("Testing find_starman() with board = board3: " + str(find_starman(board3)))
print()
# Testing Part II
board1 = [['.', '.', '.', '.', 'W'],
['.', '.', '.', '.', '.'],
['.', 'F', '*', 'F', '.'],
['.', '.', 'O', '.', 'W'],
['.', '.', 'O', '.', '.']]
board2 = [['.', '*', '.', 'F', '.'],
['.', '.', 'W', 'F', '.'],
['.', '.', 'F', '.', '.'],
['W', 'W', '.', 'O', '.'],
['.', '.', '.', '.', '.']]
board3 = [['.', 'O', '.', '.', '.'],
['.', '.', 'F', '.', '.'],
['W', '.', 'F', '.', '.'],
['*', 'O', '.', '.', '.'],
['.', '.', '.', '.', 'F']]
print("##### Part II ##### ")
print("Testing move_up() with board = board1: " + str(move_up(board1)))
print_board(board1)
print("Testing move_up() with board = board2: " + str(move_up(board2)))
print_board(board2)
print("Testing move_up() with board = board3: " + str(move_up(board3)))
print_board(board3)
# Testing Part III
board1 = [['.', '.', '.', '.', 'W'],
['.', '*', '.', '.', '.'],
['.', 'F', 'O', 'F', '.'],
['.', '.', 'O', '.', 'W'],
['.', '.', 'O', '.', '.']]
board2 = [['.', '.', '.', 'F', '.'],
['.', '.', 'W', 'F', '.'],
['.', '*', 'F', '.', '.'],
['W', 'W', '.', 'O', '.'],
['.', '.', '.', '.', '.']]
board3 = [['.', 'O', '.', '.', '.'],
['.', '.', 'F', '.', '.'],
['W', '*', 'F', '.', '.'],
['W', 'O', '.', '.', '.'],
['.', '.', '.', '.', 'F']]
print("##### Part III ##### ")
print("Testing move_down() with board = board1: " + str(move_down(board1)))
print_board(board1)
print("Testing move_down() with board = board2: " + str(move_down(board2)))
print_board(board2)
print("Testing move_down() with board = board3: " + str(move_down(board3)))
print_board(board3)
# Testing Part IV
board1 = [['.', '.', '.', '.', 'W'],
['.', '.', '.', '.', '.'],
['.', 'F', 'O', 'F', '.'],
['.', '.', 'O', '.', 'W'],
['*', '.', 'O', '.', '.']]
board2 = [['.', 'F', '.', 'F', '.'],
['.', '.', 'O', '.', '.'],
['.', '.', 'F', '.', '.'],
['W', 'W', '.', 'O', '*'],
['.', '.', '.', '.', '.']]
board3 = [['.', 'O', '.', '.', '.'],
['.', '.', 'F', '.', '.'],
['W', '*', 'F', '.', '.'],
['W', 'O', '.', '.', '.'],
['.', '.', '.', '.', 'F']]
print("##### Part IV ##### ")
print("Testing move_left() with board = board1: " + str(move_left(board1)))
print_board(board1)
print("Testing move_left() with board = board2: " + str(move_left(board2)))
print_board(board2)
print("Testing move_left() with board = board3: " + str(move_left(board3)))
print_board(board3)
# Testing Part V
board1 = [['F', 'F', 'F', '.', 'W'],
['.', '.', '.', '.', '.'],
['.', 'F', 'O', 'F', '.'],
['.', '*', '.', '.', '.'],
['.', 'O', 'O', '.', '.']]
board2 = [['F', 'W', 'F', '.', 'W'],
['.', '.', '.', '.', '.'],
['.', '*', 'O', 'F', '.'],
['W', '.', '.', '.', '.'],
['.', 'W', 'O', '.', '.']]
board3 = [['.', 'O', '.', '.', '.'],
['.', '.', 'F', '.', '.'],
['W', '*', 'F', '.', '.'],
['W', 'O', '.', '.', '.'],
['.', '.', '.', '.', 'F']]
print("##### Part V ##### ")
print("Testing move_right() with board = board1: " + str(move_right(board1)))
print_board(board1)
print("Testing move_right() with board = board2: " + str(move_right(board2)))
print_board(board2)
print("Testing move_right() with board = board3: " + str(move_right(board3)))
print_board(board3)
# Testing Part VI
board1 = [['.', 'W', 'W', '.', 'W'],
['F', 'F', '.', '.', 'F'],
['.', 'O', '.', 'W', '*'],
['.', '.', '.', 'W', '.'],
['O', '.', 'O', '.', '.']]
board2 = [['.', 'F', '.', 'F', '.'],
['.', '.', 'O', '.', '.'],
['.', '.', 'O', '*', '.'],
['W', 'W', '.', 'O', '.'],
['.', '.', '.', 'W', '.']]
board3 = [['F', 'W', 'F', '.', 'W'],
['.', '.', '.', '.', '.'],
['W', '*', 'O', 'F', '.'],
['W', '.', '.', '.', '.'],
['.', 'W', 'O', '.', '.']]
board4 = [['.', 'O', '.', '.', '.'],
['.', 'F', 'F', '.', '.'],
['W', '*', 'F', 'F', '.'],
['F', 'O', '.', '.', '.'],
['.', '.', '.', '.', 'F']]
print("##### Part VI ##### ")
print("Testing move() with board = board1, movement = 'U': " + str(move(board1, 'U')))
print_board(board1)
print("Testing move() with board = board2, movement = 'D': " + str(move(board2, 'D')))
print_board(board2)
print("Testing move() with board = board3, movement = 'L': " + str(move(board3, 'L')))
print_board(board3)
print("Testing move() with board = board4, movement = 'R': " + str(move(board4, 'R')))
print_board(board4)