-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardTwoD.java
More file actions
298 lines (253 loc) · 9.39 KB
/
BoardTwoD.java
File metadata and controls
298 lines (253 loc) · 9.39 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
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
import java.util.Random;
import java.util.Scanner;
public class BoardTwoD implements BoardIO{
Random rand = new Random();
static Scanner scanner = new Scanner(System.in);
int turnCount = 0;
Status[][] board;
final int BOARDWIDTH;
final int SETBOARDWIDTH;
public BoardTwoD(int boardwidth) {
BOARDWIDTH = boardwidth > 0 ? boardwidth : 3;
SETBOARDWIDTH = BOARDWIDTH - 1;
board = new Status[BOARDWIDTH][BOARDWIDTH];
System.out.print("Alright then, let's play Tic Tac Toe!\n");
for (int column = 0; column < BOARDWIDTH; column++) {
for (int row = 0; row < BOARDWIDTH; row++) {
board[column][row] = Status.NONE;
}
}
}
public static Status oppositeStatus(Status s) {
switch(s) {
case X: return Status.O;
case O: return Status.X;
case NONE:
throw new RuntimeException("Status.NONE was passed through oppositeStatus(). that's not supposed to happen.");
default: throw new RuntimeException();
}
}
public static String statusToString(Status s) {
switch(s) {
case X: return "X";
case O: return "O";
case NONE: return "-";
default: throw new RuntimeException();
}
}
public void place(int targColumn, int targRow, Status piece) {
board[targColumn][targRow] = piece;
}
public boolean controlledPlace(int targColumn, int targRow, Status piece, boolean retryIfTaken) {
if (board[targColumn][targRow] == Status.NONE) {
place(targColumn, targRow, piece);
return true;
}
else if (retryIfTaken) {
System.out.println("Sorry, that spot is taken.");
turnModule(piece);
}
return false;
}
public boolean controlledPlace(int targColumn, int targRow, Status piece) {
return controlledPlace(targColumn, targRow, piece, false);
}
public void printBoard() {
String fullRow = "";
for (int row = 0; row < BOARDWIDTH; row++) {
fullRow = Integer.toString(BOARDWIDTH - row) + " ";
for (int col = 0; col < BOARDWIDTH; col++) {
fullRow += statusToString(board[col][row]) + " ";
}
System.out.println(fullRow);
}
fullRow = " ";
for (int col = 0; col < BOARDWIDTH; col++) {
fullRow += Integer.toString(col + 1) + " ";
}
System.out.println(fullRow);
}
public boolean checkVerticals() {
boolean win = false;
for (int col = 0; col < BOARDWIDTH; col++) {
boolean set = board[col][0] != Status.NONE;
for (int g = 0; g < BOARDWIDTH; g++) {
set = set && board[col][0] == board[col][g];
}
win = win || set;
}
return win;
}
public boolean checkHorizontals() {
boolean win = false;
for (int ro = 0; ro < BOARDWIDTH; ro++) {
boolean set = board[0][ro] != Status.NONE;
for (int g = 0; g < BOARDWIDTH; g++) {
set = set && board[0][ro] == board[g][ro];
}
win = win || set;
}
return win;
}
public boolean checkDiagonals() {
//negative slope
boolean win = false;
boolean negSet = board[0][0] != Status.NONE;
for (int dia = 1; dia < BOARDWIDTH; dia++) {
negSet = negSet && board[dia][dia] == board[0][0];
}
win = win || negSet;
// positive slope
boolean posSet = board[SETBOARDWIDTH][0] != Status.NONE;
for (int dia = 1; dia < BOARDWIDTH; dia++) {
posSet = posSet && board[SETBOARDWIDTH - dia][dia] == board[SETBOARDWIDTH][0];
}
win = win || posSet;
return win;
}
public boolean checkDraw() {
boolean noDraw = false;
for (int column = 0; column < BOARDWIDTH; column++) {
for (int row = 0; row < BOARDWIDTH; row++) {
noDraw = noDraw || board[column][row] == Status.NONE;
}
}
return !(noDraw);
}
public boolean checkWin() {
return checkVerticals() || checkHorizontals() || checkDiagonals();
}
// ai !!!!!
public int[][] possibleWinChecker(Status checker) {
int count = 0;
int[][] duos = new int[2][];
// they call me vertical
for (int col = 0; col < BOARDWIDTH; col++) {
for(int missingValue = 0; missingValue < BOARDWIDTH; missingValue++) {
//Status baseBoard = missingValue != 0 ? board[0][0] : board[0][1]; used previously. might use again
boolean set = board[col][missingValue] == Status.NONE;
for (int ro = 0; ro < BOARDWIDTH; ro++) {
if (ro != missingValue) {
set = set && board[col][ro] == checker;
}
}
if (set) {
duos[0][count] = col;
duos[1][count] = missingValue;
count++;
}
}
}
// they call me horizontal
for (int ro = 0; ro < BOARDWIDTH; ro++) {
for(int missingValue = 0; missingValue < BOARDWIDTH; missingValue++) {
boolean set = board[missingValue][ro] == Status.NONE;
for (int col = 0; col < BOARDWIDTH; col++) {
if (col != missingValue) {
set = set && board[col][ro] == checker;
}
}
if (set) {
duos[0][count] = missingValue;
duos[1][count] = ro;
count++;
}
}
}
// they call me negative slope diagonal
for (int missingValue = 0; missingValue < BOARDWIDTH; missingValue++) {
boolean set = board[missingValue][missingValue] == Status.NONE;
for (int dia = 0; dia < BOARDWIDTH; dia++) {
if (dia != missingValue) {
set = set && board[dia][dia] == checker;
}
}
if (set) {
duos[0][count] = missingValue;
duos[1][count] = missingValue;
count++;
}
}
// they call me positive slope diagonal
for (int missingValue = 0; missingValue < BOARDWIDTH; missingValue++) {
boolean set = board[SETBOARDWIDTH - missingValue][missingValue] == Status.NONE;
for (int dia = 0; dia < BOARDWIDTH; dia++) {
if (dia != missingValue) {
set = set && board[SETBOARDWIDTH - dia][dia] == checker;
}
}
if (set) {
duos[0][count] = SETBOARDWIDTH - missingValue;
duos[1][count] = missingValue;
}
}
return duos;
}
public boolean aiOneTurnWin(Status aiPlayer, boolean self) {
int[][] duos = self ? possibleWinChecker(aiPlayer) : possibleWinChecker(oppositeStatus(aiPlayer));
if (duos[0].length != 0) {
controlledPlace(duos[0][0], duos[1][0], aiPlayer);
return true;
}
return false;
}
public void aiTurn(Status player) {
if (aiOneTurnWin(player, true)) {
}
else if (aiOneTurnWin(player, false)) {
}
else if (controlledPlace(SETBOARDWIDTH / 2, SETBOARDWIDTH / 2, player)) {
}
else if (controlledPlace(0, 0, player)) {
}
else if (controlledPlace(0, SETBOARDWIDTH, player)) {
}
else if (controlledPlace(SETBOARDWIDTH, 0, player)) {
}
else if (controlledPlace(SETBOARDWIDTH, SETBOARDWIDTH, player)) {
}
else {
while (!controlledPlace(rand.nextInt(BOARDWIDTH), rand.nextInt(BOARDWIDTH), player)) {}
}
}
public int accurateIntScan() {
int scan = scanner.nextInt();
if (scan > 0 && scan <= BOARDWIDTH) {
return scan;
}
else {
System.out.println("That is an invalid value. Please type a valid coordinate.");
return accurateIntScan();
}
}
public void turnModule(Status player) {
printBoard();
System.out.println("In which column would you like to place the " + statusToString(player) + "? \n");
int placerColumn = accurateIntScan();
printBoard();
System.out.println("In which row would you like to place the " + statusToString(player) + "? \n");
int placerRow = accurateIntScan();
System.out.println("\n\n");
controlledPlace(placerColumn - 1, BOARDWIDTH - placerRow, player, true);
}
public Status play() {
Status winner = Status.NONE;
while (winner == Status.NONE) {
turnModule(Status.X);
if (checkWin()) {
winner = Status.X;
return winner;
} else if (checkDraw()) {
return winner;
}
aiTurn(Status.O);
if (checkWin()) {
winner = Status.O;
return winner;
} else if (checkDraw()) {
return winner;
}
}
return winner;
}
}