Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
a47dc9b695 | |||
3f3017a389 | |||
f768d16a7e | |||
a658673d2b | |||
222adbb8f0 |
@ -182,7 +182,9 @@ Board Board::make_move(Move move) const {
|
||||
ret.squares[move.target_square] = this->squares[move.source_square];
|
||||
|
||||
// -- Handle en passant target being eaten
|
||||
if (move.en_passant)
|
||||
if (en_passant_target != -1
|
||||
&& (squares[move.source_square] & 0b111) == Piece::Pawn
|
||||
&& squares[move.target_square] == Piece::None)
|
||||
ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
|
||||
Piece::None;
|
||||
|
||||
@ -203,23 +205,26 @@ Board Board::make_move(Move move) const {
|
||||
|
||||
// -- Handle castling (just move the rook over)
|
||||
Coords c = Coords::from_index(move.source_square);
|
||||
if (move.castle_side & KingSide) {
|
||||
Coords rook_source{7, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{5, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
} else if (move.castle_side & QueenSide) {
|
||||
Coords rook_source{0, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{3, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
if ((squares[move.source_square] & 0b111) == Piece::King) {
|
||||
if (move.target_square - move.source_square == 2) { // king side castle
|
||||
Coords rook_source{7, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{5, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
} else if (move.target_square - move.source_square == -2) { // queen
|
||||
Coords rook_source{0, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{3, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
}
|
||||
}
|
||||
|
||||
// -- Check for castling rights
|
||||
ret.w_castle_rights = w_castle_rights;
|
||||
ret.b_castle_rights = b_castle_rights;
|
||||
bool is_capturing = squares[move.target_square] != Piece::None;
|
||||
if (white_to_play) {
|
||||
if ((squares[move.source_square] & 0b111) == King)
|
||||
ret.w_castle_rights = NeitherSide;
|
||||
@ -232,7 +237,7 @@ Board Board::make_move(Move move) const {
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (move.is_capturing && target.y == 7
|
||||
if (is_capturing && target.y == 7
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||
ret.b_castle_rights &= ~(QueenSide);
|
||||
@ -251,7 +256,7 @@ Board Board::make_move(Move move) const {
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (move.is_capturing && target.y == 0
|
||||
if (is_capturing && target.y == 0
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||
ret.w_castle_rights &= ~(QueenSide);
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string pos =
|
||||
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5";
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
// Board b = Board::setup_fen_position(pos);
|
||||
perft();
|
||||
return 0;
|
||||
|
@ -11,9 +11,6 @@ struct Move {
|
||||
int8_t source_square;
|
||||
int8_t target_square;
|
||||
|
||||
bool is_capturing = false;
|
||||
CastleSide castle_side = CastleSide::NeitherSide;
|
||||
bool en_passant = false;
|
||||
int8_t promoting_to = Piece::None;
|
||||
|
||||
std::string to_string() const {
|
||||
|
@ -60,7 +60,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
Coords{6, xy.y}.to_index(),
|
||||
.castle_side = CastleSide::KingSide
|
||||
});
|
||||
}
|
||||
|
||||
@ -68,7 +67,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
Coords{2, xy.y}.to_index(),
|
||||
.castle_side = CastleSide::QueenSide
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,6 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
left.to_index(),
|
||||
.is_capturing = true,
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
@ -43,7 +42,6 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
right.to_index(),
|
||||
.is_capturing = true,
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
@ -55,14 +53,8 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
if (b.en_passant_target != -1) {
|
||||
Coords c = Coords::from_index(b.en_passant_target);
|
||||
int dy = my_colour == Colour::White ? 1 : -1;
|
||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1)) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
c.to_index(),
|
||||
.is_capturing = true,
|
||||
.en_passant = true
|
||||
});
|
||||
}
|
||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1))
|
||||
ret.push_back(Move{xy.to_index(), c.to_index()});
|
||||
}
|
||||
|
||||
// -- Normal move + promotion
|
||||
|
@ -53,18 +53,11 @@ legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
|
||||
|
||||
std::optional<Move>
|
||||
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||
if (!dest.is_within_bounds())
|
||||
if (!dest.is_within_bounds()
|
||||
|| board.colour_at(source) == board.colour_at(dest))
|
||||
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.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 {};
|
||||
return Move{source.to_index(), dest.to_index()};
|
||||
}
|
||||
|
||||
std::vector<Move>
|
||||
@ -74,11 +67,11 @@ look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||
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});
|
||||
Coords target{xy.x + dx, xy.y + dy};
|
||||
std::optional<Move> move = move_for_position(board, xy, target);
|
||||
if (move.has_value()) {
|
||||
ret.push_back(move.value());
|
||||
if (move.value().is_capturing)
|
||||
if (board.squares[target.to_index()] != Piece::None)
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
|
Reference in New Issue
Block a user