Pseudo legal movegen #1

Closed
rektdie wants to merge 6 commits from pseudoLegalMovegen into legalMovegen
pull from: pseudoLegalMovegen
2 changed files with 187 additions and 26 deletions
Showing only changes of commit d3a26b29df - Show all commits

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ int main() {
// UCILoop(board); // UCILoop(board);
Perft(board, 10); Perft(board, 6);
return 0; return 0;
} }
+186 -25
View File
@@ -285,40 +285,85 @@ Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) {
} }
} }
// Returns the direction to target from square static Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) {
static int GetDirection(int square, int target) { Bitboard pushes;
int rankDiff = (target / 8) - (square / 8);
int fileDiff = (target % 8) - (square % 8);
// Main directions int direction = color ? south : north;
if (rankDiff == 0 && fileDiff < 0) { Bitboard secondRank = color ? ranks[r_7] : ranks[r_2];
return west;
} else if (rankDiff == 0 && fileDiff > 0) {
return east;
} else if (rankDiff < 0 && fileDiff == 0) {
return south;
} else if (rankDiff > 0 && fileDiff == 0) {
return north;
}
// Diagonals if (!occupancy.IsSet(square + direction)) {
if (abs(rankDiff) == abs(fileDiff)) { pushes.SetBit(square + direction);
if (rankDiff < 0 && fileDiff < 0) {
return soWe; if (secondRank.IsSet(square) && !occupancy.IsSet(square + 2 * direction)) {
} else if (rankDiff < 0 && fileDiff > 0) { pushes.SetBit(square + 2 * direction);
return soEa;
} else if (rankDiff > 0 && fileDiff < 0) {
return noWe;
} else if (rankDiff > 0 && fileDiff > 0) {
return noEa;
} }
} }
return 0; return pushes;
} }
static void GenPawnMoves(Board &board, bool color) { static void GenPawnMoves(Board &board, bool color) {
Bitboard pawns = board.pieces[Pawn] & board.colors[color];
while (pawns) {
int square = pawns.getLS1BIndex();
Bitboard seventhRank = color ? ranks[r_2] : ranks[r_7];
Bitboard pushes = getPawnPushes(square, color, board.occupied);
Bitboard captures = pawnAttacks[color][square] & board.colors[!color];
if (seventhRank.IsSet(square)) {
while (pushes) {
int targetSquare = pushes.getLS1BIndex();
board.AddMove(Move(square, targetSquare, queenPromotion));
board.AddMove(Move(square, targetSquare, rookPromotion));
board.AddMove(Move(square, targetSquare, bishopPromotion));
board.AddMove(Move(square, targetSquare, knightPromotion));
pushes.PopBit(targetSquare);
}
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, queenPromoCapture));
board.AddMove(Move(square, targetSquare, rookPromoCapture));
board.AddMove(Move(square, targetSquare, bishopPromoCapture));
board.AddMove(Move(square, targetSquare, knightPromoCapture));
captures.PopBit(targetSquare);
}
} else {
while (pushes) {
int targetSquare = pushes.getLS1BIndex();
// Checking for double push
if (abs(targetSquare - square) == 16) {
board.AddMove(Move(square, targetSquare, doublePawnPush));
} else {
board.AddMove(Move(square, targetSquare, quiet));
}
pushes.PopBit(targetSquare);
}
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
// Checking for en passant
if (pawnAttacks[color][square].IsSet(board.enPassantTarget)) {
board.AddMove(Move(square, board.enPassantTarget, epCapture));
}
}
pawns.PopBit(square);
}
} }
static void GenKnightMoves(Board &board, bool color) { static void GenKnightMoves(Board &board, bool color) {
@@ -352,19 +397,135 @@ static void GenKnightMoves(Board &board, bool color) {
} }
static void GenRookMoves(Board &board, bool color) { static void GenRookMoves(Board &board, bool color) {
Bitboard rooks = board.pieces[Rook] & board.colors[color];
while (rooks) {
int square = rooks.getLS1BIndex();
Bitboard moves = getRookAttack(square, board.occupied) & ~board.colors[color];
Bitboard captures = moves & board.colors[!color];
Bitboard quietMoves = moves & ~board.colors[!color];
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
while (quietMoves) {
int targetSquare = quietMoves.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
quietMoves.PopBit(targetSquare);
}
rooks.PopBit(square);
}
} }
static void GenBishopMoves(Board &board, bool color) { static void GenBishopMoves(Board &board, bool color) {
Bitboard bishops = board.pieces[Bishop] & board.colors[color];
while (bishops) {
int square = bishops.getLS1BIndex();
Bitboard moves = getBishopAttack(square, board.occupied) & ~board.colors[color];
Bitboard captures = moves & board.colors[!color];
Bitboard quietMoves = moves & ~board.colors[!color];
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
while (quietMoves) {
int targetSquare = quietMoves.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
quietMoves.PopBit(targetSquare);
}
bishops.PopBit(square);
}
} }
static void GenQueenMoves(Board &board, bool color) { static void GenQueenMoves(Board &board, bool color) {
Bitboard queens = board.pieces[Queen] & board.colors[color];
while (queens) {
int square = queens.getLS1BIndex();
Bitboard moves = getQueenAttack(square, board.occupied) & ~board.colors[color];
Bitboard captures = moves & board.colors[!color];
Bitboard quietMoves = moves & ~board.colors[!color];
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
while (quietMoves) {
int targetSquare = quietMoves.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet));
quietMoves.PopBit(targetSquare);
}
queens.PopBit(square);
}
} }
static void GenKingMoves(Board &board, bool color) { static void GenKingMoves(Board &board, bool color) {
int kingSquare = (board.pieces[King] & board.colors[color]).getLS1BIndex();
Bitboard moves = kingAttacks[kingSquare] & ~board.colors[color];
Bitboard captures = moves & board.colors[!color];
Bitboard quietMoves = moves & ~board.colors[!color];
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(kingSquare, targetSquare, capture));
captures.PopBit(targetSquare);
}
while (quietMoves) {
int targetSquare = quietMoves.getLS1BIndex();
board.AddMove(Move(kingSquare, targetSquare, quiet));
quietMoves.PopBit(targetSquare);
}
// Checking for king side castle
if (board.castlingRights[color * 2]) {
Bitboard mask = color ? 0xe000000000000000 : 0xe0;
if ((board.occupied & mask).PopCount() == 1) {
board.AddMove(Move(kingSquare, kingSquare + 2, kingCastle));
}
}
// Checking for quuen side castle
if (board.castlingRights[color * 2 + 1]) {
Bitboard mask = color ? 0xf00000000000000 : 0xf;
if ((board.occupied & mask).PopCount() == 1) {
board.AddMove(Move(kingSquare, kingSquare - 2, queenCastle));
}
}
} }
void GenerateMoves(Board &board) { void GenerateMoves(Board &board) {