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

Commit 92f7792

Browse filesBrowse files
committed
using counters to decide state of tic tac toe
1 parent 357f056 commit 92f7792
Copy full SHA for 92f7792

File tree

Expand file treeCollapse file tree

1 file changed

+105
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+105
-0
lines changed

‎Design/ticTacToeCounter.java

Copy file name to clipboard
+105Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
class TicTacToe {
2+
3+
/** Initialize your data structure here. */
4+
private int[][] board;
5+
int[] rowCounter;
6+
int[] colCounter;
7+
int diagLeft;
8+
int diagRight;
9+
int n;
10+
public TicTacToe(int n) {
11+
board = new int[n][n];
12+
rowCounter = new int[n];
13+
colCounter = new int[n];
14+
diagLeft = 0;
15+
diagRight = 0;
16+
this.n = n;
17+
}
18+
19+
/** Player {player} makes a move at ({row}, {col}).
20+
@param row The row of the board.
21+
@param col The column of the board.
22+
@param player The player, can be either 1 or 2.
23+
@return The current winning condition, can be either:
24+
0: No one wins.
25+
1: Player 1 wins.
26+
2: Player 2 wins. */
27+
public int move(int row, int col, int player) {
28+
int move = player == 1 ? -1 : 1;
29+
rowCounter[row] = rowCounter[row] + move;
30+
colCounter[col] = colCounter[col] + move;
31+
32+
if(row == col) {
33+
diagLeft = diagLeft + move;
34+
}
35+
36+
if (row == n - col - 1) {
37+
diagRight = diagRight + move;
38+
}
39+
if (rowCounter[row] == n || colCounter[col] == n || diagLeft == n || diagRight == n) {
40+
return 2;
41+
} else if (rowCounter[row] == -n || colCounter[col] == -n || diagLeft == -n || diagRight == -n) {
42+
return 1;
43+
} else {
44+
return 0;
45+
}
46+
}
47+
}
48+
49+
/**
50+
* Your TicTacToe object will be instantiated and called as such:
51+
* TicTacToe obj = new TicTacToe(n);
52+
* int param_1 = obj.move(row,col,player);
53+
*/class TicTacToe {
54+
55+
/** Initialize your data structure here. */
56+
private int[][] board;
57+
int[] rowCounter;
58+
int[] colCounter;
59+
int diagLeft;
60+
int diagRight;
61+
int n;
62+
public TicTacToe(int n) {
63+
board = new int[n][n];
64+
rowCounter = new int[n];
65+
colCounter = new int[n];
66+
diagLeft = 0;
67+
diagRight = 0;
68+
this.n = n;
69+
}
70+
71+
/** Player {player} makes a move at ({row}, {col}).
72+
@param row The row of the board.
73+
@param col The column of the board.
74+
@param player The player, can be either 1 or 2.
75+
@return The current winning condition, can be either:
76+
0: No one wins.
77+
1: Player 1 wins.
78+
2: Player 2 wins. */
79+
public int move(int row, int col, int player) {
80+
int move = player == 1 ? -1 : 1;
81+
rowCounter[row] = rowCounter[row] + move;
82+
colCounter[col] = colCounter[col] + move;
83+
84+
if(row == col) {
85+
diagLeft = diagLeft + move;
86+
}
87+
88+
if (row == n - col - 1) {
89+
diagRight = diagRight + move;
90+
}
91+
if (rowCounter[row] == n || colCounter[col] == n || diagLeft == n || diagRight == n) {
92+
return 2;
93+
} else if (rowCounter[row] == -n || colCounter[col] == -n || diagLeft == -n || diagRight == -n) {
94+
return 1;
95+
} else {
96+
return 0;
97+
}
98+
}
99+
}
100+
101+
/**
102+
* Your TicTacToe object will be instantiated and called as such:
103+
* TicTacToe obj = new TicTacToe(n);
104+
* int param_1 = obj.move(row,col,player);
105+
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.