-
Notifications
You must be signed in to change notification settings - Fork 2
/
TicTacToeBoard.java
316 lines (281 loc) · 7.89 KB
/
TicTacToeBoard.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
*
*
*This is the TicTacToe Board class, which provides a 'playing field' and is
*initialized by TicTacToe Game.
*
*/
package tictactoe2;
/**
*
* @author tmaule
* @author pfernandez
*/
import java.util.ArrayList;
import java.util.List;
public class TicTacToeBoard {
static int size = 3;
public char[][] board;
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
/**
* This exception is thrown when the user or computer
* tries to mark a spot that is already taken.
* @author tmaule
*
*/
public class SpaceTakenException extends Exception {
private static final long serialVersionUID = 1L;
}
/**
* This method takes no parameters and returns nothing;
* it simply creates an array of arrays, with dimensions
* three by three, and fills it with space characters.
*/
public void initialize(){
this.board = new char[3][3];
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
board[i][j]=' ';
}
}
}
/**
* Called for side effects alone, printboard() takes
* no parameters and simply prints the tictactoe board
* by printing a variety of strings, and iterating
* through the matrix.
*/
public void printBoard(){
String topguide = " 1 2 3 ";
String seperator = " +---+---+---+";
System.out.println("\n" + topguide);
System.out.println(seperator);
String line = "";
for (int i=0; i<3; i++){
if(i==0) { line = "R " + (i+1) + " | "; }
if(i==1) { line = "O " + (i+1) + " | "; }
if(i==2) { line = "W " + (i+1) + " | "; }
for (int j=0; j<3; j++){
line = line + this.board[i][j];
line = line + " | ";
}
System.out.println(line);
System.out.println(seperator);
}
System.out.println("\n");
}
/**
* Checks if a space is open. Parameters are row,
* an integer, column, an integer. Returns 'true'
* if the integers are between 0 and 2, and
* the space they reference is open.
* @param row
* @param column
* @return
*/
public boolean isOpen(int row, int column){
if ((row > 3) || (column > 3)){
return false;
}
return ' ' == this.board[row][column];
}
/**
* Marks a space.
* It checks if the space is within bounds, and if not returns 'false' so
* that the human or computer picks another row/column pair. Next, it
* checks if the space indicated is taken, and throws a SpaceTakenException
* if it is. Otherwise, the method marks the indicated space with the
* symbol for the current player ('X' or '0')
*
* If on impossible mode, the AI will override any moves if a defense
* is necessary or a win is possible with the current board state.
* @param row
* @param column
* @param mark
* @return
* @throws SpaceTakenException
*/
public boolean markSpace(int row, int column, char mark) throws SpaceTakenException{
if (TicTacToeGame.difficulty==2 && mark=='0'){
//System.out.println("checking 2");
if (!nearlyWonBy('X').isEmpty()){
//System.out.println(nearlyWonBy('X'));
row = nearlyWonBy('X').get(0);
column = nearlyWonBy('X').get(1);
}
if (!nearlyWonBy('0').isEmpty()){
//System.out.println(nearlyWonBy('0'));
row = nearlyWonBy('0').get(0);
column = nearlyWonBy('0').get(1);
}
}
if ((row>3)||(column>3)){
System.out.println("Out of bounds!");
return false;
} else if (board[row][column]!=' '){
System.out.println("SpaceTaken throw an exception here");
throw new SpaceTakenException();
} else {
board[row][column] = mark;
}
if (mark=='0'){System.out.println("\033[0;1m" + "The computer marks (" +
(row+1) + "," + (column+1) + ")");}
return true;
}
/**
* Checks to see if the game is tied, and
* returns true if no spaces are unmarked.
* @return
*/
public boolean isTied(){
for (int i =0; i < size; i++){
for (int j = 0; j < size; j++){
if (isOpen(i,j)){
return false;
}
}
}
return true;
}
/**
* Checks to see if a given player has won.
* Parameter: mark, 'x' or 'o', the mark of the player who may have won
* Returns true if the player with the given mark has won.
* Checks for vertical, horizontal, and diagonal 'wins.'
* @param mark
* @return
*/
public boolean isWonBy(char mark){
for (int i=0; i <size; i++){
char[] row={' ',' ',' '};
for (int j=0; j<size; j++){
row[j] = board[i][j];
}
//System.out.println("Row we're checking: " + row[0] + row[1] + row[2]);
if (match(row, mark)){
return true;
}
}
for (int i=0; i <size; i++){
char[] column = {' ',' ',' '};
for (int j=0;j<size;j++){
column[j] = board[j][i];
}
//System.out.println("Column we're checking: " + column[0] + column[1] + column[2]);
if (match(column, mark)){
return true;
}
}
char[] diagonal_a = {board[0][0],board[1][1],board[2][2]};
if (match(diagonal_a,mark)){
return true;
}
char[] diagonal_b = {board[2][0],board[1][1],board[0][2]};
if (match(diagonal_b,mark)){
return true;
}
return false;
}
/**
* Returns true if the given list contains only the given mark
* Parameters include: spaces, a list of characters; mark, a
* character to match
* @param spaces
* @param mark
* @return
*/
public boolean match(char[] spaces, char mark){
for (int s = 0; s < 3; s++){
if (spaces[s]!= mark){
return false;
}
}
return true;
}
/**
* Checks to see if a given player is one move away from victory
* Parameter: mark, 'x' or 'o', the mark of the player who may win
* Returns an List of integers that indicate possible wins
* for every pair of integers from n=0, if 2n is a row, 2n+1 is the
* corresponding column
* Checks for vertical, horizontal, and diagonal 'wins.'
* @param mark
* @return
*/
public List<Integer> nearlyWonBy(char mark){
int count;
List<Integer> locations = new ArrayList<Integer>();
for (int i=0; i <3; i++){
count = 0;
for (int j=0; j<3; j++){
if (board[i][j] == mark) {count++;}
}
if (count == 2){
//System.out.print("2!");
for (int j=0; j<3; j++){
if (board[i][j] == ' ') {
locations.add(i);
locations.add(j);
}
}
}
}
for (int i=0; i <3; i++){
count = 0;
for (int j=0;j<3;j++){
if (board[j][i] == mark) {count++;}
}
if (count == 2){
//System.out.print("2!");
for (int j=0; j<3; j++){
if (board[j][i] == ' ') {
locations.add(j);
locations.add(i);
}
}
}
}
count = 0;
for (int i=0; i<3; i++){
if (board[i][i]==mark){count++;}
}
if (count == 2){
//System.out.print("2!");
for (int j=0; j<3; j++){
if (board[j][j] == ' ') {
locations.add(j);
locations.add(j);
}
}
}
count = 0;
for (int i=0; i<3; i++){
if (board[2-i][i]==mark){count++;}
}
if (count == 2){
//System.out.print("2!");
for (int j=0; j<3; j++){
if (board[2-j][j] == ' ') {
locations.add(2-j);
locations.add(j);
}
}
}
//System.out.println(mark + " "+locations);
return locations;
}
/**
* Unused main statement, since this class is not meant to be run alone,
* only through TicTacToeGame.java
* @param args
*/
public static void main(String[] args) {}
}