-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect4.py
More file actions
58 lines (47 loc) · 1.42 KB
/
connect4.py
File metadata and controls
58 lines (47 loc) · 1.42 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
import numpy as np
import itertools
ROWS = 6
COLUMNS = 7
play_sequence = '555424551003426542401004633101'
def all_equal_fixed(fixed, iterable):
return all(i == fixed for i in iterable)
def check_board(play_sequence):
board = np.zeros((COLUMNS, ROWS), dtype=int)
row_height = np.zeros((COLUMNS,), dtype=int)
msg = '' # msg to display below the board
current_player = 1 # 1st move at player 1
def next_player():
nonlocal current_player
current_player = 3 - current_player
for play in map(int, play_sequence):
h = row_height[play]
if h < 6:
board[play][5 - h] = current_player
row_height[play] += 1
else:
msg = f"Can't play at column {play}"
return board, msg
# check winner
for i, j in itertools.product(range(COLUMNS), range(ROWS)):
for dir_x, dir_y in [(1,0), (0,1), (1,1), (1,-1)]:
try:
if all_equal_fixed(current_player,
(board[i + s * dir_x][j + s * dir_y] for s in range(4))):
print(play)
msg = f'Player {current_player} wins!'
return board, msg
except IndexError:
pass
next_player()
msg = f'Next turn: Player {current_player}'
return board, msg
def print_board(board, msg):
# display board and message
seperator = '+-+-+-+-+-+-+-+'
print(seperator)
for row in range(6):
print(f'|{"|".join(map(str, board[:, row]))}|')
print(seperator)
print(f'Message: {msg}')
if __name__ == '__main__':
print_board(*check_board(play_sequence))