validate method

  1. @override
void validate({
  1. bool? ignoreImpossibleCheck,
})
override

Checks the legality of this position.

Validation is like chess, but it allows our king to be missing. Throws a PositionError if it does not meet basic validity requirements.

Implementation

@override
void validate({bool? ignoreImpossibleCheck}) {
  if (board.occupied.isEmpty) {
    throw PositionError.empty;
  }
  if (board.kings.size > 2) {
    throw PositionError.kings;
  }
  final otherKing = board.kingOf(turn.opposite);
  if (otherKing == null) {
    throw PositionError.kings;
  }
  if (kingAttackers(otherKing, turn).isNotEmpty) {
    throw PositionError.oppositeCheck;
  }
  if (SquareSet.backranks.isIntersected(board.pawns)) {
    throw PositionError.pawnsOnBackrank;
  }
  final skipImpossibleCheck = ignoreImpossibleCheck ?? false;
  final ourKing = board.kingOf(turn);
  if (!skipImpossibleCheck && ourKing != null) {
    _validateCheckers(ourKing);
  }
}