Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 46 additions & 34 deletions Chess/MoveGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace SimpleChessEngine {
template <>
void MoveGenerator::GenerateMovesForPiece<Piece::kKing>(
Move*& moves, Position& position, Bitboard target) const;
PseudoLegalMove*& moves, Position& position, Bitboard target) const;

template MoveGenerator::Moves MoveGenerator::GenerateMoves<
MoveGenerator::Type::kAll>(Position& position) const;
Expand All @@ -18,7 +18,8 @@ template MoveGenerator::Moves MoveGenerator::GenerateMoves<

MoveGenerator::~MoveGenerator() = default;

void MoveGenerator::GenerateCastling(Move*& moves, const Position& position) {
void MoveGenerator::GenerateCastling(PseudoLegalMove*& moves,
const Position& position) {
if (position.IsUnderCheck()) {
return;
}
Expand All @@ -32,13 +33,14 @@ void MoveGenerator::GenerateCastling(Move*& moves, const Position& position) {
const auto side_idx = static_cast<size_t>(castling_side);
const auto king_to = kKingCastlingDestination[color_idx][side_idx];

*moves++ = Move::Make<MoveType::kCastling>(king_square, king_to);
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kCastling>(king_square, king_to));
}
}
}

template <Piece piece>
void MoveGenerator::GenerateMovesForPiece(Move*& moves, Position& position,
void MoveGenerator::GenerateMovesForPiece(PseudoLegalMove*& moves, Position& position,
const Bitboard target) const {
static_assert(piece != Piece::kPawn && piece != Piece::kKing);

Expand All @@ -53,7 +55,7 @@ void MoveGenerator::GenerateMovesForPiece(Move*& moves, Position& position,

template <>
void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
Move*& moves, Position& position, const Bitboard target) const {
PseudoLegalMove*& moves, Position& position, const Bitboard target) const {
const auto us = position.GetSideToMove();
const auto us_idx = static_cast<size_t>(us);
const auto them = Flip(us);
Expand All @@ -78,7 +80,7 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
while (push.Any()) {
const auto to = push.PopFirstBit();
const auto from = Shift(to, opposite_direction);
*moves++ = Move(from, to);
*moves++ = PseudoLegalMove(Move(from, to));
}

auto double_push = Shift(double_push_pawns, direction) & valid_squares;
Expand All @@ -87,7 +89,7 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
while (double_push.Any()) {
const auto to = double_push.PopFirstBit();
const auto from = Shift(Shift(to, opposite_direction), opposite_direction);
*moves++ = Move(from, to);
*moves++ = PseudoLegalMove(Move(from, to));
}

const auto attacks =
Expand All @@ -114,7 +116,7 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
while (attack_squares.Any()) {
const auto to = attack_squares.PopFirstBit();
const auto from = Shift(to, opposite_attacks[attack_direction]);
*moves++ = Move(from, to);
*moves++ = PseudoLegalMove(Move(from, to));
}
}

Expand All @@ -127,7 +129,7 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
if (attack_to.Any()) {
const auto to = en_croissant_square.value();
const auto from = Shift(to, opposite_attacks[attack_direction]);
*moves++ = Move::Make<MoveType::kEnPassant>(from, to);
*moves++ = PseudoLegalMove(Move::Make<MoveType::kEnPassant>(from, to));
}
}
}
Expand All @@ -141,10 +143,14 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
const auto to = promotion_push.PopFirstBit();
const auto from = Shift(to, opposite_direction);

*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kQueen);
*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kKnight);
*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kRook);
*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kBishop);
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kQueen));
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kKnight));
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kRook));
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kBishop));
}

for (size_t attack_direction = 0; attack_direction < attacks.size();
Expand All @@ -156,17 +162,21 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kPawn>(
const auto to = attack_squares.PopFirstBit();
const auto from = Shift(to, opposite_attacks[attack_direction]);

*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kQueen);
*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kKnight);
*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kRook);
*moves++ = Move::Make<MoveType::kPromotion>(from, to, Piece::kBishop);
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kQueen));
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kKnight));
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kRook));
*moves++ = PseudoLegalMove(
Move::Make<MoveType::kPromotion>(from, to, Piece::kBishop));
}
}
}

template <MoveGenerator::Type type>
Move* MoveGenerator::GeneratePseudoLegal(Position& position,
Move* moves) const {
PseudoLegalMove* MoveGenerator::GeneratePseudoLegal(Position& position,
PseudoLegalMove* moves) const {
const auto us = position.GetSideToMove();
const auto them = Flip(us);
auto target = ~position.GetPieces(us);
Expand Down Expand Up @@ -227,37 +237,39 @@ Move* MoveGenerator::GeneratePseudoLegal(Position& position,

template <MoveGenerator::Type type>
MoveGenerator::Moves MoveGenerator::GenerateMoves(Position& position) const {
std::array<Move, kMaxMovesPerPosition> moves;
std::array<PseudoLegalMove, kMaxMovesPerPosition> moves;
auto* end = GeneratePseudoLegal<type>(position, moves.data());
position.ComputePins(position.GetSideToMove());
Moves legal;
legal.reserve(end - moves.data());
for (auto* p = moves.data(); p != end; ++p) {
if (position.IsLegal(*p)) legal.push_back(*p);
if (position.IsLegal(*p)) legal.push_back(LegalMove(static_cast<Move>(*p)));
}
return legal;
}

bool MoveGenerator::HasLegalMove(Position& position) const {
std::array<Move, kMaxMovesPerPosition> moves;
std::array<PseudoLegalMove, kMaxMovesPerPosition> moves;
auto* end = GeneratePseudoLegal<Type::kAll>(position, moves.data());
position.ComputePins(position.GetSideToMove());
return std::any_of(moves.data(), end,
[&position](Move move) { return position.IsLegal(move); });
[&position](Move move) {
return position.IsLegal(move);
});
}

template Move* MoveGenerator::GeneratePseudoLegal<MoveGenerator::Type::kAll>(
Position&, Move*) const;
template Move*
template PseudoLegalMove* MoveGenerator::GeneratePseudoLegal<MoveGenerator::Type::kAll>(
Position&, PseudoLegalMove*) const;
template PseudoLegalMove*
MoveGenerator::GeneratePseudoLegal<MoveGenerator::Type::kCaptures>(Position&,
Move*) const;
template Move* MoveGenerator::GeneratePseudoLegal<MoveGenerator::Type::kQuiets>(
Position&, Move*) const;
template Move* MoveGenerator::GeneratePseudoLegal<
MoveGenerator::Type::kQuiescence>(Position&, Move*) const;
PseudoLegalMove*) const;
template PseudoLegalMove* MoveGenerator::GeneratePseudoLegal<MoveGenerator::Type::kQuiets>(
Position&, PseudoLegalMove*) const;
template PseudoLegalMove* MoveGenerator::GeneratePseudoLegal<
MoveGenerator::Type::kQuiescence>(Position&, PseudoLegalMove*) const;

template <>
void MoveGenerator::GenerateMovesForPiece<Piece::kKing>(Move*& moves,
void MoveGenerator::GenerateMovesForPiece<Piece::kKing>(PseudoLegalMove*& moves,
Position& position,
Bitboard target) const {
GenerateMovesFromSquare<Piece::kKing>(
Expand All @@ -266,7 +278,7 @@ void MoveGenerator::GenerateMovesForPiece<Piece::kKing>(Move*& moves,
}

template <Piece piece>
void MoveGenerator::GenerateMovesFromSquare(Move*& moves, Position& position,
void MoveGenerator::GenerateMovesFromSquare(PseudoLegalMove*& moves, Position& position,
const BitIndex from,
Bitboard target) const {
assert(position.GetPieceAt(from) == piece);
Expand All @@ -280,7 +292,7 @@ void MoveGenerator::GenerateMovesFromSquare(Move*& moves, Position& position,

while (valid_moves.Any()) {
const auto to = valid_moves.PopFirstBit();
*moves++ = Move(from, to);
*moves++ = PseudoLegalMove(Move(from, to));
}
}
} // namespace SimpleChessEngine
17 changes: 11 additions & 6 deletions Chess/MoveGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MoveGenerator {
MoveGenerator() = default;
~MoveGenerator();

using Moves = std::vector<Move>;
using Moves = std::vector<LegalMove>;

/**
* \brief Generates all possible moves for a given position.
Expand All @@ -38,8 +38,13 @@ class MoveGenerator {
[[nodiscard]] Moves GenerateMoves(Position& position) const;

template <Type type>
[[nodiscard]] Move* GeneratePseudoLegal(Position& position,
Move* moves) const;
[[nodiscard, gnu::always_inline]] Moves GenerateLegalMoves(Position& position) const {
return GenerateMoves<type>(position);
}

template <Type type>
[[nodiscard]] PseudoLegalMove* GeneratePseudoLegal(Position& position,
PseudoLegalMove* moves) const;

[[nodiscard]] bool HasLegalMove(Position& position) const;

Expand All @@ -54,7 +59,7 @@ class MoveGenerator {
* \return All possible moves for the given square.
*/
template <Piece piece>
void GenerateMovesForPiece(Move*& moves, Position& position,
void GenerateMovesForPiece(PseudoLegalMove*& moves, Position& position,
Bitboard target) const;

/**
Expand All @@ -69,10 +74,10 @@ class MoveGenerator {
* \return All possible moves for the given square and piece.
*/
template <Piece piece>
void GenerateMovesFromSquare(Move*& moves, Position& position, BitIndex from,
void GenerateMovesFromSquare(PseudoLegalMove*& moves, Position& position, BitIndex from,
Bitboard target) const;

static void GenerateCastling(Move*& moves, const Position& position);
static void GenerateCastling(PseudoLegalMove*& moves, const Position& position);
};

} // namespace SimpleChessEngine
29 changes: 15 additions & 14 deletions Chess/MovePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ void MovePicker::InitPicker(Position& position, const Searcher* searcher,
}

void MovePicker::Generate(bool quiet) {
std::array<Move, MoveGenerator::kMaxMovesPerPosition> generated;
std::array<PseudoLegalMove, MoveGenerator::kMaxMovesPerPosition> generated;
auto* begin = generated.data();
Move* end;
PseudoLegalMove* end;
const MoveGenerator generator;
if (quiet) {
end = generator.GeneratePseudoLegal<MoveGenerator::Type::kQuiets>(
Expand Down Expand Up @@ -114,29 +114,29 @@ void MovePicker::InitProbCut(Position& position, const Searcher* searcher,
Generate(false);
}

Move MovePicker::SelectProbCutMove(Eval see_threshold) {
MaybeLegalMove MovePicker::SelectProbCutMove(Eval see_threshold) {
while (current_ < end_) {
const auto move = moves_[current_++].move;
if (move.IsPromotion() && move.PromotionPiece() != Piece::kQueen) continue;
if (position_->IsLegal(move, blockers_) &&
position_->StaticExchangeEvaluation(move, see_threshold))
return Emit(move);
return Emit(Position::AssumeLegal(move));
}
return Move::None();
return MaybeLegalMove::None();
}

Move MovePicker::Emit(Move move) {
LegalMove MovePicker::Emit(LegalMove move) {
++move_count_;
return move;
}

Move MovePicker::SelectNextMove() {
MaybeLegalMove MovePicker::SelectNextMove() {
for (;;) {
switch (stage_) {
case Stage::kTT:
stage_ = Stage::kCaptureInit;
if (tt_move_.IsValid() && position_->IsLegal(tt_move_, blockers_))
return Emit(tt_move_);
return Emit(Position::AssumeLegal(tt_move_));
break;
case Stage::kCaptureInit:
Generate(false);
Expand All @@ -146,14 +146,14 @@ Move MovePicker::SelectNextMove() {
while (current_ < end_) {
const auto candidate = moves_[current_++];
if (!position_->IsLegal(candidate.move, blockers_)) continue;
if (!searcher_) return Emit(candidate.move);
if (!searcher_) return Emit(Position::AssumeLegal(candidate.move));
const auto promotion = candidate.move.IsPromotion()
? candidate.move.PromotionPiece()
: Piece::kNone;
if (!IsUnderpromotion(promotion) &&
position_->StaticExchangeEvaluation(candidate.move,
kGoodCaptureThreshold))
return Emit(candidate.move);
return Emit(Position::AssumeLegal(candidate.move));
moves_[bad_end_++] = candidate;
}
stage_ = searcher_ ? Stage::kKillers : Stage::kEnd;
Expand All @@ -175,7 +175,7 @@ Move MovePicker::SelectNextMove() {
found->move = Move::None();
}
killers_[index] = move;
return Emit(move);
return Emit(Position::AssumeLegal(move));
}
stage_ = Stage::kQuietInit;
break;
Expand All @@ -187,7 +187,8 @@ Move MovePicker::SelectNextMove() {
case Stage::kQuiet:
while (current_ < end_) {
const auto move = moves_[current_++].move;
if (position_->IsLegal(move, blockers_)) return Emit(move);
if (position_->IsLegal(move, blockers_))
return Emit(Position::AssumeLegal(move));
}
current_ = 0;
end_ = bad_end_;
Expand All @@ -196,12 +197,12 @@ Move MovePicker::SelectNextMove() {
case Stage::kBadCaptures:
while (current_ < end_) {
const auto move = moves_[current_++].move;
if (move.IsValid()) return Emit(move);
if (move.IsValid()) return Emit(Position::AssumeLegal(move));
}
stage_ = Stage::kEnd;
break;
case Stage::kEnd:
return Move::None();
return MaybeLegalMove::None();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Chess/MovePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class MovePicker {
Move tt_move = Move::None(),
Move excluded_move = Move::None());
void InitProbCut(Position& position, const Searcher* searcher, Depth ply);
[[nodiscard]] Move SelectProbCutMove(Eval see_threshold);
[[nodiscard]] Move SelectNextMove();
[[nodiscard]] MaybeLegalMove SelectProbCutMove(Eval see_threshold);
[[nodiscard]] MaybeLegalMove SelectNextMove();
[[nodiscard]] Stage GetCurrentStage() const { return stage_; }
[[nodiscard]] std::ptrdiff_t MoveCount() const { return move_count_; }

Expand All @@ -43,7 +43,7 @@ class MovePicker {
static void PartialInsertionSort(ScoredMove* begin, ScoredMove* end,
std::int64_t limit);
void Generate(bool quiet);
[[nodiscard]] Move Emit(Move move);
[[nodiscard]] LegalMove Emit(LegalMove move);

std::array<ScoredMove, MoveGenerator::kMaxMovesPerPosition> moves_;
std::array<Move, 2> killers_{};
Expand Down
2 changes: 1 addition & 1 deletion Chess/Perft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ size_t Perft(std::ostream& o_stream, Position& position, const Depth depth) {
static auto move_generator = MoveGenerator{};

const auto moves =
move_generator.GenerateMoves<MoveGenerator::Type::kAll>(position);
move_generator.GenerateLegalMoves<MoveGenerator::Type::kAll>(position);

size_t answer{};

Expand Down
Loading
Loading