Skip to content

Commit

Permalink
Added alpha-beta... still getting seg faults
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 20, 2017
1 parent 9494508 commit aa32d81
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
class MinimaxSearch
{
private:
static const int maxDepth = 7;
static const int maxDepth = 6;

double minValue(std::unique_ptr<BitBoard> &board, int currentDepth) const;
double maxValue(std::unique_ptr<BitBoard> &board, int currentDepth) const;
double alphaBeta(std::unique_ptr<BitBoard> &board, double alpha, double beta, int depth) const;


public:
Expand Down
31 changes: 29 additions & 2 deletions src/MinimaxSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ double MinimaxSearch::maxValue(bup &board, int currentDepth) const
return v;
}

//
double MinimaxSearch::alphaBeta(bup &board, double alpha, double beta, int depth) const
{
if (depth == maxDepth) return board->utility(board->player());

// black is max player here
double v;
if (board->player() == "black") {
v = std::numeric_limits<double>::lowest();
for (Move move : board->actions()) {
bup result = board->result(move);
v = std::max(v, alphaBeta(result, alpha, beta, depth + 1));
if (v >= beta) return v;
alpha = std::max(alpha, v);
}
} else {
v = std::numeric_limits<double>::max();
for (Move move : board->actions()) {
bup result = board->result(move);
v = std::min(v, alphaBeta(result, alpha, beta, depth + 1));
if (v <= alpha) return v;
beta = std::max(beta, v);
}
}
return v;
}
/* Public Functions */

Move MinimaxSearch::minimaxDecision(bup &board) const
Expand All @@ -43,10 +69,10 @@ Move MinimaxSearch::minimaxDecision(bup &board) const
if (bestMove.isEmpty()) {
bestMove = move;
bup result = board->result(bestMove);
bestMoveValue = minValue(result, 0);
bestMoveValue = alphaBeta(result, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max(), 0);
} else {
bup result = board->result(move);
double nextMoveValue = minValue(result, 0);
double nextMoveValue = alphaBeta(result, std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max(), 0);
if (bestMoveValue < nextMoveValue) {
bestMove = move;
bestMoveValue = nextMoveValue;
Expand All @@ -56,3 +82,4 @@ Move MinimaxSearch::minimaxDecision(bup &board) const

return bestMove;
}

2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, const char * argv[]) {
std::unique_ptr<BitBoard> board (new BitBoard);
MinimaxSearch searchDriver;

for (int i = 0; i < 20; ++i) {
for (int i = 0; i < 40; ++i) {
board->printState();
Move bestMove = searchDriver.minimaxDecision(board);
board = board->result(bestMove);
Expand Down

0 comments on commit aa32d81

Please sign in to comment.