We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 652c0c3 commit 47019e1Copy full SHA for 47019e1
Design/348_Design_Tic-Tac-Toe.java
@@ -0,0 +1,32 @@
1
+class TicTacToe {
2
+ private int[] rows, cols;
3
+ private int diagonal, antiDiagonal;
4
+
5
+ public TicTacToe(int n) {
6
+ rows = new int[n];
7
+ cols = new int[n];
8
+ diagonal = antiDiagonal = 0;
9
+ }
10
11
+ public int move(int row, int col, int player) {
12
+ int n = rows.length;
13
+ int mark = player == 1 ? 1 : -1;
14
15
+ rows[row] += mark;
16
+ cols[col] += mark;
17
18
+ if (row == col) {
19
+ diagonal += mark;
20
21
+ if (row + col == n - 1) {
22
+ antiDiagonal += mark;
23
24
25
+ if (Math.abs(rows[row]) == n || Math.abs(cols[col]) == n || Math.abs(diagonal) == n
26
+ || Math.abs(antiDiagonal) == n) {
27
+ return player;
28
29
30
+ return 0;
31
32
+}
0 commit comments