Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
81 lines (65 loc) · 2.12 KB

File metadata and controls

81 lines (65 loc) · 2.12 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class TicTacToe {
private int[][] board;
/** Initialize your data structure here. */
public TicTacToe(int n) {
board = new int[n][n];
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
public int move(int row, int col, int player) {
board[row][col] = player;
if (isRowWin(row, col, player) || isColWin(row, col, player) || isDiagnalWin(row, col, player) || isReverseDiagnalWin(row, col, player)) {
return player;
}
return 0;
}
private boolean isRowWin(int row, int col, int player) {
for (int i = 0; i < board.length; i++) {
if (board[i][col] != player) {
return false;
}
}
return true;
}
private boolean isColWin(int row, int col, int player) {
for (int i = 0; i < board.length; i++) {
if (board[row][i] != player) {
return false;
}
}
return true;
}
private boolean isDiagnalWin(int row, int col, int player) {
if (row != col) {
return false;
}
for (int i = 0; i < board.length; i++) {
if (board[i][i] != player) {
return false;
}
}
return true;
}
private boolean isReverseDiagnalWin(int row, int col, int player) {
if (row + col != board.length - 1) {
return false;
}
for (int i = 0; i < board.length; i++) {
if (board[i][board.length - i - 1] != player) {
return false;
}
}
return true;
}
}
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/
Morty Proxy This is a proxified and sanitized view of the page, visit original site.