-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSearcher.java
More file actions
executable file
·50 lines (44 loc) · 1.47 KB
/
SimpleSearcher.java
File metadata and controls
executable file
·50 lines (44 loc) · 1.47 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
package chess.bots;
import java.util.List;
import cse332.chess.interfaces.AbstractSearcher;
import cse332.chess.interfaces.Board;
import cse332.chess.interfaces.Evaluator;
import cse332.chess.interfaces.Move;
import cse332.exceptions.NotYetImplementedException;
/**
* This class should implement the minimax algorithm as described in the
* assignment handouts.
*/
public class SimpleSearcher<M extends Move<M>, B extends Board<M, B>> extends
AbstractSearcher<M, B> {
public M getBestMove(B board, int myTime, int opTime) {
/* Calculate the best move */
BestMove<M> best = minimax(this.evaluator, board, ply);
return best.move;
}
static <M extends Move<M>, B extends Board<M, B>> BestMove<M> minimax(Evaluator<B> evaluator, B board, int depth) {
if (depth == 0) {
return new BestMove<M>(evaluator.eval(board));
}
BestMove<M> bestMove = new BestMove<M>(Integer.MIN_VALUE);
List<M> moves = board.generateMoves();
if (moves.isEmpty()) {
if (board.inCheck()) {
bestMove.value = -evaluator.mate() - depth;
} else {
bestMove.value = -evaluator.stalemate();
}
return bestMove;
}
for (M move : moves) {
board.applyMove(move);
BestMove<M> currMove = minimax(evaluator, board, depth - 1).negate();
currMove.move = move;
board.undoMove();
if (currMove.value > bestMove.value) {
bestMove = currMove;
}
}
return bestMove;
}
}