Skip to content

Commit

Permalink
Merge pull request #3 from mbluemer/messing-with-variable-depth
Browse files Browse the repository at this point in the history
Messing with variable depth
  • Loading branch information
mbluemer committed Apr 29, 2017
2 parents 5b2b0ed + c03815a commit ecad422
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
1 change: 1 addition & 0 deletions include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class BitBoard
vmup actions() const;
bup result(mup &move) const;
void printState() const;
std::string toString() const;
};

#endif
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 = 5;
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
40 changes: 33 additions & 7 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<<avg);
if (diff == 2 || diff == 14) {
white &= ~(1<<avg);
kings &= ~(1<<avg);
}
black = (black | moveTo) & ~piece;
kings |= (black & blackKingSpots);
wasKinged = moveTo & blackKingSpots;
if (!(piece & kings) && (moveTo & blackKingSpots)) {
kings |= moveTo;
wasKinged = true;
}
} else {
if (diff == 2 || diff == 14) black &= ~(1<<avg);
if (diff == 2 || diff == 14) {
black &= ~(1<<avg);
kings &= ~(1<<avg);
}
white = (white | moveTo) & ~piece;
kings |= (white & whiteKingSpots);
wasKinged = moveTo & whiteKingSpots;
if (!(piece & kings) && (moveTo & whiteKingSpots)) {
kings |= moveTo;
wasKinged = true;
}
}

if (piece & kings) kings = moveTo | (kings & ~piece);
// if you're a king
if (piece & kings) {
kings &= ~piece; // make sure piece's spot is no longer a king
kings |= moveTo; // set piece's new spot as a king;
}

return BitBoard(black, white, kings, board.m_isBlacksTurn, wasKinged);
}
Expand Down Expand Up @@ -259,3 +273,15 @@ void BitBoard::printState() const
std::cout<<"-"<<pieceToString(19)<<"-"<<pieceToString(13)<<"-"<<pieceToString(7)<<"-"<<pieceToString(1)<<std::endl;
std::cout<<pieceToString(18)<<"-"<<pieceToString(12)<<"-"<<pieceToString(6)<<"-"<<pieceToString(0)<<"-"<<std::endl;
}

std::string BitBoard::toString() const
{
return "-"+pieceToString(11)+"-"+pieceToString(5)+"-"+pieceToString(31)+"-"+pieceToString(25)+"\n"+
pieceToString(10)+"-"+pieceToString(4)+"-"+pieceToString(30)+"-"+pieceToString(24)+"-"+"\n"+
"-"+pieceToString(3)+"-"+pieceToString(29)+"-"+pieceToString(23)+"-"+pieceToString(17)+"\n"+
pieceToString(2)+"-"+pieceToString(28)+"-"+pieceToString(22)+"-"+pieceToString(16)+"-"+"\n"+
"-"+pieceToString(27)+"-"+pieceToString(21)+"-"+pieceToString(15)+"-"+pieceToString(9)+"\n"+
pieceToString(26)+"-"+pieceToString(20)+"-"+pieceToString(14)+"-"+pieceToString(8)+"-"+"\n"+
"-"+pieceToString(19)+"-"+pieceToString(13)+"-"+pieceToString(7)+"-"+pieceToString(1)+"\n"+
pieceToString(18)+"-"+pieceToString(12)+"-"+pieceToString(6)+"-"+pieceToString(0)+"-"+"\n";
}
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);
}


21 changes: 16 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,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)
Expand Down

0 comments on commit ecad422

Please sign in to comment.