
/* custom_board.c -- a board display written entirely from library
 * primitives, in a different style from the library's own
 * SANDspBoard().
 *
 * This needs nothing beyond what examples/02_setup_position.c
 * already used: SANGetSq() to read a square's color-piece code, and
 * accv[]/acpv[]/cv_c_cpv[]/cv_p_cpv[] to turn that code into a
 * color and a piece letter. The square numbering itself
 * (map_sq_rank_file(rank, file), sandef.h) is a macro the library
 * already provides.
 */

#include <stdio.h>

#include "sandef.h"
#include "sanvar.h"

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

static void
PrintBoard (void)
{
  rankT rank;
  fileT file;
  sqT sq;
  cpT cp;
  char ch;

  printf ("   a b c d e f g h\n");
  printf ("   ===============\n");

  for (rank = rank_8; rank >= rank_1; rank--)
    {
      printf ("%d ", rank + 1);
      for (file = file_a; file <= file_h; file++)
        {
          sq = map_sq_rank_file (rank, file);
          cp = SANGetSq (sq);

          if (cp == cp_v0)
            ch = ((rank + file) % 2 == 1) ? '.' : ':';
          else
            {
              ch = acpv[cv_p_cpv[cp]];
              if (cv_c_cpv[cp] == c_b)
                ch = ch + 32;              /* lowercase for Black */
            };

          printf (" %c", ch);
        };
      
      printf ("\n");
    };

  printf ("   ===============\n");

  return;
}

int
main (void)
{
  SANInit ();

  //SANDspBoard ();
  PrintBoard ();
  
  /*
  printf ("\nAfter 1.e4 e5 2.Nf3 Nc6 3.Bb5 (the Ruy Lopez):\n");
  SANFENSet ("r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3");
  PrintBoard ();
  */
  
  printf ("\nSide to move:     %s\n",
          (curr_e.e_actc == c_w) ? "White" : "Black");

  printf ("Castling rights:  %s%s%s%s\n",
          (curr_e.e_cast & cast_wk) ? "K" : "",
          (curr_e.e_cast & cast_wq) ? "Q" : "",
          (curr_e.e_cast & cast_bk) ? "k" : "",
          (curr_e.e_cast & cast_bq) ? "q" : "");

  if (curr_e.e_epsq == sq_nil)
    printf ("En passant:       -\n");
  else
    printf ("En passant:       %c%c\n",
            acfv[map_file_sq (curr_e.e_epsq)],
            acrv[map_rank_sq (curr_e.e_epsq)]);

  printf ("Halfmove clock:   %d\n", curr_e.e_hmvc);
  printf ("Fullmove number:  %d\n", curr_e.e_fmvn);
  printf ("FEN: %s\n", SANFENGen ());
  return (0);
}
