From 668b0ed30cd45426fa29ec9a43054f980477a83f Mon Sep 17 00:00:00 2001 From: Mark Bluemer Date: Tue, 18 Apr 2017 21:10:49 -0400 Subject: [PATCH] Basic minimax search implemented, not tested --- include/BitBoard.h | 1 + include/MinimaxSearch.h | 21 +++++++++++++++++ src/BitBoard.cpp | 6 +++++ src/MinimaxSearch.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 include/MinimaxSearch.h create mode 100644 src/MinimaxSearch.cpp diff --git a/include/BitBoard.h b/include/BitBoard.h index f360b92..42290be 100644 --- a/include/BitBoard.h +++ b/include/BitBoard.h @@ -90,6 +90,7 @@ class BitBoard std::string const player(); std::vector const actions(); BitBoard const result(Move move); + double utility(std::string player); void const printState(); }; diff --git a/include/MinimaxSearch.h b/include/MinimaxSearch.h new file mode 100644 index 0000000..053d588 --- /dev/null +++ b/include/MinimaxSearch.h @@ -0,0 +1,21 @@ +#ifndef MINIMAX_SEARCH_H +#define MINIMAX_SEARCH_H + +#include +#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 diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index 199e3cb..1de01ae 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -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<<"-"< +#include +#include +#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::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::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; +} +