stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

commit b11e6be8d05fb81b7823e182e9afe7ca18c85486
parent 5dcc9ef4920e4b1ca8a653d066c9f4ace83f315c
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 13 Mar 2024 23:52:22 +0000

Small fixup

Diffstat:
Msrc/arena/arena.cpp | 4++--
Msrc/arena/game.cpp | 7++++---
Msrc/arena/game.hpp | 2+-
Msrc/arena/match.cpp | 4++--
Msrc/arena/match.hpp | 4++--
Msrc/bitboard/CMakeLists.txt | 1+
Msrc/board/board.cpp | 10++++++++--
Msrc/board/board.hpp | 13++-----------
Msrc/engine/score.hpp | 2++
Msrc/utils/bit.hpp | 6+++---
Msrc/utils/utils.hpp | 4++--
11 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/src/arena/arena.cpp b/src/arena/arena.cpp @@ -24,7 +24,7 @@ class Arena { logger::log(std::format("Arena {}: destroyed", id), logger::Debug); } - void operator()(const std::vector<std::string> positions, Match::Settings swhite, + void operator()(const std::vector<std::string> &positions, Match::Settings swhite, Match::Settings sblack) { Match match(*engine1, *engine2); for (const std::string &fen : positions) { @@ -36,7 +36,7 @@ class Arena { } private: - static void print(const Game game) { + static void print(const Game &game) { mutex.lock(); std::cout << game << std::endl; mutex.unlock(); diff --git a/src/arena/game.cpp b/src/arena/game.cpp @@ -12,7 +12,8 @@ const std::string Game::startPosition = start_position; uint16_t Game::id_t = 0; Game::~Game() { logger::log(std::format("Game {}: destroyed", id), logger::Debug); } -Game::Game(const uint16_t match_id, const std::string white, const std::string black, const std::string fen) +Game::Game(const uint16_t match_id, const std::string &white, const std::string &black, + const std::string &fen) : match_id(match_id), white(white), black(black), fen(fen) { logger::log(std::format("Game {}: started", id), logger::Debug); } @@ -60,10 +61,10 @@ const std::string Game::to_san(const Board &board, const Move move) { } res += piece::get_code(piece, WHITE); - if (target != Type::NONE) res += "x"; + if (target != Type::NO_TYPE) res += "x"; res += to_coordinates(move.target()); } else { - if (target != Type::NONE) res += std::format("{}x", to_coordinates(move.source())[0]); + if (target != Type::NO_TYPE) res += std::format("{}x", to_coordinates(move.source())[0]); res += to_coordinates(move.target()); if (move.is_promote()) res += piece::get_code(move.promoted(), WHITE); if (move.is_enpassant()) res += " e.p."; diff --git a/src/arena/game.hpp b/src/arena/game.hpp @@ -13,7 +13,7 @@ class Game { Repetition, }; - Game(const uint16_t match_id, const std::string white, const std::string black, const std::string fen); + Game(const uint16_t match_id, const std::string &white, const std::string &black, const std::string &fen); ~Game(); void play(const Move move) { list.push(move); } diff --git a/src/arena/match.cpp b/src/arena/match.cpp @@ -10,7 +10,7 @@ Match::Match(Engine &white, Engine &black) : engines({&white, &black}) { logger::log(std::format("Match {}: created", id), logger::Debug); } -Game Match::play(Settings swhite, Settings sblack, const std::string fen = Game::startPosition) { +Game Match::play(Settings swhite, Settings sblack, const std::string &fen = Game::startPosition) { const std::string position = "position " + (fen == Game::startPosition ? "startpos" : "fen " + fen); repetition::Table rtable; @@ -107,7 +107,7 @@ std::string Match::get_go(Settings &swhite, Settings &sblack, Color side) { return go; } -Move Match::parse_move(const MoveList list, const std::string &move_string) { +Move Match::parse_move(const MoveList &list, const std::string &move_string) { const Square source = from_coordinates(move_string.substr(0, 2)); const Square target = from_coordinates(move_string.substr(2, 2)); diff --git a/src/arena/match.hpp b/src/arena/match.hpp @@ -14,11 +14,11 @@ class Match { Match(Engine &white, Engine &black); ~Match(); - Game play(Settings swhite, Settings sblack, const std::string fen); + Game play(Settings swhite, Settings sblack, const std::string &fen); private: static std::string get_go(Settings &swhite, Settings &sblack, Color side); - static Move parse_move(const MoveList list, const std::string &move_string); + static Move parse_move(const MoveList &list, const std::string &move_string); std::array<Engine *, 2> engines; diff --git a/src/bitboard/CMakeLists.txt b/src/bitboard/CMakeLists.txt @@ -1,2 +1,3 @@ add_library(bitboard INTERFACE) +target_link_libraries(bitboard INTERFACE utils) target_include_directories(bitboard INTERFACE .) diff --git a/src/board/board.cpp b/src/board/board.cpp @@ -5,6 +5,7 @@ #include "board.hpp" #include "piece.hpp" +#include "utils.hpp" #include "utils_ui.hpp" #include "zobrist.hpp" @@ -64,8 +65,13 @@ std::ostream &operator<<(std::ostream &os, const Board &board) { for (int file = 0; file < 8; file++) { if (!file) os << 8 - rank << " "; auto square = static_cast<Square>((7 - rank) * 8 + file); - const piece::Piece *piece = board.get_square_piece(square); - os << (piece ? piece->code : '.') << " "; + const Type t = board.get_square_piece_type(square); + if (t == NO_TYPE) { + os << ". "; + } else { + const Color c = board.get_square_piece_color(square); + os << piece::get_code(t, c) << " "; + } } printf("\n"); } diff --git a/src/board/board.hpp b/src/board/board.hpp @@ -45,7 +45,6 @@ class Board { [[nodiscard]] inline constexpr Color get_square_piece_color(Square square) const; [[nodiscard]] inline constexpr Type get_square_piece_type(Square square) const; - [[nodiscard]] inline constexpr const piece::Piece *get_square_piece(Square square) const; /* Setters */ @@ -110,22 +109,14 @@ constexpr U64 Board::get_bitboard_square_land(Square land, Type piece, Color sid constexpr Color Board::get_square_piece_color(Square square) const { if (bit::get(colors[WHITE], square)) return WHITE; if (bit::get(colors[BLACK], square)) return BLACK; - throw std::exception(); + return COLOR_NB; } constexpr Type Board::get_square_piece_type(Square square) const { for (Type type = PAWN; type <= KING; ++type) { if (bit::get(pieces[type], square)) return type; } - return Type::NONE; -} - -constexpr const piece::Piece *Board::get_square_piece(Square square) const { - try { - return &piece::get(get_square_piece_type(square), get_square_piece_color(square)); - } catch (std::exception &e) { - return nullptr; - } + return Type::NO_TYPE; } /* Setters */ diff --git a/src/engine/score.hpp b/src/engine/score.hpp @@ -1,6 +1,8 @@ #ifndef STELLAR_SCORE_H #define STELLAR_SCORE_H +#include <array> + #include "piece.hpp" #include "utils.hpp" #define MAX_PLY 64 diff --git a/src/utils/bit.hpp b/src/utils/bit.hpp @@ -6,13 +6,13 @@ namespace bit { -inline constexpr bool get(const U64 &bitboard, uint8_t square) { return (bitboard >> (square)) & C64(1); } +inline constexpr bool get(const U64 &bitboard, uint8_t square) { return (bitboard >> square) & C64(1); } inline constexpr void set(U64 &bitboard, uint8_t square) { bitboard |= (C64(1) << square); } -inline constexpr void pop(U64 &bitboard, uint8_t square) { bitboard &= ~(C64(1) << (square)); } +inline constexpr void pop(U64 &bitboard, uint8_t square) { bitboard &= ~(C64(1) << square); } inline constexpr uint8_t count(U64 bitboard) { return std::popcount(bitboard); } inline constexpr uint8_t lsb_index(U64 bitboard) { return std::countr_zero(bitboard); } -inline constexpr U64 &lsb_pop(U64 &bitboard) { return bitboard &= bitboard & (bitboard - 1); } +inline constexpr U64 &lsb_pop(U64 &bitboard) { return bitboard = bitboard & (bitboard - 1); } #define bitboard_for_each_bit(var, bb) \ for (var = bit::lsb_index(bb); bb; bit::lsb_pop(bb), var = bit::lsb_index(bb)) diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp @@ -21,7 +21,7 @@ enum Color { COLOR_NB = 2 }; -inline constexpr const Color other(const Color color) { return color == WHITE ? BLACK : WHITE; } +inline constexpr const Color other(const Color color) { return static_cast<Color>(!color); } /* Square */ @@ -66,7 +66,7 @@ enum Type { ROOK, QUEEN, KING, - NONE = 7, + NO_TYPE, }; ENABLE_INCR_OPERATORS_ON(Type)