-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphaBeta.cpp
More file actions
67 lines (62 loc) · 1.79 KB
/
Copy pathAlphaBeta.cpp
File metadata and controls
67 lines (62 loc) · 1.79 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
#include "AlphaBeta.h"
MoveInfo AlphaBeta::search(int depth){
moveHistory = {};
alpha = -INF;
beta = INF;
double max = -INF;
MoveInfo bestMove;
GameState childGameState = rootGameState;
for (auto& move : rootGameState.generateAllMoves()){
childGameState.replayMove(move);
double result = minimax(childGameState, depth , 1, move);
if (result >= max){
max = result;
bestMove = move;
}
childGameState.undoMove(move);
}
return bestMove;
}
double AlphaBeta::minimax(GameState& childGameState, int depth, bool maximizingPlayer,
MoveInfo lastMove){
cout << "===========" << depth << "==========" << endl;
if (childGameState.checkDraw()){
return 0;
}
if (childGameState.checkVictory() != PieceColor::NONE) {
if (maximizingPlayer) return INF;
return -INF;
}
if (depth == 0){
double value = 0;
for (auto i : heuristic.evaluate(lastMove)) value += i;
return value;
}
if (maximizingPlayer){
double value = -INF;
for (auto move : childGameState.generateAllMoves()){
//TODO: if about to recalulate move
if (depth == 1) heuristic.setGameState(childGameState);
childGameState.replayMove(move);
value = std::max(value, minimax(childGameState, depth - 1, !maximizingPlayer, move));
alpha = max(alpha, value);
if (alpha >= beta) break;
childGameState.undoMove(move);
}
return value;
} else {
double value = INF;
for (auto move : childGameState.generateAllMoves()){
//TODO: if about to recalulate move
if (depth == 1) heuristic.setGameState(childGameState);
childGameState.replayMove(move);
value = std::min(value,
minimax(childGameState, depth - 1, !maximizingPlayer, move));
beta = min(beta, value);
if (alpha >= beta) break;
cout << move.toString(" MOVE ") << endl;
childGameState.undoMove(move);
}
return value;
}
}