-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobstruction.py
More file actions
389 lines (270 loc) · 9.77 KB
/
obstruction.py
File metadata and controls
389 lines (270 loc) · 9.77 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
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
# Emmanuel A Oppong
#29th September, 2022
import random, os, time
NUM_PLAYERS=2
# use random module to generate 0 and 1 at random
first_turn = random.randint(0, NUM_PLAYERS-1)
# definining players as global constants
players=("O","X")
# setting player1 according to the 0 or 1 generated randomly
if first_turn ==1:
player1=players[1]
player2=players[0]
else:
player1=players[0]
player2=players[1]
# definining number of rows and number of columns as global constants
ROWS=6
COLS=6
# definining blocker type as global constant
BLOCKER="#"
# definining letters as global constant of a tuple
letters = ('A','B','C','D','E','F','G','H','I','J')
# creating the main board as a list
board=[]
for row in range(ROWS):
row_list=[]
for col in range(COLS):
row_list.append(" ")
board.append(row_list)
# main gameplay
def main():
num_moves=0
print_board()
print("Moves are colrow eg. A5 :")
exit_game = False
#play game only when no exit flag has been called
while not exit_game:
num_moves+=1
# random player who starts is printed
if num_moves<2:
print (f"{player1} will go first. ")
player_ch= player1 if num_moves%2 >0 else player2
putChecker(player_ch)
update_board()
#executes only if checkwinner function returns true and breaks the gameplay
if checkWinner():
if num_moves %2 >0:
print(player1, "wins")
print(player2, "loses")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
break
else:print("Sorry incorrect input type. Goodbye! ")
else:
print(player2, "wins")
print(player1, "loses")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y":
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]= " "
main()
elif str =='n' or str=='N' :
print(' No you dont,Goodbye! ')
break
else:print("Sorry incorrect input type. Goodbye! ")
break
# a function that updates and prints the new board after a marker is placed
def update_board():
#sleep,clear the screen and print recently updated board
time.sleep(1.5)
os.system("clear")
print_board()
# function that prints 2D board
def print_board():
#prints column header letters
for i in letters[:COLS]:
if(i == 'A'):
print(' '+ i , end=' ')
else:
print(' '+ i , end=' ')
#print row numbers by side of table
print("\n +"+"---+"*COLS)
for row in range(ROWS):
print(str(row)+" | ", end="")
for col in range(COLS):
print(board[row][col]+" | ", end="")
print("\n +" + "---+" * COLS)
# function that loops through the board to check if all squares are occupied and returns true if so.
def checkWinner():
for row in range(ROWS):
for col in range(COLS):
if board[row][col] == " ":
return False
return True
# function that uses checkInput function before placing a checker X or O on the board and blocks areas with selected BLOCKER eg. "#"
def putChecker(player_ch):
#constantly accept input for the two players
while True:
prompt='Enter move for '+ player_ch + ': '
str = input(prompt)
# call check inputChecker function to check for correct
# ... input type else return false and print re-enter
s = checkInput(str)
if s == True:
c = (ord(str[0]) - 65)
r = int(str[1])
#check that we dont go out of the board
if r <0 or r>=ROWS or c<0 or c>=COLS:
print("Out of range. Re-enter. ")
elif board[r][c] != " " :
print("Occupied square. Re-enter. ")
else:
board[r][c] = player_ch
# conditions for placing defined BLOCKER in any area in the middle
# ...set of cells in your rectangular or
# ...square grid
if r+1<ROWS and r-1>=0 and c-1>=0 and c+1<COLS and (board[r][c]== "X" or board[r][c] == "O"):
if board[r-1][c-1] == " ":
board[r-1][c-1] = BLOCKER
if board[r-1][c] == " ":
board[r-1][c] = BLOCKER
if board[r-1][c+1] == " ":
board[r-1][c+1] = BLOCKER
if board[r][c-1] == " ":
board[r][c-1] = BLOCKER
if board[r][c+1] == " ":
board[r][c+1] = BLOCKER
if board[r+1][c-1] == " ":
board[r+1][c-1] = BLOCKER
if board[r+1][c] == " ":
board[r+1][c] = BLOCKER
if board[r+1][c+1] == " ":
board[r+1][c+1] = BLOCKER
# conditions for placing defined BLOCKER in any cell in the first row excluding
# ... its first and last cells
elif r==0 and c-1>=0 and c+1<COLS and (board[r][c]== "X" or board[r][c]== "O"):
if board[r][c+1] == " ":
board[r][c+1] = BLOCKER
if board[r][c-1] == " ":
board[r][c-1] = BLOCKER
if board[r+1][c-1] == " ":
board[r+1][c-1] = BLOCKER
if board[r+1][c] == " ":
board[r+1][c] = BLOCKER
if board[r+1][c+1] == " ":
board[r+1][c+1] = BLOCKER
# conditions for placing defined BLOCKER in any cell in the last row excluding
# ... its first and last cells
elif r==ROWS-1 and c-1>=0 and c+1<COLS and (board[r][c]== "X" or board[r][c]== "O"):
if board[ROWS-1][c+1] == " ":
board[ROWS-1][c+1] = BLOCKER
if board[ROWS-1][c-1] == " ":
board[ROWS-1][c-1] = BLOCKER
if board[ROWS-2][c-1] == " ":
board[ROWS-2][c-1] = BLOCKER
if board[ROWS-2][c] == " ":
board[ROWS-2][c] = BLOCKER
if board[ROWS-2][c+1] == " ":
board[ROWS-2][c+1] = BLOCKER
# conditions for placing defined BLOCKER in any cell in the first column excluding
# ... its first and last cells
elif c==0 and r+1<ROWS and r-1>=0 and (board[r][c]== "X" or board[r][c]== "O"):
if board[r-1][c] == " ":
board[r-1][c] = BLOCKER
if board[r+1][c] == " ":
board[r+1][c] = BLOCKER
if board[r-1][c+1] == " ":
board[r-1][c+1] = BLOCKER
if board[r][c+1] == " ":
board[r][c+1] = BLOCKER
if board[r+1][c+1] == " ":
board[r+1][c+1] = BLOCKER
# conditions for placing defined BLOCKER in any cell in the last column excluding
# ... its first and last cells
elif c==COLS-1 and r+1<ROWS and r-1>=0 and (board[r][c]== "X" or board[r][c]== "O"):
if board[r-1][COLS-1] == " ":
board[r-1][COLS-1] = BLOCKER
if board[r+1][COLS-1] == " ":
board[r+1][COLS-1] = BLOCKER
if board[r-1][COLS-2] == " ":
board[r-1][COLS-2] = BLOCKER
if board[r][COLS-2] == " ":
board[r][COLS-2] = BLOCKER
if board[r+1][COLS-2] == " ":
board[r+1][COLS-2] = BLOCKER
# conditions for placing defined BLOCKER in any of the four cells
# ... at the corner of your rectangular or square grid
else:
# conditions for placing defined BLOCKER in first row left corner cell
if r==0 and c==0 and (board[r][c]== "X" or board[r][c]== "O"):
if board[r][c+1] == " ":
board[r][c+1] = BLOCKER
if board[r+1][c] == " ":
board[r+1][c] = BLOCKER
if board[r+1][c+1] == " ":
board[r+1][c+1] = BLOCKER
# conditions for placing defined BLOCKER in first row right/last corner cell
if r==0 and c==COLS-1 and (board[r][c]== "X" or board[r][c]== "O"):
if board[r][COLS-2] == " ":
board[r][COLS-2] = BLOCKER
if board[r+1][COLS-1] == " ":
board[r+1][COLS-1] = BLOCKER
if board[r+1][COLS-2] == " ":
board[r+1][COLS-2] = BLOCKER
# conditions for placing defined BLOCKER in last row left corner cell
if r==ROWS-1 and c==0 and (board[r][c]== "X" or board[r][c]== "O"):
if board[ROWS-2][c] == " ":
board[ROWS-2][c] = BLOCKER
if board[ROWS-2][c+1] == " ":
board[ROWS-2][c+1] = BLOCKER
if board[ROWS-1][c+1] == " ":
board[ROWS-1][c+1] = BLOCKER
# conditions for placing defined BLOCKER in last row right corner cell
if r==ROWS-1 and c==COLS-1 and (board[r][c]== "X" or board[r][c]== "O"):
if board[ROWS-1][COLS-2] == " ":
board[ROWS-1][COLS-2] = BLOCKER
if board[ROWS-2][COLS-2] == " ":
board[ROWS-2][COLS-2] = BLOCKER
if board[ROWS-2][COLS-1] == " ":
board[ROWS-2][COLS-1] = BLOCKER
# print board and break from while loop
print_board()
break
# asks user to re-enter input as checkinput failed
else:
print("Incorrect input type. Re-enter. ")
# checks user input as correct position on the board
# .... and in the correct format of capital letter
# .... with a number and of correct length
def checkInput(str):
if len(str)<2 or len(str)>2:
return False
elif len(str)==2 and str[0] in ('A','B','C','D','E','F','G','H','I','J') and str[1] in ('0','1','2','3','4','5','6','7','8','9'):
return True
elif str[1] in ('A','B','C','D','E','F','G','H','I','J'):
return False
elif len(str) < 2 or len(str)>2:
return False
else:
return False
# function that checks to see if user wants to play again
def playAgain(str):
if len(str)<1 or len(str)>1:
return False
elif len(str)==1 and str[0] in ('Y','N','y','n'):
return True
elif len(str)==1 and str[0] not in ('Y','N','y','n'):
return False
else:
return False
#runs the main gameplay function for the game.
if __name__ == "__main__":
main()