Subversion Repositories Games.Chess Giants

Rev

Rev 74 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // san.cpp
  2.  
  3. #include "common.h"
  4.  
  5.  
  6. // handy definitions
  7. #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))))))))
  8. #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))))))))
  9. #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"_"))))))))
  10. #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"_"))))))))
  11.  
  12.  
  13. // prototypes of local functions
  14. static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move);
  15.  
  16.  
  17. bool Move_SetupFromSAN (boardmove_t *move, boardmove_t *new_move, int move_color)
  18. {
  19.    // helper function to translate a SAN move into numeric from and to positions on the specified board
  20.    // WARNING: THIS FUNCTION DOES NOT RETURN WITH A FULLY CONSTRUCTED MOVE!
  21.  
  22.    // FIXME: accelerate this function by reducing the number of towupper()/towlower() calls
  23.  
  24.    wchar_t character;
  25.    int fieldstart;
  26.    int fieldstop;
  27.    int length;
  28.  
  29.    // first, get move string length
  30.    length = wcslen (new_move->pgntext);
  31.  
  32.    // parse it from the beginning
  33.    fieldstart = 0;
  34.    fieldstop = 0;
  35.  
  36.    // find where it stops
  37.    while ((fieldstop < length) && !iswspace (new_move->pgntext[fieldstop]))
  38.       fieldstop++; // reach the first space
  39.    fieldstop--; // ignore the space
  40.  
  41.    // does it finish with a move value estimation ?
  42.    while ((fieldstop >= fieldstart) && ((new_move->pgntext[fieldstop] == L'?') || (new_move->pgntext[fieldstop] == L'!')))
  43.       fieldstop--; // ignore these signs, they are redundant
  44.  
  45.    // does it finish with a check or a checkmate sign ?
  46.    if ((fieldstop >= fieldstart) && ((new_move->pgntext[fieldstop] == L'+') || (new_move->pgntext[fieldstop] == L'#')))
  47.       fieldstop--; // chop off these signs, they are redundant
  48.  
  49.    // reset relevant parts of the move we're about to build
  50.    new_move->color = move_color; // save its color
  51.    new_move->part = PART_NONE;
  52.    new_move->promotion_type = PART_NONE;
  53.    new_move->source[0] = -1;
  54.    new_move->source[1] = -1;
  55.    new_move->target[0] = -1;
  56.    new_move->target[1] = -1;
  57.  
  58.    // is it a long castle ?
  59.    if ((_wcsnicmp (&new_move->pgntext[fieldstart], L"O-O-O", 5) == 0) || (wcsncmp (&new_move->pgntext[fieldstart], L"0-0-0", 5) == 0))
  60.    {
  61.       new_move->source[0] = (move_color == COLOR_WHITE ? 0 : 7);
  62.       new_move->source[1] = 4;
  63.       new_move->target[0] = (move_color == COLOR_WHITE ? 0 : 7);
  64.       new_move->target[1] = 2;
  65.       new_move->part = PART_KING; // it's a king's move
  66.    }
  67.  
  68.    // else is it a short castle ?
  69.    else if ((_wcsnicmp (&new_move->pgntext[fieldstart], L"O-O", 3) == 0) || (wcsncmp (&new_move->pgntext[fieldstart], L"0-0", 3) == 0))
  70.    {
  71.       new_move->source[0] = (move_color == COLOR_WHITE ? 0 : 7);
  72.       new_move->source[1] = 4;
  73.       new_move->target[0] = (move_color == COLOR_WHITE ? 0 : 7);
  74.       new_move->target[1] = 6;
  75.       new_move->part = PART_KING; // it's a king's move
  76.    }
  77.  
  78.    // else it's a normal move
  79.    else
  80.    {
  81.       // does it end with the optional "en passant" prefix ?
  82.       if ((fieldstop >= fieldstart + 4) && (wcsncmp (&new_move->pgntext[fieldstop - 3], L"e.p.", 4) == 0))
  83.          fieldstop -= 4; // if so, chop it off
  84.  
  85.       // 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)
  86.       if (fieldstop >= fieldstart)
  87.       {
  88.          character = towupper (new_move->pgntext[fieldstop]); // get the character here
  89.          if      (character == L'R') { fieldstop--; new_move->promotion_type = PART_ROOK;   } // there's a promotion to rook
  90.          else if (character == L'N') { fieldstop--; new_move->promotion_type = PART_KNIGHT; } // there's a promotion to knight
  91.          else if (character == L'B') { fieldstop--; new_move->promotion_type = PART_BISHOP; } // there's a promotion to bishop
  92.          else if (character == L'Q') { fieldstop--; new_move->promotion_type = PART_QUEEN;  } // there's a promotion to queen
  93.       }
  94.  
  95.       // is there the promotion's equal sign ?
  96.       if ((fieldstop >= fieldstart) && (new_move->pgntext[fieldstop] == L'='))
  97.          fieldstop--; // skip it, it's redundant
  98.  
  99.       // read the target line and column
  100.       if (fieldstop >= fieldstart)
  101.       {
  102.          new_move->target[0] = WCHAR_TO_LINE (new_move->pgntext[fieldstop]); // read the target line
  103.          fieldstop--; // proceed to previous character
  104.       }
  105.       if (fieldstop >= fieldstart)
  106.       {
  107.          new_move->target[1] = WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]); // read the target column (WARNING: ONLY IF LOWERCASE)
  108.          fieldstop--; // proceed to previous character
  109.       }
  110.  
  111.       if ((fieldstop >= fieldstart) && (new_move->pgntext[fieldstop] == L'x'))
  112.          fieldstop--; // if there's a taking piece indication, it's superfluous, so skip it
  113.  
  114.       // read the optional source line and column
  115.       if ((fieldstop >= fieldstart) && (WCHAR_TO_LINE (new_move->pgntext[fieldstop]) != -1))
  116.       {
  117.          new_move->source[0] = WCHAR_TO_LINE (new_move->pgntext[fieldstop]); // read the source line
  118.          fieldstop--; // proceed to previous character
  119.       }
  120.       if ((fieldstop >= fieldstart) && (WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]) != -1))
  121.       {
  122.          new_move->source[1] = WCHAR_TO_COLUMN (new_move->pgntext[fieldstop]); // read the source column (WARNING: ONLY IF LOWERCASE)
  123.          fieldstop--; // proceed to previous character
  124.       }
  125.  
  126.       // read the part's type (WARNING: PART TYPES ARE MANDATORILY IN UPPERCASE)
  127.       if (fieldstop >= fieldstart)
  128.       {
  129.          if      (new_move->pgntext[fieldstop] == L'R') new_move->part = PART_ROOK; // it's a rook
  130.          else if (new_move->pgntext[fieldstop] == L'N') new_move->part = PART_KNIGHT; // it's a knight
  131.          else if (new_move->pgntext[fieldstop] == L'B') new_move->part = PART_BISHOP; // it's a bishop
  132.          else if (new_move->pgntext[fieldstop] == L'Q') new_move->part = PART_QUEEN; // it's a queen
  133.          else if (new_move->pgntext[fieldstop] == L'K') new_move->part = PART_KING; // it's a king
  134.          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...)
  135.          else return (false); // on error, cancel
  136.       }
  137.       else
  138.          new_move->part = PART_PAWN; // if not specified, it's a pawn
  139.    }
  140.  
  141.    // now, disambiguate.
  142.    return (SAN_DisambiguateMove (move, new_move));
  143. }
  144.  
  145.  
  146. bool Move_DescribeInSAN (boardmove_t *move)
  147. {
  148.    // convert a board and its part placements into a SAN notation, writing in the pgntext buffer
  149.  
  150.    int line;
  151.    int column;
  152.    boardslot_t *slot;
  153.    bool needs_line;
  154.    bool needs_column;
  155.  
  156.    // build move string in abbreviated algebraic notation
  157.    move->pgntext[0] = 0;
  158.  
  159.    // is it a king castling or a normal move ?
  160.    if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 2))
  161.       wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O-O"); // long castle
  162.    else if ((move->part == PART_KING) && (move->source[1] == 4) && (move->target[1] == 6))
  163.       wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"O-O"); // short castle
  164.    else
  165.    {
  166.       // part identifier (omit it if pawn)
  167.       if (move->part == PART_ROOK)
  168.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"R");
  169.       else if (move->part == PART_KNIGHT)
  170.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"N");
  171.       else if (move->part == PART_BISHOP)
  172.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"B");
  173.       else if (move->part == PART_QUEEN)
  174.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"Q");
  175.       else if (move->part == PART_KING)
  176.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"K");
  177.  
  178.       // is it a pawn taking ?
  179.       if ((move->part == PART_PAWN) && (move->has_captured))
  180.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1]));
  181.  
  182.       // else is there a possible ambiguity ? (not for pawns)
  183.       if ((move->part != PART_PAWN) && (Move_CountPartsByColorAndType (move, move->color, move->part) > 1))
  184.       {
  185.          // assume we don't need to disambiguate neither by column nor by line
  186.          needs_column = false;
  187.          needs_line = false;
  188.  
  189.          // cycle through the board and find all this part's siblings (be sure to parse them all)
  190.          for (line = 0; line < 8; line++)
  191.             for (column = 0; column < 8; column++)
  192.             {
  193.                slot = &move->slots[line][column]; // quick access to slot
  194.  
  195.                // is this slot occupied by one of our siblings that is not the one doing the move AND its movement is valid ?
  196.                if ((slot->part == move->part) && (slot->color == move->color) && ((line != move->source[0]) || (column != move->source[1]))
  197.                    && Move_IsMoveValid (move, line, column, move->target[0], move->target[1]))
  198.                {
  199.                   if (column != move->source[1])
  200.                      needs_column = true; // if columns differ, remember to write start column
  201.                   else
  202.                      needs_line = true; // else if lines differ, remember to write start line
  203.                }
  204.             }
  205.  
  206.          // do we need to write start column or start line ?
  207.          if (needs_column)
  208.             wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->source[1])); // if so, write start column
  209.          if (needs_line)
  210.             wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->source[0])); // if so, write start line
  211.       }
  212.  
  213.       // does it capture something ?
  214.       if (move->has_captured)
  215.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"x");
  216.  
  217.       // target column, target line
  218.       wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), COLUMN_TO_WSTRING (move->target[1]));
  219.       wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), LINE_TO_WSTRING (move->target[0]));
  220.  
  221.       // is there a promotion ?
  222.       if (move->promotion_type == PART_ROOK)
  223.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=R");
  224.       else if (move->promotion_type == PART_KNIGHT)
  225.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=N");
  226.       else if (move->promotion_type == PART_BISHOP)
  227.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=B");
  228.       else if (move->promotion_type == PART_QUEEN)
  229.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=Q");
  230.       else if (move->promotion_type == PART_KING)
  231.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"=K");
  232.  
  233.       // is there a check or a checkmate ?
  234.       if (move->is_check)
  235.       {
  236.          if (move->is_stalemate)
  237.             wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"#"); // checkmate
  238.          else
  239.             wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"+"); // normal check
  240.       }
  241.  
  242.       // is it an en passant coup ?
  243.       if (move->is_enpassant)
  244.          wcscat_s (move->pgntext, WCHAR_SIZEOF (move->pgntext), L"e.p.");
  245.    }
  246.  
  247.    return (true); // finished
  248. }
  249.  
  250.  
  251. static bool SAN_DisambiguateMove (boardmove_t *move, boardmove_t *new_move)
  252. {
  253.    // handy helper to disambiguate a move literal of which we don't know the source,
  254.    // but for which we know the part type and the target slot
  255.  
  256.    int index_line;
  257.    int index_column;
  258.  
  259.    // are both source line and column known ?
  260.    if ((new_move->source[0] != -1) && (new_move->source[1] != -1))
  261.       return (true); // no need to disambiguate anything
  262.  
  263.    // else is source line known ?
  264.    else if (new_move->source[0] != -1)
  265.    {
  266.       // cycle through all the columns and find the part of the same type that has the right to move there
  267.       for (index_column = 0; index_column < 8; index_column++)
  268.          if ((move->slots[new_move->source[0]][index_column].color == new_move->color)
  269.              && (move->slots[new_move->source[0]][index_column].part == new_move->part)
  270.              && Move_IsMoveValid (move, new_move->source[0], index_column, new_move->target[0], new_move->target[1]))
  271.          {
  272.             new_move->source[1] = index_column; // save column
  273.             return (true); // we've found it, stop searching
  274.          }
  275.    }
  276.  
  277.    // else is source column known ?
  278.    else if (new_move->source[1] != -1)
  279.    {
  280.       // cycle through all the lines and find the part of the same type that has the right to move there
  281.       for (index_line = 0; index_line < 8; index_line++)
  282.          if ((move->slots[index_line][new_move->source[1]].color == new_move->color)
  283.              && (move->slots[index_line][new_move->source[1]].part == new_move->part)
  284.              && Move_IsMoveValid (move, index_line, new_move->source[1], new_move->target[0], new_move->target[1]))
  285.          {
  286.             new_move->source[0] = index_line; // save line
  287.             return (true); // we've found it, stop searching
  288.          }
  289.    }
  290.  
  291.    // else neither source line nor column is known
  292.    else
  293.    {
  294.       // cycle through all the board and find the part of the same type that has the right to move there
  295.       for (index_line = 0; index_line < 8; index_line++)
  296.          for (index_column = 0; index_column < 8; index_column++)
  297.             if ((move->slots[index_line][index_column].color == new_move->color)
  298.                 && (move->slots[index_line][index_column].part == new_move->part)
  299.                 && Move_IsMoveValid (move, index_line, index_column, new_move->target[0], new_move->target[1]))
  300.             {
  301.                new_move->source[0] = index_line; // save line and column
  302.                new_move->source[1] = index_column;
  303.                return (true); // we've found it, stop searching
  304.             }
  305.    }
  306.  
  307.    return (false); // no possibility found
  308. }
  309.