Skip to content

Commit

Permalink
Added heuristics and it should compile
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed Apr 17, 2017
1 parent c2e7f4d commit 0b02dc7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
46 changes: 27 additions & 19 deletions include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ class BitBoard
// Edge locations, all 4 sides
static const uint32_t edgeLoc = 0x86061E67;

/* Class member variables */
const uint32_t m_blackPieces = 0x41C71C3;
const uint32_t m_whitePieces = 0xE3820C38;
const uint32_t m_kings = 0;

bool m_isBlacksTurn = true;

static const uint32_t rowMask(int index){
static const uint32_t r [] = { 0x00041041,
0x00082082,
Expand All @@ -39,26 +32,41 @@ class BitBoard
return r[index];
}


/* Class member variables */
const uint32_t m_blackPieces = 0x41C71C3;
const uint32_t m_whitePieces = 0xE3820C38;
const uint32_t m_kings = 0;

bool m_isBlacksTurn = true;


// static constexpr uint32_t rowMask[8];
/* Class member functions */

// These are basic 32 bit rotation functions
// Details here: http://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c
uint32_t const rotl32 (uint32_t n, unsigned int c);
uint32_t const rotr32 (uint32_t n, unsigned int c);
uint32_t const rotl32 (uint32_t n, unsigned int c) const;
uint32_t const rotr32 (uint32_t n, unsigned int c) const;

uint32_t const playerPieces();
const uint32_t playerPieces() const;

int count(uint32_t i);
const int count(uint32_t i) const;

// Heuristic functions
int h1();
int h2();
int h3();
int h4();
int h5();
int h6();
int h7();
int h8();
void evalHueristics();

int h1() const;
int h2() const;
int h3() const;
int h4() const;
int h5() const;
int h6() const;
int h7() const;
int h8() const;
int h9() const;
int h10() const;


public:
std::string const player();
Expand Down
1 change: 0 additions & 1 deletion include/Move.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class Move
{
private:

std::vector<int> moves;

public:
Expand Down
4 changes: 2 additions & 2 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
/* Private Functions */


uint32_t const BitBoard::rotl32 (uint32_t n, unsigned int c)
uint32_t const BitBoard::rotl32 (uint32_t n, unsigned int c) const
{
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)
uint32_t const BitBoard::rotr32 (uint32_t n, unsigned int c) const
{
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);

Expand Down
66 changes: 56 additions & 10 deletions src/heuristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* Heuristic Functions */

// counts number of setbits
int BitBoard::count(uint32_t i){
const int BitBoard::count(uint32_t i) const{
int cnt;
while(i){
cnt += i & 1;
Expand All @@ -17,36 +17,40 @@ int BitBoard::count(uint32_t i){
}

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

void BitBoard::evalHueristics(){
evalScore = h1() + h2() + h3() + h4() + h5() + h6 + h7() + h8() + h9() + h10();
}

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

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

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

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

// Aggregated distance of pawns to promotion line
int BitBoard::h5()
int BitBoard::h5() const
{
int aggrDistance = 0;
if(m_isBlacksTurn)
Expand All @@ -60,7 +64,7 @@ int BitBoard::h5()
}

// Number of defender pieces, closest two rows on players side
int BitBoard::h6()
int BitBoard::h6() const
{
if(m_isBlacksTurn)
return count(m_blackPieces & (rowMask(0) | rowMask(1)));
Expand All @@ -69,7 +73,7 @@ int BitBoard::h6()
}

// Number of attacking pawns, position in upper three most rows, opponents side
int BitBoard::h7()
int BitBoard::h7() const
{
if(m_isBlacksTurn)
return count(m_blackPieces & ~m_kings & ( rowMask(7) | rowMask(6) | rowMask(5)));
Expand All @@ -78,14 +82,56 @@ int BitBoard::h7()
}

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

// Number of moveable pawns
int BitBoard::h9() const
{
uint32_t accumP;
uint32_t emptyPos = ~(m_blackPieces | m_whitePieces);

if(m_isBlacksTurn)
{
accumP |= rotr32(rotr32(m_blackPieces & ~m_kings & downLeft, 1) & emptyPos, -1);
accumP |= rotl32(rotl32(m_blackPieces & ~m_kings & downRight, 1) & emptyPos, -1);

}
else
{
accumP |= rotr32(rotr32(m_whitePieces & ~m_kings & upRight, -1) & emptyPos, 1);
accumP |= rotl32(rotl32(m_whitePieces & ~m_kings & upLeft, -1) & emptyPos, 1);
}
return count(accumP);
}

// Number of moveable kings
int BitBoard::h10() const
{
uint32_t accumK;
uint32_t kings;
uint32_t emptyPos = ~(m_blackPieces | m_whitePieces);
if(m_isBlacksTurn)
{
kings = m_blackPieces & m_kings;
accumK |= rotr32(rotr32(kings & downLeft, 1) & emptyPos, -1);
accumK |= rotl32(rotl32(kings & downRight, 1) & emptyPos, -1);
accumK |= rotr32(rotr32(kings & upRight, -1) & emptyPos, 1);
accumK |= rotl32(rotl32(kings & upLeft, -1) & emptyPos, 1);
}
else {
kings = m_whitePieces & m_kings;
accumK |= rotr32(rotr32(kings & downLeft, 1) & emptyPos, -1);
accumK |= rotl32(rotl32(kings & downRight, 1) & emptyPos, -1);
accumK |= rotr32(rotr32(kings & upRight, -1) & emptyPos, 1);
accumK |= rotl32(rotl32(kings & upLeft, -1) & emptyPos, 1);
}
return count(accumK);
}


0 comments on commit 0b02dc7

Please sign in to comment.