-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
431 lines (372 loc) · 12.8 KB
/
Board.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import java.io.IOException;
import java.util.HashMap;
public class Board {
private char[][] board = new char[6][7];
private static final int WEIGHT = 100;
public HashMap<String, Integer> scores = new HashMap<String, Integer>();
private HashMap<String, Character> letters;
int boardScore = 0;
public Board() throws IOException, InterruptedException {
scores.put("human", 0);
scores.put("computer", 0);
letters = new HashMap<>();
letters.put("human", 'H');
letters.put("computer", 'C');
initializeBoard();
drawBoard();
}
// Initialize the board with * in every spot.
private void initializeBoard() {
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
board[i][j] = '*';
}
}
}
public boolean putPiece(int y, int x) {
board[y][x] = 'C';
evaluateBoard();
boardScore += 1;
return true;
}
public boolean putPiece(String team, int x) {
int y = findYSpot(x);
if(y == -1) {
return false;
}
else {
if(team.equals("human")) {
board[y][x] = 'H';
boardScore -= 1;
}
else {
board[y][x] = 'C';
boardScore += 1;
}
evaluateBoard();
return true;
}
}
public boolean removePiece(Move m) {
int x = m.xMove;
int y = m.yMove;
if(board[y][x] == '*') {
return false;
}
else {
board[y][x] = '*';
evaluateBoard();
return true;
}
}
/* Find the spot to put the piece in a given column.
* @param y the column to search
* @return the spot to put the piece in. Return -1 if unavailable.
*/
public int findYSpot(int x) {
for(int i = 5; i > -1; i--) {
if(board[i][x] == '*') {
return i;
}
}
return -1;
}
/*
* Draw the board
*/
public void drawBoard() throws IOException, InterruptedException {
clear();
for(int y = 0; y < 6; y++) {
for(int x = 0; x < 7; x++) {
System.out.print(board[y][x]);
}
System.out.println("");
}
}
/*
* Clear the screen
*/
private void clear() throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
/* Check the sum of the diagonals
* @param team The team to evaluate
* @return the diagonal sum for the given team.
*/
private int forwardDiagonalSum(String team, String oppositeTeam) {
char letter = letters.get(team);
char oppositeLetter = letters.get(oppositeTeam);
int number = 0;
for(int row = 3; row < 6; row++) { //repeat this process for each row
for(int column = 0; column < 4; column++) { //repeat for each column
int counter = 0;
char[] piece = new char[4];
piece[0] = board[row][column];
if(piece[0] == letter) {
counter++;
}
for(int tally = 1; tally < 4; tally++) {
if(row-1 < 0) { //if we exceed the board, stop!
break;
}
else {
piece[tally] = board[row-tally][column+tally];
// if I found a piece of the right color
if(piece[tally] == letter ) {
if(counter == 0) {
counter = 1;
}
// if it's consecutive
if(tally-1 >= 0) {
if(piece[tally-1] == letter) {
counter++;
}
}
}
// If it's a blocked sequence, reset the counter
else if(piece[tally] == oppositeLetter){
if(counter > 1) {
counter = 1;
}
}
}
}
switch(counter) {
case 1:
number += WEIGHT*.01;
break;
case 2:
number += WEIGHT *.1;
break;
case 3:
number += 5*WEIGHT;
break;
case 4:
number += WEIGHT*1000;
break;
}
}
}
return number;
}
/* Check the sum of the diagonals
* @param team The team to evaluate
* @return the diagonal sum for the given team.
*/
private int backwardsDiagonalSum(String team, String oppositeTeam) {
char letter = letters.get(team);
char oppositeLetter = letters.get(oppositeTeam);
int number = 0;
for(int row = 3; row < 6; row++) { //repeat this process for each row
for(int column = 6; column > 2; column--) { //repeat for each column
int counter = 0;
char[] piece = new char[4];
piece[0] = board[row][column];
if(piece[0] == letter) {
counter++;
}
for(int tally = 1; tally < 4; tally++) {
if(row-1 < 0) { //if we exceed the board, stop!
break;
}
else {
piece[tally] = board[row-tally][column-tally];
// if i found a piece of the right color
if(piece[tally] == letter ) {
if(counter == 0) {
counter = 1;
}
// if it's consecutive
if(tally-1 >= 0) {
if(piece[tally-1] == letter) {
counter++;
}
}
}
else if(piece[tally] == oppositeLetter) {
if(counter > 1) {
counter = 1;
}
}
}
}
switch(counter) {
case 1:
number += WEIGHT *.01;
break;
case 2:
number += WEIGHT * .1;
break;
case 3:
number += 5*WEIGHT;
break;
case 4:
number += WEIGHT*1000;
break;
}
}
}
return number;
}
/* Check the sum of the verticals
* @param team The team to evaluate
* @return the diagonal sum for the given team.
*/
private int verticalSum(String team, String oppositeTeam) {
char letter = letters.get(team);
char oppositeLetter = letters.get(oppositeTeam);
int number = 0;
for(int column = 0; column < 7; column++) { // repeat this process for each row
for(int row = 0; row < 3; row++) { // go through each set of 4
int counter = 0;
char[] piece = new char[4];
piece[0] = board[row][column];
if(piece[0] == letter) {
counter++;
}
for(int tally = row+1; tally < row+4; tally++) { // find each element
piece[tally-row] = board[tally][column];
if(piece[tally-row] == letter) { // if found appropriate team piece
if(counter == 0) {
counter = 1;
}
if(piece[tally-row-1] == letter) { // if consecutive
counter++;
}
}
else if(piece[tally-row] == oppositeLetter) {
if(counter > 1) {
counter = 1;
}
}
}
switch(counter) {
case 1:
number += WEIGHT *.01;
break;
case 2:
number += WEIGHT * .1;
break;
case 3:
number += 5*WEIGHT;
break;
case 4:
number += WEIGHT*1000;
break;
}
}
}
return number;
}
/* Check the sum of the horizontal
* @param team The team to evaluate
* @return the diagonal sum for the given team.
*/
private int horizontalSum(String team, String oppositeTeam) {
char letter = letters.get(team);
char oppositeLetter = letters.get(oppositeTeam);
int number = 0;
for(int row = 0; row < 6; row++) { // repeat this process for each row
for(int column = 0; column < 4; column++) { // go through each set of 4
int counter = 0;
char[] piece = new char[4];
piece[0] = board[row][column];
if(piece[0] == letter) {
counter++;
}
for(int tally = column+1; tally < column+4; tally++) { // find each element
piece[tally-column] = board[row][tally];
if(piece[tally-column] == letter) {
if(counter == 0) {
counter = 1;
}
// If consecutive, increase counter
if(piece[tally-column-1] == letter) {
counter++;
}
}
else if(piece[tally-column] == oppositeLetter) {
if(counter > 1) {
counter = 1;
}
}
}
switch(counter) {
case 1:
number += WEIGHT *.01;
break;
case 2:
number += WEIGHT * .1;
break;
case 3:
number += 5*WEIGHT;
break;
case 4:
number += WEIGHT*1000;
break;
}
}
}
return number;
}
/* Evaluate the board and check what it's total is.
*/
private void evaluateBoard() {
//Reset the scores to zero
int compScore = 0;
int humScore = 0;
//evaluate each direction
humScore += forwardDiagonalSum("human", "computer");
humScore += backwardsDiagonalSum("human", "computer");
humScore += verticalSum("human", "computer");
humScore += horizontalSum("human", "computer");
compScore += forwardDiagonalSum("computer", "human");
compScore += backwardsDiagonalSum("computer", "human");
compScore += verticalSum("computer", "human");
compScore += horizontalSum("computer", "human");
boardScore = compScore - humScore;
scores.put("human", humScore);
scores.put("computer", compScore);
}
/*
Check to see if the game is over.
A game over is determined as (a) Human wins (b) Computer wins (c) The board is full
@return The status of the game
*/
public boolean gameOver() {
if(boardFull()) {
System.out.println("Tie game!");
return true;
}
else {
if(boardScore >= 50000) {
System.out.println("COMPUTER WINS!");
return true;
}
else if(boardScore <= -50000) {
System.out.println("HUMAN WINS!");
return true;
}
else {
return false;
}
}
}
/*
Check to see whether the board is full or not.
@return The status of the board
*/
private boolean boardFull() {
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
if(board[i][j] == '*') {
return false;
}
}
}
return true;
}
public char[][] getBoard() {
return board;
}
}