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