Final AI tweaks.
[NALCG.git] / src / ais / daniel / aidaniel.cpp
blob642d7e12b8b1678e3263441d2e7fde0a8a9a27bf
1 #include "aidaniel.h"
3 AIDaniel::AIDaniel(Position *p) : position(p), isInterrupted(false), cutoffDepth(DEFAULT_CUTOFF_DEPTH) {
6 inline int AIDaniel::alphaBeta(Position *pos, int depth, int alpha, int beta, bool max) {
7 if (isInterrupted)
9 return 0;
11 if ((depth >= cutoffDepth && pos->getNoisiness() < 50)
12 || depth >= cutoffDepth + QUIESCENCE_SEARCH_MAX_ADDITIONAL_DEPTH
13 || pos->isGameOver()) {
14 return pos->evaluate();
16 if (max) {
17 for (unsigned int i = 0; i < pos->getLegalMoves()->size(); ++i) {
18 Position child(pos, i);
19 beta = std::min(beta, alphaBeta(&child, depth + 1, alpha, beta, !max));
20 if (beta <= alpha) {
21 return alpha;
24 return beta;
26 else {
27 for (unsigned int i = 0; i < pos->getLegalMoves()->size(); ++i) {
28 Position child(pos, i);
29 alpha = std::max(alpha, alphaBeta(&child, depth + 1, alpha, beta, !max));
30 if (beta <= alpha) {
31 return beta;
34 return alpha;
38 int AIDaniel::getNextMove() {
39 isInterrupted = false;
40 std::vector<int> values;
41 int best = 0;
42 if (position->isWhiteToMove()) {
43 for (unsigned int i = 0; i < position->getLegalMoves()->size(); ++i) {
44 Position child(position, i);
45 values.push_back(alphaBeta(&child, 1, std::numeric_limits<int>::min(),std::numeric_limits<int>::max(),true));
46 //std::cout << "white: " << i << " " << values.at(i) << " " << position->getLegalMoves()->at(i) << std::endl;
48 int max = std::numeric_limits<int>::min();
49 for (unsigned int i = 0; i < values.size(); ++i) {
50 if (max < values.at(i)) {
51 max = values.at(i);
52 best = i;
56 else {
57 for (unsigned int i = 0; i < position->getLegalMoves()->size(); ++i) {
58 Position child(position, i);
59 values.push_back(alphaBeta(&child, 1, std::numeric_limits<int>::min(),std::numeric_limits<int>::max(),false));
60 //std::cout << "black: " << i << " " << values.at(i) << " " << position->getLegalMoves()->at(i) << std::endl;
62 int min = std::numeric_limits<int>::max();
63 for (unsigned int i = 0; i < values.size(); ++i) {
64 if (min > values.at(i)) {
65 min = values.at(i);
66 best = i;
70 bestMove = position->getLegalMoves()->at(best);
71 position->move(bestMove, 0);
72 return bestMove;
75 void AIDaniel::setPosition(Position *p) {
76 delete position;
77 position = p;
80 void AIDaniel::interrupt()
82 isInterrupted = true;
85 void AIDaniel::setCutoffDepth(int cutoffDepth)
87 this->cutoffDepth = cutoffDepth;
90 AIDaniel::~AIDaniel() {
91 delete position;