LeetCode 348. [Python] Design Tic-Tac-Toe
原题链接
中等
作者:
徐辰潇
,
2020-04-02 11:20:53
,
所有人可见
,
阅读 1129
class TicTacToe:
def __init__(self, n: int):
"""
Initialize your data structure here.
"""
self.Row = [0]*n
self.Col = [0]*n
self.Diag = 0
self.Anti_diag = 0
self.n = n
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:
self.Row[row] += 1
self.Col[col] += 1
if row == col:
self.Diag += 1
if row + col == self.n-1:
self.Anti_diag += 1
if self.Row[row] == self.n or self.Col[col] == self.n or self.Diag == self.n or self.Anti_diag == self.n:
return 1
if player == 2:
self.Row[row] -= 1
self.Col[col] -= 1
if row == col:
self.Diag -= 1
if row + col == self.n-1:
self.Anti_diag -= 1
if self.Row[row] == -self.n or self.Col[col] == -self.n or self.Diag == -self.n or self.Anti_diag == -self.n:
return 2
return 0