/* 02_setup_position.c -- loading and building positions.
 *
 * 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).
 */

#include "_common.h"

#include "sanint.h"
#include "sandsp.h"
#include "sanfen.h"
#include "sanval.h"

int
main (void)
{
  cpT cp;

  SANInit ();

  /* --- Method 1: a FEN string ------------------------------------ */

  printf ("Method 1: SANFENSet with a known FEN\n");
  if (!SANFENSet ("8/8/8/4k3/8/8/4P3/4K3 w - - 0 1"))
    {
      fprintf (stderr, "bad FEN\n");
      return (1);
    };
  SANDspBoard ();

  /* --- Method 2: square by square --------------------------------- */

  printf ("\nMethod 2: building the same king-and-pawn ending by hand\n");

  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 ();
  printf ("As FEN: %s\n", SANFENGen ());

  cp = SANGetSq (sq_e2);
  printf ("\nWhat's on e2? color-piece code %d (%c%c)\n",
          cp, accv[cv_c_cpv[cp]], acpv[cv_p_cpv[cp]]);

  return (0);
}
