-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.java
337 lines (285 loc) · 10.3 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
import java.util.ArrayList;
public class Board {
//All the necessary variables for our game
public static final int W = -1;
public static final int B = 1;
public static final int EMPTY = 0;
private Move lastMove;
private int lastDiscPlayed;
private int [][] gameboard;
private static final int[] OFFSET_MOVE_ROW = {-1, -1, -1, 0, 0, 1, 1, 1}; // used to move in all 8 directions around the board
private static final int[] OFFSET_MOVE_COL = {-1, 0, 1, -1, 1, -1, 0, 1}; // to validate and effect the moves
private int blackdisks,whitedisks;
private String letters=" 0 1 2 3 4 5 6 7";
private int avMoves;
private static int[][] BOARD_VALUE = { //stores the board values according to the game rules and strategies
{100, -1, 5, 3, 3, 5, -1, 100},
{-1, -10,1, 1, 1, 1,-10, -1},
{5 , 1, 2, 2, 2, 2, 1, 5},
{3 , 1, 2, 0, 0, 2, 1, 3},
{3 , 1, 2, 0, 0, 2, 1, 3},
{5 , 1, 2, 2, 2, 2, 1, 5},
{-1,-10, 1, 1, 1, 1,-10, -1},
{100, -1, 5, 3, 3, 5, -1, 100}};
public Board(){ //Initializes the board with the four default discs (two black and two white) in the middle.
lastMove = new Move();
lastDiscPlayed = W;
gameboard = new int [8][8];
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
gameboard[i][j] = EMPTY;
}
}
this.gameboard[3][3]=W;
this.gameboard[3][4]=B;
this.gameboard[4][3]=B;
this.gameboard[4][4]=W;
}
public Board(Board board){
lastMove = board.lastMove;
lastDiscPlayed = board.lastDiscPlayed;
gameboard = new int [8][8];
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
gameboard[i][j]= board.gameboard[i][j];
}
}
}
public Move getLastMove(){
return lastMove;
}
public int getLastDiscPlayed(){
return lastDiscPlayed;
}
public int [][] getGameboard(){
return gameboard;
}
public void setLastMove(Move lastMove){
this.lastMove.setRow(lastMove.getRow());
this.lastMove.setCol((lastMove.getCol()));
this.lastMove.setValue(lastMove.getValue());
}
public void setLastDiscPlayed(int lastDiscPlayed){
this.lastDiscPlayed = lastDiscPlayed;
}
public void setGameBoard(int[][] gameboard)
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
this.gameboard[i][j] = gameboard[i][j];
}
}
}
//The move is made through the effectMove method. The last move and disc played are updated.
public void makeMove (int row,int col,int disc){
gameboard[row][col] = disc;
effectMove(gameboard,row,col,disc);
lastMove =new Move(row,col);
lastDiscPlayed =disc;
}
// returns the number of available moves of a player for the current turn
public int AvailableMoves(int disc){
avMoves =0;
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
if (isValidMove(i,j,disc))
avMoves++;
}
}
return avMoves;
}
//skips your turn if there are no available moves
public void changePlayer(int disc){
if (AvailableMoves(disc)==0){
lastDiscPlayed = disc;
if(disc==B){
System.out.println("B player has no availabe moves!");
}else {
System.out.println("W player has no other moves!");
}
}
}
//checks if your move is out of bounds
//checking if the move in the board is unavailable
//checking if a move is valid based on the game rules
//OFFSET_MOVE_ROW/COLUMN are used to help us check all 8 directions
public boolean isValidMove(int row,int col,int disc){
if(row<0||row>7||col<0||col>7)
return false;
if (gameboard[row][col] != EMPTY)
return false;
int oppDisc = (disc ==1) ? -1 : 1;
boolean isValid =false;
for (int i=0; i<8; i++){
int currow = row + OFFSET_MOVE_ROW[i];
int curcol = col + OFFSET_MOVE_COL[i];
boolean hasOppDiscBetween = false;
while (currow >=0 && currow < 8 && curcol >= 0 && curcol < 8) {
if (gameboard[currow][curcol]==oppDisc)
hasOppDiscBetween =true;
else if ((gameboard[currow][curcol]==disc) && hasOppDiscBetween){
isValid =true;
break;
}else
break;
currow += OFFSET_MOVE_ROW[i];
curcol += OFFSET_MOVE_COL[i];
}
if (isValid)
break;
}
return isValid;
}
//changes the discs that need to be "flipped" according to the valid move the player has made
//OFFSET_MOVE_ROW/COLUMN again are used to check and flip the necessary discs in all 8 directions
public static int[][] effectMove(int[][] gameboard,int row,int col,int disc){
for (int i=0; i<8; i++){
int currow = row + OFFSET_MOVE_ROW[i];
int curcol = col + OFFSET_MOVE_COL[i];
boolean hasOppDiscBetween = false;
while (currow >=0 && currow < 8 && curcol >= 0 && curcol < 8) {
if (gameboard[currow][curcol]==EMPTY)
break;
if (gameboard[currow][curcol]!=disc)
hasOppDiscBetween =true;
if ((gameboard[currow][curcol] ==disc) && hasOppDiscBetween){
int effectDiscRow = row + OFFSET_MOVE_ROW[i];
int effectDiscCol = col + OFFSET_MOVE_COL[i];
while (effectDiscRow != currow || effectDiscCol != curcol){
gameboard[effectDiscRow][effectDiscCol] = disc;
effectDiscRow += OFFSET_MOVE_ROW[i];
effectDiscCol += OFFSET_MOVE_COL[i];
}
break;
}
currow += OFFSET_MOVE_ROW[i];
curcol += OFFSET_MOVE_COL[i];
}
}
return gameboard;
}
//stores the state/board of all valid moves for the current player's turn in 'children'
//used in GamePlayer class for the Minimax algorithm
public ArrayList <Board> getChildren(int disc){
ArrayList<Board> children = new ArrayList<Board>();
for(int row=0; row<8; row++){
for (int col=0; col<8; col++){
if (isValidMove(row,col,disc)){
Board child = new Board(this);
child.makeMove(row,col,disc); //stin proti sosti kinisi pou vriskei apla tin paizei? what's the point
children.add(child);
}else{
lastDiscPlayed=disc;
}
}
}
return children;
}
//checks if the board has reached a Terminal state, meaning there are no empty squares left
public boolean isTerminal(){
for(int row=0; row<8; row++)
{
for(int col=0; col<8; col++)
{
if(gameboard[row][col] == EMPTY)
{
return false;
}
}
}
return true;
}
//counts the black disks
//used mainly to count the final score
public void CountBlackDisks(){
int counter =0;
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
if (gameboard[i][j]==B){
counter++;
}
}
}
blackdisks = counter;
}
//counts the white disks
//used mainly to count the final score
public void CountWhiteDisks(){
int counter =0;
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
if (gameboard[i][j]==W){
counter++;
}
}
}
whitedisks = counter;
}
public int getBlackDisks() {
CountBlackDisks();
return blackdisks;
}
public int getWhiteDisks() {
CountWhiteDisks();
return whitedisks;
}
//the 2 heuretics used for this game
//their weight of importance can be seen in the return value
//'eval1' attempts to capture the relative difference between the number of possible moves
// for the max and the min players, with the intent of restricting the opponent’s mobility
// and increasing one’s own mobility
//'eval2' counts the board value using 'BOARD_VALUE', which is based on the game rules/strategies
public int evaluate(){
this.CountBlackDisks();
this.CountWhiteDisks();
int eval1 = AvailableMoves(B) - AvailableMoves(W);
int eval2 =0;
for (int r =0; r<8; r++){
for (int c =0; c<8; c++){
if (gameboard[r][c] ==B )
eval2 +=BOARD_VALUE[r][c];
else if (gameboard[r][c] ==W )
eval2 -= BOARD_VALUE[r][c];
}
}
return (30*eval1 + 70*eval2)/100;
}
//basic method to print out the updated board at every turn
public void print()
{
System.out.println("------------------------------------------------------------------");
System.out.println(this.letters);
System.out.println();
for(int row=0; row<8; row++)
{
System.out.print((row)+" ");
for(int col=0; col<8; col++)
{
switch (gameboard[row][col])
{
case B:
System.out.print("B ");
break;
case W:
System.out.print("W ");
break;
case EMPTY:
System.out.print("- ");
break;
default:
break;
}
if(col < 7)
{
System.out.print('\t');
}
}
System.out.println();
}
System.out.println("------------------------------------------------------------------");
}
}