-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path348-DesignTicTacToe.py
61 lines (45 loc) · 1.77 KB
/
348-DesignTicTacToe.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
class TicTacToe:
def __init__(self, n: int):
self.n = n
self.board = [["_" for i in range(n)] for j in range(n)]
self.game_over = False
self.sign = {
1: "x",
2: "o"
}
def right_diag_check(self, player):
for i in range(self.n):
if self.board[i][i] != self.sign.get(player):
return False
return True
def left_diag_check(self, player):
for i in range(self.n):
if self.board[i][self.n - i - 1] != self.sign.get(player):
return False
return True
def row_check(self, row, player):
for j in range(self.n):
if self.board[row][j] != self.sign.get(player):
return False
return True
def col_check(self, col, player):
for j in range(self.n):
if self.board[j][col] != self.sign.get(player):
return False
return True
def move(self, row: int, col: int, player: int) -> int:
if not self.game_over:
if self.board[row][col] == "_":
self.board[row][col] = self.sign.get(player)
if self.row_check(row, player):
return player
if self.col_check(col, player):
return player
if row == col and self.right_diag_check(player):
return player
if row == self.n - col - 1 and self.left_diag_check(player):
return player
return 0
# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)