-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstruction.py
165 lines (143 loc) · 6.65 KB
/
obstruction.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
import random #imports the random module
import os #imports the os module
NUM_PLAYERS=2 #constant for number of players
NUM_ROWS=6 #constant for number of rows in board
NUM_COLS=6 #constant for number of columns in board
CHECKERS=["O","X"] #list for CHECKERS
GREY="#"
board=[]
top_row=[]
checked_cells=0
x=1
if 0<NUM_ROWS<10 and 0<NUM_COLS<10 and NUM_ROWS*NUM_COLS>=16: #condition for board dimensions restriction
for i in range(NUM_ROWS): #creating the board
row_list=[]
for j in range(NUM_COLS):
row_list.append(" ")
board.append(row_list)
print(" ", end=" ")
for j in range(NUM_COLS):
print (chr(65+j), end=" ")
print()
print(" "+"+---"*NUM_COLS+"+")
for i in range(NUM_ROWS): #printing the board
if i<10:
print (i, end=" ")
else:
print (i, end="")
for j in range(NUM_COLS):
print ("|"+board[i][j],end=" ")
print("|")
print(" "+"+---"*NUM_COLS+"+")
first_turn=random.randint(0, NUM_PLAYERS-1) #choosing the first player randomly
while checked_cells<NUM_ROWS*NUM_COLS:
if first_turn==0:
current_player=input("Player O wants to place O on: ")
d=0
elif first_turn==1:
current_player=input("Player X wants to place X on: ")
d=1
while x!= 0: #initial value of x is 1
#checking multiple conditions for input
if current_player!="" and current_player[1:].isdigit() and 65<=ord(current_player[0].upper())<NUM_COLS+65 and 0<=int(current_player[1:])<NUM_ROWS and board[int(current_player[1:])][ord(current_player[0].upper())-65]==" ":
first_string=ord(current_player[0].upper())
second_string=int(current_player[1])
x=0 #while loop ends
else:
print("Invalid input."+\
"Try again with a valid input(unchecked cells within the board range) and valid form(letter followed by number) .")
current_player=input("Player "+CHECKERS[d]+ " wants to place "+CHECKERS[d]+ " on: ")
if second_string==0 and first_string==65: #for topmost left corner
board[second_string][first_string-65]=CHECKERS[d]
if second_string+1<NUM_ROWS:
board[second_string+1][first_string-65]=GREY
if first_string-64<NUM_COLS:
board[second_string][first_string-64]=GREY
if second_string+1<NUM_ROWS and first_string-64<NUM_COLS:
board[second_string+1][first_string-64]=GREY
elif second_string==0 and first_string-65==NUM_COLS-1: #for topmost right corner
board[second_string][first_string-65]=CHECKERS[d]
board[second_string][first_string-66]=GREY
if second_string+1<NUM_ROWS:
board[second_string+1][first_string-65]=GREY
board[second_string+1][first_string-66]=GREY
elif second_string==(NUM_ROWS-1) and first_string==65: #for lowermost left corner
board[second_string][first_string-65]=CHECKERS[d]
board[second_string-1][first_string-65]=GREY
if first_string-64<NUM_COLS:
board[second_string][first_string-64]=GREY
board[second_string-1][first_string-64]=GREY
elif second_string==(NUM_ROWS-1) and first_string-65==NUM_COLS-1: #for lowermost right corner
board[second_string][first_string-65]=CHECKERS[d]
board[second_string][first_string-66]=GREY
board[second_string-1][first_string-65]=GREY
board[second_string-1][first_string-66]=GREY
elif second_string==0: #for first row cells in the board
board[second_string][first_string-65]=CHECKERS[d]
board[second_string][first_string-66]=GREY
if first_string-64<NUM_COLS:
board[second_string][first_string-64]=GREY
if second_string+1<NUM_ROWS:
board[second_string+1][first_string-65]=GREY
board[second_string+1][first_string-66]=GREY
board[second_string+1][first_string-64]=GREY
elif second_string==(NUM_ROWS-1): #for last row cells in the board
board[second_string][first_string-65]=CHECKERS[d]
board[second_string-1][first_string-65]=GREY
board[second_string-1][first_string-66]=GREY
board[second_string-1][first_string-64]=GREY
board[second_string][first_string-64]=GREY
board[second_string][first_string-66]=GREY
elif first_string==65: #for first column cells in the board
board[second_string][first_string-65]=CHECKERS[d]
board[second_string-1][first_string-65]=GREY
board[second_string+1][first_string-65]=GREY
if first_string-64<NUM_COLS:
board[second_string][first_string-64]=GREY
board[second_string-1][first_string-64]=GREY
board[second_string+1][first_string-64]=GREY
elif first_string-65==NUM_COLS-1: #for last column cells in the board
board[second_string][first_string-65]=CHECKERS[d]
board[second_string-1][first_string-65]=GREY
board[second_string-1][first_string-66]=GREY
board[second_string][first_string-66]=GREY
if second_string+1<NUM_ROWS:
board[second_string+1][first_string-65]=GREY
board[second_string+1][first_string-66]=GREY
else: #for every other cells in the board except above
board[second_string][first_string-65]=CHECKERS[d]
board[second_string-1][first_string-65]=GREY
board[second_string+1][first_string-65]=GREY
board[second_string-1][first_string-66]=GREY
board[second_string][first_string-66]=GREY
board[second_string+1][first_string-66]=GREY
board[second_string-1][first_string-64]=GREY
board[second_string+1][first_string-64]=GREY
board[second_string][first_string-64]=GREY
os.system("clear")
print(" ", end=" ")
for j in range(NUM_COLS): #printing the topmost row of alphabets
print (chr(65+j), end=" ")
print()
print(" "+"+---"*NUM_COLS+"+")
for i in range(NUM_ROWS): #printing the updated board after the checker is placed
print (i, end=" ")
for j in range(NUM_COLS):
print ("|"+board[i][j],end=" ")
print("|")
print(" "+"+---"*NUM_COLS+"+")
for i in range(NUM_ROWS):
for j in range(NUM_COLS):
if board[i][j]!= " ": #checking how many cells are not empty
checked_cells=checked_cells+1
if checked_cells==NUM_ROWS*NUM_COLS: #declaraing the winner if all cells are checked
print ("Player "+CHECKERS[d]+ " wins the game!!!")
else:
checked_cells=0
x=1
if d==0: #alternating turns for the users
first_turn=1
else:
first_turn=0
else:
print("Please make sure the number of cells is between 4X4 and 9X9. Sorry for inconvenience!")