-
Notifications
You must be signed in to change notification settings - Fork 6
/
Connect4AI.java
365 lines (308 loc) · 12.3 KB
/
Connect4AI.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
import java.util.Scanner;
class Board{
byte[][] board = new byte[6][7];
public Board(){
board = new byte[][]{
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
};
}
public boolean isLegalMove(int column){
return board[0][column]==0;
}
//Placing a Move on the board
public boolean placeMove(int column, int player){
if(!isLegalMove(column)) {System.out.println("Illegal move!"); return false;}
for(int i=5;i>=0;--i){
if(board[i][column] == 0) {
board[i][column] = (byte)player;
return true;
}
}
return false;
}
public void undoMove(int column){
for(int i=0;i<=5;++i){
if(board[i][column] != 0) {
board[i][column] = 0;
break;
}
}
}
//Printing the board
public void displayBoard(){
System.out.println();
for(int i=0;i<=5;++i){
for(int j=0;j<=6;++j){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}
public class Connect4AI {
private Board b;
private Scanner scan;
private int nextMoveLocation=-1;
private int maxDepth = 9;
public Connect4AI(Board b){
this.b = b;
scan = new Scanner(System.in);
}
//Opponent's turn
public void letOpponentMove(){
System.out.println("Your move (1-7): ");
int move = scan.nextInt();
while(move<1 || move > 7 || !b.isLegalMove(move-1)){
System.out.println("Invalid move.\n\nYour move (1-7): ");
move = scan.nextInt();
}
//Assume 2 is the opponent
b.placeMove(move-1, (byte)2);
}
//Game Result
public int gameResult(Board b){
int aiScore = 0, humanScore = 0;
for(int i=5;i>=0;--i){
for(int j=0;j<=6;++j){
if(b.board[i][j]==0) continue;
//Checking cells to the right
if(j<=3){
for(int k=0;k<4;++k){
if(b.board[i][j+k]==1) aiScore++;
else if(b.board[i][j+k]==2) humanScore++;
else break;
}
if(aiScore==4)return 1; else if (humanScore==4)return 2;
aiScore = 0; humanScore = 0;
}
//Checking cells up
if(i>=3){
for(int k=0;k<4;++k){
if(b.board[i-k][j]==1) aiScore++;
else if(b.board[i-k][j]==2) humanScore++;
else break;
}
if(aiScore==4)return 1; else if (humanScore==4)return 2;
aiScore = 0; humanScore = 0;
}
//Checking diagonal up-right
if(j<=3 && i>= 3){
for(int k=0;k<4;++k){
if(b.board[i-k][j+k]==1) aiScore++;
else if(b.board[i-k][j+k]==2) humanScore++;
else break;
}
if(aiScore==4)return 1; else if (humanScore==4)return 2;
aiScore = 0; humanScore = 0;
}
//Checking diagonal up-left
if(j>=3 && i>=3){
for(int k=0;k<4;++k){
if(b.board[i-k][j-k]==1) aiScore++;
else if(b.board[i-k][j-k]==2) humanScore++;
else break;
}
if(aiScore==4)return 1; else if (humanScore==4)return 2;
aiScore = 0; humanScore = 0;
}
}
}
for(int j=0;j<7;++j){
//Game has not ended yet
if(b.board[0][j]==0)return -1;
}
//Game draw!
return 0;
}
int calculateScore(int aiScore, int moreMoves){
int moveScore = 4 - moreMoves;
if(aiScore==0)return 0;
else if(aiScore==1)return 1*moveScore;
else if(aiScore==2)return 10*moveScore;
else if(aiScore==3)return 100*moveScore;
else return 1000;
}
//Evaluate board favorableness for AI
public int evaluateBoard(Board b){
int aiScore=1;
int score=0;
int blanks = 0;
int k=0, moreMoves=0;
for(int i=5;i>=0;--i){
for(int j=0;j<=6;++j){
if(b.board[i][j]==0 || b.board[i][j]==2) continue;
if(j<=3){
for(k=1;k<4;++k){
if(b.board[i][j+k]==1)aiScore++;
else if(b.board[i][j+k]==2){aiScore=0;blanks = 0;break;}
else blanks++;
}
moreMoves = 0;
if(blanks>0)
for(int c=1;c<4;++c){
int column = j+c;
for(int m=i; m<= 5;m++){
if(b.board[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);
aiScore=1;
blanks = 0;
}
if(i>=3){
for(k=1;k<4;++k){
if(b.board[i-k][j]==1)aiScore++;
else if(b.board[i-k][j]==2){aiScore=0;break;}
}
moreMoves = 0;
if(aiScore>0){
int column = j;
for(int m=i-k+1; m<=i-1;m++){
if(b.board[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);
aiScore=1;
blanks = 0;
}
if(j>=3){
for(k=1;k<4;++k){
if(b.board[i][j-k]==1)aiScore++;
else if(b.board[i][j-k]==2){aiScore=0; blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0)
for(int c=1;c<4;++c){
int column = j- c;
for(int m=i; m<= 5;m++){
if(b.board[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);
aiScore=1;
blanks = 0;
}
if(j<=3 && i>=3){
for(k=1;k<4;++k){
if(b.board[i-k][j+k]==1)aiScore++;
else if(b.board[i-k][j+k]==2){aiScore=0;blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0){
for(int c=1;c<4;++c){
int column = j+c, row = i-c;
for(int m=row;m<=5;++m){
if(b.board[m][column]==0)moreMoves++;
else if(b.board[m][column]==1);
else break;
}
}
if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);
aiScore=1;
blanks = 0;
}
}
if(i>=3 && j>=3){
for(k=1;k<4;++k){
if(b.board[i-k][j-k]==1)aiScore++;
else if(b.board[i-k][j-k]==2){aiScore=0;blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0){
for(int c=1;c<4;++c){
int column = j-c, row = i-c;
for(int m=row;m<=5;++m){
if(b.board[m][column]==0)moreMoves++;
else if(b.board[m][column]==1);
else break;
}
}
if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);
aiScore=1;
blanks = 0;
}
}
}
}
return score;
}
public int minimax(int depth, int turn, int alpha, int beta){
if(beta<=alpha){if(turn == 1) return Integer.MAX_VALUE; else return Integer.MIN_VALUE; }
int gameResult = gameResult(b);
if(gameResult==1)return Integer.MAX_VALUE/2;
else if(gameResult==2)return Integer.MIN_VALUE/2;
else if(gameResult==0)return 0;
if(depth==maxDepth)return evaluateBoard(b);
int maxScore=Integer.MIN_VALUE, minScore = Integer.MAX_VALUE;
for(int j=0;j<=6;++j){
int currentScore = 0;
if(!b.isLegalMove(j)) continue;
if(turn==1){
b.placeMove(j, 1);
currentScore = minimax(depth+1, 2, alpha, beta);
if(depth==0){
System.out.println("Score for location "+j+" = "+currentScore);
if(currentScore > maxScore)nextMoveLocation = j;
if(currentScore == Integer.MAX_VALUE/2){b.undoMove(j);break;}
}
maxScore = Math.max(currentScore, maxScore);
alpha = Math.max(currentScore, alpha);
}
else if(turn==2){
b.placeMove(j, 2);
currentScore = minimax(depth+1, 1, alpha, beta);
minScore = Math.min(currentScore, minScore);
beta = Math.min(currentScore, beta);
}
b.undoMove(j);
if(currentScore == Integer.MAX_VALUE || currentScore == Integer.MIN_VALUE) break;
}
return turn==1?maxScore:minScore;
}
public int getAIMove(){
nextMoveLocation = -1;
minimax(0, 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
return nextMoveLocation;
}
public void playAgainstAIConsole(){
int humanMove=-1;
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to play first? (yes/no) ");
String answer = scan.next().trim();
if(answer.equalsIgnoreCase("yes")) letOpponentMove();
b.displayBoard();
b.placeMove(3, 1);
b.displayBoard();
while(true){
letOpponentMove();
b.displayBoard();
int gameResult = gameResult(b);
if(gameResult==1){System.out.println("AI Wins!");break;}
else if(gameResult==2){System.out.println("You Win!");break;}
else if(gameResult==0){System.out.println("Draw!");break;}
b.placeMove(getAIMove(), 1);
b.displayBoard();
gameResult = gameResult(b);
if(gameResult==1){System.out.println("AI Wins!");break;}
else if(gameResult==2){System.out.println("You Win!");break;}
else if(gameResult==0){System.out.println("Draw!");break;}
}
}
public static void main(String[] args) {
Board b = new Board();
Connect4AI ai = new Connect4AI(b);
ai.playAgainstAIConsole();
}
}