Skip to content

Commit

Permalink
Jump generation working
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed Apr 18, 2017
1 parent 84d4d9e commit 6c1c966
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class BitBoard
return r[index];
}

static const uint32_t whiteKingSpots = 0x00041041;
static const uint32_t blackKingSpots = 0x82000820;

/* Class member variables */
const uint32_t m_blackPieces = 0x41C71C3;
Expand Down Expand Up @@ -70,7 +72,9 @@ class BitBoard
int const getIndex(uint32_t piece);

void addJumps(std::vector<Move> &moves);
std::vector<int> generateImmediateJumps(uint32_t piece);
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);
void addNewMove(uint32_t start, uint32_t end, std::vector<Move> &moves);

public:
Expand Down
25 changes: 22 additions & 3 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void BitBoard::addJumps(std::vector<Move> &moves)
// grab the specific piece
uint32_t piece = (m_isBlacksTurn) ? m_blackPieces & 1<<i : m_whitePieces & 1<<i;
if (piece) {
//
generateAllJumps(*this, Move(), piece, moves);
}
}
}
Expand All @@ -49,10 +49,10 @@ void BitBoard::generateAllJumps(BitBoard board, Move move, uint32_t piece, std::
if (move.isEmpty()) move.addMove(getIndex(piece)); // Add start position
std::vector<uint32_t> immediateJumps = generateImmediateJumps(piece);
if (immediateJumps.size() == 0) {
if (move.length > 1) moves.push_back(move); // If no more jumps add move to list of moves
if (move.length() > 1) moves.push_back(move); // If no more jumps add move to list of moves
} else {
for (uint32_t x : immediateJumps) {
move.add(getIndex(x));
move.addMove(getIndex(x));
generateAllJumps(boardMove(board, piece, x), move, x, moves);
move.removeLast();
}
Expand All @@ -61,7 +61,25 @@ 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;

// First figure out if a piece has been take
int pieceLoc = getIndex(piece);
int moveLoc = getIndex(moveTo);
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);
} else {
if (diff == 2 || diff == 14) black = m_blackPieces & ~(1<<avg);
white = (m_whitePieces | moveTo) & ~piece;
kings = m_kings | (white & whiteKingSpots);
}

return BitBoard(black, white, kings);
}

std::vector<uint32_t> BitBoard::generateImmediateJumps(uint32_t piece)
Expand Down Expand Up @@ -127,6 +145,7 @@ std::string const BitBoard::player()
std::vector<Move> const BitBoard::actions()
{
std::vector<Move> moves;
addJumps(moves);

return moves;
}
2 changes: 1 addition & 1 deletion src/Move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void Move::removeLast()

bool Move::isEmpty()
{
return moves.isEmpty();
return moves.empty();
}
int Move::length()
{
Expand Down

0 comments on commit 6c1c966

Please sign in to comment.