-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy path348.py
More file actions
21 lines (19 loc) · 726 Bytes
/
348.py
File metadata and controls
21 lines (19 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class TicTacToe:
def __init__(self, n):
self.n = n
self.rows, self.cols = collections.defaultdict(int), collections.defaultdict(int)
self.d = self.ad = 0
def move(self, row, col, player):
add = player == 1 and 1 or -1
self.rows[row] += add
self.cols[col] += add
if row == col:
self.d += add
if row == self.n - col - 1:
self.ad += add
if self.rows[row] == self.n or self.cols[col] == self.n or self.d == self.n or self.ad == self.n:
return 1
elif self.rows[row] == -self.n or self.cols[col] == -self.n or self.d == -self.n or self.ad == -self.n:
return 2
else:
return 0