Implement Grand Chess
[capablanca.git] / lasker-2.2.3 / src / algcheck.c
1 /*
2    Copyright (c) 1993 Richard V. Nash.
3    Copyright (c) 2000 Dan Papasian
4    Copyright (C) Andrew Tridgell 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
22 #include "includes.h"
23
24 /* Well, lets see if I can list the possibilities
25  * Piece moves
26  * Ne4
27  * Nxe4
28  * Nce4
29  * Ncxe4
30  * R2f3
31  * R2xf3
32  * Special pawn moves
33  * e4
34  * ed
35  * exd
36  * exd5
37  * ed5
38  * Drop moves (bughouse, board edit)
39  * P@f7 P*f7
40  * #f7 #Nf7
41  * (o-o, o-o-o) Castling is handled earlier, so don't worry about that
42  * Of course any of these can have a + or ++ or = string on the end, just
43  * cut that off.
44  */
45
46 /* f - file
47  * r - rank
48  * p - piece
49  * x - x
50  * @ - drop character (bughouse)
51  */
52 static char *alg_list[] = {
53   "fxfr", "pxfr",               /* These two get confused in case of bishop */
54   "ffr", "pfr",                 /* These two get confused in case of bishop */
55   "pffr",
56   "pfxfr",
57   "prfr",
58   "prxfr",
59   "fr",
60   "ff",
61   "fxf",
62   "p@fr",
63   "#fr",
64   "#pfr",
65   NULL
66 };
67
68 #define ALG_UNKNOWN -1
69
70 static int get_move_info(const char *str, int *piece, int *ff, int *fr, int *tf, int *tr, int *bishconfusion)
71 {
72   char tmp[1024];
73   char *s;
74   int i, j, len;
75   char c;
76   int matchVal = -1;
77   int lpiece, lff, lfr, ltf, ltr;
78
79   *bishconfusion = 0;
80   strlcpy(tmp, str, sizeof(tmp));
81   if ((s = strchr(tmp, '+'))) { /* Cut off any check marks */
82     *s = '\0';
83   }
84   if ((s = strchr(tmp, '='))) { /* Cut off any promotion marks */
85     *s = '\0';
86   }
87   if ((s = strchr(tmp, '#'))) { /* Cut off any 'mates' marks */
88     *s = '\0';
89   }
90   *piece = *ff = *fr = *tf = *tr = ALG_UNKNOWN;
91   len = strlen(tmp);
92   for (i = 0; alg_list[i]; i++) {
93     lpiece = lff = lfr = ltf = ltr = ALG_UNKNOWN;
94     if (strlen(alg_list[i]) != len)
95       continue;
96     for (j = len - 1; j >= 0; j--) {
97       switch (alg_list[i][j]) {
98       case 'f':
99         if ((tmp[j] < 'a') || (tmp[j] > 'l')) // [HGM] upto l-file
100           goto nomatch;
101         if (ltf == ALG_UNKNOWN)
102           ltf = tmp[j] - 'a';
103         else
104           lff = tmp[j] - 'a';
105         break;
106       case 'r':
107         if ((tmp[j] < '0') || (tmp[j] > '9')) // [HGM] also match 0- and 9-rank
108           goto nomatch;
109         if (ltr == ALG_UNKNOWN)\r
110           ltr = tmp[j] - '0'; // [HGM] allow 0-rank for Xiangqi, correct later\r
111         else\r
112           lfr = tmp[j] - '0';
113         break;
114       case 'p':
115         if (isupper(tmp[j]))
116           c = tolower(tmp[j]);
117         else
118           c = tmp[j];
119         if (c == 'k')
120           lpiece = KING;
121         else if (c == 'e')   // [HGM] note that som piece indicators are ambiguous,
122           lpiece = ELEPHANT; //       and their true meaning depends on the variant,
123         else if (c == 'v')   //       which we do not know at this point.
124           lpiece = CENTAUR;
125         else if (c == 's')
126           lpiece = SILVER;
127         else if (c == 'g')
128           lpiece = GOLD;
129         else if (c == 'l')
130           lpiece = LANCE;
131         else if (c == 'f')
132           lpiece = FERZ;
133         else if (c == 'h')
134           lpiece = HORSE;
135         else if (c == 'w')
136           lpiece = WAZIR;
137         else if (c == 'o')
138           lpiece = SQUIRREL;
139         else if (c == 'q')
140           lpiece = QUEEN;
141         else if (c == 'c')
142           lpiece = MARSHALL;
143         else if (c == 'a')
144           lpiece = CARDINAL;
145         else if (c == 'm')
146           lpiece = MAN;
147         else if (c == 'r')
148           lpiece = ROOK;
149         else if (c == 'b')
150           lpiece = BISHOP;
151         else if (c == 'n')
152           lpiece = KNIGHT;
153         else if (c == 'p')
154           lpiece = PAWN;
155         else if (c == 'd')
156           lpiece = DRAGONKING;
157         else
158           goto nomatch;
159         break;
160       case 'x':
161         if ((tmp[j] != 'x') && (tmp[j] != 'X'))
162           goto nomatch;
163         break;
164       case '@':
165         if (tmp[j] != '@' && tmp[j] != '*')
166           goto nomatch;
167         lff = lfr = ALG_DROP;
168         break;
169       case '#':
170         if (tmp[j] != '#')
171           goto nomatch;
172         lff = lfr = ALG_DROP;
173         break;
174       default:
175         d_printf( "Unknown character in algebraic parsing\n");
176         break;
177       }
178     }
179     if (lpiece == ALG_UNKNOWN)
180       lpiece = PAWN;
181     if (lpiece == PAWN && (lfr == ALG_UNKNOWN)) {       /* ffr or ff */
182       if (lff != ALG_UNKNOWN) {
183         if (lff == ltf)
184           goto nomatch;
185         if ((lff - ltf != 1) && (ltf - lff != 1))
186           goto nomatch;
187       }
188     }
189     *piece = lpiece;            /* We have a match */
190     *tf = ltf;
191     *tr = ltr;
192     *ff = lff;
193     *fr = lfr;
194     if (matchVal != -1) {
195       /* We have two matches, it must be that Bxc4 vs. bxc4 problem */
196       /* Or it could be the Bc4 vs bc4 problem */
197       *bishconfusion = 1;
198     }
199     matchVal = i;
200 nomatch:;
201   }
202
203   if (matchVal != -1)
204     return MS_ALG;
205   else
206     return MS_NOTMOVE;
207 }
208
209 int alg_is_move(const char *mstr)
210 {
211         int piece=0, ff=0, fr=0, tf=0, tr=0, bc=0;
212
213         return get_move_info(mstr, &piece, &ff, &fr, &tf, &tr, &bc);
214 }
215
216 /* add any promotion qualifier from a move string */
217 static void add_promotion(struct game_state_t *gs, const char *mstr, struct move_t * mt)
218 {
219         char *s;
220         int piece, i;
221         s = strchr(mstr, '=');
222         if (s == NULL) {
223                 return;
224         }
225         if(gs->promoType == 3) { // handle Shogi promotions
226                 piece = gs->board[mt->fromFile][mt->fromRank];
227                 if(colorval(piece) == WHITE && mt->fromRank < gs->ranks - gs->ranks/3
228                                             && mt->toRank   < gs->ranks - gs->ranks/3 ) return;
229                 if(colorval(piece) == BLACK && mt->fromRank >= gs->ranks/3
230                                             && mt->toRank   >= gs->ranks/3 ) return;
231                 switch(piecetype(piece)) {
232                     case PAWN:
233                     case LANCE:
234                     case HONORABLEHORSE:
235                     case SILVER:
236                         if(s[1] != '+' && s[1] != '^' && s[1] != 'G' && s[1] != 'g') return;
237                         piece = GOLD; break;
238                     case BISHOP:
239                         if(s[1] != '+' && s[1] != '^' && s[1] != 'H' && s[1] != 'h') return;
240                         piece = DRAGONHORSE; break;
241                     case ROOK:
242                         if(s[1] != '+' && s[1] != '^' && s[1] != 'D' && s[1] != 'd') return;
243                         piece = DRAGONKING; break;
244                     default: return; // others do not promote, so ignore
245                 }
246                 mt->piecePromotionTo = piece | colorval(gs->board[mt->fromFile][mt->fromRank]);
247                 return;
248         }
249
250         if (piecetype(gs->board[mt->fromFile][mt->fromRank]) != PAWN) {
251                 return;
252         }
253         if (mt->toRank < gs->ranks - gs->promoZone && mt->toRank >= gs->promoZone) {
254                 return;
255         }
256
257         switch (tolower(s[1])) {
258         case 'f':
259                 piece = FERZ2;
260                 break;
261         case 'q':
262                 piece = QUEEN;
263                 break;
264         case 'c':\r
265                 if(!gs->capablancaPieces) return; // [HGM] should make variant-dependent piece mask\r
266                 piece = MARSHALL;\r
267                 break;\r
268         case 'a':\r
269                 if(!gs->capablancaPieces) return;\r
270                 piece = CARDINAL;\r
271                 break;\r
272         case 'm':\r
273                 if(!gs->royalKnight) return; // [HGM] only in knightmate\r
274                 piece = MAN;\r
275                 break;\r
276         case 'r':
277                 piece = ROOK;
278                 break;
279         case 'b':
280                 piece = BISHOP;
281                 break;
282         case 'n':
283                 if(gs->royalKnight) return; // [HGM] not in knightmate\r
284                 piece = KNIGHT;
285                 break;
286         // Superchess promotons: filtered out later by promoType
287         case 'g':
288                 piece = MASTODON;
289                 break;
290         case 'o':
291                 piece = SQUIRREL;
292                 break;
293         case 'w':
294                 piece = WOODY;
295                 break;
296         case 'v':
297                 piece = CENTAUR;
298                 break;
299         case 'e':\r
300                 piece = EMPRESS;
301                 break;
302         case 's':
303                 piece = PRINCESS;
304                 break;
305         default:
306                 return;
307         }
308         i = colorval(gs->board[mt->fromFile][mt->fromRank]) == WHITE ? 0 : 1;
309         if(gs->promoType == 2 && gs->holding[i][piece-1] == 0) return; // only if piece was captured
310         if(piece >= WOODY && (gs->promoType != 2 || gs->promoZone == 3)) return; // reserved for Superchess
311
312         mt->piecePromotionTo = piece | colorval(gs->board[mt->fromFile][mt->fromRank]);
313 }
314
315 /* We already know it is algebraic, get the move squares */
316 int alg_parse_move(char *mstr, struct game_state_t * gs, struct move_t * mt)
317 {
318         int f=0, r=0, tmpr=0, posf=0, posr=0, posr2=0;
319         int piece=0, ff=0, fr=0, tf=0, tr=0, bc=0;
320
321   if (get_move_info(mstr, &piece, &ff, &fr, &tf, &tr, &bc) != MS_ALG) {
322     d_printf( "CHESSD: Shouldn't try to algebraicly parse non-algabraic move string.\n");
323     return MOVE_ILLEGAL;
324   }
325   // [HGM] check if move does not stray off board\r
326   if(gs->ranks < 10) { \r
327     if(tr == 0 || fr == 0) return MOVE_ILLEGAL; // used nonexistent 0-rank\r
328     if(tr != ALG_UNKNOWN) tr--; if(fr != ALG_UNKNOWN) fr--; // shift to lowest rank = 1\r
329   }\r
330   if(tr >= gs->ranks || fr >= gs->ranks || tf >= gs->files || ff >= gs->files)\r
331     return MOVE_ILLEGAL;\r
332
333   // [HGM] resolve ambiguity in piece, type based on variant
334   switch(piece) {
335     case ELEPHANT:
336       if(strstr(gs->variant, "super"))   piece = EMPRESS; else
337       if(strstr(gs->variant, "great"))   piece = MODERNELEPHANT; else
338       if(strstr(gs->variant, "courier")) piece = ALFIL2;
339       break;
340     case CARDINAL:
341       if(strstr(gs->variant, "super")) piece = AMAZON; else
342       if(strstr(gs->variant, "xiangqi")) piece = MANDARIN;
343       break;
344     case MARSHALL:
345       if(strstr(gs->variant, "xiangqi")) piece = CANNON;
346       break;
347     case SILVER:
348       if(strstr(gs->variant, "super")) piece = PRINCESS;
349       if(strstr(gs->variant, "great")) piece = MAN2;
350       break;
351     case BISHOP:
352       if(strstr(gs->variant, "shatranj")) piece = ALFIL;
353       break;
354     case QUEEN:
355       if(strstr(gs->variant, "shatranj")) piece = FERZ;
356       break;
357     case WAZIR:
358       if(strstr(gs->variant, "super")) piece = WOODY;
359       break;
360     case KNIGHT:
361       if(strstr(gs->variant, "shogi")) piece = HONORABLEHORSE;
362       break;
363     case MAN:
364       if(strstr(gs->variant, "great")) piece = MINISTER;
365       break;
366     case HORSE:
367       if(strstr(gs->variant, "great")) piece = PRIESTESS;
368       if(strstr(gs->variant, "shogi")) piece = DRAGONHORSE;
369       break;
370     case GOLD:
371       if(strstr(gs->variant, "great")) piece = MASTODON;
372       break;
373   }
374 \r
375   /* Resolve ambiguities in to-ness */
376   if (tf == ALG_UNKNOWN) {
377           d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
378           return MOVE_AMBIGUOUS;        /* Must always know to file */
379   }
380   if (tr == ALG_UNKNOWN) {
381     posr = posr2 = ALG_UNKNOWN;
382     if (piece != PAWN) {
383             d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
384             return MOVE_AMBIGUOUS;
385     }
386     if (ff == ALG_UNKNOWN) {
387             d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
388             return MOVE_AMBIGUOUS;
389     }
390     /* Need to find pawn on ff that can take to tf and fill in ranks */
391     for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
392          NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
393       if ((ff != ALG_UNKNOWN) && (ff != f))
394         continue;
395       if (piecetype(gs->board[f][r]) != piece)
396         continue;
397       if (gs->onMove == WHITE) {
398         tmpr = r + 1;
399       } else {
400         tmpr = r - 1;
401       }
402 /*      if ((gs->board[tf][tmpr] == NOPIECE) ||
403           (iscolor(gs->board[tf][tmpr], gs->onMove))) continue;*/
404 /* patch from Soso, added by Sparky 3/16/95                    */
405       if (gs->board[tf][tmpr] == NOPIECE) {
406         if ((gs->ep_possible[((gs->onMove == WHITE) ? 0 : 1)][ff]) != (tf - ff))
407           continue;
408       } else {
409         if (iscolor(gs->board[tf][tmpr], gs->onMove))
410           continue;
411       }
412
413       if (legal_andcheck_move(gs, f, r, tf, tmpr)) {
414               if ((posr != ALG_UNKNOWN) && (posr2 != ALG_UNKNOWN)) {
415                       d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
416                       return MOVE_AMBIGUOUS;
417               }
418         posr = tmpr;
419         posr2 = r;
420       }
421     }
422     tr = posr;
423     fr = posr2;
424   } else if (bc) {              /* Could be bxc4 or Bxc4, tr is known */
425     ff = ALG_UNKNOWN;
426     fr = ALG_UNKNOWN;
427     for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
428          NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
429             if ((piecetype(gs->board[f][r]) != PAWN) && (piecetype(gs->board[f][r]) != piece)) {
430                     // note that the interpretation Bxc4 is matched last, and has set piece to BISHOP
431                     continue;
432             }
433             if (legal_andcheck_move(gs, f, r, tf, tr)) {
434                     if ((piecetype(gs->board[f][r]) == PAWN) && (f != tolower(mstr[0]) - 'a')) {
435                             continue;
436                     }
437
438                     /* if its a lowercase 'b' then prefer the pawn move if there is one */
439                     if ((ff != ALG_UNKNOWN) && (fr != ALG_UNKNOWN) &&
440                         piecetype(gs->board[f][r]) == PAWN && mstr[0] >= 'a') {
441                             ff = f;
442                             fr = r;
443                             continue;
444                     }
445
446                     if ((ff != ALG_UNKNOWN) && (fr != ALG_UNKNOWN) &&
447                         piecetype(gs->board[ff][fr]) == PAWN && mstr[0] >= 'a') {
448                             continue;
449                     }
450
451                     if ((ff != ALG_UNKNOWN) && (fr != ALG_UNKNOWN)) {
452                             d_printf("Ambiguous %s(%d) mstr=%s\n", __FUNCTION__, __LINE__, mstr);
453                             return (MOVE_AMBIGUOUS);
454                     }
455                     ff = f;
456                     fr = r;
457             }
458     }
459   } else {                      /* The from position is unknown */
460     posf = ALG_UNKNOWN;
461     posr = ALG_UNKNOWN;
462     if ((ff == ALG_UNKNOWN) || (fr == ALG_UNKNOWN)) {
463       /* Need to find a piece that can go to tf, tr */
464       for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
465            NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
466         if ((ff != ALG_UNKNOWN) && (ff != f))
467           continue;
468         if ((fr != ALG_UNKNOWN) && (fr != r))
469           continue;
470         if (piecetype(gs->board[f][r]) != piece)
471           continue;
472         if (legal_andcheck_move(gs, f, r, tf, tr)) {
473                 if ((posf != ALG_UNKNOWN) && (posr != ALG_UNKNOWN)) {
474                         d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
475                         return MOVE_AMBIGUOUS;
476                 }
477           posf = f;
478           posr = r;
479         }
480       }
481     } else if (ff == ALG_DROP) {
482       if (legal_andcheck_move(gs, ALG_DROP, piece, tf, tr)) {
483         posf = ALG_DROP;
484         posr = piece;
485       }
486     }
487     ff = posf;
488     fr = posr;
489   }
490   if ((tf == ALG_UNKNOWN) || (tr == ALG_UNKNOWN) ||
491       (ff == ALG_UNKNOWN) || (fr == ALG_UNKNOWN))
492     return MOVE_ILLEGAL;
493   mt->fromFile = ff;
494   mt->fromRank = fr;
495   mt->toFile = tf;
496   mt->toRank = tr;
497
498   add_promotion(gs, mstr, mt);
499
500   return MOVE_OK;
501 }
502
503 /* A assumes the move has yet to be made on the board */
504
505 /* Soso: rewrote alg_unparse function.
506  * Algebraic deparser - sets the mStr variable with move description
507  * in short notation. Used in last move report and in 'moves' command.
508  */
509
510 char *alg_unparse(struct game_state_t * gs, struct move_t * mt)
511 {
512   static char mStr[20];
513   char tmp[20];
514   int piece, f, r;
515   int ambig, r_ambig, f_ambig;
516   struct game_state_t fakeMove;
517
518   if (mt->fromFile == ALG_DROP) {
519     piece = mt->fromRank;
520   } else {
521     piece = piecetype(gs->board[mt->fromFile][mt->fromRank]);
522   }
523
524   if ((mt->fromFile == ALG_CASTLE) && (mt->toFile > mt->toRank)) { // [HGM] castle: K ends right of R\r
525     strcpy(mStr, "O-O");\r
526     goto check;\r
527   }\r
528   if ((mt->fromFile == ALG_CASTLE) && (mt->toFile < mt->toRank)) { // [HGM] castle: K ends left of R\r
529     strcpy(mStr, "O-O-O");\r
530     goto check;\r
531   }\r
532   strcpy(mStr, "");
533   switch (piece) {
534   case PAWN:
535     if (mt->fromFile == ALG_DROP) {
536       strcpy(mStr,"P");
537     } else if (mt->fromFile != mt->toFile 
538             || gs->board[mt->toFile][mt->toRank] != NOPIECE) { // [HGM] XQ: forward captures as "axa6"
539       sprintf(tmp, "%c", mt->fromFile + 'a');
540       strcpy(mStr, tmp);
541     }
542     break;
543   case KNIGHT:
544     strcpy(mStr, "N");
545     break;
546   case BISHOP:
547     strcpy(mStr, "B");
548     break;
549   case ROOK:
550     strcpy(mStr, "R");
551     break;
552   case ALFIL2:
553   case AMAZON:
554   case CARDINAL:\r
555     strcpy(mStr, "A");\r
556     break;\r
557   case CANNON:
558   case MARSHALL:\r
559     strcpy(mStr, "C");\r
560     break;\r
561   case MAN:\r
562     strcpy(mStr, "M");\r
563     break;\r
564   case FERZ:
565   case QUEEN:
566     strcpy(mStr, "Q");
567     break;
568   case EMPRESS:
569   case ELEPHANT:
570     strcpy(mStr, "E");
571     break;
572   case ALFIL:
573     strcpy(mStr, "B");
574     break;
575   case FERZ2:
576     strcpy(mStr, "F");
577     break;
578   case WOODY:
579   case WAZIR:
580     strcpy(mStr, "W");
581     break;
582   case SQUIRREL:
583     strcpy(mStr, "O");
584     break;
585   case CENTAUR:
586     strcpy(mStr, "V");
587     break;
588   case HORSE:
589     strcpy(mStr, "H");
590     break;
591   case HONORABLEHORSE:
592     strcpy(mStr, "N");
593     break;
594   case DRAGONKING:
595     strcpy(mStr, "D");
596     break;
597   case DRAGONHORSE:
598     strcpy(mStr, "H");
599     break;
600   case LANCE:
601     strcpy(mStr, "L");
602     break;
603   case PRINCESS:
604   case SILVER:
605     strcpy(mStr, "S");
606     break;
607   case MASTODON:
608   case GOLD:
609     strcpy(mStr, "G");
610     break;
611   case MANDARIN:
612     strcpy(mStr, "A");
613     break;
614   case KING:
615     strcpy(mStr, "K");
616     break;
617   default:
618     strcpy(mStr, "");
619     break;
620   }
621
622   if (mt->fromFile == ALG_DROP) {
623     strcat(mStr, "@");
624   } else {
625   /* Checks for ambiguity in short notation ( Ncb3, R8e8 or so) */
626   if (piece != PAWN) {
627     ambig = r_ambig = f_ambig = 0;
628     for (r = 0; r < gs->ranks; r++)\r
629       for (f = 0; f < gs->files; f++) {\r
630         if ((gs->board[f][r] != NOPIECE) && iscolor(gs->board[f][r], gs->onMove)
631             && (piecetype(gs->board[f][r]) == piece) &&
632             ((f != mt->fromFile) || (r != mt->fromRank))) {
633           if (legal_move(gs, f, r, mt->toFile, mt->toRank)) {
634             fakeMove = *gs;
635             fakeMove.board[f][r] = NOPIECE;
636             fakeMove.board[mt->toFile][mt->toRank] = piece | gs->onMove;
637             fakeMove.onMove = CToggle(fakeMove.onMove);
638             gs->onMove = CToggle(gs->onMove);
639
640 #if 0
641             d_printf("possible move %c%d%c%d against %c%d%c%d\n",
642                      'a' + f, r+1,
643                      'a' + mt->toFile, mt->toRank+1,
644                      'a' + mt->fromFile, mt->fromRank+1,
645                      'a' + mt->toFile, mt->toRank+1);
646 #endif
647
648             if (!in_check(&fakeMove)) {
649                     ambig++;
650                     if (f == mt->fromFile) {
651                             ambig++;
652                             f_ambig++;
653                     }
654                     if (r == mt->fromRank) {
655                             ambig++;
656                             r_ambig++;
657                     }
658             }
659             gs->onMove = CToggle(gs->onMove);
660           }
661         }
662       }
663     if (ambig > 0) {\r
664       /* Ambiguity in short notation, need to add file,rank or _both_ in\r
665          notation */\r
666       if (f_ambig == 0) {\r
667         sprintf(tmp, "%c", mt->fromFile + 'a');\r
668         strcat(mStr, tmp);\r
669       } else if (r_ambig == 0) {\r
670         sprintf(tmp, "%d", mt->fromRank + 1 - (gs->ranks > 9));\r
671         strcat(mStr, tmp);\r
672       } else {\r
673         sprintf(tmp, "%c%d", mt->fromFile + 'a', mt->fromRank + 1 - (gs->ranks > 9));\r
674         strcat(mStr, tmp);\r
675       }\r
676     }\r
677   }\r
678   if ((gs->board[mt->toFile][mt->toRank] != NOPIECE) ||\r
679       ((piece == PAWN) && (mt->fromFile != mt->toFile))) {\r
680     strcat(mStr, "x");\r
681   }\r
682   }\r
683   sprintf(tmp, "%c%d", mt->toFile + 'a', mt->toRank + 1 - (gs->ranks > 9));\r
684   strcat(mStr, tmp);\r
685
686   if ((piece == PAWN || gs->promoType == 3) && (mt->piecePromotionTo != NOPIECE)) {\r
687     strcat(mStr, "=");          /* = before promoting piece */\r
688     switch (piecetype(mt->piecePromotionTo)) {\r
689     case KNIGHT:\r
690       strcat(mStr, "N");\r
691       break;\r
692     case BISHOP:\r
693       strcat(mStr, "B");\r
694       break;\r
695     case ROOK:\r
696       strcat(mStr, "R");\r
697       break;\r
698     case CARDINAL:\r
699       strcat(mStr, "A");\r
700       break;\r
701     case MARSHALL:\r
702       strcat(mStr, "C");\r
703       break;\r
704     case MAN:\r
705       strcat(mStr, "M");\r
706       break;\r
707     case QUEEN:\r
708       strcat(mStr, "Q");\r
709       break;\r
710     case FERZ2:\r
711       strcat(mStr, "F");\r
712       break;\r
713     case WOODY:\r
714       strcat(mStr, "W");\r
715       break;\r
716     case EMPRESS:\r
717       strcat(mStr, "E");\r
718       break;\r
719     case CENTAUR:\r
720       strcat(mStr, "V");\r
721       break;\r
722     case PRINCESS:\r
723       strcat(mStr, "S");\r
724       break;\r
725     case SQUIRREL:\r
726       strcat(mStr, "O");\r
727       break;\r
728     case MASTODON:\r
729       strcat(mStr, "G");\r
730       break;\r
731     case GOLD: // [HGM] Shogi promotions: avoid use of '+'\r
732       strcat(mStr, "G");\r
733       break;\r
734     case DRAGONHORSE:\r
735       strcat(mStr, "H");\r
736       break;\r
737     case DRAGONKING:\r
738       strcat(mStr, "D");\r
739       break;\r
740     default:\r
741       break;\r
742     }\r
743   }\r
744 check:;
745   fakeMove = *gs;
746   execute_move(&fakeMove, mt, 0);
747   fakeMove.onMove = CToggle(fakeMove.onMove);
748   if (in_check(&fakeMove)) {
749     strcat(mStr, "+");
750   }
751   return mStr;
752 }