diff --git a/Makefile b/Makefile index 5b554bc..c391a73 100644 --- a/Makefile +++ b/Makefile @@ -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): diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 9738321..4448f7d 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -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 { diff --git a/source/board.cpp b/source/board.cpp index b531a49..598a89f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,7 +1,10 @@ #include "board.h" #include "movegen.h" -#include #include "tt.h" +#include +#include +#include +#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 tokens = split(fen, ' '); + std::vector 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) { diff --git a/source/board.h b/source/board.h index f1e097a..bb7ffdf 100644 --- a/source/board.h +++ b/source/board.h @@ -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); diff --git a/source/perft.cpp b/source/perft.cpp index b9ca8da..8c89554 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -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; } diff --git a/source/search.cpp b/source/search.cpp index 518371a..bb4274d 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -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() { diff --git a/source/types.h b/source/types.h index 7cc13b4..48d28f2 100644 --- a/source/types.h +++ b/source/types.h @@ -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, diff --git a/source/uci.cpp b/source/uci.cpp index 42480b0..36e8b5d 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -7,121 +7,31 @@ #include "movegen.h" #include "search.h" #include "benchmark.h" -#include #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 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; + } } } diff --git a/source/uci.h b/source/uci.h index a355491..a565f06 100644 --- a/source/uci.h +++ b/source/uci.h @@ -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 diff --git a/source/utils.cpp b/source/utils.cpp new file mode 100644 index 0000000..3911661 --- /dev/null +++ b/source/utils.cpp @@ -0,0 +1,68 @@ +#include "utils.h" + +std::vector split(std::string_view str, char delim) { + std::vector 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); +} diff --git a/source/utils.h b/source/utils.h new file mode 100644 index 0000000..6c23585 --- /dev/null +++ b/source/utils.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include +#include +#include +#include "move.h" +#include "board.h" + +std::vector split(std::string_view str, char delim); + +int parseSquare(std::string_view str); + +Move parseMove(Board &board, std::string_view str);