forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_219.py
116 lines (88 loc) · 2.52 KB
/
problem_219.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
from enum import Enum
ROWS = 6
COLS = 7
class Color(Enum):
RED = 1
BLACK = 2
class Board:
def __init__(self):
self.grid = list()
for _ in range(COLS):
col = list()
for _ in range(ROWS):
col.append(None)
self.grid.append(col)
self.occupancy = [0] * COLS
class IllegalMove(Exception):
pass
def play_piece(board, played_column, played_color):
if board.occupancy[played_column] == 6:
raise IllegalMove("Illegal move in this column")
played_row = board.occupancy[played_column]
board.grid[played_column][played_row] = played_color
board.occupancy[played_column] += 1
# check vertical
consecutive = 0
if len(board.grid[played_column]) > 4:
for color in board.grid[played_column]:
if color == played_color:
consecutive += 1
else:
consecutive = 0
if consecutive == 4:
return True
# check horizontal
consecutive = 0
for i in range(COLS):
color = board.grid[i][played_row]
if color == played_color:
consecutive += 1
else:
consecutive = 0
if consecutive == 4:
return True
# check positive-slope diagonal
consecutive = 0
offset = min(played_column, played_row)
col = played_column - offset
row = played_row - offset
while col < COLS and row < ROWS:
color = board.grid[col][row]
if color == played_color:
consecutive += 1
else:
consecutive = 0
if consecutive == 4:
return True
# check negative-slope diagonal
consecutive = 0
col = played_column + offset
row = played_row - offset
while col > 0 and row < ROWS:
color = board.grid[col][row]
if color == played_color:
consecutive += 1
else:
consecutive = 0
if consecutive == 4:
return True
return False
def play_game():
board = Board()
print("New board initialized")
turn = 0
players = [Color.RED, Color.BLACK]
while True:
player = players[turn % (len(players))]
print("{}'s turn.".format(player))
col_num = int(input("Enter a column number: "))
try:
won = play_piece(board, col_num, player)
except IllegalMove as e:
print(e)
continue
if won:
print("{} wins".format(player))
break
turn += 1
play_game()