Subversion Repositories Games.Chess Giants

Rev

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

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