{ 04_move_generation.pas -- generating the list of legal moves.
  Pascal translation of ../examples/04_move_generation.c.

  Functions demonstrated:
    SANGenMoves()  -- (re)generate the full legal move list for the
                      side to move in the current position
    SANEncode()    -- format each generated move as SAN

  After SANGenMoves() runs, the result lives in the global curr_g
  (TGen, sankit.pas) -- curr_g.g_gmvc is how many moves there are,
  curr_g.g_base[i] is the i'th one (a real, typed TMove pointer this
  time, not the opaque PMove 01-03 used -- see TGen's own header
  comment in sankit.pas for why). }

program move_generation;

uses
  SanKit;

procedure ListLegalMoves;
var
  i: Integer;
  san: TSanBuf;
begin
  SANGenMoves;

  WriteLn('Position: ', SANFENGen);
  WriteLn(curr_g.g_gmvc, ' legal move(s):');

  { the C original's printf("%s%-7s", san, atEndOfRow ? "\n" : "")
    left-justifies the SECOND argument -- "\n" or "" -- in a 7-wide
    field, not the move text itself: at the end of a row that pads
    the newline with 6 trailing spaces (visible as leading spaces on
    the next line), everywhere else it's just 7 plain spaces after
    the move. Reproduced exactly, quirk included, rather than
    "corrected" into a cleaner alignment this example never had. }
  for i := 0 to curr_g.g_gmvc - 1 do
    begin
      FillChar(san, SizeOf(san), 0);
      SANEncode(PMove(@curr_g.g_base[i]), SanBufPtr(san));
      Write(SanBufPtr(san));
      if (i mod 8) = 7 then
        Write(#10'      ')
      else
        Write('       ');
    end;
  WriteLn;
end;

begin
  SANInit;

  ListLegalMoves;

  { a position with far fewer options: White's king is in check
    from a lone rook, so almost every move is illegal -- only
    moving the king or capturing the rook keeps it out of check }
  WriteLn;
  SANFENSet('6k1/8/8/8/8/8/6r1/6K1 w - - 0 1');
  ListLegalMoves;
end.
