/* 03_san_notation.c -- encoding and decoding moves in SAN.
 *
 * Functions demonstrated:
 *   SANDecodeAux() -- parse a SAN string into a move pointer
 *   SANEncode()    -- format a move pointer back into a SAN string
 *
 * A "move" inside the SAN Kit is not something you build yourself;
 * it's always a pointer (mptrT) into the list of currently legal
 * moves, produced by the move generator (see 04_move_generation.c).
 * SANDecodeAux() looks up a typed-in SAN string against that list
 * and returns a pointer to the matching legal move, or NULL if the
 * text doesn't match any legal move from the current position.
 *
 * Note the sanT buffer type: it's a fixed 16-byte char array
 * (#define sanL 16 in sandef.h), not a plain char*. Copy your text
 * into one of these rather than passing a string literal directly,
 * to keep the compiler's bounds checking happy.
 */

#include "_common.h"

#include "sanint.h"
#include "sanmnd.h"
#include "sanmne.h"

static void
try_decode (const char *text)
{
  sanT buf;
  mptrT mptr;

  strncpy (buf, text, sanL - 1);
  buf[sanL - 1] = '\0';

  mptr = SANDecodeAux (buf);
  if (mptr == NULL)
    printf ("  %-6s -> not a legal move here\n", text);
  else
    {
      sanT canon;
      SANEncode (mptr, canon);
      printf ("  %-6s -> legal; canonical SAN is \"%s\"\n", text, canon);
    };

  return;
}

int
main (void)
{
  SANInit ();
  optnv[optn_nabd] = 1;
  optnv[optn_namd] = 1;

  printf ("From the starting position:\n");
  try_decode ("e4");          /* a normal pawn push */
  try_decode ("Nf3");         /* a normal piece move */
  try_decode ("e5");          /* illegal: no black pawn can reach e5 yet */
  try_decode ("Nc3");         /* legal, alternate knight */
  try_decode ("O-O");         /* illegal: can't castle yet */

  return (0);
}
