/* 06_game_status.c -- checking status, check, and checkmate.
 *
 * Functions demonstrated:
 *   SANStatus()        -- classify the current position (gsT):
 *                          normal, checkmate, stalemate, draws...
 *   SANWhiteInCheck()  -- is White's king currently attacked?
 *   SANBlackInCheck()  -- is Black's king currently attacked?
 *   SANAttack()        -- is a given square attacked by a given color?
 */

#include "_common.h"

#include "sanint.h"
#include "sanrst.h"
#include "sanatk.h"
#include "sanfen.h"
#include "sanmnd.h"
#include "sanmpu.h"

static const char *
status_name (gsT gs)
{
  switch (gs)
    {
    case gs_norm: return ("normal, game continues");
    case gs_wcmt: return ("checkmate -- White is mated");
    case gs_bcmt: return ("checkmate -- Black is mated");
    case gs_wsmt: return ("stalemate -- White has no move");
    case gs_bsmt: return ("stalemate -- Black has no move");
    case gs_dhmc: return ("draw -- fifty-move rule");
    case gs_dmat: return ("draw -- insufficient material");
    default:      return ("(other)");
    };
}

int
main (void)
{
  sanT buf;
  mptrT mptr;

  SANInit ();
  optnv[optn_nabd] = 1;
  optnv[optn_namd] = 1;

  printf ("Starting position status: %s\n", status_name (SANStatus ()));

  /* the "fool's mate": the fastest possible checkmate */
  strncpy (buf, "f3", sanL); mptr = SANDecodeAux (buf); SANPlayMove (mptr);
  strncpy (buf, "e5", sanL); mptr = SANDecodeAux (buf); SANPlayMove (mptr);
  strncpy (buf, "g4", sanL); mptr = SANDecodeAux (buf); SANPlayMove (mptr);
  strncpy (buf, "Qh4", sanL); mptr = SANDecodeAux (buf); SANPlayMove (mptr);

  printf ("\nAfter 1.f3 e5 2.g4 Qh4:\n");
  printf ("  FEN:              %s\n", SANFENGen ());
  printf ("  White in check?   %s\n", SANWhiteInCheck () ? "yes" : "no");
  printf ("  Status:           %s\n", status_name (SANStatus ()));

  return (0);
}
