implemented bishop legal moves
This commit is contained in:
parent
8bf164cb05
commit
01c912435b
23
cpp/src/pieces/bishop.cpp
Normal file
23
cpp/src/pieces/bishop.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "../board.hpp"
|
||||
#include "../coords.hpp"
|
||||
#include "../move.hpp"
|
||||
#include "piece.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<Move> bishop_moves(const Board& b, const Coords xy) {
|
||||
std::vector<Move> ret;
|
||||
auto ne = look_direction(b, xy, 1, 1);
|
||||
ret.insert(ret.end(), ne.begin(), ne.end());
|
||||
|
||||
auto se = look_direction(b, xy, 1, -1);
|
||||
ret.insert(ret.end(), se.begin(), se.end());
|
||||
|
||||
auto sw = look_direction(b, xy, -1, -1);
|
||||
ret.insert(ret.end(), sw.begin(), sw.end());
|
||||
|
||||
auto nw = look_direction(b, xy, -1, 1);
|
||||
ret.insert(ret.end(), nw.begin(), nw.end());
|
||||
|
||||
return ret;
|
||||
}
|
@ -36,3 +36,37 @@ std::vector<Move> legal_moves(
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<Move>
|
||||
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||
if (dest.is_within_bounds())
|
||||
return {};
|
||||
|
||||
int8_t piece = board.squares[dest.to_index()];
|
||||
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;
|
||||
if (source_colour != dest_colour)
|
||||
return Move{source.to_index(), dest.to_index(), true};
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<Move>
|
||||
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||
std::vector<Move> ret;
|
||||
for (int d = 0; d < 8; d++) {
|
||||
int dx = mult_dx * d;
|
||||
int dy = mult_dy * d;
|
||||
|
||||
std::optional<Move> move =
|
||||
move_for_position(board, xy, Coords{xy.x + dx, xy.y + dy});
|
||||
if (move.has_value()) {
|
||||
ret.push_back(move.value());
|
||||
if (move.value().is_capturing)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "../move.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
enum Piece : int8_t {
|
||||
@ -25,6 +26,8 @@ struct Coords;
|
||||
|
||||
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool);
|
||||
std::vector<Move> keep_only_blocking(const std::vector<Move>, const Board&);
|
||||
std::optional<Move> move_for_position(const Board&, const Coords, const Coords);
|
||||
std::vector<Move> look_direction(const Board&, const Coords, int, int);
|
||||
|
||||
std::vector<Move> pawn_moves(const Board&, const Coords);
|
||||
std::vector<Move> rook_moves(const Board&, const Coords);
|
||||
|
Loading…
x
Reference in New Issue
Block a user