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];
|
ret.squares[move.target_square] = this->squares[move.source_square];
|
||||||
|
|
||||||
// -- Handle en passant target being eaten
|
// -- 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)] =
|
ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
|
||||||
Piece::None;
|
Piece::None;
|
||||||
|
|
||||||
@ -203,23 +205,26 @@ Board Board::make_move(Move move) const {
|
|||||||
|
|
||||||
// -- Handle castling (just move the rook over)
|
// -- Handle castling (just move the rook over)
|
||||||
Coords c = Coords::from_index(move.source_square);
|
Coords c = Coords::from_index(move.source_square);
|
||||||
if (move.castle_side & KingSide) {
|
if ((squares[move.source_square] & 0b111) == Piece::King) {
|
||||||
Coords rook_source{7, c.y};
|
if (move.target_square - move.source_square == 2) { // king side castle
|
||||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
Coords rook_source{7, c.y};
|
||||||
ret.squares[rook_source.to_index()] = Piece::None;
|
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||||
Coords rook_dest{5, c.y};
|
ret.squares[rook_source.to_index()] = Piece::None;
|
||||||
ret.squares[rook_dest.to_index()] = old_rook;
|
Coords rook_dest{5, c.y};
|
||||||
} else if (move.castle_side & QueenSide) {
|
ret.squares[rook_dest.to_index()] = old_rook;
|
||||||
Coords rook_source{0, c.y};
|
} else if (move.target_square - move.source_square == -2) { // queen
|
||||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
Coords rook_source{0, c.y};
|
||||||
ret.squares[rook_source.to_index()] = Piece::None;
|
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||||
Coords rook_dest{3, c.y};
|
ret.squares[rook_source.to_index()] = Piece::None;
|
||||||
ret.squares[rook_dest.to_index()] = old_rook;
|
Coords rook_dest{3, c.y};
|
||||||
|
ret.squares[rook_dest.to_index()] = old_rook;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Check for castling rights
|
// -- Check for castling rights
|
||||||
ret.w_castle_rights = w_castle_rights;
|
ret.w_castle_rights = w_castle_rights;
|
||||||
ret.b_castle_rights = b_castle_rights;
|
ret.b_castle_rights = b_castle_rights;
|
||||||
|
bool is_capturing = squares[move.target_square] != Piece::None;
|
||||||
if (white_to_play) {
|
if (white_to_play) {
|
||||||
if ((squares[move.source_square] & 0b111) == King)
|
if ((squares[move.source_square] & 0b111) == King)
|
||||||
ret.w_castle_rights = NeitherSide;
|
ret.w_castle_rights = NeitherSide;
|
||||||
@ -232,7 +237,7 @@ Board Board::make_move(Move move) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Coords target = Coords::from_index(move.target_square);
|
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) {
|
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||||
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
|
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||||
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);
|
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) {
|
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||||
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
|
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||||
ret.w_castle_rights &= ~(QueenSide);
|
ret.w_castle_rights &= ~(QueenSide);
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
std::string pos =
|
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);
|
// Board b = Board::setup_fen_position(pos);
|
||||||
perft();
|
perft();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -11,9 +11,6 @@ struct Move {
|
|||||||
int8_t source_square;
|
int8_t source_square;
|
||||||
int8_t target_square;
|
int8_t target_square;
|
||||||
|
|
||||||
bool is_capturing = false;
|
|
||||||
CastleSide castle_side = CastleSide::NeitherSide;
|
|
||||||
bool en_passant = false;
|
|
||||||
int8_t promoting_to = Piece::None;
|
int8_t promoting_to = Piece::None;
|
||||||
|
|
||||||
std::string to_string() const {
|
std::string to_string() const {
|
||||||
|
@ -60,7 +60,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
|||||||
ret.push_back(Move{
|
ret.push_back(Move{
|
||||||
xy.to_index(),
|
xy.to_index(),
|
||||||
Coords{6, xy.y}.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{
|
ret.push_back(Move{
|
||||||
xy.to_index(),
|
xy.to_index(),
|
||||||
Coords{2, xy.y}.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{
|
ret.push_back(Move{
|
||||||
xy.to_index(),
|
xy.to_index(),
|
||||||
left.to_index(),
|
left.to_index(),
|
||||||
.is_capturing = true,
|
|
||||||
.promoting_to = (int8_t) (my_colour | piece)
|
.promoting_to = (int8_t) (my_colour | piece)
|
||||||
});
|
});
|
||||||
else
|
else
|
||||||
@ -43,7 +42,6 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
|||||||
ret.push_back(Move{
|
ret.push_back(Move{
|
||||||
xy.to_index(),
|
xy.to_index(),
|
||||||
right.to_index(),
|
right.to_index(),
|
||||||
.is_capturing = true,
|
|
||||||
.promoting_to = (int8_t) (my_colour | piece)
|
.promoting_to = (int8_t) (my_colour | piece)
|
||||||
});
|
});
|
||||||
else
|
else
|
||||||
@ -55,14 +53,8 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
|||||||
if (b.en_passant_target != -1) {
|
if (b.en_passant_target != -1) {
|
||||||
Coords c = Coords::from_index(b.en_passant_target);
|
Coords c = Coords::from_index(b.en_passant_target);
|
||||||
int dy = my_colour == Colour::White ? 1 : -1;
|
int dy = my_colour == Colour::White ? 1 : -1;
|
||||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1)) {
|
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1))
|
||||||
ret.push_back(Move{
|
ret.push_back(Move{xy.to_index(), c.to_index()});
|
||||||
xy.to_index(),
|
|
||||||
c.to_index(),
|
|
||||||
.is_capturing = true,
|
|
||||||
.en_passant = true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Normal move + promotion
|
// -- 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>
|
std::optional<Move>
|
||||||
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
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 {};
|
return {};
|
||||||
|
|
||||||
int8_t piece = board.squares[dest.to_index()];
|
return Move{source.to_index(), 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 {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Move>
|
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 dx = mult_dx * d;
|
||||||
int dy = mult_dy * d;
|
int dy = mult_dy * d;
|
||||||
|
|
||||||
std::optional<Move> move =
|
Coords target{xy.x + dx, xy.y + dy};
|
||||||
move_for_position(board, xy, Coords{xy.x + dx, xy.y + dy});
|
std::optional<Move> move = move_for_position(board, xy, target);
|
||||||
if (move.has_value()) {
|
if (move.has_value()) {
|
||||||
ret.push_back(move.value());
|
ret.push_back(move.value());
|
||||||
if (move.value().is_capturing)
|
if (board.squares[target.to_index()] != Piece::None)
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user