Skip to content

Instantly share code, notes, and snippets.

@Folling
Created August 4, 2017 15:12
Show Gist options
  • Save Folling/1122995cb7ac879974a6af71ff0ced79 to your computer and use it in GitHub Desktop.
Save Folling/1122995cb7ac879974a6af71ff0ced79 to your computer and use it in GitHub Desktop.
void generateAllMoves(const position* pos, moveList* list)
{
// TODO reduce pawn move generation to not use the "if" and maybe reduce it to one addPawnMove function
ASSERT(pos->checkBoard());
list->count = 0;
int side = pos->side;
if(side == WHITE)
{
for(int i = 0; i < pos->pieceNum[P]; i++)
{
int sq = pos->pList[P][i];
ASSERT(sqOnBoard(sq));
// nonCap moves
if(pos->pieces[sq + pawnRange] == EMPTY)
{
// pawns move bitwise 10 forward as our board is 10 wide
addWhitePawnMove(pos, sq, sq + pawnRange, EMPTY, list);
if (squareRanks[sq] == RANK_2 && pos->pieces[sq + psRange] == EMPTY)
{
// pawns can move two squares if they're on the second rank
addQuietMove(pos, MOVE(sq, sq + psRange, EMPTY, NOPROM, mFlagPS), list);
}
}
// cap moves
if(sqOnBoard(sq + left) && pieceColours[pos->pieces[sq + left]] == BLACK)
{
addWhitePawnMove(pos, sq, sq + left, pos->pieces[sq + left], list);
}
if (sqOnBoard(sq + right) && pieceColours[pos->pieces[sq + right]] == BLACK)
{
addWhitePawnMove(pos, sq, sq + right, pos->pieces[sq + right], list);
}
// enPassent moves
if(sq + left == pos->enPassentSquare)
{
addCaptureMove(pos, MOVE(sq, sq + left, EMPTY, EMPTY, mFlagEP), list);
}
if(sq + right == pos->enPassentSquare)
{
addCaptureMove(pos, MOVE(sq, sq + right, EMPTY, EMPTY, mFlagEP), list);
}
}
}
else // black pawns
{
for (int i = 0; i < pos->pieceNum[p]; i++)
{
int sq = pos->pList[p][i];
ASSERT(sqOnBoard(sq));
// nonCap moves
if (pos->pieces[sq - pawnRange] == EMPTY)
{
// pawns move bitwise 10 forward as our board is 10 wide
addBlackPawnMove(pos, sq, sq - pawnRange, EMPTY, list);
if (squareRanks[sq] == RANK_7 && pos->pieces[sq - psRange] == EMPTY)
{
// pawns can move two squares if they're on the second rank
addQuietMove(pos, MOVE(sq, sq - psRange, EMPTY, NOPROM, mFlagPS), list);
}
}
// cap moves
if (sqOnBoard(sq - left) && pieceColours[pos->pieces[sq - left]] == WHITE)
{
addBlackPawnMove(pos, sq, sq - left, pos->pieces[sq - left], list);
}
if (sqOnBoard(sq - right) && pieceColours[pos->pieces[sq - right]] == WHITE)
{
addBlackPawnMove(pos, sq, sq - right, pos->pieces[sq - right], list);
}
// enPassent moves
if (sq - left == pos->enPassentSquare)
{
addCaptureMove(pos, MOVE(sq, sq - left, EMPTY, EMPTY, mFlagEP), list);
}
if (sq - right == pos->enPassentSquare)
{
addCaptureMove(pos, MOVE(sq, sq - right, EMPTY, EMPTY, mFlagEP), list);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment