validate method

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

Checks the legality of this position.

Throws a PositionSetupException if it does not meet basic validity requirements.

Implementation

@override
void validate({bool? ignoreImpossibleCheck}) {
  if (board.occupied.isEmpty) {
    throw PositionSetupException.empty;
  }

  if (board.kings.size != 1) {
    throw PositionSetupException.kings;
  }

  final otherKing = board.kingOf(turn.opposite);
  if (otherKing != null && kingAttackers(otherKing, turn).isNotEmpty) {
    throw PositionSetupException.oppositeCheck;
  }

  // white can have pawns on back rank
  if (SquareSet.backranks.isIntersected(board.black.intersect(board.pawns))) {
    throw PositionSetupException.pawnsOnBackrank;
  }

  final skipImpossibleCheck = ignoreImpossibleCheck ?? false;
  final ourKing = board.kingOf(turn);
  if (!skipImpossibleCheck && ourKing != null) {
    _validateCheckers(ourKing);
  }
}