1
1
class TicTacToe {
2
2
3
3
/** Initialize your data structure here. */
4
- int [][] board ;
5
- int n ;
6
- boolean win ;
4
+ private int [] rows ;
5
+ private int [] cols ;
6
+ private int leftDiagCount ;
7
+ private int rightDiagCount ;
8
+ private int n ;
7
9
8
10
public TicTacToe (int n ) {
9
11
this .n = n ;
10
- board = new int [n ][n ];
12
+ this .rows = new int [n ];
13
+ this .cols = new int [n ];
14
+ leftDiagCount = 0 ;
15
+ rightDiagCount = 0 ;
11
16
}
12
17
13
18
/** Player {player} makes a move at ({row}, {col}).
@@ -19,104 +24,29 @@ public TicTacToe(int n) {
19
24
1: Player 1 wins.
20
25
2: Player 2 wins. */
21
26
public int move (int row , int col , int player ) {
22
- if (win ) {
23
- return 0 ;
24
- }
25
-
26
- board [row ][col ] = player ;
27
-
28
- boolean cCheck = columnCheck (col , n , player );
29
- boolean rCheck = rowCheck (row , n , player );
30
- boolean diagCheck = diagonalCheck (row , col , n , player );
31
-
32
- if (cCheck || rCheck || diagCheck ) {
33
- win = true ;
27
+ int moveValue = player == 1 ? 1 : -1 ;
28
+ rows [row ] += moveValue ;
29
+ cols [col ] += moveValue ;
30
+ if (row == col ) {
31
+ leftDiagCount += moveValue ;
32
+ }
33
+ if (col == n - row - 1 ) {
34
+ rightDiagCount += moveValue ;
35
+ }
36
+ if (
37
+ Math .abs (rows [row ]) == n ||
38
+ Math .abs (cols [col ]) == n ||
39
+ Math .abs (leftDiagCount ) == n ||
40
+ Math .abs (rightDiagCount ) == n
41
+ ) {
34
42
return player ;
35
43
}
36
-
37
44
return 0 ;
38
45
}
39
-
40
- private boolean diagonalCheck (int row , int col , int n , int player ) {
41
- int count = 0 ;
42
-
43
- int rowNum = row ;
44
- int colNum = col ;
45
-
46
- while (rowNum < n && colNum < n ) {
47
- if (board [rowNum ][colNum ] == player ) {
48
- count ++;
49
- }
50
-
51
- rowNum ++;
52
- colNum ++;
53
- }
54
-
55
- rowNum = row -1 ;
56
- colNum = col -1 ;
57
-
58
- while (rowNum >= 0 && colNum >= 0 ) {
59
- if (board [rowNum ][colNum ] == player ) {
60
- count ++;
61
- }
62
-
63
- rowNum --;
64
- colNum --;
65
- }
66
-
67
- if (count == n ) {
68
- return true ;
69
- }
70
-
71
- count = 0 ;
72
- rowNum = row ;
73
- colNum = col ;
74
-
75
- while (rowNum >= 0 && colNum < n ) {
76
- if (board [rowNum ][colNum ] == player ) {
77
- count ++;
78
- }
79
-
80
- rowNum --;
81
- colNum ++;
82
- }
83
-
84
- rowNum = row ;
85
- colNum = col ;
86
-
87
- while (rowNum < n && colNum >= 0 ) {
88
- if (board [rowNum ][colNum ] == player ) {
89
- count ++;
90
- }
91
-
92
- rowNum ++;
93
- colNum --;
94
- }
95
-
96
- if (count == n +1 ) {
97
- return true ;
98
- }
99
-
100
- return false ;
101
- }
102
-
103
- private boolean rowCheck (int row , int n , int player ) {
104
- for (int i =0 ; i <n ;i ++) {
105
- if (board [row ][i ] != player ) {
106
- return false ;
107
- }
108
- }
109
-
110
- return true ;
111
- }
112
-
113
- private boolean columnCheck (int col , int n , int player ) {
114
- for (int i =0 ; i <n ; i ++) {
115
- if (board [i ][col ] != player ) {
116
- return false ;
117
- }
118
- }
119
-
120
- return true ;
121
- }
122
46
}
47
+
48
+ /**
49
+ * Your TicTacToe object will be instantiated and called as such:
50
+ * TicTacToe obj = new TicTacToe(n);
51
+ * int param_1 = obj.move(row,col,player);
52
+ */
0 commit comments