Subversion Repositories Games.Chess Giants

Rev

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