Pseudo legal movegen #1
No files matched your search
@@ -17,7 +17,7 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o)))
|
|||||||
|
|
||||||
# Target executable
|
# Target executable
|
||||||
Eleanor$(EXE_EXT): $(OBJS)
|
Eleanor$(EXE_EXT): $(OBJS)
|
||||||
g++ -o $@ $^
|
g++ -o $@ $^
|
||||||
# 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 $@ $<
|
||||||
|
|||||||
@@ -36,9 +36,7 @@ void Bitboard::SetBit(int square) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Bitboard::PopBit(int square) {
|
void Bitboard::PopBit(int square) {
|
||||||
if (Bitboard(m_board).IsSet(square)) {
|
m_board &= ~(1ULL << square);
|
||||||
m_board &= (~Bitboard::GetSquare(square)).GetBoard();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bitboard::PopCount() const {
|
int Bitboard::PopCount() const {
|
||||||
@@ -115,4 +113,4 @@ Bitboard Bitboard::operator>>(int other) {
|
|||||||
|
|
||||||
Bitboard Bitboard::operator~() {
|
Bitboard Bitboard::operator~() {
|
||||||
return ~(m_board);
|
return ~(m_board);
|
||||||
}
|
}
|
||||||
+2
-6
@@ -11,7 +11,7 @@ public:
|
|||||||
|
|
||||||
Bitboard(const U64& board)
|
Bitboard(const U64& board)
|
||||||
: m_board(board) {}
|
: m_board(board) {}
|
||||||
|
|
||||||
U64 GetBoard() const;
|
U64 GetBoard() const;
|
||||||
bool IsSet(int square) const;
|
bool IsSet(int square) const;
|
||||||
void SetBit(int square);
|
void SetBit(int square);
|
||||||
@@ -22,10 +22,6 @@ 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) {
|
|
||||||
return (1ULL << square);
|
|
||||||
};
|
|
||||||
|
|
||||||
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 Bitboard& other) const;
|
Bitboard operator^(const Bitboard& other) const;
|
||||||
@@ -38,4 +34,4 @@ public:
|
|||||||
Bitboard operator<<(int other);
|
Bitboard operator<<(int other);
|
||||||
Bitboard operator>>(int other);
|
Bitboard operator>>(int other);
|
||||||
Bitboard operator~();
|
Bitboard operator~();
|
||||||
};
|
};
|
||||||
+9
-7
@@ -1,5 +1,4 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "movegen.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void Board::Init() {
|
void Board::Init() {
|
||||||
@@ -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;
|
||||||
@@ -236,18 +237,19 @@ void Board::PrintBoard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -410,4 +412,4 @@ void Board::MakeMove(Move move) {
|
|||||||
if (halfMoves & 2 == 0) fullMoves++;
|
if (halfMoves & 2 == 0) fullMoves++;
|
||||||
sideToMove = !attackerColor;
|
sideToMove = !attackerColor;
|
||||||
enPassantTarget = newEpTarget;
|
enPassantTarget = newEpTarget;
|
||||||
}
|
}
|
||||||
+3
-2
@@ -17,7 +17,8 @@ public:
|
|||||||
|
|
||||||
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;
|
||||||
@@ -46,4 +47,4 @@ public:
|
|||||||
|
|
||||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||||
void MakeMove(Move move);
|
void MakeMove(Move move);
|
||||||
};
|
};
|
||||||
+1
-1
@@ -16,4 +16,4 @@ int main() {
|
|||||||
Perft(board, 10);
|
Perft(board, 10);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+6
-6
@@ -18,7 +18,7 @@ static Bitboard notHGfile = notHfile & ~files[G];
|
|||||||
|
|
||||||
Bitboard maskPawnAttacks(int side, int square) {
|
Bitboard maskPawnAttacks(int side, int square) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = (1ULL << square);
|
||||||
|
|
||||||
// White
|
// White
|
||||||
if (!side) {
|
if (!side) {
|
||||||
@@ -35,7 +35,7 @@ Bitboard maskPawnAttacks(int side, int square) {
|
|||||||
|
|
||||||
Bitboard maskKnightAttacks(int square) {
|
Bitboard maskKnightAttacks(int square) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = (1ULL << square);
|
||||||
|
|
||||||
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
||||||
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
||||||
@@ -52,7 +52,7 @@ Bitboard maskKnightAttacks(int square) {
|
|||||||
|
|
||||||
Bitboard maskKingAttacks(int square) {
|
Bitboard maskKingAttacks(int square) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = (1ULL << square);
|
||||||
|
|
||||||
attacks |= (attacker << north);
|
attacks |= (attacker << north);
|
||||||
attacks |= (attacker << south);
|
attacks |= (attacker << south);
|
||||||
@@ -369,12 +369,12 @@ static void GenKingMoves(Board &board, bool color) {
|
|||||||
|
|
||||||
void GenerateMoves(Board &board) {
|
void GenerateMoves(Board &board) {
|
||||||
bool side = board.sideToMove;
|
bool side = board.sideToMove;
|
||||||
board.moveList.clear();
|
board.ResetMoves();
|
||||||
|
|
||||||
GenPawnMoves(board, side);
|
GenPawnMoves(board, side);
|
||||||
GenKnightMoves(board, side);
|
GenKnightMoves(board, side);
|
||||||
GenKingMoves(board, side);
|
GenKingMoves(board, side);
|
||||||
GenBishopMoves(board, side);
|
GenBishopMoves(board, side);
|
||||||
GenRookMoves(board, side);
|
GenRookMoves(board, side);
|
||||||
GenQueenMoves(board, side);
|
GenQueenMoves(board, side);
|
||||||
}
|
}
|
||||||
+8
-10
@@ -4,16 +4,15 @@
|
|||||||
U64 HelperPerft(Board board, int depth) {
|
U64 HelperPerft(Board board, int depth) {
|
||||||
GenerateMoves(board);
|
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;
|
||||||
}
|
}
|
||||||
@@ -24,11 +23,10 @@ void Perft(Board &board, int depth) {
|
|||||||
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()];
|
||||||
@@ -56,4 +54,4 @@ void Perft(Board &board, int depth) {
|
|||||||
std::cout << "Total nodes: " << totalNodes << '\n';
|
std::cout << "Total nodes: " << totalNodes << '\n';
|
||||||
std::cout << "Time took: " << elapsed_seconds << "s (";
|
std::cout << "Time took: " << elapsed_seconds << "s (";
|
||||||
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
|
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user