Skip to content

Commit

Permalink
Some very basic variable depth
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 29, 2017
1 parent 5b2b0ed commit 2108fe2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
5 changes: 4 additions & 1 deletion include/Heuristic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
4 changes: 3 additions & 1 deletion include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ typedef std::unique_ptr<Move> mup;
class MinimaxSearch
{
private:
static const int maxDepth = 8;
static const int MIN_DEPTH = 3;
static const int MAX_DEPTH = 9;
Heuristic heuristic;

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


public:
Expand Down
31 changes: 30 additions & 1 deletion src/Heuristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ int Heuristic::evaluate(bup &bitboard){
coeff[6] * attackerCount() +
coeff[7] * openPromotionCount() +
coeff[8] * moveablePawns() +
coeff[9] * moveableKings();
coeff[9] * moveableKings() +
coeff[10] * jumpeablePieces();
}

// Number of pawns
Expand Down Expand Up @@ -133,4 +134,32 @@ int Heuristic::moveableKings() const
return (isBlacksTurn ? 1 : -1) * count(accumK);
}

int Heuristic::jumpeablePieces() const
{
uint32_t accumP = 0;
uint32_t temp = 0;
uint32_t kings = active & k;
uint32_t emptyPos = ~(b | w);
if (isBlacksTurn) {
temp = (board->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);
}


24 changes: 19 additions & 5 deletions src/MinimaxSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef std::unique_ptr<Move> 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<double>::max();
for (mup &move : board->actions()) {
bup result = board->result(move);
Expand All @@ -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<double>::lowest();
for (mup &move : board->actions()) {
bup result = board->result(move);
Expand All @@ -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") {
Expand All @@ -60,6 +60,20 @@ 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;

bool jumpAvailable = heuristic.jumpeablePieces() != 0;

if (jumpAvailable) return false;
else if (depth >= MIN_DEPTH) return true;
else return false;

}

/* Public Functions */

mup MinimaxSearch::minimaxDecision(bup &board)
Expand Down

0 comments on commit 2108fe2

Please sign in to comment.