From 860d8145f1394d5fa92371cd166359ca4284eb75 Mon Sep 17 00:00:00 2001 From: David Jardim Date: Sat, 15 Apr 2017 21:40:43 -0400 Subject: [PATCH] Added hueristics.cpp with 8 Heuristic functions --- src/heuristics.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/heuristics.cpp diff --git a/src/heuristics.cpp b/src/heuristics.cpp new file mode 100644 index 0000000..14ea28a --- /dev/null +++ b/src/heuristics.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#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)&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 )); +} + +