diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index 332d741..ae39018 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -3,8 +3,8 @@ #include #include -Board* Board::setup_fen_position(std::string fen) { - Board* board = new Board(); +Board Board::setup_fen_position(std::string fen) { + Board board; std::map c2p{ {'k', Piece::King}, {'p', Piece::Pawn}, @@ -30,7 +30,7 @@ Board* Board::setup_fen_position(std::string fen) { std::isupper(symbol) ? Colour::White : Colour::Black; Piece piece = c2p[std::tolower(symbol)]; - board->squares[rank * 8 + file] = colour | piece; + board.squares[rank * 8 + file] = colour | piece; file++; } } @@ -75,3 +75,24 @@ std::string Board::to_fen() { } return ret; } + +Board Board::make_move(Move move) { + int8_t dest_piece = this->squares[move.target_square]; + + Board ret; + std::copy( + std::begin(this->squares), + std::end(this->squares), + std::begin(ret.squares) + ); + ret.white_to_play = !this->white_to_play; + + // -- Actually make the move + ret.squares[move.source_square] = Piece::None; + ret.squares[move.target_square] = this->squares[move.source_square]; + + // -- Handle en passant target being eaten + if (move.en_passant) + ret.squares[move.target_square - 8] = Piece::None; + return ret; +} diff --git a/cpp/src/board.hpp b/cpp/src/board.hpp index 923a712..a239783 100644 --- a/cpp/src/board.hpp +++ b/cpp/src/board.hpp @@ -4,15 +4,25 @@ #include -class Board { - private: +struct Coords { + int x, y; + + int8_t to_index() const { + return this->y * 8 + this->x; + } + + static Coords from_index(int idx) { + return {idx % 8, idx / 8}; + } +}; + +struct Board { int8_t squares[64] = {Piece::None}; - Colour turn; + bool white_to_play; int8_t w_castle_rights; int8_t b_castle_rights; + static Board setup_fen_position(std::string fen); - public: - static Board* setup_fen_position(std::string fen); - + Board make_move(Move); std::string to_fen(); };