-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLiteMoveThread.java
More file actions
48 lines (41 loc) · 1.65 KB
/
LiteMoveThread.java
File metadata and controls
48 lines (41 loc) · 1.65 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
import java.util.concurrent.Callable;
/**
* The LookAheadMoveThread objects are what is launched for each move the current player has.
* They implement the minimax algorithm as well as the alpha-beta pruning. They have timeouts
* and will return the best move they've seen so far if it expires.
*/
public class LiteMoveThread implements Callable<BestMove> {
private final LiteMinimax minimax;
private final LiteBoard origBoard;
private final boolean maximize;
private final LiteBoard board;
private final BestMove best;
private final int depth;
private final Move move;
LiteMoveThread(final LiteBoard orig, final LiteMinimax minimax, boolean maximize, final Move move, int depth) {
this.best = new BestMove(maximize);
this.maximize = maximize;
this.minimax = minimax;
this.origBoard = orig;
this.depth = depth;
this.move = move;
this.board = new LiteBoard(orig);
this.board.executeMove(move);
this.board.advanceTurn();
this.minimax.addNumMovesExamined(1);
}
// This call is made to us when we are launched
@Override
public BestMove call() {
Thread.yield();
int lookAheadVal = minimax.minmax(board, LiteUtil.MIN_VALUE, LiteUtil.MAX_VALUE,
depth - 1, !maximize);
if ((maximize && lookAheadVal >= best.value) || (!maximize && lookAheadVal <= best.value)) {
best.value = lookAheadVal;
best.move = move;
best.move.setValue(best.value);
minimax.cachedMoves.addMoveValue(origBoard.board, maximize, best.move, best.value, 1);
}
return best;
}
}