-
Notifications
You must be signed in to change notification settings - Fork 1
/
RecursiveMinimaxSearch.cpp
122 lines (102 loc) · 3.53 KB
/
RecursiveMinimaxSearch.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//Copyright 2011 Spencer Clark
/*
---------------------------------------------------------------------
This file is part of DeathReversi.
DeathReversi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DeathReversi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DeathReversi. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------
*/
#include "RecursiveMinimaxSearch.h"
#include <QtConcurrentRun>
#include <QList>
#include <QCoreApplication>
#include <QtDebug>
quint16 guiRedrawCounter = 0;
const quint16 GUI_REDRAW_INTERVAL = 8000;
RecursiveMinimaxSearch::RecursiveMinimaxSearch(QSharedPointer<ReversiBoard> rootNodesBoard,quint8 maxDepth) :
rootNodesBoard(rootNodesBoard), maxDepth(maxDepth)
{
BoardPos bestMove = {0,0};
this->bestMove = bestMove;
}
RecursiveMinimaxSearch::~RecursiveMinimaxSearch()
{
}
qint16 RecursiveMinimaxSearch::doSearch()
{
const qint16 estimatedBestScore = this->visit(this->rootNodesBoard,0,-10000,10000);
return estimatedBestScore;
}
//private
qint16 RecursiveMinimaxSearch::visit(QSharedPointer<ReversiBoard> board, quint8 depth, qint16 alpha, qint16 beta)
{
//If we're at maximum depth or an end state, return
if (++depth > this->maxDepth || board->isGameOver())
return board->getScore();
//Hack to let Qt event loop process every once in a while.
//We should move this someplace gui-related.
if (++guiRedrawCounter >= GUI_REDRAW_INTERVAL)
{
guiRedrawCounter = 0;
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
const CELL_STATE whoseTurn = board->getWhoseTurn();
const QList<BoardPos> moves = board->getValidMoves(whoseTurn);
BoardPos bestMove = {0,0};
foreach(const BoardPos move, moves)
{
QSharedPointer<ReversiBoard> simBoard(new ReversiBoard(*board));
simBoard->makeMove(move,whoseTurn);
qint16 score = this->visit(simBoard,depth,alpha,beta);
if (whoseTurn == WHITE_CELL)
{
if (score > alpha)
{
alpha = score;
bestMove = move;
}
else if (score == alpha && qrand() % 2)
bestMove = move;
if (beta <= alpha)
break;
}
else if (whoseTurn == BLACK_CELL)
{
if (score < beta)
{
beta = score;
bestMove = move;
}
else if (score == beta && qrand() % 2)
bestMove = move;
if (beta <= alpha)
break;
}
}
/*
If we've returned from all of our recursion and are back at level 1 of the tree,
then we can say that one move seems better than the others
*/
if (depth == 1)
this->bestMove = bestMove;
/*
Return the score of the board configuration created by optimal-play by both players if the
so-called best move is taken
*/
if (whoseTurn == WHITE_CELL)
return alpha;
else
return beta;
}
BoardPos RecursiveMinimaxSearch::getBestMove() const
{
return this->bestMove;
}