Skip to content

Commit

Permalink
some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 18, 2017
1 parent e64dd59 commit ca81798
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
7 changes: 4 additions & 3 deletions include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ class BitBoard
static const uint32_t whiteKingSpots = 0x00041041;
static const uint32_t blackKingSpots = 0x82000820;

// Used for determing board from move
static const uint32_t topCorner = 0xC3C;

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

bool m_isBlacksTurn = true;

// static constexpr uint32_t rowMask[8];
/* Class member functions */
Expand Down Expand Up @@ -76,7 +77,7 @@ class BitBoard
void addNormalMoves(std::vector<Move> &moves);
void generateAllJumps(BitBoard board, Move move, uint32_t piece, std::vector<Move> &moves);
BitBoard boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo);
std::vector<uint32_t> generateImmediateJumps(uint32_t piece);
std::vector<uint32_t> generateImmediateJumps(BitBoard &board, uint32_t piece);
void addNewMove(uint32_t start, uint32_t end, std::vector<Move> &moves);
std::string pieceToString(int piece);

Expand Down
57 changes: 34 additions & 23 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void BitBoard::addNormalMoves(std::vector<Move> &moves)
void BitBoard::generateAllJumps(BitBoard board, Move move, uint32_t piece, std::vector<Move> &moves)
{
if (move.isEmpty()) move.addMove(getIndex(piece)); // Add start position
std::vector<uint32_t> immediateJumps = generateImmediateJumps(piece);
std::vector<uint32_t> immediateJumps = generateImmediateJumps(board, piece);
if (immediateJumps.size() == 0) {
if (move.length() > 1) moves.push_back(move); // If no more jumps add move to list of moves
} else {
Expand All @@ -96,59 +96,67 @@ void BitBoard::generateAllJumps(BitBoard board, Move move, uint32_t piece, std::
BitBoard BitBoard::boardMove(BitBoard &board, uint32_t piece, uint32_t moveTo)
{
// Constructor needs black, white, and kings locations so just derive those and instantiate new board object
uint32_t white, black, kings;
uint32_t white = board.m_whitePieces;
uint32_t black = board.m_blackPieces;
uint32_t kings = board.m_kings;

// First figure out if a piece has been take
int pieceLoc = getIndex(piece);
int moveLoc = getIndex(moveTo);
if (piece & topCorner) pieceLoc += 32;
if (moveTo & topCorner) moveLoc += 32;
int diff = std::abs((double)pieceLoc - moveLoc);
int avg = (int)((pieceLoc + moveLoc) / 2);
if (m_isBlacksTurn) {
if (diff == 2 || diff == 14) white = m_whitePieces & ~(1<<avg);
black = (m_blackPieces | moveTo) & ~piece;
kings = m_kings | (black & blackKingSpots);
int avg = ((pieceLoc + moveLoc) / 2);
if (board.m_isBlacksTurn) {
if (diff == 2 || diff == 14) {
white &= ~(1<<avg);
}
black = (black | moveTo) & ~piece;
kings = kings | (black & blackKingSpots);
} else {
if (diff == 2 || diff == 14) black = m_blackPieces & ~(1<<avg);
white = (m_whitePieces | moveTo) & ~piece;
kings = m_kings | (white & whiteKingSpots);
if (diff == 2 || diff == 14) black = black & ~(1<<avg);
white = (white | moveTo) & ~piece;
kings = kings | (white & whiteKingSpots);
}

if (piece & kings) kings = moveTo | (kings & ~piece);

return BitBoard(black, white, kings);
}

std::vector<uint32_t> BitBoard::generateImmediateJumps(uint32_t piece)
std::vector<uint32_t> BitBoard::generateImmediateJumps(BitBoard &board, uint32_t piece)
{
std::vector<uint32_t> moves;
uint32_t notOcc = ~(m_whitePieces|m_blackPieces);
uint32_t notOcc = ~(board.m_whitePieces|board.m_blackPieces);
uint32_t temp;
bool isKing = piece & m_kings;
bool isKing = piece & board.m_kings;
if (m_isBlacksTurn) {
temp = (rotl32(piece, 7) & upLeft) & m_whitePieces;
temp = (rotl32(piece, 7) & upLeft) & board.m_whitePieces;
if ((rotl32(temp, 7) & upLeft) & notOcc)
moves.push_back(rotl32(temp, 7));
temp = (rotl32(piece, 1) & upRight) & m_whitePieces;
temp = (rotl32(piece, 1) & upRight) & board.m_whitePieces;
if ((rotl32(temp, 1) & upRight) & notOcc)
moves.push_back(rotl32(temp, 1));
if (isKing) {
temp = (rotr32(piece, 7) & downRight) & m_whitePieces;
temp = (rotr32(piece, 7) & downRight) & board.m_whitePieces;
if ((rotr32(temp, 7) & downRight) & notOcc)
moves.push_back(rotr32(temp, 7));
temp = (rotr32(piece, 1) & downLeft) & m_whitePieces;
temp = (rotr32(piece, 1) & downLeft) & board.m_whitePieces;
if ((rotr32(temp, 1) & downLeft) & notOcc)
moves.push_back(rotr32(temp, 1));
}
} else {
temp = (rotr32(piece, 7) & downRight) & m_blackPieces;
temp = (rotr32(piece, 7) & downRight) & board.m_blackPieces;
if ((rotr32(temp, 7) & downRight) & notOcc)
moves.push_back(rotr32(temp, 7));
temp = (rotr32(piece, 1) & downLeft) & m_blackPieces;
temp = (rotr32(piece, 1) & downLeft) & board.m_blackPieces;
if ((rotr32(temp, 1) & downLeft) & notOcc)
moves.push_back(rotr32(temp, 1));
if (isKing) {
temp = (rotl32(piece, 7) & upLeft) & m_blackPieces;
temp = (rotl32(piece, 7) & upLeft) & board.m_blackPieces;
if ((rotl32(temp, 7) & upLeft) & notOcc)
moves.push_back(rotl32(temp, 7));
temp = (rotl32(piece, 1) & upRight) & m_blackPieces;
temp = (rotl32(piece, 1) & upRight) & board.m_blackPieces;
if ((rotl32(temp, 1) & upRight) & notOcc)
moves.push_back(rotl32(temp, 1));
}
Expand Down Expand Up @@ -202,15 +210,17 @@ std::vector<Move> const BitBoard::actions()
BitBoard const BitBoard::result(Move move)
{
BitBoard currBoard = *this;
int start;
int start{-1};
for (int x : move.getMoves()) {
if (!start) {
if (start == -1) {
start = x;
} else {
currBoard = boardMove(currBoard, 1<<start, 1<<x);
start = x;
}
}

currBoard.m_isBlacksTurn = !m_isBlacksTurn;
return currBoard;
}

Expand All @@ -225,3 +235,4 @@ void const BitBoard::printState()
std::cout<<"-"<<pieceToString(19)<<"-"<<pieceToString(13)<<"-"<<pieceToString(7)<<"-"<<pieceToString(1)<<std::endl;
std::cout<<pieceToString(18)<<"-"<<pieceToString(12)<<"-"<<pieceToString(6)<<"-"<<pieceToString(0)<<"-"<<std::endl;
}

0 comments on commit ca81798

Please sign in to comment.