when loading a fen ref, i return the object and

not a pointer
This commit is contained in:
Karma Riuk 2025-02-02 18:17:02 +01:00
parent d6baf1ee53
commit 1a4e33201e
2 changed files with 40 additions and 9 deletions

View File

@ -3,8 +3,8 @@
#include <cctype>
#include <map>
Board* Board::setup_fen_position(std::string fen) {
Board* board = new Board();
Board Board::setup_fen_position(std::string fen) {
Board board;
std::map<char, Piece> 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;
}

View File

@ -4,15 +4,25 @@
#include <string>
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();
};