// san.cpp
 
 
 
#include "common.h"
 
 
 
 
 
// handy definitions
 
#define WCHAR_TO_COLUMN(a) ((a) == L'a' ? 0 : ((a) == L'b' ? 1 : ((a) == L'c' ? 2 : ((a) == L'd' ? 3 : ((a) == L'e' ? 4 : ((a) == L'f' ? 5 : ((a) == L'g' ? 6 : ((a) == L'h' ? 7 : -1))))))))
 
#define WCHAR_TO_LINE(a) ((a) == L'1' ? 0 : ((a) == L'2' ? 1 : ((a) == L'3' ? 2 : ((a) == L'4' ? 3 : ((a) == L'5' ? 4 : ((a) == L'6' ? 5 : ((a) == L'7' ? 6 : ((a) == L'8' ? 7 : -1))))))))
 
#define COLUMN_TO_WSTRING(c) ((c) == 0 ? L"a" : ((c) == 1 ? L"b" : ((c) == 2 ? L"c" : ((c) == 3 ? L"d" : ((c) == 4 ? L"e" : ((c) == 5 ? L"f" : ((c) == 6 ? L"g" : ((c) == 7 ? L"h" : L"_"))))))))
 
#define LINE_TO_WSTRING(l) ((l) == 0 ? L"1" : ((l) == 1 ? L"2" : ((l) == 2 ? L"3" : ((l) == 3 ? L"4" : ((l) == 4 ? L"5" : ((l) == 5 ? L"6" : ((l) == 6 ? L"7" : ((l) == 7 ? L"8" : L"_"))))))))
 
 
 
 
 
// prototypes of local functions
 
static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move);
 
 
 
 
 
bool Move_SetupFromSAN (boardmove_t *move, boardmove_t *new_move, int move_color)
 
{
 
   // helper function to translate a SAN move into numeric from and to positions on the specified board
 
   // WARNING: THIS FUNCTION DOES NOT RETURN WITH A FULLY CONSTRUCTED MOVE!
 
 
 
   // FIXME: accelerate this function by reducing the number of towupper()/towlower() calls
 
 
 
   wchar_t character;
 
   int fieldstart;
 
   int fieldstop;
 
   int length;
 
 
 
   // first, get move string length
 
   length = wcslen (new_move->pgntext);
 
 
 
   // parse it from the beginning
 
   fieldstart = 0;
 
   fieldstop = 0;
 
 
 
   // find where it stops
 
   while ((fieldstop < length) && !iswspace (new_move->pgntext[fieldstop]))
 
      fieldstop++; // reach the first space
 
   fieldstop--; // ignore the space
 
 
 
   // does it finish with a move value estimation ?
 
   while ((fieldstop >= fieldstart) && ((new_move->pgntext[fieldstop] == L'?') || (new_move->pgntext[fieldstop] == L'!')))
 
      fieldstop--; // ignore these signs, they are redundant
 
 
 
   // does it finish with a check or a checkmate sign ?
 
   if ((fieldstop >= fieldstart) && ((new_move->pgntext[fieldstop] == L'+') || (new_move->pgntext[fieldstop] == L'#')))
 
      fieldstop--; // chop off these signs, they are redundant
 
 
 
   // reset relevant parts of the move we're about to build
 
   new_move->color = move_color; // save its color
 
   new_move->part = PART_NONE;
 
   new_move->promotion_type = PART_NONE;
 
   new_move->source[0] = -1;
 
   new_move->source[1] = -1;
 
   new_move->target[0] = -1;
 
   new_move->target[1] = -1;
 
 
 
   // is it a long castle ?
 
   if ((_wcsnicmp (&new_move->pgntext[fieldstart], L"O-O-O", 5) == 0) || (wcsncmp (&new_move->pgntext[fieldstart], L"0-0-0", 5) == 0))
 
   {
 
      new_move->source[0] = (move_color == COLOR_WHITE ? 0 : 7);
 
      new_move->source[1] = 4;
 
      new_move->target[0] = (move_color == COLOR_WHITE ? 0 : 7);
 
      new_move->target[1] = 2;
 
      new_move->part = PART_KING; // it's a king's move
 
   }
 
 
 
   // else is it a short castle ?
 
   else if ((_wcsnicmp (&new_move->pgntext[fieldstart], L"O-O", 3) == 0) || (wcsncmp (&new_move->pgntext[fieldstart], L"0-0", 3) == 0))
 
   {
 
      new_move->source[0] = (move_color == COLOR_WHITE ? 0 : 7);
 
      new_move->source[1] = 4;
 
      new_move->target[0] = (move_color == COLOR_WHITE ? 0 : 7);
 
      new_move->target[1] = 6;
 
      new_move->part = PART_KING; // it's a king's move
 
   }
 
 
 
   // else it's a normal move
 
   else
 
   {
 
      // does it end with the optional "en passant" prefix ?
 
      if ((fieldstop >= fieldstart + 4) && (wcsncmp (&new_move->pgntext[fieldstop - 3], L"e.p.", 4) == 0))
 
         fieldstop -= 4; // if so, chop it off
 
 
 
      // is the last character a part type ? (WARNING: PART TYPES ARE SUPPOSED TO BE UPPERCASE BUT SOME ENGINES DON'T COMPLY AND SEND IT IN LOWERCASE)
 
      if (fieldstop >= fieldstart)
 
      {
 
         character = towupper (new_move->pgntext[fieldstop]); // get the character here
 
         if      (character == L'R') { fieldstop--; new_move->promotion_type = PART_ROOK;   } // there's a promotion to rook
 
         else if (character == L'N') { fieldstop--; new_move->promotion_type = PART_KNIGHT; } // there's a promotion to knight
 
         else if (character == L'B') { fieldstop--; new_move->promotion_type = PART_BISHOP; } // there's a promotion to bishop
 
         else if (character == L'Q') { fieldstop--; new_move->promotion_type = PART_QUEEN;  } // there's a promotion to queen
 
      }
 
 
 
      // is there the promotion's equal sign ?
 
      if ((fieldstop >= fieldstart) && (new_move->pgntext[fieldstop] == L'='))
 
         fieldstop--; // skip it, it's redundant
 
 
 
      // read the target line and column
 
      if (fieldstop >= fieldstart)
 
      {
 
         new_move->target[0] = WCHAR_TO_LINE (new_move->pgntext[fieldstop]); // read the target line
 
         fieldstop--; // proceed to previous character
 
      }
 
      if (fieldstop >= fieldstart)
 
      {
 
         new_move->target[1] = WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]); // read the target column (WARNING: ONLY IF LOWERCASE)
 
         fieldstop--; // proceed to previous character
 
      }
 
 
 
      if ((fieldstop >= fieldstart) && (new_move->pgntext[fieldstop] == L'x'))
 
         fieldstop--; // if there's a taking piece indication, it's superfluous, so skip it
 
 
 
      // read the optional source line and column
 
      if ((fieldstop >= fieldstart) && (WCHAR_TO_LINE (new_move->pgntext[fieldstop]) != -1))
 
      {
 
         new_move->source[0] = WCHAR_TO_LINE (new_move->pgntext[fieldstop]); // read the source line
 
         fieldstop--; // proceed to previous character
 
      }
 
      if ((fieldstop >= fieldstart) && (WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]) != -1))
 
      {
 
         new_move->source[1] = WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]); // read the source column (WARNING: ONLY IF LOWERCASE)
 
         fieldstop--; // proceed to previous character
 
      }
 
 
 
      // read the part's type (WARNING: PART TYPES ARE MANDATORILY IN UPPERCASE)
 
      if (fieldstop >= fieldstart)
 
      {
 
         if      (new_move->pgntext[fieldstop] == L'R') new_move->part = PART_ROOK; // it's a rook
 
         else if (new_move->pgntext[fieldstop] == L'N') new_move->part = PART_KNIGHT; // it's a knight
 
         else if (new_move->pgntext[fieldstop] == L'B') new_move->part = PART_BISHOP; // it's a bishop
 
         else if (new_move->pgntext[fieldstop] == L'Q') new_move->part = PART_QUEEN; // it's a queen
 
         else if (new_move->pgntext[fieldstop] == L'K') new_move->part = PART_KING; // it's a king
 
         else if (new_move->pgntext[fieldstop] == L'P') new_move->part = PART_PAWN; // it's a pawn (Wikipedia says "P" is a valid part type in PGN texts...)
 
         else return (false); // on error, cancel
 
      }
 
      else
 
         new_move->part = PART_PAWN; // if not specified, it's a pawn
 
   }
 
 
 
   // now, disambiguate.
 
   return (SAN_DisambiguateMove (move, new_move));
 
}
 
 
 
 
 
bool Move_DescribeInSAN (boardmove_t *move)
 
{
 
   // convert a board and its part placements into a SAN notation, writing in the pgntext buffer
 
 
 
   int line;
 
   int column;
 
   boardslot_t *slot;
 
   bool needs_line;
 
   bool needs_column;
 
 
 
   // build move string in abbreviated algebraic notation
 
   move->pgntext[0] = 0;
 
 
 
   // is it a king castling or a normal move ?
 
   if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 2))
 
      wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O-O"); // long castle
 
   else if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 6))
 
      wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O"); // short castle
 
   else
 
   {
 
      // part identifier (omit it if pawn)
 
      if (move->part == PART_ROOK)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"R");
 
      else if (move->part == PART_KNIGHT)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"N");
 
      else if (move->part == PART_BISHOP)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"B");
 
      else if (move->part == PART_QUEEN)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"Q");
 
      else if (move->part == PART_KING)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"K");
 
 
 
      // is it a pawn taking ?
 
      if ((move->part == PART_PAWN) && (move->has_captured))
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1]));
 
 
 
      // else is there a possible ambiguity ? (not for pawns)
 
      if ((move->part != PART_PAWN) && (Move_CountPartsByColorAndType (move, move->color, move->part) > 1))
 
      {
 
         // assume we don't need to disambiguate neither by column nor by line
 
         needs_column = false;
 
         needs_line = false;
 
 
 
         // cycle through the board and find all this part's siblings (be sure to parse them all)
 
         for (line = 0; line < 8; line++)
 
            for (column = 0; column < 8; column++)
 
            {
 
               slot = &move->slots[line][column]; // quick access to slot
 
 
 
               // is this slot occupied by one of our siblings that is not the one doing the move AND its movement is valid ?
 
               if ((slot->part == move->part) && (slot->color == move->color) && ((line != move->source[0]) || (column != move->source[1]))
 
                   && Move_IsMoveValid (move, line, column, move->target[0], move->target[1]))
 
               {
 
                  if (column != move->source[1])
 
                     needs_column = true; // if columns differ, remember to write start column
 
                  else
 
                     needs_line = true; // else if lines differ, remember to write start line
 
               }
 
            }
 
 
 
         // do we need to write start column or start line ?
 
         if (needs_column)
 
            wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1])); // if so, write start column
 
         if (needs_line)
 
            wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->source[0])); // if so, write start line
 
      }
 
 
 
      // does it capture something ?
 
      if (move->has_captured)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"x");
 
 
 
      // target column, target line
 
      wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->target[1]));
 
      wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->target[0]));
 
 
 
      // is there a promotion ?
 
      if (move->promotion_type == PART_ROOK)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=R");
 
      else if (move->promotion_type == PART_KNIGHT)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=N");
 
      else if (move->promotion_type == PART_BISHOP)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=B");
 
      else if (move->promotion_type == PART_QUEEN)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=Q");
 
      else if (move->promotion_type == PART_KING)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=K");
 
 
 
      // is there a check or a checkmate ?
 
      if (move->is_check)
 
      {
 
         if (move->is_stalemate)
 
            wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"#"); // checkmate
 
         else
 
            wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"+"); // normal check
 
      }
 
 
 
      // is it an en passant coup ?
 
      if (move->is_enpassant)
 
         wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"e.p.");
 
   }
 
 
 
   return (true); // finished
 
}
 
 
 
 
 
static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move)
 
{
 
   // handy helper to disambiguate a move literal of which we don't know the source,
 
   // but for which we know the part type and the target slot
 
 
 
   int index_line;
 
   int index_column;
 
 
 
   // are both source line and column known ?
 
   if ((new_move->source[0] != -1) && (new_move->source[1] != -1))
 
      return (true); // no need to disambiguate anything
 
 
 
   // else is source line known ?
 
   else if (new_move->source[0] != -1)
 
   {
 
      // cycle through all the columns and find the part of the same type that has the right to move there
 
      for (index_column = 0; index_column < 8; index_column++)
 
         if ((move->slots[new_move->source[0]][index_column].color == new_move->color)
 
             && (move->slots[new_move->source[0]][index_column].part == new_move->part)
 
             && Move_IsMoveValid (move, new_move->source[0], index_column, new_move->target[0], new_move->target[1]))
 
         {
 
            new_move->source[1] = index_column; // save column
 
            return (true); // we've found it, stop searching
 
         }
 
   }
 
 
 
   // else is source column known ?
 
   else if (new_move->source[1] != -1)
 
   {
 
      // cycle through all the lines and find the part of the same type that has the right to move there
 
      for (index_line = 0; index_line < 8; index_line++)
 
         if ((move->slots[index_line][new_move->source[1]].color == new_move->color)
 
             && (move->slots[index_line][new_move->source[1]].part == new_move->part)
 
             && Move_IsMoveValid (move, index_line, new_move->source[1], new_move->target[0], new_move->target[1]))
 
         {
 
            new_move->source[0] = index_line; // save line
 
            return (true); // we've found it, stop searching
 
         }
 
   }
 
 
 
   // else neither source line nor column is known
 
   else
 
   {
 
      // cycle through all the board and find the part of the same type that has the right to move there
 
      for (index_line = 0; index_line < 8; index_line++)
 
         for (index_column = 0; index_column < 8; index_column++)
 
            if ((move->slots[index_line][index_column].color == new_move->color)
 
                && (move->slots[index_line][index_column].part == new_move->part)
 
                && Move_IsMoveValid (move, index_line, index_column, new_move->target[0], new_move->target[1]))
 
            {
 
               new_move->source[0] = index_line; // save line and column
 
               new_move->source[1] = index_column;
 
               return (true); // we've found it, stop searching
 
            }
 
   }
 
 
 
   return (false); // no possibility found
 
}