got rid of magic numbers

This commit is contained in:
Karma Riuk 2025-02-02 21:13:30 +01:00
parent 1625345f08
commit b329c41bea
4 changed files with 15 additions and 10 deletions

View File

@ -63,9 +63,7 @@ std::string Board::to_fen() const {
int full_piece = this->squares[rank * 8 + file]; int full_piece = this->squares[rank * 8 + file];
char piece = p2c[full_piece & 0b111]; char piece = p2c[full_piece & 0b111];
Colour colour = (full_piece & 0b11000) == Colour::White int8_t colour = colour_at({file, rank});
? Colour::White
: Colour::Black;
if (empty_cell_counter > 0) { if (empty_cell_counter > 0) {
ret += std::to_string(empty_cell_counter); ret += std::to_string(empty_cell_counter);

View File

@ -19,4 +19,12 @@ struct Board {
Board make_move(Move) const; Board make_move(Move) const;
std::string to_fen() const; std::string to_fen() const;
bool is_check_for(int8_t) const; bool is_check_for(int8_t) const;
int8_t colour_at(int8_t idx) const {
return squares[idx] & 0b11000;
}
int8_t colour_at(Coords xy) const {
return colour_at(xy.to_index());
}
}; };

View File

@ -5,8 +5,7 @@
std::vector<Move> pawn_moves(const Board& b, const Coords xy) { std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
std::vector<Move> ret{}; std::vector<Move> ret{};
int8_t me = b.squares[xy.to_index()]; int8_t my_colour = b.colour_at(xy);
int8_t my_colour = me & 0b11000;
// -- Capture to the left // -- Capture to the left
if (xy.x > 0) { if (xy.x > 0) {
@ -14,7 +13,7 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
Coords left{xy.x - 1, xy.y + dy}; Coords left{xy.x - 1, xy.y + dy};
int8_t capturable_piece = b.squares[left.to_index()]; int8_t capturable_piece = b.squares[left.to_index()];
if (capturable_piece != 0) { if (capturable_piece != 0) {
if (my_colour != (capturable_piece & 0b11000)) if (my_colour != b.colour_at(left))
ret.push_back(Move{xy.to_index(), left.to_index()}); ret.push_back(Move{xy.to_index(), left.to_index()});
} }
} }
@ -25,7 +24,7 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
Coords right{xy.x + 1, xy.y + dy}; Coords right{xy.x + 1, xy.y + dy};
int8_t capturable_piece = b.squares[right.to_index()]; int8_t capturable_piece = b.squares[right.to_index()];
if (capturable_piece != 0) { if (capturable_piece != 0) {
if (my_colour != (capturable_piece & 0b11000)) if (my_colour != b.colour_at(right))
ret.push_back(Move{xy.to_index(), right.to_index()}); ret.push_back(Move{xy.to_index(), right.to_index()});
} }
} }

View File

@ -8,7 +8,7 @@ keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
if (candidates.size() == 0) if (candidates.size() == 0)
return {}; return {};
int8_t my_colour = board.squares[candidates[0].source_square] & 0b11000; int8_t my_colour = board.colour_at(candidates[0].source_square);
std::vector<Move> ret; std::vector<Move> ret;
for (Move move : candidates) { for (Move move : candidates) {
Board board_after_move = board.make_move(move); Board board_after_move = board.make_move(move);
@ -46,8 +46,8 @@ move_for_position(const Board& board, const Coords source, const Coords dest) {
if (piece == Piece::None) if (piece == Piece::None)
return Move{source.to_index(), dest.to_index()}; return Move{source.to_index(), dest.to_index()};
int8_t source_colour = board.squares[source.to_index()] & 0b11000; int8_t source_colour = board.colour_at(source);
int8_t dest_colour = piece & 0b11000; int8_t dest_colour = board.colour_at(dest);
if (source_colour != dest_colour) if (source_colour != dest_colour)
return Move{source.to_index(), dest.to_index(), true}; return Move{source.to_index(), dest.to_index(), true};
return {}; return {};