{ 06_game_status.pas -- checking status, check, and checkmate.
  Pascal translation of ../examples/06_game_status.c.

  Functions demonstrated:
    SANStatus()        -- classify the current position: normal,
                          checkmate, stalemate, draws...
    SANWhiteInCheck()  -- is White's king currently attacked? }

program game_status;

uses
  SanKit;

function StatusName(gs: LongInt): string;
begin
  case gs of
    gs_norm: StatusName := 'normal, game continues';
    gs_wcmt: StatusName := 'checkmate -- White is mated';
    gs_bcmt: StatusName := 'checkmate -- Black is mated';
    gs_wsmt: StatusName := 'stalemate -- White has no move';
    gs_bsmt: StatusName := 'stalemate -- Black has no move';
    gs_dhmc: StatusName := 'draw -- fifty-move rule';
    gs_dmat: StatusName := 'draw -- insufficient material';
  else
    StatusName := '(other)';
  end;
end;

procedure PascalToSanBuf(const s: string; var buf: TSanBuf);
var
  i, n: Integer;
begin
  FillChar(buf, SizeOf(buf), 0);
  n := Length(s);
  if n > sanL - 1 then
    n := sanL - 1;
  for i := 1 to n do
    buf[i - 1] := s[i];
end;

procedure Play(const text: string);
var
  buf: TSanBuf;
  mptr: PMove;
begin
  PascalToSanBuf(text, buf);
  mptr := SANDecodeAux(SanBufPtr(buf));
  SANPlayMove(mptr);
end;

begin
  SANInit;
  optnv[optn_nabd] := 1;
  optnv[optn_namd] := 1;

  WriteLn('Starting position status: ', StatusName(SANStatus));

  { the "fool's mate": the fastest possible checkmate }
  Play('f3');
  Play('e5');
  Play('g4');
  Play('Qh4');

  WriteLn;
  WriteLn('After 1.f3 e5 2.g4 Qh4:');
  WriteLn('  FEN:              ', SANFENGen);
  if SANWhiteInCheck <> 0 then
    WriteLn('  White in check?   yes')
  else
    WriteLn('  White in check?   no');
  WriteLn('  Status:           ', StatusName(SANStatus));
end.
