Skip to content

Commit

Permalink
Basic minimax search implemented, not tested
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 19, 2017
1 parent 4a63697 commit 668b0ed
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class BitBoard
std::string const player();
std::vector<Move> const actions();
BitBoard const result(Move move);
double utility(std::string player);
void const printState();
};

Expand Down
21 changes: 21 additions & 0 deletions include/MinimaxSearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef MINIMAX_SEARCH_H
#define MINIMAX_SEARCH_H

#include <string>
#include "Move.h"
#include "BitBoard.h"

class MinimaxSearch
{
private:
static const int maxDepth = 5;

double minValue(BitBoard board, int currentDepth);
double maxValue(BitBoard board, int currentDepth);

public:
Move minimaxDecision(BitBoard &board);

};

#endif
6 changes: 6 additions & 0 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ BitBoard const BitBoard::result(Move move)
return currBoard;
}

double BitBoard::utility(std::string player)
{
// TODO: Do something useful here
return 0.0;
}

void const BitBoard::printState()
{
std::cout<<"-"<<pieceToString(11)<<"-"<<pieceToString(5)<<"-"<<pieceToString(31)<<"-"<<pieceToString(25)<<std::endl;
Expand Down
51 changes: 51 additions & 0 deletions src/MinimaxSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <string>
#include <limits>
#include <algorithm>
#include "BitBoard.h"
#include "Move.h"
#include "MinimaxSearch.h"

/* Private Functions */

double MinimaxSearch::minValue(BitBoard board, int currentDepth)
{
if (currentDepth == maxDepth) return board.utility(board.player());
double v = std::numeric_limits<double>::lowest();
for (Move move : board.actions()) {
v = std::min(v, maxValue(board.result(move), ++currentDepth));
}
return v;
}

double MinimaxSearch::maxValue(BitBoard board, int currentDepth)
{
if (currentDepth == maxDepth) return board.utility(board.player());
double v = std::numeric_limits<double>::max();
for (Move move : board.actions()) {
v = std::max(v, minValue(board.result(move), ++currentDepth));
}
return v;
}

/* Public Functions */

Move MinimaxSearch::minimaxDecision(BitBoard &board)
{
Move bestMove;
double bestMoveValue{0.0};

for (Move move : board.actions()) {
if (bestMove.isEmpty()) {
bestMove = move;
bestMoveValue = minValue(board.result(bestMove), 0);
} else {
double nextMoveValue = minValue(board.result(move), 0);
if (bestMoveValue < nextMoveValue) {
bestMove = move;
bestMoveValue = nextMoveValue;
}
}
}
return bestMove;
}

0 comments on commit 668b0ed

Please sign in to comment.