diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index ae39018..1dfa233 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -1,7 +1,11 @@ #include "board.hpp" +#include "pieces/piece.hpp" + +#include #include #include +#include Board Board::setup_fen_position(std::string fen) { Board board; @@ -37,7 +41,7 @@ Board Board::setup_fen_position(std::string fen) { return board; } -std::string Board::to_fen() { +std::string Board::to_fen() const { std::map p2c{ {Piece::King, 'k'}, {Piece::Pawn, 'p'}, @@ -76,7 +80,7 @@ std::string Board::to_fen() { return ret; } -Board Board::make_move(Move move) { +Board Board::make_move(Move move) const { int8_t dest_piece = this->squares[move.target_square]; Board ret; @@ -96,3 +100,33 @@ Board Board::make_move(Move move) { ret.squares[move.target_square - 8] = Piece::None; return ret; } + +int8_t Board::get_king_of(int8_t colour) const { + for (int i = 0; i < 64; i++) + if (this->squares[i] == (colour | Piece::King)) + return i; + throw std::domain_error( + "Apparently there no kings of the such color in this board" + ); +} + +std::vector to_target_square(std::vector moves) { + std::vector ret; + for (Move move : moves) + ret.push_back(move.target_square); + return ret; +} + +bool Board::is_check_for(int8_t colour) const { + int8_t king_idx = this->get_king_of(colour); + for (int i = 0; i < 64; i++) { + if (this->squares[i] == Piece::None) + continue; + std::vector moves = + legal_moves(this->squares[i], *this, Coords::from_index(i), true); + std::vector targets = to_target_square(moves); + if (std::find(targets.begin(), targets.end(), i) != targets.end()) + return true; + } + return false; +} diff --git a/cpp/src/board.hpp b/cpp/src/board.hpp index a239783..0936109 100644 --- a/cpp/src/board.hpp +++ b/cpp/src/board.hpp @@ -17,12 +17,18 @@ struct Coords { }; struct Board { + private: + int8_t get_king_of(int8_t colour) const; + + public: int8_t squares[64] = {Piece::None}; bool white_to_play; int8_t w_castle_rights; int8_t b_castle_rights; + static Board setup_fen_position(std::string fen); - Board make_move(Move); - std::string to_fen(); + Board make_move(Move) const; + std::string to_fen() const; + bool is_check_for(int8_t) const; }; diff --git a/cpp/src/pieces/piece.cpp b/cpp/src/pieces/piece.cpp index 9138015..b849c1d 100644 --- a/cpp/src/pieces/piece.cpp +++ b/cpp/src/pieces/piece.cpp @@ -2,11 +2,23 @@ #include "../board.hpp" +std::vector +keep_only_blocking(const std::vector candidates, const Board& board) { + if (candidates.size() == 0) + return {}; + + int8_t my_colour = board.squares[candidates[0].source_square] & 0b11000; + std::vector ret; + for (Move move : candidates) { + Board board_after_move = board.make_move(move); + if (!board_after_move.is_check_for(my_colour)) + ret.push_back(move); + } + return ret; +} + std::vector legal_moves( - const Piece p, - const Board& b, - const Coords xy, - bool looking_for_check = false + int8_t p, const Board& b, const Coords xy, bool looking_for_check = false ) { std::vector ret; switch (p) { diff --git a/cpp/src/pieces/piece.hpp b/cpp/src/pieces/piece.hpp index 121335d..dd1a46c 100644 --- a/cpp/src/pieces/piece.hpp +++ b/cpp/src/pieces/piece.hpp @@ -23,7 +23,8 @@ enum Colour : int8_t { class Board; struct Coords; -std::vector legal_moves(const Piece, const Board&, const Coords, bool); +std::vector legal_moves(int8_t, const Board&, const Coords, bool); +std::vector keep_only_blocking(const std::vector, const Board&); std::vector pawn_moves(const Board&, const Coords); std::vector rook_moves(const Board&, const Coords);