-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.cpp
37 lines (36 loc) · 929 Bytes
/
minimax.cpp
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
#include <algorithm>
#include "minimax.hpp"
#include "eval.hpp"
#include "board.hpp"
std::pair<int, int> minimax(Board& board, int depth, char comp, bool Max, int d, int alpha, int beta) {
int best = -1, bestev = Max?-board.WIDTH*board.HEIGHT:board.WIDTH*board.HEIGHT;
for(int i = 0; i < board.WIDTH && beta > alpha; i++) {
if(board.move(i, Max?comp:(comp=='O'?'X':'O'))) {
int ev = eval(board);
if(ev != 0) {
ev = (Max?1:-1)*board.WIDTH*board.HEIGHT;
} else if(d < depth) {
ev = minimax(board, depth, comp, !Max, d+1, alpha, beta).first;
if(ev != 0)
ev += (ev>0?-1:1);
}
if(Max) {
if(ev >= bestev) {
bestev = ev;
best = i;
if(d > 0)
alpha = std::max(alpha, bestev);
}
} else {
if(ev <= bestev) {
bestev = ev;
best = i;
if(d > 0)
beta = std::min(beta, bestev);
}
}
board.unmove();
}
}
return {best==-1?0:bestev, best};
}