-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignTicTacToe.java
49 lines (45 loc) · 1.25 KB
/
DesignTicTacToe.java
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
class TicTacToe {
int[][] board;
int n;
public TicTacToe(int n) {
this.n = n;
this.board = new int[n][n];
}
public int move(int row, int col, int player) {
board[row][col] = player;
if(row == col){
boolean hasWon = true;
for(int i = 0; i < n; i++) {
if(board[i][i] != player){hasWon=false;break;}
}
if(hasWon)return player;
}
if(row + col == n-1) {
boolean hasWon = true;
for(int i = 0; i < n; i++) {
if(board[i][n-i-1] != player){hasWon=false;break;}
}
if(hasWon)return player;
}
boolean hasWon = true;
for(int i = 0; i < n; i++) {
if(board[row][i]!=player){
hasWon=false;break;
}
}
if(hasWon)return player;
hasWon = true;
for(int i = 0; i < n; i++) {
if(board[i][col]!=player){
hasWon=false;break;
}
}
if(hasWon)return player;
else return 0;
}
}
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/