/* 04_move_generation.c -- generating the list of legal moves.
 *
 * Functions demonstrated:
 *   SANGenMoves()  -- (re)generate the full legal move list for the
 *                     side to move in the current position
 *   SANEncode()    -- format each generated move as SAN
 *
 * After SANGenMoves() runs, the result lives in the global
 * "curr_g" generation record (see the gT type in sandef.h):
 *
 *   curr_g.g_gmvc          number of legal moves generated
 *   curr_g.g_base[i]       the i'th move, 0 <= i < curr_g.g_gmvc
 *
 * This is exactly the list "san"'s own interactive "srch"/"enum"
 * commands and the built-in strategies search over -- it's the
 * single source of truth for "what moves are legal right now".
 */

#include "_common.h"

#include "sanint.h"
#include "sangen.h"
#include "sanmne.h"
#include "sanfen.h"

static void
list_legal_moves (void)
{
  siT i;
  sanT san;

  SANGenMoves ();

  printf ("Position: %s\n", SANFENGen ());
  printf ("%d legal move(s):\n", curr_g.g_gmvc);

  for (i = 0; i < curr_g.g_gmvc; i++)
    {
      SANEncode (&curr_g.g_base[i], san);
      printf ("%s%-7s", san, ((i % 8) == 7) ? "\n" : "");
    };
  printf ("\n");

  return;
}

int
main (void)
{
  SANInit ();

  list_legal_moves ();

  /* a position with far fewer options: White's king is in check
     from a lone rook, so almost every move is illegal -- only
     moving the king or capturing the rook keeps it out of check */
  printf ("\n");
  SANFENSet ("6k1/8/8/8/8/8/6r1/6K1 w - - 0 1");
  list_legal_moves ();

  return (0);
}
