stellar

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

game.cpp (3962B)


0 #include "game.hpp"
2 #include "board.hpp"
3 #include "logger.hpp"
4 #include "timer.hpp"
6 #include <format>
8 bool Game::san = true;
9 const std::string Game::startPosition = start_position;
11 uint16_t Game::id_t = 0;
12 Game::~Game() { logger::log(std::format("Game {}: destroyed", id), logger::Debug); }
13 Game::Game(const uint16_t match_id, const std::string white, const std::string black, const std::string fen)
14 : match_id(match_id), white(white), black(black), fen(fen) {
15 logger::log(std::format("Game {}: started", id), logger::Debug);
16 }
18 const std::string Game::get_moves(void) const {
19 std::string res;
20 if (list.size()) res += (std::string)list[0];
21 for (int i = 1; i < list.size(); i++)
22 res += " " + (std::string)list[i];
23 return res;
24 }
26 const std::string Game::to_san(const Board &board, const Move move) {
27 Board copy = board;
28 if (!move.make(copy)) {
29 logger::log("illegal move", logger::Critical);
30 throw std::runtime_error("illegal move");
31 }
33 if (move.is_castle_king()) return "O-O";
34 if (move.is_castle_queen()) return "O-O-O";
36 const piece::Type piece = board.get_square_piece_type(move.source());
37 const piece::Type target = board.get_square_piece_type(move.target());
39 std::string res;
40 if (piece != piece::PAWN) {
41 U64 potential = board.get_bitboard_square_land(move.target(), piece, board.get_side());
43 if (bit::count(potential) > 1) {
44 int file[9] = {0}, rank[9] = {0};
45 uint8_t square_i;
46 bitboard_for_each_bit(square_i, potential) {
47 const std::string crd = square::to_coordinates(static_cast<square::Square>(square_i));
48 file[crd[0] & 0x3]++;
49 rank[crd[1] & 0x3]++;
50 }
52 const std::string crd = square::to_coordinates(move.source());
53 if (file[crd[0] & 0x3] == 1) res += crd[0];
54 else if (rank[crd[1] & 0x3] == 1)
55 res += crd[1];
56 else
57 res += crd;
58 }
60 res += piece::get_code(piece, color::WHITE);
61 if (target != piece::NONE) res += "x";
62 res += square::to_coordinates(move.target());
63 } else {
64 if (target != piece::NONE) res += std::format("{}x", square::to_coordinates(move.source())[0]);
65 res += square::to_coordinates(move.target());
66 if (move.is_promote()) res += piece::get_code(move.promoted(), color::WHITE);
67 if (move.is_enpassant()) res += " e.p.";
68 }
70 if (!MoveList(copy, false, true).size()) res += "#";
71 else if (copy.is_check())
72 res += "+";
74 return res;
75 }
77 std::ostream &operator<<(std::ostream &os, const Game &game) {
78 static const std::string name[] = {"death", "time forfeit", "rules infraction", "repetition"};
79 os << std::format("[Event \"Match {}\"]", game.match_id);
80 os << std::format("\n[Site \"{}\"]", "Stellar Arena");
81 os << std::format("\n[Date \"{}\"]", timer::get_ms());
82 os << std::format("\n[Round \"{}\"]", game.id);
83 os << std::format("\n[White \"{}\"]", game.get_white());
84 os << std::format("\n[Black \"{}\"]", game.get_black());
85 os << std::format("\n[Result \"{}-{}\"]", (int)game.is_win_white(), (int)game.is_win_black());
86 os << std::format("\n[Termination \"{}\"]", name[to_underlying(game.get_terminate())]);
87 if (game.fen != Game::startPosition) {
88 os << std::format("\n[SetUp \"1\"]");
89 os << std::format("\n[FEN \"{}\"]", game.fen);
90 }
91 os << '\n';
93 if (!game.list.size()) return os;
94 Board board(game.fen);
96 const color::Color side = board.get_side();
97 if (side == color::BLACK) os << std::format("1. ... ");
98 for (int i = 0; i < game.list.size(); i++) {
99 if (i % 2 == to_underlying(side)) os << std::format("{}. ", i / 2 + 1);
100 os << std::format("{} ", Game::san ? Game::to_san(board, game.list[i]) : (std::string)game.list[i]);
101 game.list[i].make(board);
104 return os;