{ 07_perft_enumerate.pas -- counting move-tree leaves ("perft").
  Pascal translation of ../examples/07_perft_enumerate.c.

  Function demonstrated:
    SANEnumerate(depth) -- recursively play out every legal move to
                           the given depth and return the total leaf
                           count.

  Note: when called at the top level, SANEnumerate() also prints a
  "divide"-style breakdown (one line per root move, with that move's
  own subtree leaf count) directly to stdout as a side effect -- see
  Flush(Output)/FlushCOutput(nil) around the call below for why that
  needs handling here the way it does everywhere a SAN Kit call
  might print (sankit.pas's own header comment goes over this in
  full). }

program perft_enumerate;

uses
  SanKit;

var
  depth: Integer;
  nodes: Int64;

begin
  SANInit;

  for depth := 1 to 4 do
    begin
      WriteLn('--- perft(', depth, ') ---');
      Flush(Output);

      { SANEnumerate() prints its own "divide" breakdown via C's own
        stdio as a side effect of this call -- computed into "nodes"
        as its own statement, exactly as the C original evaluates it
        as printf's argument before that printf's "Nodes: ..." text
        is built, rather than folded into the WriteLn below, so
        there's no question of which side's output would come first
        within a single mixed statement (see "A note on mixed
        C/Pascal output" in sankit.pas) }
      nodes := SANEnumerate(depth);
      FlushCOutput(nil);

      WriteLn('Nodes: ', nodes);
      WriteLn;
    end;

  { known-correct perft(4) from the initial position is 197281;
    this is the standard first sanity check for any move generator }
end.
