-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.java
More file actions
151 lines (120 loc) · 3.92 KB
/
Agent.java
File metadata and controls
151 lines (120 loc) · 3.92 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
import java.util.List;
public class Agent {
Board board;
boolean isAgentWhite;
int depth;
Agent(Board board, boolean white, int depth) {
this.isAgentWhite = white;
this.board = board;
this.depth = depth;
}
// white is always positive
private int max(int depth, int alpha, int beta) {
if (board.inCheckmate(true)) {
return Integer.MIN_VALUE;
}
if (board.inCheckmate(false)) {
return Integer.MAX_VALUE;
}
if (depth == 0) {
return board.evaluate();
}
boolean inCheck = this.board.inCheck(true);
List<Move> allMoves;
if (inCheck) {
allMoves = board.getCheckBreakingMoves(true);
} else {
allMoves = this.board.getAllMoves(true);
}
int maxEval = Integer.MIN_VALUE;
for (Move move : allMoves) {
this.board.makeMove(move);
if (this.board.inCheck(true)) {
this.board.unmakeMove(move);
continue;
}
int eval = min(depth - 1, alpha, beta);
maxEval = Math.max(eval, maxEval);
this.board.unmakeMove(move);
if (maxEval >= beta) {
break;
}
alpha = Math.max(alpha, maxEval);
}
return maxEval;
}
// black is always negative
private int min(int depth, int alpha, int beta) {
if (board.inCheckmate(false)) {
return Integer.MAX_VALUE;
}
if (board.inCheckmate(true)) {
return Integer.MIN_VALUE;
}
if (depth == 0) {
return board.evaluate();
}
boolean inCheck = this.board.inCheck(false);
List<Move> allMoves;
if (inCheck) {
allMoves = board.getCheckBreakingMoves(false);
} else {
allMoves = this.board.getAllMoves(false);
}
int minEval = Integer.MAX_VALUE;
for (Move move : allMoves) {
this.board.makeMove(move);
if (this.board.inCheck(false)) {
this.board.unmakeMove(move);
continue;
}
int eval = max(depth - 1, alpha, beta);
minEval = Math.min(eval, minEval);
this.board.unmakeMove(move);
if (minEval <= alpha) {
break;
}
beta = Math.min(beta, minEval);
}
return minEval;
}
private Move findBestMove() {
Move bestMove = null;
int bestEval = isAgentWhite ? Integer.MIN_VALUE : Integer.MAX_VALUE;
boolean inCheck = this.board.inCheck(isAgentWhite);
List<Move> allMoves;
if (inCheck) {
allMoves = board.getCheckBreakingMoves(this.isAgentWhite);
} else {
allMoves = this.board.getAllMoves(this.isAgentWhite);
}
for (Move move : allMoves) {
this.board.makeMove(move);
// don't consider moves that put agent in check
if (this.board.inCheck(isAgentWhite)) {
this.board.unmakeMove(move);
continue;
}
int eval = isAgentWhite ? min(this.depth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE) : max(this.depth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
if (isAgentWhite && eval > bestEval) {
bestEval = eval;
bestMove = move;
}
if (!isAgentWhite && eval < bestEval) {
bestEval = eval;
bestMove = move;
}
this.board.unmakeMove(move);
}
return bestMove;
}
public boolean makeBestMove() {
Move move = findBestMove();
if (move == null) {
return false;
}
this.board.makeMove(move);
this.board.setLastMoveOrigin(move.getOrigin());
return true;
}
}