-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAiPlayer.java
119 lines (105 loc) · 3.08 KB
/
AiPlayer.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
/*H*************************************************************************************
* FILENAME : AiPlayer.java
*
* DESCRIPTION :
* Determines which move to make
*
* PUBLIC FUNCTIONS :
* int findBestPlay( GameBoard, int, int )
* Move minimax( GameBoard, final int, int, final boolean, int, int, int )
* int eval( GameBoard, int )
*
* NOTES :
* These functions use minimax and an
* evaluation table to choose a move.
*
* Copyright 2019, Jacob Wilkins. All rights reserved.
*
* AUTHOR : Jacob Wilkins START DATE : 4 Mar 19
*
*H*/
import java.util.*;
import static java.lang.Integer.*;
public class AiPlayer {
public AiPlayer() {}
public int findBestPlay(GameBoard currentGame, int maxDepth, int computerNum) {
Move playChoice;
Random randy = new Random();
int MAX = MAX_VALUE;
int MIN = MIN_VALUE;
playChoice = minimax(currentGame, 0, maxDepth, true, MIN, MAX, computerNum);
while (!currentGame.isValidPlay(playChoice.getMove())) {
playChoice.move = randy.nextInt(7);
}
return playChoice.getMove();
}
// Find best move
private Move minimax(GameBoard currentGame, final int depth, int maxDepth, final boolean player, int alpha, int beta, int computerNum) {
final Move best = new Move();
Move reply;
// Base case
if (depth == maxDepth) {
final Move fakeBest = new Move();
fakeBest.setScore(eval(currentGame, computerNum));
return fakeBest;
}
if (player) {
best.setScore(alpha);
} else {
best.setScore(beta);
}
// Make ArrayList of valid moves to consider
ArrayList<Integer> moves = new ArrayList<Integer>();
for (int i = 0; i < 7; i++) {
if (currentGame.isValidPlay(i)) {
moves.add(i);
}
}
for (int move = 0; move < moves.size(); move++) {
currentGame.playPiece(moves.get(move));
reply = minimax(currentGame, depth + 1, maxDepth, !player, alpha, beta, computerNum);
currentGame.removePiece(moves.get(move));
if (player && reply.getScore() > best.getScore()) {
best.setMove(moves.get(move));
best.setScore(reply.getScore());
alpha = reply.getScore();
} else if (!player && reply.getScore() < best.getScore()) {
best.setMove(moves.get(move));
best.setScore(reply.getScore());
beta = reply.getScore();
}
if (alpha >= beta) {
return best;
}
}
return best;
}
// Calculate score of game board
private int eval(GameBoard currentGame, int computerNum) {
int totScore = currentGame.getScore(computerNum);
int opponentNum;
final int[][] evaluationTable = new int[][] {
{3, 4, 5, 7, 5, 4, 3},
{4, 6, 8, 10, 8, 6, 4},
{5, 8, 11, 13, 11, 8, 5},
{5, 8, 11, 13, 11, 8, 5},
{4, 6, 8, 10, 8, 6, 4},
{3, 4, 5, 7, 5, 4, 3}
};
if (computerNum == 1) {
opponentNum = 2;
} else {
opponentNum = 1;
}
for (int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
if (currentGame.playBoard[i][j] == computerNum) {
totScore += evaluationTable[i][j];
} else if (currentGame.playBoard[i][j] == opponentNum) {
totScore -= evaluationTable[i][j];
}
}
}
return totScore;
}
}