-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbegin.py
518 lines (465 loc) · 14.7 KB
/
begin.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#
# Project: Kunstmatige intelligentie in Vier op een rij
#
# names: Dylan Ploeger & Christiaan de Jong
#
def in_a_row_n_east(char, row_start, col_start, data, length):
"""Starting from (row, col) of (row_start, col_start)
within the 2d list-of-lists data (array),
returns True if there are length amount of chars in a row
heading east and returns False otherwise.
"""
height = len(data)
width = len(data[0])
if row_start < 0 or row_start > height - 1:
return False
if col_start < 0 or col_start + (length-1) > width - 1:
return False
for i in range(length):
if data[row_start][col_start+i] != char:
return False
return True
def in_a_row_n_south(char, row_start, col_start, data, length):
"""Starting from (row, col) of (row_start, col_start)
within the 2d list-of-lists data (array),
returns True if there are length amount of chars in a row
heading south and returns False otherwise.
"""
height = len(data)
width = len(data[0])
if row_start < 0 or row_start + (length - 1) > height - 1:
return False
if col_start < 0 or col_start > width - 1:
return False
for i in range(length):
if data[row_start + i][col_start] != char:
return False
return True
def in_a_row_n_northeast(char, row_start, col_start, data, length):
"""Starting from (row, col) of (row_start, col_start)
within the 2d list-of-lists data (array),
returns True if there are length amount of chars in a row
heading northeast and returns False otherwise.
"""
height = len(data)
width = len(data[0])
if row_start - (length - 1) < 0 or row_start > height - 1:
return False
if col_start < 0 or col_start + (length - 1) > width - 1:
return False
for i in range(length):
if data[row_start - i][col_start + i] != char:
return False
return True
def in_a_row_n_southeast(char, row_start, col_start, data, length):
"""Starting from (row, col) of (row_start, col_start)
within the 2d list-of-lists data (array),
returns True if there are length amount of chars in a row
heading southeast and returns False otherwise.
"""
height = len(data)
width = len(data[0])
if row_start < 0 or row_start + (length - 1) > height - 1:
return False
if col_start < 0 or col_start + (length - 1) > width - 1:
return False
for i in range(length):
if data[row_start + i][col_start + i] != char:
return False
return True
class Board:
"""A data type representing a Connect-4 board
with an arbitrary number of rows and columns.
"""
def __init__(self, width, height):
"""Construct objects of type Board, with the given width and height."""
self.width = width
self.height = height
self.data = [[' ']*width for row in range(height)]
self.win_length = 4
def __repr__(self):
"""This method returns a string representation
for an object of type Board.
"""
s = ''
for row in range(0, self.height):
s += '|'
for col in range(0, self.width):
s += self.data[row][col] + '|'
s += '\n'
s += (2 * self.width + 1) * '-' + '\n'
for col in range(0, self.width):
s += ' ' + str(col % 10)
return s
def add_move(self, col, ox):
"""Adds a move to the Board.data, with given (int) column and (String x|o)stone character"""
for row in range(self.height - 1, -1, -1):
if self.data[row][col] == ' ':
self.data[row][col] = ox
break
def clear(self):
"""Clears the (self.data) board"""
self.data = [[' '] * self.width for row in range(self.height)]
def set_board(self, move_string):
"""Accepts a string of columns and places
alternating checkers in those columns,
starting with 'X'.
For example, call b.set_board('012345')
to see 'X's and 'O's alternate on the
bottom row, or b.set_board('000000') to
see them alternate in the left column.
move_string must be a string of one-digit integers.
"""
next_checker = 'X' # we starten door een 'X' te spelen
for col_char in move_string:
col = int(col_char)
if 0 <= col <= self.width:
self.add_move(col, next_checker)
if next_checker == 'X':
next_checker = 'O'
else:
next_checker = 'X'
def allow_move(self, col):
"""Checks if the move is valid."""
if col < 0 or col >= self.width:
return False
for row in range(self.height - 1, -1, -1):
if self.data[row][col] == ' ':
return True
return False
def is_full(self):
"""Checks if the Board full"""
for row in range(0, self.height):
for col in range(0, self.width):
if self.data[row][col] == ' ':
return False
return True
def delete_move(self, col):
"""Deletes last move in a column"""
for row in range(0, self.height):
if self.data[row][col] == 'X' or self.data[row][col] == 'O':
self.data[row][col] = ' '
break
def wins_for(self, char):
"""Checks char is a winner."""
for row in range(self.height):
for col in range(self.width):
if in_a_row_n_east(char, row, col, self.data, self.win_length):
return True
if in_a_row_n_south(char, row, col, self.data, self.win_length):
return True
if in_a_row_n_northeast(char, row, col, self.data, self.win_length):
return True
if in_a_row_n_southeast(char, row, col, self.data, self.win_length):
return True
return False
def host_game(self):
"""Host a game of connect 4"""
print('Connect 4')
print(self)
while True:
# Player X check input
while True:
column_for_x = int(input("Choose a column player X: "))
if self.allow_move(column_for_x):
break
# Player X set input
self.add_move(column_for_x, 'X')
print(self)
# Player X check win.
if self.wins_for('X'):
print('Player X wins!')
break
# Check if board is full
if self.is_full():
print("Nobody wins and the board is full!")
break
# Player O check input
while True:
column_for_o = int(input("Choose a column player O: "))
if self.allow_move(column_for_o):
break
# Player O set input
self.add_move(column_for_o, 'O')
print(self)
# Player O check win.
if self.wins_for('O'):
print('Player O wins!')
break
# Check if board is full
if self.is_full():
print("Nobody wins and the board is full!")
break
class Player:
"""An AI player for Connect Four."""
def __init__(self, char, tie_breaking_type, turns):
"""Construct a player for a given checker, tie-breaking type,
and turns."""
self.char = char
self.tie_breaking_type = tie_breaking_type
self.turns = turns
def __repr__(self):
"""Create a string represenation of the player."""
s = "Player: char = " + self.char + ", "
s += "tie_breaking_type = " + self.tie_breaking_type + ", "
s += "turns = " + str(self.turns)
return s
def opp_char(self):
"""Returns the opponents (String) character"""
if self.char == 'X':
return 'O'
elif self.char == 'O':
return 'X'
# else: error char needs to be X or O...
def score_board(self, board):
"""Gives a score to board,
100.0 points if self.char wins,
50.0 points if self.char did not win or lose,
0.0 if self.char loses.
"""
if board.wins_for(self.char):
return 100.0
elif board.wins_for(self.opp_char()):
return 0.0
else:
return 50.0
#
# Tests Class Board
#
board1 = Board(7, 6)
board2 = Board(10, 10)
board3 = Board(3, 3)
board4 = Board(5, 5)
board5 = Board(6, 7)
#
# Tests Board.__repr__()
#
assert board1.__repr__() == \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'---------------\n' \
' 0 1 2 3 4 5 6'
assert board2.__repr__() == \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'---------------------\n' \
' 0 1 2 3 4 5 6 7 8 9'
assert board3.__repr__() == \
'| | | |\n' \
'| | | |\n' \
'| | | |\n' \
'-------\n' \
' 0 1 2'
assert board4.__repr__() == \
'| | | | | |\n' \
'| | | | | |\n' \
'| | | | | |\n' \
'| | | | | |\n' \
'| | | | | |\n' \
'-----------\n' \
' 0 1 2 3 4'
assert board5.__repr__() == \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'-------------\n' \
' 0 1 2 3 4 5'
#
# Tests Board.add_move(col, ox)
#
board1.add_move(0, 'X')
board1.add_move(0, 'O')
board1.add_move(0, 'X')
board1.add_move(3, 'O')
board1.add_move(4, 'O')
board1.add_move(5, 'O')
board1.add_move(6, 'O')
assert board1.__repr__() == \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'|X| | | | | | |\n' \
'|O| | | | | | |\n' \
'|X| | |O|O|O|O|\n' \
'---------------\n' \
' 0 1 2 3 4 5 6'
board2.add_move(5, 'X')
board2.add_move(6, 'O')
board2.add_move(6, 'X')
board2.add_move(7, 'O')
board2.add_move(7, 'O')
board2.add_move(7, 'X')
board2.add_move(8, 'O')
board2.add_move(8, 'O')
board2.add_move(8, 'O')
board2.add_move(8, 'X')
board2.add_move(9, 'O')
board2.add_move(9, 'O')
board2.add_move(9, 'O')
board2.add_move(9, 'O')
board2.add_move(9, 'X')
assert board2.__repr__() == \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | |X|\n' \
'| | | | | | | | |X|O|\n' \
'| | | | | | | |X|O|O|\n' \
'| | | | | | |X|O|O|O|\n' \
'| | | | | |X|O|O|O|O|\n' \
'---------------------\n' \
' 0 1 2 3 4 5 6 7 8 9'
board3.add_move(1, 'X')
board3.add_move(1, 'O')
board3.add_move(1, 'X')
assert board3.__repr__() == \
'| |X| |\n' \
'| |O| |\n' \
'| |X| |\n' \
'-------\n' \
' 0 1 2'
#
# Tests
#
board1.clear()
assert board1.__repr__() == \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'| | | | | | | |\n' \
'---------------\n' \
' 0 1 2 3 4 5 6'
board2.clear()
assert board2.__repr__() == \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'| | | | | | | | | | |\n' \
'---------------------\n' \
' 0 1 2 3 4 5 6 7 8 9'
board3.clear()
assert board3.__repr__() == \
'| | | |\n' \
'| | | |\n' \
'| | | |\n' \
'-------\n' \
' 0 1 2'
#
# Tests Board.allow_move(col)
#
board3.add_move(0, 'X')
board3.add_move(0, 'X')
board3.add_move(0, 'X')
assert not board3.allow_move(0)
assert board3.allow_move(1)
assert board3.allow_move(2)
assert not board3.allow_move(-1)
assert not board3.allow_move(3)
#
# Tests Board.is_full()
#
assert not board3.is_full()
board3.add_move(1, 'X')
board3.add_move(1, 'X')
board3.add_move(1, 'X')
board3.add_move(2, 'X')
board3.add_move(2, 'X')
board3.add_move(2, 'X')
assert board3.is_full()
board4.add_move(0, 'X')
assert not board4.is_full()
board4.set_board("0000011111222223333344444")
assert board4.is_full()
#
# Tests Board.delete_move()
#
board5.add_move(1, 'X')
board5.add_move(2, 'O')
board5.add_move(2, 'O')
board5.add_move(3, 'O')
board5.add_move(1, 'X')
board5.delete_move(0)
board5.delete_move(1)
board5.delete_move(1)
board5.delete_move(1)
board5.delete_move(2)
assert board5.__repr__() == \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | | | | | |\n' \
'| | |O|O| | |\n' \
'-------------\n' \
' 0 1 2 3 4 5'
#
# Tests Board.wins_for(ox)
#
board1.set_board('00102030')
assert board1.wins_for('X')
assert board1.wins_for('O')
board1.clear()
board1.set_board('23344545515')
assert board1.wins_for('X')
assert not board1.wins_for('O')
#
# Tests Class Player
#
#
# Tests Player.__repr__()
#
player1 = Player('X', 'LEFT', 10)
assert player1.__repr__() == "Player: char = X, tie_breaking_type = LEFT, turns = 10"
player2 = Player('O', 'RANDOM', 7)
assert player2.__repr__() == "Player: char = O, tie_breaking_type = RANDOM, turns = 7"
player3 = Player('O', 'RIGHT', 6)
assert player3.__repr__() == "Player: char = O, tie_breaking_type = RIGHT, turns = 6"
player4 = Player('X', 'RANDOM', 5)
assert player4.__repr__() == "Player: char = X, tie_breaking_type = RANDOM, turns = 5"
player5 = Player('O', 'LEFT', 1)
assert player5.__repr__() == "Player: char = O, tie_breaking_type = LEFT, turns = 1"
#
# Tests Player.opp_char()
#
assert player1.opp_char() == 'O'
assert player2.opp_char() == 'X'
assert player3.opp_char() == 'X'
assert player4.opp_char() == 'O'
assert player5.opp_char() == 'X'
#
# Tests Player.score_board(board)
#
board1.clear()
board1.set_board('01020305')
assert player1.score_board(board1) == 100.0
assert player2.score_board(board1) == 0.0
board1.clear()
assert player1.score_board(board1) == 50.0
assert player2.score_board(board1) == 50.0