Skip to content

Commit 1da07e4

Browse files
author
freemanzhang
committed
refactor code
1 parent 942f2de commit 1da07e4

File tree

1 file changed

+119
-99
lines changed

1 file changed

+119
-99
lines changed

src/games/TicTacToe.java

Lines changed: 119 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,120 @@
1-
2-
package games;
3-
4-
public class TicTacToe
5-
{
6-
7-
private int[] player1Row;
8-
private int[] player1Col;
9-
private int player1Diagnal;
10-
private int player1ReverseDiagnal;
11-
private int[] player2Row;
12-
private int[] player2Col;
13-
private int player2Diagnal;
14-
private int player2ReverseDiagnal;
15-
private int boardSize;
16-
17-
/** Initialize your data structure here. */
18-
public TicTacToe( int n )
19-
{
20-
player1Row = new int[n];
21-
player1Col = new int[n];
22-
player2Row = new int[n];
23-
player2Col = new int[n];
24-
boardSize = n;
25-
}
26-
27-
/**
28-
* Player {player} makes a move at ({row}, {col}).
29-
*
30-
* @param row
31-
* The row of the board.
32-
* @param col
33-
* The column of the board.
34-
* @param player
35-
* The player, can be either 1 or 2.
36-
* @return The current winning condition, can be either: 0: No one wins. 1:
37-
* Player 1 wins. 2: Player 2 wins.
38-
*/
39-
public int move( int row, int col, int player )
40-
{
41-
if ( player == 1 )
42-
{
43-
// record current move
44-
player1Row[row] += 1;
45-
player1Col[col] += 1;
46-
if ( row == col )
47-
{
48-
player1Diagnal += 1;
49-
}
50-
if ( row + col == boardSize - 1 )
51-
{
52-
player1ReverseDiagnal += 1;
53-
}
54-
55-
// check if win
56-
if ( player1Row[row] == boardSize
57-
|| player1Col[col] == boardSize
58-
|| player1Diagnal == boardSize
59-
|| player1ReverseDiagnal == boardSize )
60-
{
61-
return 1;
62-
}
63-
else
64-
{
65-
return 0;
66-
}
67-
}
68-
else
69-
{
70-
player2Row[row] += 1;
71-
player2Col[col] += 1;
72-
if ( row == col )
73-
{
74-
player2Diagnal += 1;
75-
}
76-
if ( row + col == boardSize - 1 )
77-
{
78-
player2ReverseDiagnal += 1;
79-
}
80-
81-
// check if win
82-
if ( player2Row[row] == boardSize
83-
|| player2Col[col] == boardSize
84-
|| player2Diagnal == boardSize
85-
|| player2ReverseDiagnal == boardSize )
86-
{
87-
return 2;
88-
}
89-
else
90-
{
91-
return 0;
92-
}
93-
}
94-
}
95-
}
96-
97-
/**
98-
* Your TicTacToe object will be instantiated and called as such: TicTacToe obj
99-
* = new TicTacToe(n); int param_1 = obj.move(row,col,player);
1+
2+
package games;
3+
4+
/*
5+
Design a Tic-tac-toe game that is played between two players on a n x n grid.
6+
7+
You may assume the following rules:
8+
9+
A move is guaranteed to be valid and is placed on an empty block.
10+
Once a winning condition is reached, no more moves is allowed.
11+
A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
12+
Example:
13+
Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.
14+
15+
TicTacToe toe = new TicTacToe(3);
16+
17+
toe.move(0, 0, 1); -> Returns 0 (no one wins)
18+
|X| | |
19+
| | | | // Player 1 makes a move at (0, 0).
20+
| | | |
21+
22+
toe.move(0, 2, 2); -> Returns 0 (no one wins)
23+
|X| |O|
24+
| | | | // Player 2 makes a move at (0, 2).
25+
| | | |
26+
27+
toe.move(2, 2, 1); -> Returns 0 (no one wins)
28+
|X| |O|
29+
| | | | // Player 1 makes a move at (2, 2).
30+
| | |X|
31+
32+
toe.move(1, 1, 2); -> Returns 0 (no one wins)
33+
|X| |O|
34+
| |O| | // Player 2 makes a move at (1, 1).
35+
| | |X|
36+
37+
toe.move(2, 0, 1); -> Returns 0 (no one wins)
38+
|X| |O|
39+
| |O| | // Player 1 makes a move at (2, 0).
40+
|X| |X|
41+
42+
toe.move(1, 0, 2); -> Returns 0 (no one wins)
43+
|X| |O|
44+
|O|O| | // Player 2 makes a move at (1, 0).
45+
|X| |X|
46+
47+
toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
48+
|X| |O|
49+
|O|O| | // Player 1 makes a move at (2, 1).
50+
|X|X|X|
51+
Follow up:
52+
Could you do better than O(n2) per move() operation?
53+
54+
Hint:
55+
56+
Could you trade extra space such that move() operation can be done in O(1)?
57+
You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.
58+
* */
59+
60+
public class TicTacToe
61+
{
62+
63+
private int[] rows;
64+
private int[] cols;
65+
private int diagnal;
66+
private int reverseDiagnal;
67+
private int boardSize;
68+
69+
/** Initialize your data structure here. */
70+
public TicTacToe( int n )
71+
{
72+
rows = new int[n];
73+
cols = new int[n];
74+
boardSize = n;
75+
}
76+
77+
/**
78+
* Player {player} makes a move at ({row}, {col}).
79+
*
80+
* @param row
81+
* The row of the board.
82+
* @param col
83+
* The column of the board.
84+
* @param player
85+
* The player, can be either 1 or 2.
86+
* @return The current winning condition, can be either: 0: No one wins. 1:
87+
* Player 1 wins. 2: Player 2 wins.
88+
*/
89+
public int move( int row, int col, int player )
90+
{
91+
int toAdd = player == 1 ? 1 : -1;
92+
93+
// record current move
94+
rows[row] += toAdd;
95+
cols[col] += toAdd;
96+
if ( row == col )
97+
{
98+
diagnal += toAdd;
99+
}
100+
if ( row + col == boardSize - 1 )
101+
{
102+
reverseDiagnal += toAdd;
103+
}
104+
105+
// check if win
106+
if ( Math.abs( rows[row] ) == boardSize
107+
|| Math.abs( cols[col] ) == boardSize
108+
|| Math.abs( diagnal ) == boardSize
109+
|| Math.abs( reverseDiagnal ) == boardSize )
110+
{
111+
return player;
112+
}
113+
return 0;
114+
}
115+
}
116+
117+
/**
118+
* Your TicTacToe object will be instantiated and called as such: TicTacToe obj
119+
* = new TicTacToe(n); int param_1 = obj.move(row,col,player);
100120
*/

0 commit comments

Comments
 (0)