Io rework #15

Merged
rektdie merged 7 commits from IO_rework into legalMovegen 2025-03-12 19:42:20 +01:00
11 changed files with 168 additions and 304 deletions

No files matched your search

+2 -2
View File
@@ -29,11 +29,11 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o)))
# Target executable
$(EXE)$(EXE_EXT): $(OBJS)
$(CXX) -o $@ $^ -O3 -lpthread
$(CXX) -std=c++23 -o $@ $^ -O3 -lpthread
# Rule to compile source files to object files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
$(CXX) -c -o $@ $< -O3 -lpthread
$(CXX) -std=c++23 -c -o $@ $< -O3 -lpthread
# Create the object directory if it doesn't exist
$(OBJ_DIR):
+5 -5
View File
@@ -3,7 +3,7 @@
void Bitboard::PrintBoard() const {
for (int rank = 7; rank >= 0; rank--) {
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
std::cout << "| ";
for (int file = 0; file < 8; file++) {
@@ -15,12 +15,12 @@ void Bitboard::PrintBoard() const {
std::cout << ". | ";
}
}
std::cout << ' ' << rank + 1 << '\n';
std::cout << ' ' << rank + 1 << std::endl;
}
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << " a b c d e f g h\n\n";
std::cout << " Board: 0x" << std::hex << m_board << '\n';
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
std::cout << " a b c d e f g h" << std::endl << std::endl;
std::cout << " Board: 0x" << std::hex << m_board << std::endl;
}
bool Bitboard::IsSet(int square) const {
+42 -157
View File
@@ -1,7 +1,10 @@
#include "board.h"
#include "movegen.h"
#include <iostream>
#include "tt.h"
#include <iostream>
#include <ranges>
#include <string_view>
#include "utils.h"
static inline int prevEPTarget = noEPTarget;
@@ -24,174 +27,56 @@ void Board::Reset() {
hashKey = 0ULL;
}
void Board::SetByFen(const char* fen) {
void Board::SetByFen(std::string_view fen) {
Reset();
// Starting from top left
Bitboard currentSquare = 0x100000000000000;
int currSquare = a8;
int digits = 0;
while (*fen != ' ' && *fen) {
PIECE pieceType = Pawn;
COLOR pieceColor = Black;
std::vector<std::string> tokens = split(fen, ' ');
std::vector<std::string> pieceTokens = split(tokens[0], '/');
switch (*fen) {
case 'p':
pieceType = Pawn;
pieceColor = Black;
break;
case 'P':
pieceType = Pawn;
pieceColor = White;
break;
case 'n':
pieceType = Knight;
pieceColor = Black;
break;
case 'N':
pieceType = Knight;
pieceColor = White;
break;
case 'b':
pieceType = Bishop;
pieceColor = Black;
break;
case 'B':
pieceType = Bishop;
pieceColor = White;
break;
case 'r':
pieceType = Rook;
pieceColor = Black;
break;
case 'R':
pieceType = Rook;
pieceColor = White;
break;
case 'k':
pieceType = King;
pieceColor = Black;
break;
case 'K':
pieceType = King;
pieceColor = White;
break;
case 'q':
pieceType = Queen;
pieceColor = Black;
break;
case 'Q':
pieceType = Queen;
pieceColor = White;
break;
case '/':
currentSquare = currentSquare >> (8 + digits);
digits = 0;
fen++;
continue;
default:
int num = (*fen) - 48;
fen++;
if (*fen != '/') {
currentSquare = currentSquare << num;
digits += num;
constexpr std::string_view pieceTypes = "pnbrqk";
for (std::string_view rank : pieceTokens) {
for (const char piece : rank) {
if (std::isdigit(piece)) {
currSquare += piece - '0';
continue;
}
continue;
}
pieces[pieceType] |= currentSquare;
colors[pieceColor] |= currentSquare;
bool side = std::islower(piece);
int pieceType = pieceTypes.find(std::tolower(piece));
fen++;
if (*fen != '/') {
digits++;
currentSquare = currentSquare << 1;
colors[side].SetBit(currSquare);
pieces[pieceType].SetBit(currSquare);
currSquare++;
}
currSquare -= 16;
}
fen++; // skipping previous space
int section = 0;
sideToMove = tokens[1] == "b";
while (*fen) {
switch (*fen) {
case ' ':
fen++;
section++;
continue;
case 'w':
if (section == 0) {
sideToMove = White;
}
break;
case 'b':
if (section == 0) {
sideToMove = Black;
}
break;
case 'K':
castlingRights |= whiteKingRight;
break;
case 'Q':
castlingRights |= whiteQueenRight;
break;
case 'k':
castlingRights |= blackKingRight;
break;
case 'q':
castlingRights |= blackQueenRight;
break;
default:
if (*fen != '-') {
if (section == 2) {
int passantFile;
switch (*fen)
{
case 'a':
passantFile = 0;
break;
case 'b':
passantFile = 1;
break;
case 'c':
passantFile = 2;
break;
case 'd':
passantFile = 3;
break;
case 'e':
passantFile = 4;
break;
case 'f':
passantFile = 5;
break;
case 'g':
passantFile = 6;
break;
case 'h':
passantFile = 7;
break;
}
fen++;
const int passantRank = *fen - 49;
enPassantTarget = passantFile + passantRank*8;
}
}
for (const char piece : tokens[2]) {
if (piece == 'K') castlingRights |= whiteKingRight;
else if (piece == 'Q') castlingRights |= whiteQueenRight;
else if (piece == 'k') castlingRights |= blackKingRight;
else if (piece == 'q') castlingRights |= blackQueenRight;
}
break;
}
fen++;
}
if (tokens[3] != "-") enPassantTarget = parseSquare(tokens[3]);
occupied = (colors[White] | colors[Black]);
GenAttackMaps(*this);
occupied = colors[White] | colors[Black];
hashKey = GetHashKey(*this);
GenAttackMaps(*this);
}
void Board::PrintBoard() {
for (int rank = 7; rank >= 0; rank--) {
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
std::cout << "| ";
for (int file = 0; file < 8; file++) {
@@ -212,21 +97,21 @@ void Board::PrintBoard() {
std::cout << " | ";
}
}
std::cout << ' ' << rank + 1 << '\n';
std::cout << ' ' << rank + 1 << std::endl;
}
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << " a b c d e f g h\n\n";
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
std::cout << " a b c d e f g h" << std::endl << std::endl;
std::cout << " Side to move: ";
if (!sideToMove) {
std::cout << "White\n";
std::cout << "White" << std::endl;
} else {
std::cout << "Black\n";
std::cout << "Black" << std::endl;
}
if (enPassantTarget != noEPTarget) {
std::cout << " En Passant square: " << squareCoords[enPassantTarget] << '\n';
std::cout << " En Passant square: " << squareCoords[enPassantTarget] << std::endl;
} else {
std::cout << " En Passant square: None" << '\n';
std::cout << " En Passant square: None" << std::endl;
}
std::cout << " Castling rights: ";
@@ -234,9 +119,9 @@ void Board::PrintBoard() {
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\n";
std::cout << std::endl << std::endl;
std::cout << " Hashkey: " << std::hex << hashKey << '\n';
std::cout << " Hashkey: 0x" << std::hex << hashKey << std::dec << std::endl;
}
void Board::AddMove(Move move) {
+1 -1
View File
@@ -31,7 +31,7 @@ public:
void Init();
void Reset();
void SetByFen(const char* fen);
void SetByFen(std::string_view fen);
void PrintBoard();
void AddMove(Move move);
+4 -4
View File
@@ -32,11 +32,11 @@ void Perft(Board &board, int depth) {
totalNodes += nodeCount;
board.moveList[i].PrintMove();
std::cout << ": " << nodeCount << '\n';
std::cout << ": " << nodeCount << std::endl;
}
std::cout << "\nDepth: " << depth << '\n';
std::cout << "Total nodes: " << totalNodes << '\n';
std::cout << std::endl << "Depth: " << depth << std::endl;
std::cout << "Total nodes: " << totalNodes << std::endl;
std::cout << "Time took: " << sw.GetElapsedSec() << "s (";
std::cout << int(totalNodes / sw.GetElapsedSec()) << " nodes/sec)\n";
std::cout << int(totalNodes / sw.GetElapsedSec()) << " nodes/sec)" << std::endl;
}
+1 -1
View File
@@ -314,7 +314,7 @@ void SearchPosition(Board &board, SearchParams params) {
std::cout << "bestmove ";
results.bestMove.PrintMove();
std::cout << '\n';
std::cout << std::endl;
}
void StopSearch() {
+3 -3
View File
@@ -4,9 +4,9 @@
using U64 = uint64_t;
constexpr char StartingFen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
constexpr char KiwiPeteFen[] = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -";
constexpr char KillerFen[] = "6k1/3q1pp1/pp5p/1r5n/8/1P3PP1/PQ4BP/2R3K1 w - - 0 1";
constexpr std::string_view StartingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
constexpr std::string_view KiwiPeteFen = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -";
constexpr std::string_view KillerFen = "6k1/3q1pp1/pp5p/1r5n/8/1P3PP1/PQ4BP/2R3K1 w - - 0 1";
enum COLOR {
White,
+28 -127
View File
@@ -7,121 +7,31 @@
#include "movegen.h"
#include "search.h"
#include "benchmark.h"
#include <cstring>
#include "tt.h"
#include "perft.h"
#include "utils.h"
Move ParseMove(Board &board, const char* moveString) {
GenerateMoves(board, board.sideToMove);
// Source square
std::string fromString = "";
fromString += moveString[0];
fromString += moveString[1];
auto it = std::find(squareCoords.begin(), squareCoords.end(), fromString);
int from = std::distance(squareCoords.begin(), it);
// Target square
std::string toString = "";
toString += moveString[2];
toString += moveString[3];
it = std::find(squareCoords.begin(), squareCoords.end(), toString);
int to = std::distance(squareCoords.begin(), it);
std::string promotionString = "";
// Finding the move from available moves
for (Move move : board.moveList) {
if (move.MoveFrom() == from && move.MoveTo() == to) {
// Checking for promotion
if (std::string(moveString).length() == 5) {
int promotePiece = 0;
// Checking for capture promotion
if (board.GetPieceColor(from) != board.GetPieceColor(to)) {
switch (moveString[4])
{
case 'r':
promotePiece = rookPromoCapture;
break;
case 'q':
promotePiece = queenPromoCapture;
break;
case 'b':
promotePiece = bishopPromoCapture;
break;
case 'n':
promotePiece = knightPromoCapture;
break;
}
} else {
switch (moveString[4])
{
case 'r':
promotePiece = rookPromotion;
break;
case 'q':
promotePiece = queenPromotion;
break;
case 'b':
promotePiece = bishopPromotion;
break;
case 'n':
promotePiece = knightPromotion;
break;
}
}
return Move(from, to, promotePiece);
}
return move;
}
}
return Move();
}
void ParsePosition(Board &board, const char* command) {
command += 9; // Skip "position"
if (strncmp(command, "fen", 3) == 0) {
command += 4; // Skip "fen "
std::string fen;
while (*command && strncmp(command, " moves", 6) != 0) {
fen += *command;
command++;
}
board.SetByFen(fen.c_str());
if (strncmp(command, " moves", 6) == 0) {
command += 6; // Skip " moves"
}
} else if (strncmp(command, "startpos", 8) == 0) {
command += 8; // Skip "startpos"
void ParsePosition(Board &board, std::string_view command) {
if (command.find("startpos") != std::string::npos) {
board.SetByFen(StartingFen);
}
if (strncmp(command, " moves", 6) == 0) {
command += 6; // Skip " moves"
int fenIndex = command.find("fen");
int movesIndex = command.find("moves");
if (fenIndex != std::string::npos) {
if (movesIndex != std::string::npos) {
board.SetByFen(command.substr(fenIndex + 4, movesIndex - 2 - (fenIndex + 3)));
} else {
board.SetByFen(command.substr(fenIndex + 4, command.length() - (fenIndex + 3)));
}
}
while (*command) {
while (*command == ' ') command++; // Skip spaces
if (movesIndex != std::string::npos) {
std::vector<std::string> moves = split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' ');
std::string moveString;
while (*command && *command != ' ') {
moveString += *command;
command++;
}
if (!moveString.empty()) {
board.MakeMove(ParseMove(board, moveString.c_str()));
for (std::string_view move : moves) {
board.MakeMove(parseMove(board, move));
}
}
}
@@ -170,37 +80,23 @@ static void SetOption(std::string &command) {
}
static void PrintEngineInfo() {
std::cout << "id name Eleanor\n";
std::cout << "id author rektdie\n";
std::cout << "option name Hash type spin default 5 min 1 max 64\n";
std::cout << "option name Threads type spin default 1 min 1 max 1\n";
std::cout << "uciok\n";
std::cout << "id name Eleanor" << std::endl;
std::cout << "id author rektdie" << std::endl;
std::cout << "option name Hash type spin default 5 min 1 max 64" << std::endl;
std::cout << "option name Threads type spin default 1 min 1 max 1" << std::endl;
std::cout << "uciok" << std::endl;
}
void UCILoop(Board &board) {
// reset STDIN & STDOUT buffers
setbuf(stdin, NULL);
setbuf(stdout, NULL);
// define user / GUI input buffer
std::string input = "";
// main loop
while (true) {
// reset user / GUI input
getline(std::cin, input);
// make sure output reaches the GUI
fflush(stdout);
// make sure input is available
if (input[0] == '\n') {
continue;
}
std::getline(std::cin, input);
// parse UCI "isready" command
if (input.find("isready") != std::string::npos) {
std::cout << "readyok\n";
std::cout << "readyok" << std::endl;
continue;
}
@@ -258,5 +154,10 @@ void UCILoop(Board &board) {
Perft(board, ReadParam("perft", input));
continue;
}
if (input.find("print") != std::string::npos) {
board.PrintBoard();
continue;
}
}
}
+1 -4
View File
@@ -13,11 +13,8 @@ public:
SearchParams(){}
};
// Returns Move object from a string
Move ParseMove(Board &board, const char* moveString);
// Initializes the position with given parameters
void ParsePosition(Board &board, const char* command);
void ParsePosition(Board &board, std::string_view command);
// Parse UCI "go" command
// GUI sends the engine this command if it should start searching for the best move
+68
View File
@@ -0,0 +1,68 @@
#include "utils.h"
std::vector<std::string> split(std::string_view str, char delim) {
std::vector<std::string> results;
std::istringstream stream(std::string{str});
for (std::string token{}; std::getline(stream, token, delim);) {
if (token.empty()) continue;
results.push_back(token);
}
return results;
}
int parseSquare(std::string_view str) {
constexpr std::string_view files = "abcdefgh";
int file = files.find(str[0]);
int rank = str[1] - '0' - 1;
return file + rank * 8;
}
Move parseMove(Board &board, std::string_view str) {
int from = parseSquare(str.substr(0, 2));
int to = parseSquare(str.substr(2, 2));
int flag = 0;
// Promotion
if (str.length() > 4) {
if (board.GetPieceColor(from) != board.GetPieceColor(to)) {
switch(str[4]) {
case 'r':
flag = rookPromoCapture;
break;
case 'q':
flag = queenPromoCapture;
break;
case 'b':
flag = bishopPromoCapture;
break;
case 'n':
flag = knightPromoCapture;
break;
}
} else {
switch(str[4]) {
case 'r':
flag = rookPromotion;
break;
case 'q':
flag = queenPromotion;
break;
case 'b':
flag = bishopPromotion;
break;
case 'n':
flag = knightPromotion;
break;
}
}
}
return Move(from, to, flag);
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <vector>
#include <string>
#include <string_view>
#include <sstream>
#include "move.h"
#include "board.h"
std::vector<std::string> split(std::string_view str, char delim);
int parseSquare(std::string_view str);
Move parseMove(Board &board, std::string_view str);