diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index 3c986f9..439b72e 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -63,9 +63,7 @@ std::string Board::to_fen() const { int full_piece = this->squares[rank * 8 + file]; char piece = p2c[full_piece & 0b111]; - Colour colour = (full_piece & 0b11000) == Colour::White - ? Colour::White - : Colour::Black; + int8_t colour = colour_at({file, rank}); if (empty_cell_counter > 0) { ret += std::to_string(empty_cell_counter); diff --git a/cpp/src/board.hpp b/cpp/src/board.hpp index 6d7fc60..72c6d44 100644 --- a/cpp/src/board.hpp +++ b/cpp/src/board.hpp @@ -19,4 +19,12 @@ struct Board { Board make_move(Move) const; std::string to_fen() 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()); + } }; diff --git a/cpp/src/pieces/pawn.cpp b/cpp/src/pieces/pawn.cpp index bb5f3d7..90776bc 100644 --- a/cpp/src/pieces/pawn.cpp +++ b/cpp/src/pieces/pawn.cpp @@ -5,8 +5,7 @@ std::vector pawn_moves(const Board& b, const Coords xy) { std::vector ret{}; - int8_t me = b.squares[xy.to_index()]; - int8_t my_colour = me & 0b11000; + int8_t my_colour = b.colour_at(xy); // -- Capture to the left if (xy.x > 0) { @@ -14,7 +13,7 @@ std::vector pawn_moves(const Board& b, const Coords xy) { Coords left{xy.x - 1, xy.y + dy}; int8_t capturable_piece = b.squares[left.to_index()]; 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()}); } } @@ -25,7 +24,7 @@ std::vector pawn_moves(const Board& b, const Coords xy) { Coords right{xy.x + 1, xy.y + dy}; int8_t capturable_piece = b.squares[right.to_index()]; 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()}); } } diff --git a/cpp/src/pieces/piece.cpp b/cpp/src/pieces/piece.cpp index a30daac..9c192cc 100644 --- a/cpp/src/pieces/piece.cpp +++ b/cpp/src/pieces/piece.cpp @@ -8,7 +8,7 @@ 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; + int8_t my_colour = board.colour_at(candidates[0].source_square); std::vector ret; for (Move move : candidates) { 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) return Move{source.to_index(), dest.to_index()}; - int8_t source_colour = board.squares[source.to_index()] & 0b11000; - int8_t dest_colour = piece & 0b11000; + int8_t source_colour = board.colour_at(source); + int8_t dest_colour = board.colour_at(dest); if (source_colour != dest_colour) return Move{source.to_index(), dest.to_index(), true}; return {};