-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoughtsCrosses.java
More file actions
181 lines (150 loc) · 3.36 KB
/
NoughtsCrosses.java
File metadata and controls
181 lines (150 loc) · 3.36 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
public class NoughtsCrosses
{
public static final int BLANK = 0;
public static final int CROSS = 1;
public static final int NOUGHT = 2;
private boolean crossTurn;
private int[][] board;
private boolean isMyTurn; // check if the turn is of the current user
private int turn;
private boolean inVisible = false; // check if the user is in game
private boolean userWinner = false; // check if the user is a winner
private boolean isPlaying = true;
/**
Create a new game with empty board
*/
public NoughtsCrosses()
{
board = new int[3][3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = BLANK;
}
}
crossTurn = true;
}
/**
Get symbol at given location
@param i the row
@param j the column
@return the symbol at that location
*/
public int get(int i, int j)
{
return board[i][j];
}
/**
Is it cross's turn?
@return true if it is cross's turn, false for nought's turn
*/
public boolean isCrossTurn()
{
return crossTurn;
}
/**
Let the player whose turn it is play at a particular location
@param i the row
@param j the column
*/
public void turn(int i, int j)
{
if(board[i][j] == BLANK)
{
if(crossTurn)
{
board[i][j] = CROSS;
}
else
{
board[i][j] = NOUGHT;
}
crossTurn = !crossTurn;
}
else
{
throw new IllegalArgumentException("Board not empty at (" + i + ", " + j + ")");
}
}
private boolean winner(int player)
{
return
(board[0][0] == player && board[0][1] == player && board[0][2] == player) ||
(board[1][0] == player && board[1][1] == player && board[1][2] == player) ||
(board[2][0] == player && board[2][1] == player && board[2][2] == player) ||
(board[0][0] == player && board[1][0] == player && board[2][0] == player) ||
(board[0][1] == player && board[1][1] == player && board[2][1] == player) ||
(board[0][2] == player && board[1][2] == player && board[2][2] == player) ||
(board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player);
}
/**
Determine who (if anyone) has won
@return CROSS if cross has won, NOUGHT if nought has won, otherwise BLANK
*/
public int whoWon()
{
if(winner(CROSS))
{
return CROSS;
}
else if(winner(NOUGHT))
{
return NOUGHT;
}
else
{
return BLANK;
}
}
/**
Start a new game
*/
public void newGame()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = BLANK;
}
}
crossTurn = true;
}
/*
*
* Set and get method for the fields
*
*/
public boolean getIsMyTurn(){
return this.isMyTurn;
}
public void setIsMyTurn(boolean turn) {
this.isMyTurn = turn;
}
public int getTurn(){
return this.turn;
}
public void setTurn(int turn){
this.turn = turn;
}
public boolean isInVisible() {
return inVisible;
}
public void setInVisible(boolean inVisible) {
this.inVisible = inVisible;
}
public boolean isUserWinner() {
return userWinner;
}
public void setUserWinner(boolean userWinner) {
this.userWinner = userWinner;
}
public boolean isPlaying() {
return isPlaying;
}
public void setPlaying(boolean isPlaying) {
this.isPlaying = isPlaying;
}
}