Pseudo legal movegen #1
No files matched your search
@@ -17,11 +17,10 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o)))
|
||||
|
||||
# Target executable
|
||||
Eleanor$(EXE_EXT): $(OBJS)
|
||||
g++ -o $@ $^
|
||||
|
||||
g++ -o $@ $^ -O3
|
||||
# Rule to compile source files to object files
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR)
|
||||
g++ -c -o $@ $<
|
||||
g++ -c -o $@ $< -O3
|
||||
|
||||
# Create the object directory if it doesn't exist
|
||||
$(OBJ_DIR):
|
||||
|
||||
+30
-9
@@ -23,22 +23,16 @@ void Bitboard::PrintBoard() const {
|
||||
std::cout << " Board: 0x" << std::hex << m_board << '\n';
|
||||
}
|
||||
|
||||
U64 Bitboard::GetBoard() const {
|
||||
return m_board;
|
||||
}
|
||||
|
||||
bool Bitboard::IsSet(int square) const {
|
||||
return (1ULL << square) & m_board;
|
||||
return GetSquare(square) & m_board;
|
||||
}
|
||||
|
||||
void Bitboard::SetBit(int square) {
|
||||
m_board |= (1ULL << square);
|
||||
m_board |= GetSquare(square);
|
||||
}
|
||||
|
||||
void Bitboard::PopBit(int square) {
|
||||
if (Bitboard(m_board).IsSet(square)) {
|
||||
m_board &= (~Bitboard::GetSquare(square)).GetBoard();
|
||||
}
|
||||
m_board &= ~GetSquare(square);
|
||||
}
|
||||
|
||||
int Bitboard::PopCount() const {
|
||||
@@ -86,6 +80,19 @@ Bitboard Bitboard::operator*(const Bitboard& other) const {
|
||||
return m_board * other.m_board;
|
||||
}
|
||||
|
||||
Bitboard Bitboard::operator&(const uint64_t& other) const {
|
||||
return m_board & other;
|
||||
}
|
||||
Bitboard Bitboard::operator|(const uint64_t& other) const {
|
||||
return m_board | other;
|
||||
}
|
||||
Bitboard Bitboard::operator^(const uint64_t& other) const {
|
||||
return m_board ^ other;
|
||||
}
|
||||
Bitboard Bitboard::operator*(const uint64_t& other) const {
|
||||
return m_board * other;
|
||||
}
|
||||
|
||||
Bitboard Bitboard::operator&=(const Bitboard& other) {
|
||||
return m_board &= other.m_board;
|
||||
}
|
||||
@@ -96,6 +103,16 @@ Bitboard Bitboard::operator^=(const Bitboard& other) {
|
||||
return m_board ^= other.m_board;
|
||||
}
|
||||
|
||||
Bitboard Bitboard::operator&=(const uint64_t& other) {
|
||||
return m_board &= other;
|
||||
}
|
||||
Bitboard Bitboard::operator|=(const uint64_t& other) {
|
||||
return m_board |= other;
|
||||
}
|
||||
Bitboard Bitboard::operator^=(const uint64_t& other) {
|
||||
return m_board ^= other;
|
||||
}
|
||||
|
||||
Bitboard Bitboard::operator<<(int other) {
|
||||
if (other > 0) {
|
||||
return m_board << other;
|
||||
@@ -116,3 +133,7 @@ Bitboard Bitboard::operator>>(int other) {
|
||||
Bitboard Bitboard::operator~() {
|
||||
return ~(m_board);
|
||||
}
|
||||
|
||||
Bitboard::operator uint64_t() {
|
||||
return m_board;
|
||||
}
|
||||
+10
-1
@@ -12,7 +12,6 @@ public:
|
||||
Bitboard(const U64& board)
|
||||
: m_board(board) {}
|
||||
|
||||
U64 GetBoard() const;
|
||||
bool IsSet(int square) const;
|
||||
void SetBit(int square);
|
||||
void PopBit(int square);
|
||||
@@ -31,11 +30,21 @@ public:
|
||||
Bitboard operator^(const Bitboard& other) const;
|
||||
Bitboard operator*(const Bitboard& other) const;
|
||||
|
||||
Bitboard operator&(const uint64_t& other) const;
|
||||
Bitboard operator|(const uint64_t& other) const;
|
||||
Bitboard operator^(const uint64_t& other) const;
|
||||
Bitboard operator*(const uint64_t& other) const;
|
||||
|
||||
Bitboard operator&=(const Bitboard& other);
|
||||
Bitboard operator|=(const Bitboard& other);
|
||||
Bitboard operator^=(const Bitboard& other);
|
||||
|
||||
Bitboard operator&=(const uint64_t& other);
|
||||
Bitboard operator|=(const uint64_t& other);
|
||||
Bitboard operator^=(const uint64_t& other);
|
||||
|
||||
Bitboard operator<<(int other);
|
||||
Bitboard operator>>(int other);
|
||||
Bitboard operator~();
|
||||
operator uint64_t();
|
||||
};
|
||||
+22
-33
@@ -1,5 +1,4 @@
|
||||
#include "board.h"
|
||||
#include "movegen.h"
|
||||
#include <iostream>
|
||||
|
||||
void Board::Init() {
|
||||
@@ -7,7 +6,7 @@ void Board::Init() {
|
||||
}
|
||||
|
||||
void Board::Reset() {
|
||||
castlingRights = { false, false, false, false };
|
||||
castlingRights = 0;
|
||||
enPassantTarget = -1;
|
||||
halfMoves = 0;
|
||||
fullMoves = 0;
|
||||
@@ -16,6 +15,8 @@ void Board::Reset() {
|
||||
|
||||
pieces = std::array<Bitboard, 6>();
|
||||
colors = std::array<Bitboard, 2>();
|
||||
|
||||
moveList = std::array<Move, 218>();
|
||||
}
|
||||
|
||||
void Board::SetByFen(const char* fen) {
|
||||
@@ -79,7 +80,7 @@ void Board::SetByFen(const char* fen) {
|
||||
pieceColor = White;
|
||||
break;
|
||||
case '/':
|
||||
currentSquare = currentSquare >> 8 + digits;
|
||||
currentSquare = currentSquare >> (8 + digits);
|
||||
digits = 0;
|
||||
fen++;
|
||||
continue;
|
||||
@@ -123,16 +124,16 @@ void Board::SetByFen(const char* fen) {
|
||||
}
|
||||
break;
|
||||
case 'K':
|
||||
castlingRights[White] = true;
|
||||
castlingRights |= whiteKingRight;
|
||||
break;
|
||||
case 'Q':
|
||||
castlingRights[White + 1] = true;
|
||||
castlingRights |= whiteQueenRight;
|
||||
break;
|
||||
case 'k':
|
||||
castlingRights[Black * 2] = true;
|
||||
castlingRights |= blackKingRight;
|
||||
break;
|
||||
case 'q':
|
||||
castlingRights[Black * 2 + 1] = true;
|
||||
castlingRights |= blackQueenRight;
|
||||
break;
|
||||
default:
|
||||
if (*fen != '-') {
|
||||
@@ -183,7 +184,6 @@ void Board::SetByFen(const char* fen) {
|
||||
}
|
||||
|
||||
occupied = (colors[White] | colors[Black]);
|
||||
GenAttackMaps(*this);
|
||||
}
|
||||
|
||||
void Board::PrintBoard() {
|
||||
@@ -229,26 +229,27 @@ void Board::PrintBoard() {
|
||||
}
|
||||
|
||||
std::cout << " Castling rights: ";
|
||||
if (castlingRights[0]) std::cout << "K"; else std::cout << "-";
|
||||
if (castlingRights[1]) std::cout << "Q"; else std::cout << "-";
|
||||
if (castlingRights[2]) std::cout << "k"; else std::cout << "-";
|
||||
if (castlingRights[3]) std::cout << "q"; else std::cout << "-";
|
||||
if (castlingRights & whiteKingRight) std::cout << "K"; else std::cout << "-";
|
||||
if (castlingRights & whiteQueenRight) std::cout << "Q"; else std::cout << "-";
|
||||
if (castlingRights & blackKingRight) std::cout << "k"; else std::cout << "-";
|
||||
if (castlingRights & blackQueenRight) std::cout << "q"; else std::cout << "-";
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
void Board::AddMove(Move move) {
|
||||
moveList.push_back(move);
|
||||
moveList[currentMoveIndex] = move;
|
||||
currentMoveIndex++;
|
||||
}
|
||||
|
||||
void Board::ResetMoves() {
|
||||
moveList.clear();
|
||||
currentMoveIndex = 0;
|
||||
}
|
||||
|
||||
void Board::ListMoves() {
|
||||
int count = 1;
|
||||
for (Move move : moveList) {
|
||||
for (int i = 0; i < currentMoveIndex; i++) {
|
||||
std::cout << count << ". ";
|
||||
move.PrintMove();
|
||||
moveList[i].PrintMove();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@@ -265,18 +266,8 @@ int Board::GetPieceColor(int square) {
|
||||
return (colors[Black].IsSet(square));
|
||||
}
|
||||
|
||||
U64 Board::GetAttackMaps(bool side) {
|
||||
U64 combined = 0ULL;
|
||||
for (int type = Pawn; type <= King; type++) {
|
||||
combined |= attackMaps[side][type];
|
||||
}
|
||||
return combined;
|
||||
}
|
||||
|
||||
bool Board::InCheck(bool side) {
|
||||
Bitboard myKingSquare = colors[side] & pieces[King];
|
||||
|
||||
return GetAttackMaps(!side) & myKingSquare.GetBoard();
|
||||
}
|
||||
|
||||
void Board::SetPiece(int piece, int square, bool color) {
|
||||
@@ -298,13 +289,13 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color)
|
||||
int kingSideRook = color ? h8 : h1;
|
||||
|
||||
if (square == queenSideRook) {
|
||||
board.castlingRights[color * 2 + 1] = false;
|
||||
board.castlingRights &= color ? blackQueenRight : whiteQueenRight;
|
||||
} else if (square == kingSideRook) {
|
||||
board.castlingRights[color * 2] = false;
|
||||
board.castlingRights &= color ? blackKingRight : whiteKingRight;
|
||||
}
|
||||
} else if (type == King) {
|
||||
board.castlingRights[color * 2 + 1] = false;
|
||||
board.castlingRights[color * 2] = false;
|
||||
board.castlingRights &= color ? blackQueenRight : whiteQueenRight;
|
||||
board.castlingRights &= color ? blackKingRight : whiteKingRight;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,9 +399,7 @@ void Board::MakeMove(Move move) {
|
||||
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
||||
|
||||
halfMoves++;
|
||||
if (halfMoves & 2 == 0) fullMoves++;
|
||||
if (halfMoves % 2 == 0) fullMoves++;
|
||||
sideToMove = !attackerColor;
|
||||
enPassantTarget = newEpTarget;
|
||||
|
||||
GenAttackMaps(*this);
|
||||
}
|
||||
+3
-5
@@ -3,7 +3,6 @@
|
||||
#include "bitboards.h"
|
||||
#include "types.h"
|
||||
#include "move.h"
|
||||
#include <vector>
|
||||
|
||||
class Board {
|
||||
public:
|
||||
@@ -13,17 +12,17 @@ public:
|
||||
|
||||
std::array<Bitboard, 6> pieces;
|
||||
std::array<Bitboard, 2> colors;
|
||||
U64 attackMaps[2][64];
|
||||
|
||||
Bitboard occupied;
|
||||
|
||||
std::vector<Move> moveList;
|
||||
std::array<Move, 218> moveList;
|
||||
int currentMoveIndex = 0;
|
||||
|
||||
bool sideToMove = White;
|
||||
int halfMoves = 0;
|
||||
int fullMoves = 0;
|
||||
|
||||
std::array<bool, 4> castlingRights = { false, false, false, false };
|
||||
uint8_t castlingRights = 0;
|
||||
int enPassantTarget;
|
||||
|
||||
void Init();
|
||||
@@ -38,7 +37,6 @@ public:
|
||||
int GetPieceType(int square);
|
||||
int GetPieceColor(int square);
|
||||
|
||||
U64 GetAttackMaps(bool side);
|
||||
bool InCheck(bool side);
|
||||
|
||||
void SetPiece(int piece, int square, bool color);
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ static int GetPositionalScore(Board &board, int type, int color) {
|
||||
|
||||
Bitboard pieces = board.pieces[type] & board.colors[color];
|
||||
|
||||
while (pieces.GetBoard()) {
|
||||
while (pieces) {
|
||||
int square = pieces.getLS1BIndex();
|
||||
|
||||
// White
|
||||
|
||||
+3
-1
@@ -11,7 +11,9 @@ int main() {
|
||||
|
||||
Board board;
|
||||
|
||||
UCILoop(board);
|
||||
// UCILoop(board);
|
||||
|
||||
Perft(board, 6);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+146
-594
@@ -22,13 +22,13 @@ Bitboard maskPawnAttacks(int side, int square) {
|
||||
|
||||
// White
|
||||
if (!side) {
|
||||
if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa);
|
||||
if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe);
|
||||
if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa);
|
||||
if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe);
|
||||
}
|
||||
// Black
|
||||
else {
|
||||
if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa);
|
||||
if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe);
|
||||
if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa);
|
||||
if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe);
|
||||
}
|
||||
return attacks;
|
||||
}
|
||||
@@ -37,15 +37,15 @@ Bitboard maskKnightAttacks(int square) {
|
||||
Bitboard attacks;
|
||||
Bitboard attacker = Bitboard::GetSquare(square);
|
||||
|
||||
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
||||
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
||||
if (((attacker << noWe + west) & notHGfile).GetBoard()) attacks |= (attacker << noWe + west);
|
||||
if (((attacker << noEa + east) & notABfile).GetBoard()) attacks |= (attacker << noEa + east);
|
||||
if (((attacker << noEa + north) & notAfile)) attacks |= (attacker << noEa + north);
|
||||
if (((attacker << noWe + north) & notHfile)) attacks |= (attacker << noWe + north);
|
||||
if (((attacker << noWe + west) & notHGfile)) attacks |= (attacker << noWe + west);
|
||||
if (((attacker << noEa + east) & notABfile)) attacks |= (attacker << noEa + east);
|
||||
|
||||
if (((attacker << soWe + south) & notHfile).GetBoard()) attacks |= (attacker << soWe + south);
|
||||
if (((attacker << soEa + south) & notAfile).GetBoard()) attacks |= (attacker << soEa + south);
|
||||
if (((attacker << soEa + east) & notABfile).GetBoard()) attacks |= (attacker << soEa + east);
|
||||
if (((attacker << soWe + west) & notHGfile).GetBoard()) attacks |= (attacker << soWe + west);
|
||||
if (((attacker << soWe + south) & notHfile)) attacks |= (attacker << soWe + south);
|
||||
if (((attacker << soEa + south) & notAfile)) attacks |= (attacker << soEa + south);
|
||||
if (((attacker << soEa + east) & notABfile)) attacks |= (attacker << soEa + east);
|
||||
if (((attacker << soWe + west) & notHGfile)) attacks |= (attacker << soWe + west);
|
||||
|
||||
return attacks;
|
||||
}
|
||||
@@ -57,12 +57,12 @@ Bitboard maskKingAttacks(int square) {
|
||||
attacks |= (attacker << north);
|
||||
attacks |= (attacker << south);
|
||||
|
||||
if (((attacker << west) & notHfile).GetBoard()) attacks |= (attacker << west);
|
||||
if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe);
|
||||
if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa);
|
||||
if (((attacker << east) & notAfile).GetBoard()) attacks |= (attacker << east);
|
||||
if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa);
|
||||
if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe);
|
||||
if (((attacker << west) & notHfile)) attacks |= (attacker << west);
|
||||
if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe);
|
||||
if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa);
|
||||
if (((attacker << east) & notAfile)) attacks |= (attacker << east);
|
||||
if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa);
|
||||
if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe);
|
||||
|
||||
return attacks;
|
||||
}
|
||||
@@ -140,25 +140,25 @@ Bitboard bishopAttacksOnTheFly(int square, Bitboard block) {
|
||||
for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) {
|
||||
currentSquare = (1ULL << (r * 8 + f));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
// North East
|
||||
for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) {
|
||||
currentSquare = (1ULL << (r * 8 + f));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
//South West
|
||||
for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) {
|
||||
currentSquare = (1ULL << (r * 8 + f));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
//South East
|
||||
for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) {
|
||||
currentSquare = (1ULL << (r * 8 + f));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
|
||||
return attacks;
|
||||
@@ -179,25 +179,25 @@ Bitboard rookAttacksOnTheFly(int square, Bitboard block) {
|
||||
for (int r = tr + 1; r <= 7; r++) {
|
||||
currentSquare = (1ULL << (r * 8 + tf));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
// South
|
||||
for (int r = tr - 1; r >= 0; r--) {
|
||||
currentSquare = (1ULL << (r * 8 + tf));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
// East
|
||||
for (int f = tf + 1; f <= 7; f++) {
|
||||
currentSquare = (1ULL << (tr * 8 + f));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
// West
|
||||
for (int f = tf - 1; f >= 0; f--) {
|
||||
currentSquare = (1ULL << (tr * 8 + f));
|
||||
attacks |= currentSquare;
|
||||
if ((currentSquare & block).GetBoard()) break;
|
||||
if ((currentSquare & block)) break;
|
||||
}
|
||||
|
||||
return attacks;
|
||||
@@ -221,7 +221,7 @@ void initSliderAttacks() {
|
||||
for (int index = 0; index < 512; index++) {
|
||||
Bitboard occupancy = Bitboard::getOccupancy(index, bishopMasks[square]);
|
||||
|
||||
int magicIndex = (occupancy.GetBoard() * bishopMagicNumbers[square])
|
||||
int magicIndex = (occupancy * bishopMagicNumbers[square])
|
||||
>> (64 - bishopRelevantBits[square]);
|
||||
|
||||
bishopAttacks[square][magicIndex] = bishopAttacksOnTheFly(square, occupancy);
|
||||
@@ -230,7 +230,7 @@ void initSliderAttacks() {
|
||||
for (int index = 0; index < 4096; index++) {
|
||||
Bitboard occupancy = Bitboard::getOccupancy(index, rookMasks[square]);
|
||||
|
||||
int magicIndex = (occupancy.GetBoard() * rookMagicNumbers[square])
|
||||
int magicIndex = (occupancy * rookMagicNumbers[square])
|
||||
>> (64 - rookRelevantBits[square]);
|
||||
|
||||
rookAttacks[square][magicIndex] = rookAttacksOnTheFly(square, occupancy);
|
||||
@@ -239,7 +239,7 @@ void initSliderAttacks() {
|
||||
}
|
||||
|
||||
Bitboard getBishopAttack(int square, U64 occupancy) {
|
||||
occupancy &= bishopMasks[square].GetBoard();
|
||||
occupancy &= bishopMasks[square];
|
||||
occupancy *= bishopMagicNumbers[square];
|
||||
occupancy >>= (64 - bishopRelevantBits[square]);
|
||||
|
||||
@@ -247,7 +247,7 @@ Bitboard getBishopAttack(int square, U64 occupancy) {
|
||||
}
|
||||
|
||||
Bitboard getRookAttack(int square, U64 occupancy) {
|
||||
occupancy &= rookMasks[square].GetBoard();
|
||||
occupancy &= rookMasks[square];
|
||||
occupancy *= rookMagicNumbers[square];
|
||||
occupancy >>= (64 - rookRelevantBits[square]);
|
||||
|
||||
@@ -258,11 +258,11 @@ Bitboard getQueenAttack(int square, U64 occupancy) {
|
||||
U64 bishopOccupancy = occupancy;
|
||||
U64 rookOccupancy = occupancy;
|
||||
|
||||
bishopOccupancy &= bishopMasks[square].GetBoard();
|
||||
bishopOccupancy &= bishopMasks[square];
|
||||
bishopOccupancy *= bishopMagicNumbers[square];
|
||||
bishopOccupancy >>= (64 - bishopRelevantBits[square]);
|
||||
|
||||
rookOccupancy &= rookMasks[square].GetBoard();
|
||||
rookOccupancy &= rookMasks[square];
|
||||
rookOccupancy *= rookMagicNumbers[square];
|
||||
rookOccupancy >>= (64 - rookRelevantBits[square]);
|
||||
|
||||
@@ -302,266 +302,65 @@ static Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) {
|
||||
return pushes;
|
||||
}
|
||||
|
||||
// Returns bitboard of checkers
|
||||
static Bitboard Checkers(Board &board, bool color) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
Bitboard checkerBoard = 0ULL;
|
||||
|
||||
for (int type = Pawn; type < King; type++) {
|
||||
Bitboard targets = getPieceAttacks(kingSquare, type, color, board.occupied.GetBoard());
|
||||
checkerBoard |= targets & board.pieces[type] & board.colors[!color];
|
||||
}
|
||||
return checkerBoard;
|
||||
}
|
||||
|
||||
// Returns the direction to target from square
|
||||
static int GetDirection(int square, int target) {
|
||||
int rankDiff = (target / 8) - (square / 8);
|
||||
int fileDiff = (target % 8) - (square % 8);
|
||||
|
||||
// Main directions
|
||||
if (rankDiff == 0 && fileDiff < 0) {
|
||||
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 (abs(rankDiff) == abs(fileDiff)) {
|
||||
if (rankDiff < 0 && fileDiff < 0) {
|
||||
return soWe;
|
||||
} else if (rankDiff < 0 && fileDiff > 0) {
|
||||
return soEa;
|
||||
} else if (rankDiff > 0 && fileDiff < 0) {
|
||||
return noWe;
|
||||
} else if (rankDiff > 0 && fileDiff > 0) {
|
||||
return noEa;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Returns a bitboard of the squares from attacker to target
|
||||
static Bitboard SliderRayToSquare(int attacker, int target) {
|
||||
Bitboard ray;
|
||||
int direction = GetDirection(attacker, target);
|
||||
|
||||
if (direction) {
|
||||
while (attacker != target) {
|
||||
ray.SetBit(attacker);
|
||||
attacker += direction;
|
||||
}
|
||||
ray.SetBit(target);
|
||||
}
|
||||
|
||||
return ray;
|
||||
}
|
||||
|
||||
static Bitboard GetPinningRay(Board &board, int square, int direction) {
|
||||
Bitboard ray;
|
||||
int currentSquare = (board.pieces[King] & board.colors[board.sideToMove]).getLS1BIndex();
|
||||
int previousRank = currentSquare / 8;
|
||||
|
||||
currentSquare += direction;
|
||||
while (currentSquare < 64 && currentSquare >= 0) {
|
||||
int currentRank = currentSquare / 8;
|
||||
|
||||
int difference = abs(currentRank - previousRank);
|
||||
|
||||
if ((abs(direction) == 7 || abs(direction) == 9)
|
||||
&& difference != 1) {
|
||||
break;
|
||||
} else if (abs(direction) == 1 && difference) {
|
||||
break;
|
||||
}
|
||||
previousRank = currentRank;
|
||||
|
||||
ray.SetBit(currentSquare);
|
||||
if (board.occupied.IsSet(currentSquare)) {
|
||||
if (currentSquare == square) {
|
||||
currentSquare += direction;
|
||||
continue;
|
||||
}
|
||||
int attackerType = board.GetPieceType(currentSquare);
|
||||
int attackerColor = board.GetPieceColor(currentSquare);
|
||||
|
||||
if (attackerColor != board.sideToMove) {
|
||||
// Pinned by rook or queen
|
||||
if (abs(direction) == 1 || abs(direction) == 8) {
|
||||
if (attackerType == Rook || attackerType == Queen) {
|
||||
return ray;
|
||||
}
|
||||
// Pinned by bishop or queen
|
||||
} else if (direction % 2 != 0) {
|
||||
if (attackerType == Bishop || attackerType == Queen) {
|
||||
return ray;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
currentSquare += direction;
|
||||
}
|
||||
|
||||
return 0ULL;
|
||||
}
|
||||
|
||||
static bool IsPinned(Board &board, int square) {
|
||||
int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex();
|
||||
int directionToKing = GetDirection(square, kingSquare);
|
||||
|
||||
if (directionToKing) {
|
||||
// Checking if there are any pieces between square and kingSquare
|
||||
if ((SliderRayToSquare(square, kingSquare)
|
||||
& board.occupied).PopCount() != 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return GetPinningRay(board, square, -directionToKing).PopCount();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool IsLegalEnPassant(Board &board, int square) {
|
||||
Bitboard kingSquare = board.colors[board.sideToMove] & board.pieces[King];
|
||||
int pushDirection = board.sideToMove ? south : north;
|
||||
Bitboard newOccupancy = board.occupied;
|
||||
newOccupancy.PopBit(square);
|
||||
newOccupancy.PopBit(board.enPassantTarget - pushDirection);
|
||||
|
||||
// Looking for horizontal checks
|
||||
int directionToKing = GetDirection(square, kingSquare.getLS1BIndex());
|
||||
|
||||
// Either east or west
|
||||
if (abs(directionToKing) == 1) {
|
||||
int currentSq = kingSquare.getLS1BIndex() - directionToKing;
|
||||
|
||||
Bitboard endingFile = directionToKing > 0 ? files[H] : files[A];
|
||||
|
||||
while (!endingFile.IsSet(currentSq)) {
|
||||
if (newOccupancy.IsSet(currentSq)) {
|
||||
int squareColor = board.GetPieceColor(currentSq);
|
||||
int squarePiece = board.GetPieceType(currentSq);
|
||||
|
||||
if (squareColor != board.sideToMove) {
|
||||
if (squarePiece == Rook || squarePiece == Queen) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
currentSq -= directionToKing;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void GenPawnMoves(Board &board, bool color) {
|
||||
Bitboard pawns = board.colors[color] & board.pieces[Pawn];
|
||||
Bitboard pawns = board.pieces[Pawn] & board.colors[color];
|
||||
|
||||
while (pawns.GetBoard()) {
|
||||
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];
|
||||
Bitboard captures = pawnAttacks[color][square] & board.colors[!color];
|
||||
|
||||
if (IsPinned(board, square)) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
int directionToKing = GetDirection(square, kingSquare);
|
||||
if (seventhRank.IsSet(square)) {
|
||||
while (pushes) {
|
||||
int targetSquare = pushes.getLS1BIndex();
|
||||
|
||||
Bitboard pinningRay = GetPinningRay(board, square, -directionToKing);
|
||||
pushes &= pinningRay;
|
||||
captures &= pinningRay;
|
||||
board.AddMove(Move(square, targetSquare, queenPromotion));
|
||||
board.AddMove(Move(square, targetSquare, rookPromotion));
|
||||
board.AddMove(Move(square, targetSquare, bishopPromotion));
|
||||
board.AddMove(Move(square, targetSquare, knightPromotion));
|
||||
|
||||
// Checking for en passant
|
||||
if (captures.IsSet(board.enPassantTarget)) {
|
||||
if (IsLegalEnPassant(board, square)) {
|
||||
board.AddMove(Move(square, board.enPassantTarget, epCapture));
|
||||
}
|
||||
pushes.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
// Removing own pieces from capture mask
|
||||
captures &= board.colors[!color];
|
||||
|
||||
while (pushes.GetBoard()) {
|
||||
int pushSquare = pushes.getLS1BIndex();
|
||||
|
||||
if (abs(square - pushSquare) == 16) {
|
||||
board.AddMove(Move(square, pushSquare, doublePawnPush));
|
||||
} else {
|
||||
board.AddMove(Move(square, pushSquare, quiet));
|
||||
}
|
||||
|
||||
pushes.PopBit(pushSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
while (captures) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
|
||||
captures.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
pawns.PopBit(square);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Checking for en passant
|
||||
if (captures.IsSet(board.enPassantTarget)) {
|
||||
// Checking if the move gives discovered check to own king
|
||||
if (IsLegalEnPassant(board, square)) {
|
||||
board.AddMove(Move(square, board.enPassantTarget, epCapture));
|
||||
}
|
||||
}
|
||||
|
||||
// Removing own pieces from capture mask
|
||||
captures &= board.colors[!color];
|
||||
|
||||
Bitboard lastRank = color ? ranks[r_1] : ranks[r_8];
|
||||
|
||||
while (pushes.GetBoard()) {
|
||||
int pushSquare = pushes.getLS1BIndex();
|
||||
|
||||
// Checking for promotion
|
||||
if (lastRank.IsSet(pushSquare)) {
|
||||
board.AddMove(Move(square, pushSquare, knightPromotion));
|
||||
board.AddMove(Move(square, pushSquare, bishopPromotion));
|
||||
board.AddMove(Move(square, pushSquare, rookPromotion));
|
||||
board.AddMove(Move(square, pushSquare, queenPromotion));
|
||||
} else {
|
||||
if (abs(square - pushSquare) == 16) {
|
||||
board.AddMove(Move(square, pushSquare, doublePawnPush));
|
||||
} else {
|
||||
board.AddMove(Move(square, pushSquare, quiet));
|
||||
}
|
||||
}
|
||||
|
||||
pushes.PopBit(pushSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
// Checking for promotion
|
||||
if (lastRank.IsSet(targetSquare)) {
|
||||
board.AddMove(Move(square, targetSquare, knightPromoCapture));
|
||||
board.AddMove(Move(square, targetSquare, bishopPromoCapture));
|
||||
board.AddMove(Move(square, targetSquare, rookPromoCapture));
|
||||
board.AddMove(Move(square, targetSquare, queenPromoCapture));
|
||||
} else {
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
}
|
||||
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);
|
||||
}
|
||||
@@ -570,27 +369,14 @@ static void GenPawnMoves(Board &board, bool color) {
|
||||
static void GenKnightMoves(Board &board, bool color) {
|
||||
Bitboard knights = board.colors[color] & board.pieces[Knight];
|
||||
|
||||
while (knights.GetBoard()) {
|
||||
while (knights) {
|
||||
int square = knights.getLS1BIndex();
|
||||
|
||||
if (IsPinned(board, square)) {
|
||||
knights.PopBit(square);
|
||||
continue;
|
||||
}
|
||||
|
||||
Bitboard moves = knightAttacks[square] & ~board.colors[color];
|
||||
Bitboard captures = moves & board.colors[!color];
|
||||
moves &= ~captures;
|
||||
Bitboard quietMoves = moves & ~board.colors[!color];
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
while (captures) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
@@ -598,38 +384,29 @@ static void GenKnightMoves(Board &board, bool color) {
|
||||
captures.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (quietMoves) {
|
||||
int targetSquare = quietMoves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
quietMoves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
knights.PopBit(square);
|
||||
}
|
||||
}
|
||||
|
||||
static void GenRookMoves(Board &board, bool color) {
|
||||
Bitboard rooks = board.colors[color] & board.pieces[Rook];
|
||||
Bitboard rooks = board.pieces[Rook] & board.colors[color];
|
||||
|
||||
while (rooks.GetBoard()) {
|
||||
while (rooks) {
|
||||
int square = rooks.getLS1BIndex();
|
||||
|
||||
Bitboard moves = getRookAttack(square, board.occupied.GetBoard()) & ~board.colors[color];
|
||||
Bitboard moves = getRookAttack(square, board.occupied) & ~board.colors[color];
|
||||
Bitboard captures = moves & board.colors[!color];
|
||||
moves &= ~captures;
|
||||
Bitboard quietMoves = moves & ~board.colors[!color];
|
||||
|
||||
if (IsPinned(board, square)) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
int directionToKing = GetDirection(square, kingSquare);
|
||||
|
||||
Bitboard pinningRay = GetPinningRay(board, square, -directionToKing);
|
||||
|
||||
moves &= pinningRay;
|
||||
captures &= pinningRay;
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
while (captures) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
@@ -637,25 +414,12 @@ static void GenRookMoves(Board &board, bool color) {
|
||||
captures.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
rooks.PopBit(square);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
while (quietMoves) {
|
||||
int targetSquare = quietMoves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
|
||||
captures.PopBit(targetSquare);
|
||||
quietMoves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
rooks.PopBit(square);
|
||||
@@ -663,33 +427,16 @@ static void GenRookMoves(Board &board, bool color) {
|
||||
}
|
||||
|
||||
static void GenBishopMoves(Board &board, bool color) {
|
||||
Bitboard bishops = board.colors[color] & board.pieces[Bishop];
|
||||
Bitboard bishops = board.pieces[Bishop] & board.colors[color];
|
||||
|
||||
while (bishops.GetBoard()) {
|
||||
while (bishops) {
|
||||
int square = bishops.getLS1BIndex();
|
||||
|
||||
Bitboard moves = getBishopAttack(square, board.occupied.GetBoard()) & ~board.colors[color];
|
||||
Bitboard moves = getBishopAttack(square, board.occupied) & ~board.colors[color];
|
||||
Bitboard captures = moves & board.colors[!color];
|
||||
moves &= ~captures;
|
||||
Bitboard quietMoves = moves & ~board.colors[!color];
|
||||
|
||||
if (IsPinned(board, square)) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
int directionToKing = GetDirection(square, kingSquare);
|
||||
|
||||
Bitboard pinningRay = GetPinningRay(board, square, -directionToKing);
|
||||
|
||||
moves &= pinningRay;
|
||||
captures &= pinningRay;
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
while (captures) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
@@ -697,25 +444,12 @@ static void GenBishopMoves(Board &board, bool color) {
|
||||
captures.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
bishops.PopBit(square);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
while (quietMoves) {
|
||||
int targetSquare = quietMoves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
|
||||
captures.PopBit(targetSquare);
|
||||
quietMoves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
bishops.PopBit(square);
|
||||
@@ -723,33 +457,16 @@ static void GenBishopMoves(Board &board, bool color) {
|
||||
}
|
||||
|
||||
static void GenQueenMoves(Board &board, bool color) {
|
||||
Bitboard queens = board.colors[color] & board.pieces[Queen];
|
||||
Bitboard queens = board.pieces[Queen] & board.colors[color];
|
||||
|
||||
while (queens.GetBoard()) {
|
||||
while (queens) {
|
||||
int square = queens.getLS1BIndex();
|
||||
|
||||
Bitboard moves = getQueenAttack(square, board.occupied.GetBoard()) & ~board.colors[color];
|
||||
Bitboard moves = getQueenAttack(square, board.occupied) & ~board.colors[color];
|
||||
Bitboard captures = moves & board.colors[!color];
|
||||
moves &= ~captures;
|
||||
Bitboard quietMoves = moves & ~board.colors[!color];
|
||||
|
||||
if (IsPinned(board, square)) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
int directionToKing = GetDirection(square, kingSquare);
|
||||
|
||||
Bitboard pinningRay = GetPinningRay(board, square, -directionToKing);
|
||||
|
||||
moves &= pinningRay;
|
||||
captures &= pinningRay;
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
while (captures) {
|
||||
int targetSquare = captures.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, capture));
|
||||
@@ -757,232 +474,67 @@ static void GenQueenMoves(Board &board, bool color) {
|
||||
captures.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
queens.PopBit(square);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int targetSquare = moves.getLS1BIndex();
|
||||
while (quietMoves) {
|
||||
int targetSquare = quietMoves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(square, targetSquare, quiet));
|
||||
|
||||
moves.PopBit(targetSquare);
|
||||
quietMoves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
queens.PopBit(square);
|
||||
}
|
||||
}
|
||||
|
||||
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(square, targetSquare, capture));
|
||||
board.AddMove(Move(kingSquare, targetSquare, capture));
|
||||
|
||||
captures.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
queens.PopBit(square);
|
||||
while (quietMoves) {
|
||||
int targetSquare = quietMoves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(kingSquare, targetSquare, quiet));
|
||||
|
||||
quietMoves.PopBit(targetSquare);
|
||||
}
|
||||
|
||||
// Checking for king side castle
|
||||
if (board.castlingRights & (whiteKingRight | blackKingRight)) {
|
||||
Bitboard mask = color ? 0xe000000000000000 : 0xe0;
|
||||
|
||||
if ((board.occupied & mask).PopCount() == 1) {
|
||||
board.AddMove(Move(kingSquare, kingSquare + 2, kingCastle));
|
||||
}
|
||||
}
|
||||
|
||||
static void GenKingMoves(Board &board, bool color, bool isInCheck) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
// Checking for queen side castle
|
||||
if (board.castlingRights & (whiteQueenRight | blackQueenRight)) {
|
||||
Bitboard mask = color ? 0xf00000000000000 : 0xf;
|
||||
|
||||
Bitboard moves = (kingAttacks[kingSquare] & ~board.GetAttackMaps(!color)) & ~board.colors[color];
|
||||
Bitboard captures = moves & board.colors[!color];
|
||||
moves &= ~board.colors[!color];
|
||||
|
||||
while (moves.GetBoard()) {
|
||||
int square = moves.getLS1BIndex();
|
||||
|
||||
board.AddMove(Move(kingSquare, square, quiet));
|
||||
|
||||
moves.PopBit(square);
|
||||
}
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
int square = captures.getLS1BIndex();
|
||||
|
||||
Move move(kingSquare, square, capture);
|
||||
board.AddMove(move);
|
||||
|
||||
captures.PopBit(square);
|
||||
}
|
||||
|
||||
// Generating castles
|
||||
if (!isInCheck) {
|
||||
if (board.castlingRights[color * 2 + 1]) {
|
||||
U64 QueenSide = color ? 0x1f00000000000000 : 0x1f;
|
||||
U64 mask = color ? 0x1c00000000000000 : 0x1c;
|
||||
int targetSquare = color ? c8 : c1;
|
||||
if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2
|
||||
&& !(mask & board.GetAttackMaps(!color))) {
|
||||
board.AddMove(Move(kingSquare, targetSquare, queenCastle));
|
||||
}
|
||||
}
|
||||
|
||||
if (board.castlingRights[color * 2]) {
|
||||
U64 KingSide = color ? 0xf000000000000000 : 0xf0;
|
||||
U64 mask = color ? 0x7000000000000000 : 0x70;
|
||||
int targetSquare = color ? g8 : g1;
|
||||
if ((Bitboard(KingSide) & board.occupied).PopCount() == 2
|
||||
&& !(mask & board.GetAttackMaps(!color))) {
|
||||
board.AddMove(Move(kingSquare, targetSquare, kingCastle));
|
||||
}
|
||||
if ((board.occupied & mask).PopCount() == 1) {
|
||||
board.AddMove(Move(kingSquare, kingSquare - 2, queenCastle));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generates moves to get out of check
|
||||
static void GenCheckEvasions(Board &board, bool color) {
|
||||
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
|
||||
|
||||
// Adding moves that step out of check
|
||||
GenKingMoves(board, color, true);
|
||||
|
||||
Bitboard captureMask = Checkers(board, color); // Checker locations
|
||||
Bitboard pushMask; // Where blockers can move to
|
||||
if (captureMask.PopCount() == 1) {
|
||||
int checkerSquare = captureMask.getLS1BIndex();
|
||||
int checkerType = board.GetPieceType(checkerSquare);
|
||||
|
||||
if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) {
|
||||
pushMask = SliderRayToSquare(checkerSquare, kingSquare);
|
||||
pushMask.PopBit(kingSquare);
|
||||
pushMask.PopBit(checkerSquare);
|
||||
}
|
||||
|
||||
// looping through own pieces to add captures and blocks
|
||||
for (int type = Pawn; type < King; type++) {
|
||||
Bitboard currentPiece = board.colors[color] & board.pieces[type];
|
||||
|
||||
while (currentPiece.GetBoard()) {
|
||||
int square = currentPiece.getLS1BIndex();
|
||||
|
||||
if (IsPinned(board, square)) {
|
||||
currentPiece.PopBit(square);
|
||||
continue;
|
||||
}
|
||||
|
||||
Bitboard captures;
|
||||
Bitboard pushes;
|
||||
|
||||
if (type == Pawn) {
|
||||
pushes |= getPawnPushes(square, color, board.occupied) & pushMask;
|
||||
// Checking for en passant capture
|
||||
Bitboard attackedSquare = pawnAttacks[color][square]
|
||||
& Bitboard::GetSquare(board.enPassantTarget);
|
||||
if (attackedSquare.GetBoard()) {
|
||||
// Checking if it's legal
|
||||
if (IsLegalEnPassant(board, square)) {
|
||||
int direction = color ? south : north;
|
||||
int targetSquare = captureMask.getLS1BIndex() + direction;
|
||||
captureMask.SetBit(targetSquare);
|
||||
if ((attackedSquare & pushMask).GetBoard()
|
||||
|| (attackedSquare & captureMask).GetBoard()) {
|
||||
board.AddMove(Move(square, board.enPassantTarget, epCapture));
|
||||
}
|
||||
captureMask.PopBit(targetSquare);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pushes |= getPieceAttacks(square, type, color, board.occupied.GetBoard()) & pushMask;
|
||||
}
|
||||
|
||||
captures |= getPieceAttacks(square, type, color, board.occupied.GetBoard()) & captureMask;
|
||||
|
||||
Bitboard lastRank = color ? ranks[r_1] : ranks[r_8];
|
||||
|
||||
while (captures.GetBoard()) {
|
||||
int captureSquare = captures.getLS1BIndex();
|
||||
|
||||
// Checking for promotion
|
||||
if (type == Pawn) {
|
||||
if (lastRank.IsSet(captureSquare)) {
|
||||
board.AddMove(Move(square, captureSquare, knightPromoCapture));
|
||||
board.AddMove(Move(square, captureSquare, bishopPromoCapture));
|
||||
board.AddMove(Move(square, captureSquare, rookPromoCapture));
|
||||
board.AddMove(Move(square, captureSquare, queenPromoCapture));
|
||||
} else {
|
||||
board.AddMove(Move(square, captureSquare, capture));
|
||||
}
|
||||
} else {
|
||||
board.AddMove(Move(square, captureSquare, capture));
|
||||
}
|
||||
|
||||
captures.PopBit(captureSquare);
|
||||
}
|
||||
|
||||
while (pushes.GetBoard()) {
|
||||
int pushSquare = pushes.getLS1BIndex();
|
||||
|
||||
if (type == Pawn) {
|
||||
// Checking for promotion
|
||||
if (lastRank.IsSet(pushSquare)) {
|
||||
board.AddMove(Move(square, pushSquare, knightPromotion));
|
||||
board.AddMove(Move(square, pushSquare, bishopPromotion));
|
||||
board.AddMove(Move(square, pushSquare, rookPromotion));
|
||||
board.AddMove(Move(square, pushSquare, queenPromotion));
|
||||
} else {
|
||||
if (abs(square - pushSquare) == 16) {
|
||||
board.AddMove(Move(square, pushSquare, doublePawnPush));
|
||||
} else {
|
||||
board.AddMove(Move(square, pushSquare, quiet));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
board.AddMove(Move(square, pushSquare, quiet));
|
||||
}
|
||||
|
||||
pushes.PopBit(pushSquare);
|
||||
}
|
||||
|
||||
currentPiece.PopBit(square);
|
||||
}
|
||||
}
|
||||
} else { // We can only step out of double check
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GenAttackMaps(Board &board) {
|
||||
for (int color = White; color <= Black; color++) {
|
||||
for (int type = Pawn; type <= King; type++) {
|
||||
board.attackMaps[color][type] = 0ULL;
|
||||
Bitboard pieces = board.pieces[type] & board.colors[color];
|
||||
|
||||
while (pieces.GetBoard()) {
|
||||
int square = pieces.getLS1BIndex();
|
||||
if (type == Pawn) {
|
||||
board.attackMaps[color][type] |= pawnAttacks[color][square].GetBoard();
|
||||
} else if (type == Knight) {
|
||||
board.attackMaps[color][type] |= knightAttacks[square].GetBoard();
|
||||
} else if (type == King) {
|
||||
board.attackMaps[color][type] |= kingAttacks[square].GetBoard();
|
||||
} else if (type == Rook) {
|
||||
board.attackMaps[color][type] |= getRookAttack(square, (board.occupied
|
||||
& ~(board.colors[!color] & board.pieces[King])).GetBoard()).GetBoard();
|
||||
} else if (type == Bishop) {
|
||||
board.attackMaps[color][type] |= getBishopAttack(square, (board.occupied
|
||||
& ~(board.colors[!color] & board.pieces[King])).GetBoard()).GetBoard();
|
||||
} else if (type == Queen) {
|
||||
board.attackMaps[color][type] |= getQueenAttack(square, (board.occupied
|
||||
& ~(board.colors[!color] & board.pieces[King])).GetBoard()).GetBoard();
|
||||
}
|
||||
pieces.PopBit(square);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GenerateMoves(Board &board, bool side) {
|
||||
board.moveList.clear();
|
||||
|
||||
if (board.InCheck(side)) {
|
||||
GenCheckEvasions(board, side);
|
||||
return;
|
||||
}
|
||||
void GenerateMoves(Board &board) {
|
||||
bool side = board.sideToMove;
|
||||
board.ResetMoves();
|
||||
|
||||
GenPawnMoves(board, side);
|
||||
GenKnightMoves(board, side);
|
||||
GenKingMoves(board, side, false);
|
||||
GenKingMoves(board, side);
|
||||
GenBishopMoves(board, side);
|
||||
GenRookMoves(board, side);
|
||||
GenQueenMoves(board, side);
|
||||
|
||||
+1
-3
@@ -208,6 +208,4 @@ Bitboard getQueenAttack(int square, U64 occupancy);
|
||||
// Returns attacks regardless of board (so occupancy is 0ULL)
|
||||
Bitboard getPieceAttacks(int square, int piece, int color);
|
||||
|
||||
void GenAttackMaps(Board &board);
|
||||
|
||||
void GenerateMoves(Board &board, bool side);
|
||||
void GenerateMoves(Board &board);
|
||||
+9
-11
@@ -2,33 +2,31 @@
|
||||
#include "perft.h"
|
||||
|
||||
U64 HelperPerft(Board board, int depth) {
|
||||
GenerateMoves(board, board.sideToMove);
|
||||
GenerateMoves(board);
|
||||
|
||||
if (depth == 1) return board.moveList.size();
|
||||
if (depth == 1) return board.currentMoveIndex;
|
||||
if (depth == 0) return 1ULL;
|
||||
|
||||
U64 nodes = 0;
|
||||
|
||||
for (int i = 0; i < board.moveList.size(); i++) {
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
Board copy = board;
|
||||
board.MakeMove(board.moveList[i]);
|
||||
nodes += HelperPerft(board, depth - 1);
|
||||
board = copy;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
nodes += HelperPerft(copy, depth - 1);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
void Perft(Board &board, int depth) {
|
||||
GenerateMoves(board, board.sideToMove);
|
||||
GenerateMoves(board);
|
||||
|
||||
U64 totalNodes = 0;
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < board.moveList.size(); i++) {
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
Board copy = board;
|
||||
board.MakeMove(board.moveList[i]);
|
||||
U64 nodeCount = HelperPerft(board, depth - 1);
|
||||
board = copy;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
U64 nodeCount = HelperPerft(copy, depth - 1);
|
||||
totalNodes += nodeCount;
|
||||
std::cout << squareCoords[board.moveList[i].MoveFrom()]
|
||||
<< squareCoords[board.moveList[i].MoveTo()];
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@ int Quiescence(Board board, int alpha, int beta) {
|
||||
alpha = staticScore;
|
||||
}
|
||||
|
||||
GenerateMoves(board, board.sideToMove);
|
||||
GenerateMoves(board);
|
||||
|
||||
for (Move move : board.moveList) {
|
||||
if (move.IsCapture()) {
|
||||
@@ -40,7 +40,7 @@ int Quiescence(Board board, int alpha, int beta) {
|
||||
}
|
||||
|
||||
int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
|
||||
GenerateMoves(board, board.sideToMove);
|
||||
GenerateMoves(board);
|
||||
|
||||
if (board.moveList.size() == 0) {
|
||||
if (board.InCheck(board.sideToMove)) { // checkmate
|
||||
|
||||
@@ -20,6 +20,13 @@ enum PIECE {
|
||||
King
|
||||
};
|
||||
|
||||
enum CASTLING_RIGHTS {
|
||||
whiteKingRight = 1,
|
||||
whiteQueenRight = 2,
|
||||
blackKingRight = 4,
|
||||
blackQueenRight = 8
|
||||
};
|
||||
|
||||
constexpr std::array<const char, 12> PIECE_LETTERS = {
|
||||
'P', 'p',
|
||||
'N', 'n',
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
#include "search.h"
|
||||
|
||||
Move ParseMove(Board &board, const char* moveString) {
|
||||
GenerateMoves(board, board.sideToMove);
|
||||
GenerateMoves(board);
|
||||
|
||||
// Source square
|
||||
std::string fromString = "";
|
||||
|
||||
Reference in new issue
Block a user