-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.py
328 lines (190 loc) · 7.53 KB
/
connect4.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
class Board:
def __init__(self, height, width):
'''constructs a new board object by initializing the following three attributes'''
self.height = height
self.width = width
self.slots = [[' '] * width for row in range(self.height)]
#2
def __str__(self):
""" Returns a string representation for a Board object.
"""
s = '' # begin with an empty string
for row in range(self.height):
s += '|' # one vertical bar at the start of the row
for col in range(self.width):
s += self.slots[row][col] + '|'
s += '\n' # newline at the end of the row
for row in range(1):
for col in range(self.width):
s += '--'
s+= '-'
s+= '\n'
for row in range(1):
for col in range(self.width):
s += ' ' + str(col)
s+= '\n'
# Add code here for the hyphens at the bottom of the board
# and the numbers underneath it.
return s
#3
def __repr__(self):
""" returns string representing the called Board object """
return str(self) # calls our __str__
#4
def add_checker(self, checker, col):
""" adds a checker x or o to specified column on the board
"""
assert(checker == 'X' or checker == 'O')
assert(col >= 0 and col < self.width)
if self.slots[0][col] == ' ':
for i in range(self.height):
if self.slots[i][col] =='X' or self.slots[i][col]== 'O':
self.slots[i-1][col] = checker
break
if self.slots[-1][col] == ' ':
self.slots[-1][col] = checker
#5
def clear(self):
'''clears the board'''
for r in range(self.height):
for c in range(self.width):
self.slots[r][c] = ' '
#6
def add_checkers(self, colnums):
""" takes in a string of column numbers and places alternating
checkers in those columns of the called Board object,
starting with 'X'.
"""
checker = 'X' # start by playing 'X'
for col_str in colnums:
col = int(col_str)
if 0 <= col < self.width:
self.add_checker(checker, col)
# switch to the other checker
if checker == 'X':
checker = 'O'
else:
checker = 'X'
#7
def can_add_to(self, col):
'''returns true if it is valid to place a checker in column col on calling board object
or it will return false'''
if col not in range(self.width):
return False
elif self.slots[0][col] != ' ':
return False
else:
return True
#8
def is_full(self):
'''returns True if the called Board object is completely full of checkers,
and returns False otherwise'''
for c in range(self.width):
if self.can_add_to(c)==True:
return False
return True
#9
def remove_checker(self, col):
'''removers top checker from column col of called Board object'''
for i in range(self.height):
if self.slots[i][col] =='X' or self.slots[i][col]== 'O':
self.slots[i][col]=' '
break
#10
#helper function
def is_horizontal_win(self, checker):
""" Checks for a horizontal win for the specified checker."""
for row in range(self.height):
for col in range(self.width - 3):
# Check if the next four columns in this row
# contain the specified checker.
if self.slots[row][col] == checker and \
self.slots[row][col + 1] == checker and \
self.slots[row][col + 2] == checker and \
self.slots[row][col + 3] == checker:
return True
# if we make it here, there were no horizontal wins
return False
def is_vertical_win(self,checker):
'''Checks for a vertical win for the specified checker.'''
for row in range(self.height-3):
for col in range(self.width):
if self.slots[row][col] == checker and \
self.slots[row+1][col] == checker and \
self.slots[row+2][col] == checker and \
self.slots[row+3][col] == checker:
return True
return False
def is_up_diagonal_win(self, checker):
'''Accepts a parameter checker that is either X or O and returns True if there
there are four consecutive slots containing checkers on the board going up diagonally'''
for row in range(self.height):
for col in range(self.width-3):
if self.slots[row][col] == checker and \
self.slots[row-1][col+1] == checker and \
self.slots[row-2][col+2] == checker and \
self.slots[row-3][col+3] == checker:
return True
return False
def is_down_diagonal_win(self, checker):
'''Accepts a parameter checker that is either X or O and returns True if there
there are four consecutive slots containing checkers on the board going down diagonally'''
for row in range(self.height-3):
for col in range(self.width-3):
if self.slots[row][col] == checker and \
self.slots[row+1][col+1] == checker and \
self.slots[row+2][col+2] == checker and \
self.slots[row+3][col+3] == checker:
return True
return False
def is_win_for(self,checker):
'''accepts a parameter checker that is either x or o and returns True
if there are four consecutive slots containing checker on the board'''
assert(checker == 'X' or checker == 'O')
if self.is_horizontal_win(checker)==True:
return True
elif self.is_vertical_win(checker)==True:
return True
elif self.is_up_diagonal_win(checker)==True:
return True
elif self.is_down_diagonal_win(checker)==True:
return True
else:
return False
# Write your Player class below.
#1
class Player:
def __init__(self,checker):
'''constructs a new Player object by initializing the following attributes'''
assert(checker == 'X' or checker == 'O')
self.checker= checker
self.num_moves=0
#2
def __str__(self):
'''returns a string representing a Player object'''
s='Player'+' '+(self.checker)
return s
#3
def __repr__(self):
'''returns a string representing a Player object'''
return str(self)
#4
def opponent_checker(self):
'''returns one_character string representing the checker'''
if self.checker=='X':
return 'O'
else:
return 'X'
#5
def next_move(self,board):
'''accepts a Board object as a parameter and returns the column
where the player wants to make the next move'''
self.num_moves += 1
while True:
col = int(input('Enter a column: '))
if board.can_add_to(col):
return col
else:
print('Try again!')
# if valid column index, return that integer
# else, print 'Try again!' and keep looping