Skip to content

Commit

Permalink
Added hueristics.cpp with 8 Heuristic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed Apr 16, 2017
1 parent 12d9671 commit 860d814
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/heuristics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <cstdint>
#include <limits.h>
#include <string>
#include "BitBoard.h"

/* Heuristic Functions */

// counts number of setbits
int const count(uint32_t i){
unt cnt;
while(i){
cnt += i & 1;
i >>= 1;
}
return cnt;

}

// current players pieces
uint32_t const BitBoard::playerPieces(){
return misBLacksTurn ? m_blackPieces : m_whitePieces;
}

// Number of pawns
int const BitBoard::h1()
{
return count(playerPieces() & ~m_kings);
}

// Number of kings
int const BitBoard::h2()
{
return count(playerPieces() & m_kings);
}

// Number of safe pawns
int const BitBoard::h3()
{
return count(playerPieces() & ~m_kings & edgeLoc);
}

// Number of safe kings
int const BitBoard::h4()
{
return count(playerPieces() & m_kings & edgeLoc);
}

// Aggregated distance of pawns to promotion line
int const BitBoard::h5()
{
int aggrDistance = 0;
if(m_isBlacksTurn)
for(int i=0, i<7; i++)
aggrDistance += (7-i)*count(m_blackPieces & m_kings & rowMask[i]);
else
for(int i=1, i<8; i++)
aggrDistance += i*count(m_whitePieces & m_kings & rowMask[i]);

return aggrDistance;
}

// Number of defender pieces, closest two rows on players side
int const BitBoard::h6()
{
if(m_isBlacksTurn)
return count(m_blackPieces & (rowMask[0] | rowMask[1]));
else
return count(m_whitePieces & (rowMask[6] | rowMask[7]));
}

// Number of attacking pawns, position in upper three most rows, opponents side
int const BitBoard::h7()
{
if(m_isBlacksTurn)
return count(m_blackPieces & ~m_kings & ( rowMask[7] | rowMask[6] | rowMask[5]));
else
return count(m_whitePieces & ~m_kings & ( rowMask[2] | rowMask[1] | rowMask[0]));
}

// Number of unoccupied fields on promotion line
int const BitBoard::h8()
{
if(m_isBlacksTurn)
return count( (m_blackPieces | m_whitePieces) & rowMask[7]);
else
return count( (m_blackPieces | m_whitePieces) & rowMask[0]);
}



uint32_t const BitBoard::rotl32 (uint32_t n, unsigned int c)
{
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);

c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n<<c) | (n>>( (-c)&mask ));
}

uint32_t const BitBoard::rotr32 (uint32_t n, unsigned int c)
{
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);

c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n>>c) | (n<<( (-c)&mask ));
}


0 comments on commit 860d814

Please sign in to comment.