when loading a fen ref, i return the object and
not a pointer
This commit is contained in:
parent
d6baf1ee53
commit
1a4e33201e
@ -3,8 +3,8 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
Board* Board::setup_fen_position(std::string fen) {
|
Board Board::setup_fen_position(std::string fen) {
|
||||||
Board* board = new Board();
|
Board board;
|
||||||
std::map<char, Piece> c2p{
|
std::map<char, Piece> c2p{
|
||||||
{'k', Piece::King},
|
{'k', Piece::King},
|
||||||
{'p', Piece::Pawn},
|
{'p', Piece::Pawn},
|
||||||
@ -30,7 +30,7 @@ Board* Board::setup_fen_position(std::string fen) {
|
|||||||
std::isupper(symbol) ? Colour::White : Colour::Black;
|
std::isupper(symbol) ? Colour::White : Colour::Black;
|
||||||
|
|
||||||
Piece piece = c2p[std::tolower(symbol)];
|
Piece piece = c2p[std::tolower(symbol)];
|
||||||
board->squares[rank * 8 + file] = colour | piece;
|
board.squares[rank * 8 + file] = colour | piece;
|
||||||
file++;
|
file++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,3 +75,24 @@ std::string Board::to_fen() {
|
|||||||
}
|
}
|
||||||
return ret;
|
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;
|
||||||
|
}
|
||||||
|
@ -4,15 +4,25 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Board {
|
struct Coords {
|
||||||
private:
|
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};
|
int8_t squares[64] = {Piece::None};
|
||||||
Colour turn;
|
bool white_to_play;
|
||||||
int8_t w_castle_rights;
|
int8_t w_castle_rights;
|
||||||
int8_t b_castle_rights;
|
int8_t b_castle_rights;
|
||||||
|
static Board setup_fen_position(std::string fen);
|
||||||
|
|
||||||
public:
|
Board make_move(Move);
|
||||||
static Board* setup_fen_position(std::string fen);
|
|
||||||
|
|
||||||
std::string to_fen();
|
std::string to_fen();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user