-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
384 lines (369 loc) · 15.4 KB
/
Board.java
File metadata and controls
384 lines (369 loc) · 15.4 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
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
import java.util.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.Point;
public class Board{
private char [][] board;
private int movesLeft;
private int turn;
//testing
// public static void main(String[]args){
// Board b = new Board();
// b.printBoard(1);
// System.out.println(b.evaluate('O'));
// }
public Board(){
board = new char[8][8];
movesLeft = 64;
//populate empty board
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
board[i][j] = '_';
}
}
//test generate random boards
// Random r = new Random();
// for(int i=0;i<8;i++){
// for(int j=0;j<8;j++){
// if(r.nextInt(3)==0) board[i][j]='X';
// else if(r.nextInt(3)==1) board[i][j]='O';
// else board[i][j]='_';
// }
// }
//visual test board
// board=new char[][]{ {'_','_','_','_','_','_','_','_'},
// {'_','_','_','_','_','_','_','_'},
// {'_','O','_','_','X','_','_','_'},
// {'_','O','_','_','X','_','_','_'},
// {'_','_','_','_','_','_','_','_'},
// {'_','_','_','_','_','_','_','_'},
// {'_','_','_','_','_','_','_','_'},
// {'_','_','_','_','_','_','_','_'}};
}
public Board(Board b){
board = new char[8][8];
movesLeft = b.getMovesLeft();
//populate empty board
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
board[i][j] = b.getCell(i,j);
}
}
turn = b.getTurn();
}
public int getTurn(){
return turn;
}
public void setTurn(int c){
turn = c;
}
// public Board(char[][] b){
// public Board(Board copy){
// board = new char[8][8];
//
// //populate empty board
// for(int i=0;i<board.length;i++){
// for(int j=0;j<board[i].length;j++){
// board[i][j] = copy[i][j];
// }
// }
// movesLeft = copy.getMovesLeft();
// }
// copy constructor
public void copy(Board copy) {
board = new char[8][8];
//populate empty board
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
board[i][j] = copy.getCell(i,j);
}
}
movesLeft = copy.getMovesLeft();
turn = copy.getTurn();
}
public int getMovesLeft(){
return movesLeft;
}
private void decrementMoveCount(){
movesLeft--;
}
public boolean isEmpty(){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){
if(board[i][j]!='_')
return false;
}
}
return true;
}
public boolean getTerminalState(){
if(checkFours('X') || checkFours('O') || movesLeft == 0)
return true;
else
return false;
}
public int evaluate(char c){
//TODO: Daniel please write a kick-ass eval function here
//Given a board, call it's eval function to give a rating on its desirability
char enemy;
if(c=='X') enemy = 'O';
else enemy = 'X';
boolean fours = checkFours(c);
if(fours) return 100000000;
boolean enemyFours = checkFours(enemy);
if(enemyFours) return -100000000;
int threes = checkThrees(c) * 100000;
int twos = checkTwos(c) * 10;
int enemyThrees = checkThrees(enemy) * 100000;
int enemyTwos = checkTwos(enemy) * 10;
// System.out.println(threes+" "+twos+" "+enemyThrees+" "+enemyTwos);
int turnScore = 0;
if(turn==0 && c=='X'){
turnScore = -1;
}
if(turn==1 && c=='O'){
turnScore = -1;
}
return threes + twos - enemyThrees - enemyTwos + turnScore;
}
private int checkTwos(char c){
// System.out.println("Turn "+c);
int score = 0;
//check row
for(int col=0;col<5;col++){
for(int row=0;row<board.length;row++){
if( (board[row][col]==c && board[row][col]==board[row][col+1] &&
board[row][col+2]=='_' && board[row][col+3]=='_') ||
(board[row][col]==c && board[row][col]==board[row][col+2] &&
board[row][col+1]=='_' && board[row][col+3]=='_') ||
(board[row][col]==c && board[row][col]==board[row][col+3] &&
board[row][col+1]=='_' && board[row][col+2]=='_') ){
if(col>1 && board[row][col-2]=='_' && board[row][col-1]=='_') score+=2;
else score++;
// System.out.println(board[row][col]+" "+board[row][col+1]+" "+board[row][col+2]);
}
if(col<4){
if( (board[row][col+2]==c && board[row][col+2]==board[row][col+3] &&
board[row][col]=='_' && board[row][col+1]=='_') ){
score++;
}
}
if(col==4){
if( (board[row][col+1]==c && board[row][col+1]==board[row][col+2] &&
board[row][col]=='_' && board[row][col+3]=='_') ||
(board[row][col+1]==c && board[row][col+1]==board[row][col+3] &&
board[row][col]=='_' && board[row][col+2]=='_') ||
(board[row][col+2]==c && board[row][col+2]==board[row][col+3] &&
board[row][col]=='_' && board[row][col+1]=='_') ){
score++;
// System.out.println(board[row][col]+" "+board[row][col+1]+" "+board[row][col+2]+" "+board[row][col+3]);
}
}
}
}
//check columns
for(int row=0;row<5;row++){
for(int col=0;col<board.length;col++){
if( (board[row][col]==c && board[row][col]==board[row+1][col] &&
board[row+2][col]=='_' && board[row+3][col]=='_') ||
(board[row][col]==c && board[row][col]==board[row+2][col] &&
board[row+1][col]=='_' && board[row+3][col]=='_') ||
(board[row][col]==c && board[row][col]==board[row+3][col] &&
board[row+1][col]=='_' && board[row+2][col]=='_') ){
// System.out.println(board[row][col]+" "+board[row+1][col]+" "+board[row+2][col]+" "+board[row+3][col]);
if(row>1 && board[row-2][col]=='_' && board[row-1][col]=='_'){
// System.out.println("YES "+board[row-2][col]+" "+board[row-1][col]+" "+board[row][col]+" "+board[row+1][col]+" "+board[row+2][col]+" "+board[row+3][col]);
score+=2;
}
else score++;
}
if(row<4){
if( (board[row+2][col]==c && board[row+2][col]==board[row+3][col] &&
board[row][col]=='_' && board[row+1][col]=='_') ){
score++;
}
}
if(row==4){
if( (board[row+1][col]==c && board[row+1][col]==board[row+2][col] &&
board[row][col]=='_' && board[row+3][col]=='_') ||
(board[row+1][col]==c && board[row+1][col]==board[row+3][col] &&
board[row][col]=='_' && board[row][col]=='_') ||
(board[row+2][col]==c && board[row+2][col]==board[row+3][col] &&
board[row][col]=='_' && board[row+1][col]=='_') ){
score++;
}
}
}
}
// System.out.println(score+" twos");
return score;
}
private int checkThrees(char c){
int score = 0;
//check rows
for(int col=0;col<5;col++){
for(int row=0;row<board.length;row++){
if( (board[row][col]==c && board[row][col]==board[row][col+1] &&
board[row][col+1]==board[row][col+2] && board[row][col+3]=='_') ||
(board[row][col]==c && board[row][col]==board[row][col+1] &&
board[row][col+1]==board[row][col+3] && board[row][col+2]=='_') ||
(board[row][col]==c && board[row][col]==board[row][col+2] &&
board[row][col+2]==board[row][col+3] && board[row][col+1]=='_') ){
// System.out.println(board[row][col]+" "+board[row][col+1]+" "+board[row][col+2]+" "+board[row][col+3]);
if(col>0 && board[row][col-1]=='_') score+=2;
else score++;
}
if(col==4){
if(board[row][col+1]==c && board[row][col+1]==board[row][col+2] &&
board[row][col+2]==board[row][col+3] && board[row][col]=='_'){
score++;
}
}
}
}
//check columns
for(int row=0;row<5;row++){
for(int col=0;col<board.length;col++){
if( (board[row][col]==c && board[row][col]==board[row+1][col] &&
board[row+1][col]==board[row+2][col] && board[row+3][col]=='_') ||
(board[row][col]==c && board[row][col]==board[row+1][col] &&
board[row+1][col]==board[row+3][col] && board[row+2][col]=='_') ||
(board[row][col]==c && board[row][col]==board[row+2][col] &&
board[row+2][col]==board[row+3][col] && board[row+1][col]=='_') ){
if(row>0 && board[row-1][col]=='_') score+=2;
else score++;
}
if(row==4){
if(board[row+1][col]==c && board[row+1][col]==board[row+2][col] &&
board[row+2][col]==board[row+3][col] && board[row][col]=='_'){
score++;
}
}
}
}
// System.out.println(score+" threes");
return score;
}
private boolean checkFours(char c){
//check for rows for victory, check 5 positions across 8 columns
for(int col=0;col<5;col++){
for(int row=0;row<board.length;row++){
if(board[row][col]==board[row][col+1] &&
board[row][col+1]==board[row][col+2] &&
board[row][col+2]==board[row][col+3]){
if(board[row][col]==c){
// System.out.println("ROW: row "+(char)('@'+row+1)+" col "+(col+1));
return true;
}
}
}
}
//check columns for victory, check 5 positions across 8 rows
for(int row=0;row<5;row++){
for(int col=0;col<board.length;col++){
if(board[row][col]==board[row+1][col] &&
board[row+1][col]==board[row+2][col] &&
board[row+2][col]==board[row+3][col]){
if(board[row][col]==c){
// System.out.println("COLUMN: row "+(char)('@'+row+1)+" col "+(col+1));
return true;
}
}
}
}
return false;
}
public char getCell(int x, int y) {
return board[x][y];
}
public void updateBoard(Point newPosition, char value){
board[newPosition.x][newPosition.y] = value;
decrementMoveCount();
if(value=='X') turn = 0;
else turn = 1;
}
public boolean tryMove(String move, char c){
if (((int)move.charAt(0) > 96) && ((int)move.charAt(0) < 105) && (move.charAt(1) >= 0) && (move.charAt(1) - '0' < 9)){//if the row/col is valid (input: a-h,1-8)
if (getCell(((int)move.charAt(0)) -97,move.charAt(1) - '0' -1) == '_'){//the move is an empty tile
//If valid move, update the board
Point validMove = new Point(((int)move.charAt(0)-97),(move.charAt(1)-'0'-1));
updateBoard(validMove, c);
return true;
}
else{
System.out.println("This is not an empty space. Try again.");
return false;
}
}
else{
System.out.println("Error: Move out of bounds. Try again. " + (int)move.charAt(0) + " " + move.charAt(1));
return false;
}
}
public boolean tryMoveAI(Point p, char c){
if ((p.x>=0 && p.x<8) && (p.y>=0 && p.y<8)){//if the row/col is valid
if (getCell(p.x,p.y) == '_'){//the move is an empty tile
//If valid move, update the board
updateBoard(p, c);
return true;
}
else{
return false;
}
}
else{
return false;
}
}
public void print(){
System.out.println("test");
for(int row=0;row<9;row++){//rowumn
for(int col=0;col<9;col++){//col
if(row-1 >= 0 && col-1 >= 0)//if not (1st column and 1st row), print board cells
System.out.print(board[row-1][col-1]+" ");
else{//1st row or 1st col
if(row==0 && col==0){//cell[0][0]
System.out.print(" ");
}else if(row==0){//first row, prints numbers
System.out.print(col+" ");
}else if(col==0){//first column prints letters
System.out.print((char)('@'+row)+" ");
}else;
}
}System.out.println();
}System.out.println();
}
public void printBoard(int startPlayer,List<String> moveRecord){
for(int row=0;row<9;row++){//rowumn
for(int col=0;col<9;col++){//col
if(row-1 >= 0 && col-1 >= 0)//if not (1st column and 1st row), print board cells
System.out.print(board[row-1][col-1]+" ");
else{//1st row or 1st col
if(row==0 && col==0){//cell[0][0]
System.out.print(" ");
}else if(row==0){//first row, prints numbers
System.out.print(col+" ");
}else if(col==0){//first column prints letters
System.out.print((char)('@'+row)+" ");
}else;
}
}System.out.println();
}System.out.println();
//print record of moves
if(startPlayer==0){
System.out.println("Player vs Opponent");
}else{
System.out.println("Opponent vs Player");
}
for(int i=0;i<moveRecord.size();i++){
//round of turns from both players
if(i%2==0){
//print new line after every 2 moves
if(i!=0) System.out.println();
//print round number
System.out.print(((i/2)+1)+". ");
}
System.out.print(moveRecord.get(i)+" ");
}System.out.println();
}
}