PositionError

enum class chesscxx::PositionError : uint8_t

Represents errors that can occur during Position creation.

Values:

enumerator kSideNotToMoveIsUnderAttack

The king of the player NOT to move is currently attacked by the player TO move.

enumerator kFullmoveNumberOutOfRange

Fullmove number out of valid range [kMinFullmoveNumber, kMaxFullmoveNumber].

enumerator kHalfmoveClockOutOfRange

The halfmove clock value is inconsistent with the current fullmove number and active color.

enumerator kInvalidCastlingRightsForPiecePositions

The asserted castling rights conflict with the current positions of the king or rooks.

enumerator kEnPassantTargetSquareOccupied

An en passant target square is specified, but this square is currently occupied by a piece.

enumerator kEnPassantNoCapturablePawn

An en passant target square is specified, but the opponent’s pawn that supposedly just moved two squares (and would be captured en passant) is not correctly placed or is not an opponent’s pawn.

enumerator kEnPassantTargetSquareInvalidRank

An en passant target square is specified, but it’s on an invalid rank based on the current active color.

Helper classes

template<>
struct formatter : public chesscxx::internal::NoSpec
#include <position_error_formatter.h>

formatting support for chesscxx::PositionError

Examples

#include <chesscxx/castling_rights.h>
#include <chesscxx/color.h>
#include <chesscxx/parse.h>
#include <chesscxx/piece_placement.h>
#include <chesscxx/position.h>
#include <chesscxx/position_error.h>

#include <cstdlib>
#include <print>

namespace {
void verify(bool check) {
  if (!check) std::abort();
}

void printErrorOrValue(auto created_object) {
  if (created_object) {
    std::println("{}", created_object.value());
  } else {
    std::println("{}", created_object.error());
  }
}

void createPositionAndPrint(const auto& params) {
  auto position = chesscxx::Position::fromParams(params);
  printErrorOrValue(position);
}
}  // namespace

auto main() -> int {
  std::println("{}", chesscxx::PositionError::kEnPassantTargetSquareOccupied);
  std::println("{}\n", chesscxx::PositionError::kSideNotToMoveIsUnderAttack);

  chesscxx::Position::Params params = {.fullmove_number = 0};
  createPositionAndPrint(params);

  params.fullmove_number = 1;
  createPositionAndPrint(params);

  auto piece_placement =
      chesscxx::parse<chesscxx::PiecePlacement>("k7/1B6/8/8/8/8/8/7K");
  verify(piece_placement.has_value());
  params.halfmove_clock = 0;
  params.piece_placement = piece_placement.value();
  createPositionAndPrint(params);

  params.castling_rights = chesscxx::CastlingRights(0);
  createPositionAndPrint(params);

  params.active_color = chesscxx::Color::kBlack;
  createPositionAndPrint(params);
}

Output:

En passant target square occupied
Side not to move is under attack

Fullmove number out of range
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Invalid castling rights for piece positions
Side not to move is under attack
k7/1B6/8/8/8/8/8/7K b - - 0 1