-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added hueristics.cpp with 8 Heuristic functions
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 )); | ||
} | ||
|
||
|