From d28008d913413f84dc663a98ffe01f16003486cd Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Fri, 7 Feb 2025 15:08:53 +0100 Subject: [PATCH] committing to stash and compare --- cpp/src/main.cpp | 4 +-- cpp/src/model/board/board.cpp | 59 ++++++++++++++++++++++++++++----- cpp/src/model/board/board.hpp | 26 ++++----------- cpp/src/model/perft/perft.cpp | 60 ++++++++++++++++++---------------- cpp/src/model/pieces/king.cpp | 9 +++-- cpp/src/model/pieces/piece.cpp | 4 ++- cpp/src/model/pieces/piece.hpp | 7 +++- 7 files changed, 105 insertions(+), 64 deletions(-) diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index f2979f7..ab1e7fd 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -31,8 +31,8 @@ int main(int argc, char* argv[]) { Controller& controller = manual; - controller.start(); + // controller.start(); - // perft(pos); + perft(); return 0; } diff --git a/cpp/src/model/board/board.cpp b/cpp/src/model/board/board.cpp index 44019c9..be21f03 100644 --- a/cpp/src/model/board/board.cpp +++ b/cpp/src/model/board/board.cpp @@ -94,10 +94,10 @@ Board Board::setup_fen_position(std::string fen) { index = fen.find(' ', index) + 1; board.n_full_moves = std::stoi(fen.substr(index)); - board.w_check = board.is_check_for(White); - board.b_check = board.is_check_for(Black); - board.w_nlm = board.no_legal_moves_for(White); - board.b_nlm = board.no_legal_moves_for(Black); + board.w_check = board._is_check_for(White); + board.b_check = board._is_check_for(Black); + board.w_nlm = board._no_legal_moves_for(White); + board.b_nlm = board._no_legal_moves_for(Black); return board; } @@ -175,7 +175,7 @@ std::string Board::to_fen() const { return ret; } -Board Board::make_move(Move move) const { +Board Board::make_move(Move move, bool recurse_call) const { Board ret; std::copy( std::begin(this->squares), @@ -278,6 +278,17 @@ Board Board::make_move(Move move) const { if (!white_to_play) ret.n_full_moves = n_full_moves + 1; + if (ret.n_half_moves > 20) { + std::cerr << "too many recursions" << std::endl; + exit(1); + } + + if (recurse_call) { + ret.w_check = ret._is_check_for(White); + ret.b_check = ret._is_check_for(Black); + ret.w_nlm = ret._no_legal_moves_for(White); + ret.b_nlm = ret._no_legal_moves_for(Black); + } return ret; } @@ -314,7 +325,7 @@ int8_t Board::get_king_of(int8_t colour) const { throw std::domain_error(ss.str()); } -bool Board::is_check_for(int8_t colour) const { +bool Board::_is_check_for(Colour colour) const { int8_t king_idx = this->get_king_of(colour); std::vector all_moves; all_moves.reserve(50); @@ -343,15 +354,23 @@ bool Board::is_check_for(int8_t colour) const { all_moves.insert(all_moves.end(), moves.begin(), moves.end()); } - for (const Move& move : all_moves) - if (move.target_square == king_idx) + for (const Move& move : all_moves) { + // if (colour == White + // && move.target_square + // == Coords::from_algebraic("b6").to_index()) + // std::cout << "am here" << std::endl; + if (move.target_square == king_idx) { + std::cout << "indeed check for " << to_string(colour) + << std::endl; return true; + } + } all_moves.clear(); } return false; } -bool Board::no_legal_moves_for(int8_t colour) const { +bool Board::_no_legal_moves_for(Colour colour) const { for (int i = 0; i < 64; i++) { if (colour_at(i) == colour) { std::vector moves = @@ -363,6 +382,28 @@ bool Board::no_legal_moves_for(int8_t colour) const { return true; } +bool Board::no_legal_moves_for(int8_t colour) const { + return colour == White ? w_nlm : b_nlm; +} + +bool Board::is_check_for(int8_t colour) const { + return colour == White ? w_check : b_check; +} + +bool Board::is_checkmate_for(Colour colour) const { + return no_legal_moves_for(colour) && is_check_for(colour); +} + +bool Board::is_stalemate_for(Colour colour) const { + return no_legal_moves_for(colour) && !is_check_for(colour); +} + +bool Board::is_terminal() const { + return n_half_moves == 100 || insufficient_material() || white_to_play + ? is_checkmate_for(White) || is_stalemate_for(White) + : is_checkmate_for(Black) || is_stalemate_for(Black); +} + std::vector Board::all_legal_moves() const { std::vector ret; for (int i = 0; i < 64; i++) { diff --git a/cpp/src/model/board/board.hpp b/cpp/src/model/board/board.hpp index a96b771..98a4703 100644 --- a/cpp/src/model/board/board.hpp +++ b/cpp/src/model/board/board.hpp @@ -9,7 +9,8 @@ struct Board { private: int8_t get_king_of(int8_t) const; - bool no_legal_moves_for(int8_t) const; + bool _no_legal_moves_for(Colour) const; + bool _is_check_for(Colour) const; bool w_nlm = false, b_nlm = false, w_check = false, b_check = false; public: @@ -23,8 +24,9 @@ struct Board { static Board setup_fen_position(std::string fen); - Board make_move(Move) const; + Board make_move(Move, bool = true) const; std::string to_fen() const; + bool no_legal_moves_for(int8_t) const; bool is_check_for(int8_t) const; bool insufficient_material_for(Colour) const; @@ -35,25 +37,11 @@ struct Board { std::vector all_legal_moves() const; - bool is_checkmate_for(int8_t colour) const { - if (colour == White) - return w_nlm && w_check; - return b_nlm && b_check; - // return no_legal_moves_for(colour) && is_check_for(colour); - } + bool is_checkmate_for(Colour) const; - bool is_stalemate_for(int8_t colour) const { - if (colour == White) - return w_nlm && !w_check; - return b_nlm && !b_check; - // return no_legal_moves_for(colour) && !is_check_for(colour); - } + bool is_stalemate_for(Colour) const; - bool is_terminal() const { - return n_half_moves == 100 || insufficient_material() || white_to_play - ? is_checkmate_for(White) || is_stalemate_for(White) - : is_checkmate_for(Black) || is_stalemate_for(Black); - } + bool is_terminal() const; inline Piece piece_at(int8_t idx) const { return (Piece) (squares[idx] & 0b00111); diff --git a/cpp/src/model/perft/perft.cpp b/cpp/src/model/perft/perft.cpp index 204af8a..d9fb38e 100644 --- a/cpp/src/model/perft/perft.cpp +++ b/cpp/src/model/perft/perft.cpp @@ -119,43 +119,45 @@ int move_generation_test( res.clear(); } - if (b.is_terminal()) - return 0; + if (b.is_checkmate_for(b.white_to_play ? White : Black) + || b.is_stalemate_for(b.white_to_play ? White : Black)) + return 1; if (depth == 0) return 1; std::vector moves = b.all_legal_moves(); - if (depth == 1) - return moves.size(); + // if (depth == 1) + // return moves.size(); int num_pos = 0; - if (depth == max_depth) { - // Parallel execution at the top level - std::vector> futures; - for (const Move& move : moves) { - Board tmp_board = b.make_move(move); - futures.push_back(pool.enqueue( - move_generation_test, - tmp_board, - depth - 1, - max_depth, - std::ref(pool) - )); - } - - for (auto& future : futures) - num_pos += future.get(); // Retrieve the result of each task - } else { - // Regular sequential execution - for (const Move& move : moves) { - Board tmp_board = b.make_move(move); - int n = move_generation_test(tmp_board, depth - 1, max_depth, pool); - if (depth == max_depth) - res << move << ": " << n << std::endl; - num_pos += n; - } + // if (depth == max_depth) { + // // Parallel execution at the top level + // std::vector> futures; + // for (const Move& move : moves) { + // Board tmp_board = b.make_move(move); + // futures.push_back(pool.enqueue( + // move_generation_test, + // tmp_board, + // depth - 1, + // max_depth, + // std::ref(pool) + // )); + // } + // + // for (auto& future : futures) + // num_pos += future.get(); // Retrieve the result of each task + // } else { + // Regular sequential execution + for (const Move& move : moves) { + std::cout << "Looking at " << move << std::endl; + Board tmp_board = b.make_move(move); + int n = move_generation_test(tmp_board, depth - 1, max_depth, pool); + if (depth == max_depth) + res << move << ": " << n << std::endl; + num_pos += n; } + // } return num_pos; } diff --git a/cpp/src/model/pieces/king.cpp b/cpp/src/model/pieces/king.cpp index ffdda83..00771ab 100644 --- a/cpp/src/model/pieces/king.cpp +++ b/cpp/src/model/pieces/king.cpp @@ -10,7 +10,7 @@ static bool is_clear_king_side(const Board& b, const Coords xy) { return false; std::optional move = move_for_position(b, xy, c); - Board board_after_move = b.make_move(move.value()); + Board board_after_move = b.make_move(move.value(), false); if (board_after_move.is_check_for(b.colour_at(xy))) return false; } @@ -24,7 +24,7 @@ static bool is_clear_queen_side(const Board& b, const Coords xy) { return false; std::optional move = move_for_position(b, xy, c); - Board board_after_move = b.make_move(move.value()); + Board board_after_move = b.make_move(move.value(), false); if (dx < 3 && board_after_move.is_check_for(b.colour_at(xy))) return false; } @@ -46,8 +46,11 @@ std::vector king_moves(const Board& b, const Coords xy) { } } - if (b.is_check_for(b.colour_at(xy))) + if (b.is_check_for(b.colour_at(xy))) { + std::cout << b.to_fen() << std::endl; + std::cout << "Check for " << to_string(b.colour_at(xy)) << std::endl; return keep_only_blocking(ret, b); + } // -- Castles int8_t castling_rights = b.colour_at(xy) == Colour::White diff --git a/cpp/src/model/pieces/piece.cpp b/cpp/src/model/pieces/piece.cpp index feff59b..b7ec4c2 100644 --- a/cpp/src/model/pieces/piece.cpp +++ b/cpp/src/model/pieces/piece.cpp @@ -12,9 +12,11 @@ keep_only_blocking(const std::vector candidates, const Board& board) { Colour my_colour = board.colour_at(candidates[0].source_square); std::vector ret; for (Move move : candidates) { - Board board_after_move = board.make_move(move); + Board board_after_move = board.make_move(move, false); if (!board_after_move.is_check_for(my_colour)) ret.push_back(move); + else + std::cout << "removing " << move << std::endl; } return ret; } diff --git a/cpp/src/model/pieces/piece.hpp b/cpp/src/model/pieces/piece.hpp index 802a17a..7911b1e 100644 --- a/cpp/src/model/pieces/piece.hpp +++ b/cpp/src/model/pieces/piece.hpp @@ -50,7 +50,12 @@ inline const char* to_string(Piece c) { } } -inline std::ostream& operator<<(std::ostream& os, const int8_t& i) { +inline std::ostream& operator<<(std::ostream& os, const Colour& i) { + os << std::to_string(i); + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const Piece& i) { os << std::to_string(i); return os; }