{ 02_setup_position.pas -- loading and building positions.
  Pascal translation of ../examples/02_setup_position.c.

  Functions demonstrated:
    SANFENSet()   -- load a position from a FEN string
    SANClrBoard() -- empty the board
    SANPutSq()    -- place a piece on a square
    SANPutACTC()  -- set which color is to move
    SANUpdate()   -- recompute derived state after manual edits
    SANGetSq()    -- read back what's on a square

  There are two ways to set up a position: parse a whole FEN string
  in one call (the normal way), or build it square by square with
  the SANPutSq/SANGetSq family (useful for generating test positions
  programmatically, e.g. from a loop).
}

program setup_position;

uses
  SanKit;

var
  cp: LongInt;

begin
  SANInit;

  { --- Method 1: a FEN string ------------------------------------ }

  WriteLn('Method 1: SANFENSet with a known FEN');
  Flush(Output);
  if SANFENSet('8/8/8/4k3/8/8/4P3/4K3 w - - 0 1') = 0 then
    begin
      WriteLn(ErrOutput, 'bad FEN');
      Halt(1);
    end;
  SANDspBoard;
  FlushCOutput(nil);

  { --- Method 2: square by square --------------------------------- }

  WriteLn;
  WriteLn('Method 2: building the same king-and-pawn ending by hand');
  Flush(Output);

  SANClrBoard;                          { empty every square }

  SANPutSq(sq_e1, cp_wk);               { white king on e1 }
  SANPutSq(sq_e2, cp_wp);               { white pawn on e2 }
  SANPutSq(sq_e5, cp_bk);               { black king on e5 }

  SANPutACTC(c_w);                      { White to move }
  SANPutCAST(0);                        { no one may castle }
  SANPutEPSQ(sq_nil);                   { no en passant target }
  SANPutHMVC(0);
  SANPutFMVN(1);

  SANUpdate;                            { refresh piece lists etc. }

  SANDspBoard;
  FlushCOutput(nil);
  WriteLn('As FEN: ', SANFENGen);

  cp := SANGetSq(sq_e2);
  WriteLn;
  WriteLn('What''s on e2? color-piece code ', cp, ' (',
          accv[cv_c_cpv[cp]], acpv[cv_p_cpv[cp]], ')');
end.
