-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphabeta.cpp
35 lines (26 loc) · 1.05 KB
/
alphabeta.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
#include "include/alphabeta.hpp"
#include <algorithm>
#include <iostream>
#include <limits>
std::pair<Action, int> negamax(SearchableGame::Game game, int depth, double alpha, double beta) {
if (game->isTerminal() || depth == 0)
return std::pair<Action, double>(nullptr, game->utility());
std::pair<Action, double> best(nullptr, -std::numeric_limits<double>::infinity());
for (auto const& move : game->legalActions()) {
SearchableGame::Game imagine = game->clone();
imagine->makeMove(move);
std::pair<Action, double> result = negamax(imagine, depth-1, -beta, -alpha);
if (result.second*-1 > best.second) {
best.first = move;
best.second = result.second*-1;
}
alpha = std::max(alpha, best.second);
if (alpha >= beta)
break;
}
return best;
}
Action alphabeta(SearchableGame::Game game, int depth){
double inifiniy = std::numeric_limits<double>::infinity();
return negamax(game, depth, -inifiniy, inifiniy).first;
}