From 21136a26f07e86d2f05aefb8cb4b096578ff1a4b Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Mon, 3 Feb 2025 00:08:37 +0100 Subject: [PATCH] implemented the last details to make everything work (hopefully) --- cpp/src/board.cpp | 27 +++++++++++++++++++++++++++ cpp/src/board.hpp | 18 +++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index 251f4d8..cb7f11b 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -292,3 +292,30 @@ bool Board::is_check_for(int8_t colour) const { } return false; } + +bool Board::no_legal_moves_for(int8_t colour) const { + for (int i = 0; i < 64; i++) { + if ((colour_at(i) == White && white_to_play) + || (colour_at(i) == Black && !white_to_play)) { + if (legal_moves(squares[i] & 0b111, *this, Coords::from_index(i)) + .size() + > 0) + return false; + } + } + return true; +} + +std::vector Board::all_legal_moves() const { + std::vector ret; + for (int i = 0; i < 64; i++) { + if ((colour_at(i) == White && white_to_play) + || (colour_at(i) == Black && !white_to_play)) { + std::vector moves = + legal_moves(squares[i] & 0b111, *this, Coords::from_index(i)); + if (moves.size() > 0) + ret.insert(ret.end(), moves.begin(), moves.end()); + } + } + return ret; +} diff --git a/cpp/src/board.hpp b/cpp/src/board.hpp index 9a04afb..9689271 100644 --- a/cpp/src/board.hpp +++ b/cpp/src/board.hpp @@ -8,7 +8,8 @@ struct Board { private: - int8_t get_king_of(int8_t colour) const; + int8_t get_king_of(int8_t) const; + bool no_legal_moves_for(int8_t) const; public: int8_t squares[64] = {Piece::None}; @@ -25,6 +26,21 @@ struct Board { std::string to_fen() const; bool is_check_for(int8_t) const; + std::vector all_legal_moves() const; + + bool is_checkmate_for(int8_t colour) const { + return is_checkmate_for(colour) && no_legal_moves_for(colour); + } + + bool is_stalemate_for(int8_t colour) const { + return !is_checkmate_for(colour) && no_legal_moves_for(colour); + } + + bool is_terminal() const { + return is_checkmate_for(White) || is_checkmate_for(Black) + || is_stalemate_for(White) || is_stalemate_for(Black); + } + int8_t colour_at(int8_t idx) const { return squares[idx] & 0b11000; }