Pseudo legal movegen #1

Closed
rektdie wants to merge 6 commits from pseudoLegalMovegen into legalMovegen
pull from: pseudoLegalMovegen
13 changed files with 280 additions and 707 deletions

No files matched your search

+2 -3
View File
@@ -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
View File
@@ -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;
}
+11 -2
View File
@@ -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);
@@ -22,7 +21,7 @@ public:
int getLS1BIndex() const; int getLS1BIndex() const;
static Bitboard getOccupancy(int index, Bitboard attackMask); static Bitboard getOccupancy(int index, Bitboard attackMask);
static Bitboard GetSquare(int square) { static Bitboard GetSquare(int square) {
return (1ULL << square); return (1ULL << 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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -11,7 +11,9 @@ int main() {
Board board; Board board;
UCILoop(board); // UCILoop(board);
Perft(board, 6);
return 0; return 0;
} }
+188 -636
View File
File diff suppressed because it is too large. Load diff
+1 -3
View File
@@ -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
View File
@@ -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
View File
@@ -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
+7
View File
@@ -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
View File
@@ -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 = "";