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):
|
||||
|
||||
+31
-10
@@ -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;
|
||||
@@ -115,4 +132,8 @@ Bitboard Bitboard::operator>>(int other) {
|
||||
|
||||
Bitboard Bitboard::operator~() {
|
||||
return ~(m_board);
|
||||
}
|
||||
}
|
||||
|
||||
Bitboard::operator uint64_t() {
|
||||
return m_board;
|
||||
}
|
||||
+13
-4
@@ -11,8 +11,7 @@ public:
|
||||
|
||||
Bitboard(const U64& board)
|
||||
: m_board(board) {}
|
||||
|
||||
U64 GetBoard() const;
|
||||
|
||||
bool IsSet(int square) const;
|
||||
void SetBit(int square);
|
||||
void PopBit(int square);
|
||||
@@ -22,7 +21,7 @@ public:
|
||||
int getLS1BIndex() const;
|
||||
static Bitboard getOccupancy(int index, Bitboard attackMask);
|
||||
|
||||
static Bitboard GetSquare(int square) {
|
||||
static Bitboard GetSquare(int square) {
|
||||
return (1ULL << square);
|
||||
};
|
||||
|
||||
@@ -30,12 +29,22 @@ public:
|
||||
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 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();
|
||||
};
|
||||
+23
-34
@@ -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);
|
||||
}
|
||||
}
|
||||
+4
-6
@@ -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);
|
||||
@@ -46,4 +44,4 @@ public:
|
||||
|
||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||
void MakeMove(Move move);
|
||||
};
|
||||
};
|
||||
+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
|
||||
|
||||
+4
-2
@@ -11,7 +11,9 @@ int main() {
|
||||
|
||||
Board board;
|
||||
|
||||
UCILoop(board);
|
||||
// UCILoop(board);
|
||||
|
||||
Perft(board, 6);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+193
-641
File diff suppressed because it is too large.
Load diff
+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);
|
||||
+10
-12
@@ -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()];
|
||||
@@ -56,4 +54,4 @@ void Perft(Board &board, int depth) {
|
||||
std::cout << "Total nodes: " << totalNodes << '\n';
|
||||
std::cout << "Time took: " << elapsed_seconds << "s (";
|
||||
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
|
||||
}
|
||||
}
|
||||
+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
|
||||
|
||||
+8
-1
@@ -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',
|
||||
@@ -87,4 +94,4 @@ enum DIRECTION {
|
||||
noEa = north + east,
|
||||
soWe = south + west,
|
||||
soEa = south + east
|
||||
};
|
||||
};
|
||||
+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