diff --git a/include/BitBoard.h b/include/BitBoard.h index 2f55290..87e2bff 100644 --- a/include/BitBoard.h +++ b/include/BitBoard.h @@ -67,9 +67,15 @@ class BitBoard int h8() const; int h9() const; int h10() const; + int const getIndex(uint32_t piece); + + void addBlackJumps(std::vector &moves); + std::vector generateImmediateJumps(uint32_t piece); + void addNewMove(uint32_t start, uint32_t end, std::vector &moves); + +public: + BitBoard(uint32_t black, uint32_t white, uint32_t kings); - - public: std::string const player(); std::vector const actions(); BitBoard const result(Move move); diff --git a/include/Move.h b/include/Move.h index 648688d..cb8ff26 100644 --- a/include/Move.h +++ b/include/Move.h @@ -2,6 +2,7 @@ #define MOVE_H #include +#include class Move { @@ -10,8 +11,9 @@ class Move public: - Move() : moves() {}; - void printMessage(); + Move(); + Move(int start); + void addMove(int move); }; #endif diff --git a/src/BitBoard.cpp b/src/BitBoard.cpp index 423eb0f..c2b7708 100644 --- a/src/BitBoard.cpp +++ b/src/BitBoard.cpp @@ -1,11 +1,15 @@ #include #include #include +#include +#include +#include "Move.h" #include "BitBoard.h" /* Private 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 BitBoard::rotl32 (uint32_t n, unsigned int c) const { const unsigned int mask = (CHAR_BIT*sizeof(n)-1); @@ -22,9 +26,87 @@ uint32_t const BitBoard::rotr32 (uint32_t n, unsigned int c) const return (n>>c) | (n<<( (-c)&mask )); } +// Given a piece represented by a binary value return it's index +int const BitBoard::getIndex(uint32_t piece) +{ + return (int)std::log2(piece); +} + +void BitBoard::addBlackJumps(std::vector &moves) +{ + uint32_t notOcc = ~(m_whitePieces|m_blackPieces); + for (int i = 0; i < 32; ++i) { + // grab the specific piece + uint32_t piece = m_blackPieces & 1< BitBoard::generateImmediateJumps(uint32_t piece) +{ + std::vector moves; + uint32_t notOcc = ~(m_whitePieces|m_blackPieces); + uint32_t temp; + bool isKing = piece & m_kings; + if (m_isBlacksTurn) { + temp = (rotl32(piece, 7) & upLeft) & m_whitePieces; + if ((rotl32(temp, 7) & upLeft) & notOcc) + addNewMove(piece, rotl32(piece, 14), moves); + temp = (rotl32(piece, 1) & upRight) & m_whitePieces; + if ((rotl32(temp, 1) & upRight) & notOcc) + addNewMove(piece, rotl32(piece, 2), moves); + if (isKing) { + temp = (rotr32(piece, 7) & downRight) & m_whitePieces; + if ((rotr32(temp, 7) & downRight) & notOcc) + addNewMove(piece, rotr32(piece, 14), moves); + temp = (rotr32(piece, 1) & downLeft) & m_whitePieces; + if ((rotr32(temp, 1) & downLeft) & notOcc) + addNewMove(piece, rotr32(piece, 2), moves); + } + } else { + temp = (rotr32(piece, 7) & downRight) & m_blackPieces; + if ((rotr32(temp, 7) & downRight) & notOcc) + addNewMove(piece, rotr32(piece, 14), moves); + temp = (rotr32(piece, 1) & downLeft) & m_blackPieces; + if ((rotr32(temp, 1) & downLeft) & notOcc) + addNewMove(piece, rotr32(piece, 2), moves); + if (isKing) { + temp = (rotl32(piece, 7) & upLeft) & m_blackPieces; + if ((rotl32(temp, 7) & upLeft) & notOcc) + addNewMove(piece, rotl32(piece, 14), moves); + temp = (rotl32(piece, 1) & upRight) & m_blackPieces; + if ((rotl32(temp, 1) & upRight) & notOcc) + addNewMove(piece, rotl32(piece, 2), moves); + } + } + return moves; +} + +void BitBoard::addNewMove(uint32_t start, uint32_t end, std::vector &moves) +{ + Move newMove(getIndex(start)); + newMove.addMove(getIndex(end)); + moves.push_back(newMove); +} + /* Public functions */ +BitBoard::BitBoard(uint32_t black, uint32_t white, uint32_t kings) : + m_blackPieces(black), m_whitePieces(white), m_kings(kings) +{ + m_isBlacksTurn = true; +} + std::string const BitBoard::player() { return m_isBlacksTurn ? "black" : "white"; } + +std::vector const BitBoard::actions() +{ + std::vector moves; + + return moves; +} diff --git a/src/Move.cpp b/src/Move.cpp index a91d1ba..cd24105 100644 --- a/src/Move.cpp +++ b/src/Move.cpp @@ -1,7 +1,15 @@ #include #include "Move.h" -void Move::printMessage() +Move::Move() : moves() {}; + +Move::Move(int start) { - std::cout << "Here is an example message" << std::endl; + moves = {start}; } + +void Move::addMove(int move) +{ + moves.push_back(move); +} +