forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
design-tic-tac-toe.py
56 lines (48 loc) · 1.56 KB
/
design-tic-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
# Time: O(1), per move.
# Space: O(n^2)
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
class TicTacToe(object):
def __init__(self, n):
"""
Initialize your data structure here.
:type n: int
"""
self.__size = n
self.__rows = [[0, 0] for _ in xrange(n)]
self.__cols = [[0, 0] for _ in xrange(n)]
self.__diagonal = [0, 0]
self.__anti_diagonal = [0, 0]
def move(self, row, col, player):
"""
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.
:type row: int
:type col: int
:type player: int
:rtype: int
"""
i = player - 1
self.__rows[row][i] += 1
self.__cols[col][i] += 1
if row == col:
self.__diagonal[i] += 1
if col == len(self.__rows) - row - 1:
self.__anti_diagonal[i] += 1
if any(self.__rows[row][i] == self.__size,
self.__cols[col][i] == self.__size,
self.__diagonal[i] == self.__size,
self.__anti_diagonal[i] == self.__size):
return player
return 0
# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)