1
+ class TicTacToe (object ):
2
+
3
+ def __init__ (self , n ):
4
+ """
5
+ Initialize your data structure here.
6
+ :type n: int
7
+ """
8
+ self .size = n
9
+ self .board = [[0 for _ in range (n )] for j in range (n ) ]
10
+ def move (self , row , col , player ):
11
+ """
12
+ Player {player} makes a move at ({row}, {col}).
13
+ @param row The row of the board.
14
+ @param col The column of the board.
15
+ @param player The player, can be either 1 or 2.
16
+ @return The current winning condition, can be either:
17
+ 0: No one wins.
18
+ 1: Player 1 wins.
19
+ 2: Player 2 wins.
20
+ :type row: int
21
+ :type col: int
22
+ :type player: int
23
+ :rtype: int
24
+ """
25
+ self .board [row ][col ] = player
26
+ if self .check (row , col , player ):
27
+ return player
28
+
29
+ return 0
30
+
31
+
32
+ def check (self , row , col , toe ):
33
+ row_valid = True
34
+ col_valid = True
35
+
36
+
37
+ for i in range (self .size ):
38
+ if self .board [row ][i ] != toe :
39
+ row_valid = False
40
+ if self .board [i ][col ] != toe :
41
+ # print i, self.board[col][i], toe, col_valid
42
+ col_valid = False
43
+
44
+ if row != col and row + col != self .size - 1 :
45
+ return row_valid or col_valid
46
+
47
+ dia_valid1 = False
48
+ dia_valid2 = False
49
+ if row == col :
50
+ dia_valid1 = True
51
+ for i in range (self .size ):
52
+ if self .board [i ][i ] != toe :
53
+ dia_valid1 = False
54
+ if row + col == self .size - 1 :
55
+ dia_valid2 = True
56
+ for i in range (self .size ):
57
+ if self .board [i ][self .size - 1 - i ] != toe :
58
+ dia_valid2 = False
59
+ # print self.board
60
+ # print (row, col), toe, row_valid, col_valid ,dia_valid1 ,dia_valid2
61
+ return row_valid or col_valid or dia_valid1 or dia_valid2
62
+
63
+
64
+ # for j in range(self.)
65
+
66
+
67
+
68
+ # Your TicTacToe object will be instantiated and called as such:
69
+ # obj = TicTacToe(n)
70
+ # param_1 = obj.move(row,col,player)
0 commit comments