diff --git a/include/BitBoard.h b/include/BitBoard.h index 7f8f290..3d1ffbf 100644 --- a/include/BitBoard.h +++ b/include/BitBoard.h @@ -89,6 +89,7 @@ class BitBoard vmup actions() const; bup result(mup &move) const; void printState() const; + std::string toString() const; }; #endif diff --git a/include/Heuristic.hpp b/include/Heuristic.hpp index 37b43f5..2178dfe 100644 --- a/include/Heuristic.hpp +++ b/include/Heuristic.hpp @@ -6,7 +6,7 @@ class Heuristic { // Feature coefficients - int coeff[10] = {1,1,1,1,1,1,1,1,1,1}; + int coeff[11] = {1,1,1,1,1,1,1,1,1,1,1}; // Feature Functions int pawnCount() const; @@ -29,6 +29,9 @@ class Heuristic public: // Heuristic(int *co[10]) { coeff = *co;}; int evaluate(bup &bitboard); + + // Needed public for variable depth search + int jumpeablePieces() const; }; #endif diff --git a/include/MinimaxSearch.h b/include/MinimaxSearch.h index be8d9ce..b92ffec 100644 --- a/include/MinimaxSearch.h +++ b/include/MinimaxSearch.h @@ -13,12 +13,14 @@ typedef std::unique_ptr mup; class MinimaxSearch { private: - static const int maxDepth = 8; + static const int MIN_DEPTH = 5; + static const int MAX_DEPTH = 9; Heuristic heuristic; double minValue(std::unique_ptr &board, int currentDepth); double maxValue(std::unique_ptr &board, int currentDepth); double alphaBeta(std::unique_ptr &board, double alpha, double beta, int depth); + bool shouldStop(std::unique_ptr &board, int depth); public: diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index 07316ab..ec78925 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -125,18 +125,32 @@ BitBoard BitBoard::boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo) c int diff = std::abs((double)pieceLoc - moveLoc); int avg = ((pieceLoc + moveLoc) / 2); if (board.m_isBlacksTurn) { - if (diff == 2 || diff == 14) white &= ~(1<rotr32(emptyPos, 7) & BB::downRight) & w; + accumP |= (board->rotr32(temp, 7) & BB::downRight) & b; + temp = (board->rotr32(emptyPos, 1) & BB::downLeft) & w; + accumP |= (board->rotr32(temp, 1) & BB::downLeft) & b; + temp = (board->rotl32(emptyPos, 7) & BB::upLeft) & w; + accumP |= (board->rotr32(temp, 7) & BB::upLeft) & kings; + temp = (board->rotl32(emptyPos, 1) & BB::upRight) & w; + accumP |= (board->rotl32(temp, 1) & BB::upRight) & kings; + } else { + temp = (board->rotl32(emptyPos, 7) & BB::upLeft) & b; + accumP |= (board->rotr32(temp, 7) & BB::upLeft) & w; + temp = (board->rotl32(emptyPos, 1) & BB::upRight) & b; + accumP |= (board->rotl32(temp, 1) & BB::upRight) & w; + temp = (board->rotr32(emptyPos, 7) & BB::downRight) & b; + accumP |= (board->rotr32(temp, 7) & BB::downRight) & kings; + temp = (board->rotr32(emptyPos, 1) & BB::downLeft) & b; + accumP |= (board->rotr32(temp, 1) & BB::downLeft) & kings; + } + return (isBlacksTurn ? 1 : -1) * count(accumP); +} + diff --git a/src/MinimaxSearch.cpp b/src/MinimaxSearch.cpp index b2ffda6..8e066d1 100644 --- a/src/MinimaxSearch.cpp +++ b/src/MinimaxSearch.cpp @@ -14,7 +14,7 @@ typedef std::unique_ptr mup; double MinimaxSearch::minValue(bup &board, int currentDepth) { - if (currentDepth == maxDepth) return heuristic.evaluate(board); + if (currentDepth == MAX_DEPTH) return heuristic.evaluate(board); double v = std::numeric_limits::max(); for (mup &move : board->actions()) { bup result = board->result(move); @@ -25,7 +25,7 @@ double MinimaxSearch::minValue(bup &board, int currentDepth) double MinimaxSearch::maxValue(bup &board, int currentDepth) { - if (currentDepth == maxDepth) return heuristic.evaluate(board); + if (currentDepth == MAX_DEPTH) return heuristic.evaluate(board); double v = std::numeric_limits::lowest(); for (mup &move : board->actions()) { bup result = board->result(move); @@ -35,9 +35,9 @@ double MinimaxSearch::maxValue(bup &board, int currentDepth) } double MinimaxSearch::alphaBeta(bup &board, double alpha, double beta, int depth) -{ - if (depth == maxDepth) return heuristic.evaluate(board); - +{ + if (shouldStop(board, depth)) return heuristic.evaluate(board); +// // black is max player here double v; if (board->player() == "black") { @@ -60,6 +60,17 @@ double MinimaxSearch::alphaBeta(bup &board, double alpha, double beta, int depth return v; } + +bool MinimaxSearch::shouldStop(bup &board, int depth) +{ + // always return at some max depth regardless + if (depth >= MAX_DEPTH) return true; + else if (heuristic.jumpeablePieces() != 0) return false; + else if (depth >= MIN_DEPTH) return true; + else return false; + +} + /* Public Functions */ mup MinimaxSearch::minimaxDecision(bup &board)