-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignTic-Tac-Toe.py
69 lines (64 loc) · 2.09 KB
/
DesignTic-Tac-Toe.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
class TicTacToe:
'''
o(n) - ac
use two lists col and row to store the state in each row and col.
and diag for diagnal, rdiag for reverse diagonal
after each move, add 1 for player1 (or -1 for player2) to the col and row of the move.
if one item in col/row or diag or rdiag reach n, player1 win; if reach -n, player2 win.
'''
def __init__(self, n: int):
"""
Initialize your data structure here.
"""
self.n = n
self.d = []
for i in range(n):
self.d.append([0]*n)
self.row = [0]*n
self.col = [0]*n
self.diag = 0
self.rdiag = 0
def move(self, row: int, col: int, player: int) -> int:
"""
Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins.
"""
if player == 1:
if row == col:
self.diag += 1
if self.n-1-row == col:
self.rdiag += 1
self.col[col] += 1
self.row[row] += 1
elif player == 2:
if row == col:
self.diag -= 1
if self.n-1-row == col:
self.rdiag -= 1
self.col[col] -= 1
self.row[row] -= 1
for i in self.col:
if i == self.n:
return 1
elif i == -self.n:
return 2
for i in self.row:
if i == self.n:
return 1
elif i == -self.n:
return 2
if self.diag == self.n or self.rdiag == self.n:
return 1
elif self.diag == -self.n or self.rdiag == -self.n:
return 2
print(self.row)
return 0
# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)