perft function

int perft(
  1. Position p,
  2. int depth
)

Count leaf nodes at depth — the standard move-generator correctness test.

Implementation

int perft(Position p, int depth) {
  if (depth == 0) return 1;
  final moves = <int>[];
  p.generatePseudoLegal(moves);
  var nodes = 0;
  for (final m in moves) {
    p.makeMove(m);
    if (!p.isSquareAttacked(p._kingSquareOf(p.turn ^ 1), p.turn)) {
      nodes += depth == 1 ? 1 : perft(p, depth - 1);
    }
    p.unmakeMove();
  }
  return nodes;
}