aa7d5af20234175f5e72457413e08fda8c3ca4e7
[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)
110           ltr = tmp[j] - '0'; // [HGM] allow 0-rank for Xiangqi, correct later
111         else
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':
265                 if(!gs->capablancaPieces) return; // [HGM] should make variant-dependent piece mask
266                 piece = MARSHALL;
267                 break;
268         case 'a':
269                 if(!gs->capablancaPieces) return;
270                 piece = CARDINAL;
271                 break;
272         case 'm':
273                 if(!gs->royalKnight) return; // [HGM] only in knightmate
274                 piece = MAN;
275                 break;
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
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':
300                 piece = gs->drops == 2 ? SELEPHANT : EMPRESS; // for Seirawan
301                 break;
302         case 's':
303                 piece = PRINCESS;
304                 break;
305         case 'h':
306                 if(gs->drops != 2) return;
307                 piece = HAWK;
308                 break;
309         default:
310                 return;
311         }
312         i = colorval(gs->board[mt->fromFile][mt->fromRank]) == WHITE ? 0 : 1;
313         if(gs->promoType == 2 && gs->holding[i][piece-1] == 0) return; // only if piece was captured
314         if(piece >= WOODY && piece < KING && (gs->promoType != 2 || gs->promoZone == 3)) return; // reserved for Superchess
315
316         mt->piecePromotionTo = piece | colorval(gs->board[mt->fromFile][mt->fromRank]);
317 }
318
319 /* We already know it is algebraic, get the move squares */
320 int alg_parse_move(char *mstr, struct game_state_t * gs, struct move_t * mt)
321 {
322         int f=0, r=0, tmpr=0, posf=0, posr=0, posr2=0;
323         int piece=0, ff=0, fr=0, tf=0, tr=0, bc=0;
324
325   if (get_move_info(mstr, &piece, &ff, &fr, &tf, &tr, &bc) != MS_ALG) {
326     d_printf( "CHESSD: Shouldn't try to algebraicly parse non-algabraic move string.\n");
327     return MOVE_ILLEGAL;
328   }
329   // [HGM] check if move does not stray off board
330   if(gs->ranks < 10) { 
331     if(tr == 0 || fr == 0) return MOVE_ILLEGAL; // used nonexistent 0-rank
332     if(tr != ALG_UNKNOWN) tr--; if(fr != ALG_UNKNOWN) fr--; // shift to lowest rank = 1
333   }
334   if(tr >= gs->ranks || fr >= gs->ranks || tf >= gs->files || ff >= gs->files)
335     return MOVE_ILLEGAL;
336
337   // [HGM] resolve ambiguity in piece, type based on variant
338   switch(piece) {
339     case ELEPHANT:
340       if(strstr(gs->variant, "super"))   piece = EMPRESS; else
341       if(strstr(gs->variant, "great"))   piece = MODERNELEPHANT; else
342       if(strstr(gs->variant, "courier")) piece = ALFIL2;
343       break;
344     case CARDINAL:
345       if(strstr(gs->variant, "super")) piece = AMAZON; else
346       if(strstr(gs->variant, "xiangqi")) piece = MANDARIN;
347       break;
348     case MARSHALL:
349       if(strstr(gs->variant, "xiangqi")) piece = CANNON;
350       break;
351     case SILVER:
352       if(strstr(gs->variant, "super")) piece = PRINCESS;
353       if(strstr(gs->variant, "great")) piece = MAN2;
354       break;
355     case BISHOP:
356       if(strstr(gs->variant, "shatranj")) piece = ALFIL;
357       break;
358     case QUEEN:
359       if(strstr(gs->variant, "shatranj")) piece = FERZ;
360       break;
361     case WAZIR:
362       if(strstr(gs->variant, "super")) piece = WOODY;
363       break;
364     case KNIGHT:
365       if(strstr(gs->variant, "shogi")) piece = HONORABLEHORSE;
366       break;
367     case MAN:
368       if(strstr(gs->variant, "great")) piece = MINISTER;
369       break;
370     case HORSE:
371       if(strstr(gs->variant, "great")) piece = PRIESTESS;
372       if(strstr(gs->variant, "shogi")) piece = DRAGONHORSE;
373       break;
374     case GOLD:
375       if(strstr(gs->variant, "great")) piece = MASTODON;
376       break;
377   }
378
379   /* Resolve ambiguities in to-ness */
380   if (tf == ALG_UNKNOWN) {
381           d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
382           return MOVE_AMBIGUOUS;        /* Must always know to file */
383   }
384   if (tr == ALG_UNKNOWN) {
385     posr = posr2 = ALG_UNKNOWN;
386     if (piece != PAWN) {
387             d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
388             return MOVE_AMBIGUOUS;
389     }
390     if (ff == ALG_UNKNOWN) {
391             d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
392             return MOVE_AMBIGUOUS;
393     }
394     /* Need to find pawn on ff that can take to tf and fill in ranks */
395     for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
396          NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
397       if ((ff != ALG_UNKNOWN) && (ff != f))
398         continue;
399       if (piecetype(gs->board[f][r]) != piece)
400         continue;
401       if (gs->onMove == WHITE) {
402         tmpr = r + 1;
403       } else {
404         tmpr = r - 1;
405       }
406 /*      if ((gs->board[tf][tmpr] == NOPIECE) ||
407           (iscolor(gs->board[tf][tmpr], gs->onMove))) continue;*/
408 /* patch from Soso, added by Sparky 3/16/95                    */
409       if (gs->board[tf][tmpr] == NOPIECE) {
410         if ((gs->ep_possible[((gs->onMove == WHITE) ? 0 : 1)][ff]) != (tf - ff))
411           continue;
412       } else {
413         if (iscolor(gs->board[tf][tmpr], gs->onMove))
414           continue;
415       }
416
417       if (legal_andcheck_move(gs, f, r, tf, tmpr)) {
418               if ((posr != ALG_UNKNOWN) && (posr2 != ALG_UNKNOWN)) {
419                       d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
420                       return MOVE_AMBIGUOUS;
421               }
422         posr = tmpr;
423         posr2 = r;
424       }
425     }
426     tr = posr;
427     fr = posr2;
428   } else if (bc) {              /* Could be bxc4 or Bxc4, tr is known */
429     ff = ALG_UNKNOWN;
430     fr = ALG_UNKNOWN;
431     for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
432          NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
433             if ((piecetype(gs->board[f][r]) != PAWN) && (piecetype(gs->board[f][r]) != piece)) {
434                     // note that the interpretation Bxc4 is matched last, and has set piece to BISHOP
435                     continue;
436             }
437             if (legal_andcheck_move(gs, f, r, tf, tr)) {
438                     if ((piecetype(gs->board[f][r]) == PAWN) && (f != tolower(mstr[0]) - 'a')) {
439                             continue;
440                     }
441
442                     /* if its a lowercase 'b' then prefer the pawn move if there is one */
443                     if ((ff != ALG_UNKNOWN) && (fr != ALG_UNKNOWN) &&
444                         piecetype(gs->board[f][r]) == PAWN && mstr[0] >= 'a') {
445                             ff = f;
446                             fr = r;
447                             continue;
448                     }
449
450                     if ((ff != ALG_UNKNOWN) && (fr != ALG_UNKNOWN) &&
451                         piecetype(gs->board[ff][fr]) == PAWN && mstr[0] >= 'a') {
452                             continue;
453                     }
454
455                     if ((ff != ALG_UNKNOWN) && (fr != ALG_UNKNOWN)) {
456                             d_printf("Ambiguous %s(%d) mstr=%s\n", __FUNCTION__, __LINE__, mstr);
457                             return (MOVE_AMBIGUOUS);
458                     }
459                     ff = f;
460                     fr = r;
461             }
462     }
463   } else {                      /* The from position is unknown */
464     posf = ALG_UNKNOWN;
465     posr = ALG_UNKNOWN;
466     if ((ff == ALG_UNKNOWN) || (fr == ALG_UNKNOWN)) {
467       /* Need to find a piece that can go to tf, tr */
468       for (InitPieceLoop(gs->board, &f, &r, gs->onMove);
469            NextPieceLoop(gs->board, &f, &r, gs->onMove, gs->files, gs->ranks);) {
470         if ((ff != ALG_UNKNOWN) && (ff != f))
471           continue;
472         if ((fr != ALG_UNKNOWN) && (fr != r))
473           continue;
474         if (piecetype(gs->board[f][r]) != piece)
475           continue;
476         if (legal_andcheck_move(gs, f, r, tf, tr)) {
477                 if ((posf != ALG_UNKNOWN) && (posr != ALG_UNKNOWN)) {
478                         d_printf("Ambiguous %s(%d)\n", __FUNCTION__, __LINE__);
479                         return MOVE_AMBIGUOUS;
480                 }
481           posf = f;
482           posr = r;
483         }
484       }
485     } else if (ff == ALG_DROP) {
486       if (legal_andcheck_move(gs, ALG_DROP, piece, tf, tr)) {
487         posf = ALG_DROP;
488         posr = piece;
489       }
490     }
491     ff = posf;
492     fr = posr;
493   }
494   if ((tf == ALG_UNKNOWN) || (tr == ALG_UNKNOWN) ||
495       (ff == ALG_UNKNOWN) || (fr == ALG_UNKNOWN))
496     return MOVE_ILLEGAL;
497   mt->fromFile = ff;
498   mt->fromRank = fr;
499   mt->toFile = tf;
500   mt->toRank = tr;
501
502   add_promotion(gs, mstr, mt);
503
504   return MOVE_OK;
505 }
506
507 /* A assumes the move has yet to be made on the board */
508
509 /* Soso: rewrote alg_unparse function.
510  * Algebraic deparser - sets the mStr variable with move description
511  * in short notation. Used in last move report and in 'moves' command.
512  */
513
514 char *alg_unparse(struct game_state_t * gs, struct move_t * mt)
515 {
516   static char mStr[20];
517   char tmp[20];
518   int piece, f, r;
519   int ambig, r_ambig, f_ambig;
520   struct game_state_t fakeMove;
521
522   if (mt->fromFile == ALG_DROP) {
523     piece = mt->fromRank;
524   } else {
525     piece = piecetype(gs->board[mt->fromFile][mt->fromRank]);
526   }
527
528   if ((mt->fromFile == ALG_CASTLE) && (mt->toFile > mt->toRank)) { // [HGM] castle: K ends right of R
529     strcpy(mStr, "O-O");
530     goto check;
531   }
532   if ((mt->fromFile == ALG_CASTLE) && (mt->toFile < mt->toRank)) { // [HGM] castle: K ends left of R
533     strcpy(mStr, "O-O-O");
534     goto check;
535   }
536   strcpy(mStr, "");
537   switch (piece) {
538   case PAWN:
539     if (mt->fromFile == ALG_DROP) {
540       strcpy(mStr,"P");
541     } else if (mt->fromFile != mt->toFile 
542             || gs->board[mt->toFile][mt->toRank] != NOPIECE) { // [HGM] XQ: forward captures as "axa6"
543       sprintf(tmp, "%c", mt->fromFile + 'a');
544       strcpy(mStr, tmp);
545     }
546     break;
547   case KNIGHT:
548     strcpy(mStr, "N");
549     break;
550   case BISHOP:
551     strcpy(mStr, "B");
552     break;
553   case ROOK:
554     strcpy(mStr, "R");
555     break;
556   case ALFIL2:
557   case AMAZON:
558   case CARDINAL:
559     strcpy(mStr, "A");
560     break;
561   case CANNON:
562   case MARSHALL:
563     strcpy(mStr, "C");
564     break;
565   case MAN:
566     strcpy(mStr, "M");
567     break;
568   case FERZ:
569   case QUEEN:
570     strcpy(mStr, "Q");
571     break;
572   case EMPRESS:
573   case ELEPHANT:
574     strcpy(mStr, "E");
575     break;
576   case ALFIL:
577     strcpy(mStr, "B");
578     break;
579   case FERZ2:
580     strcpy(mStr, "F");
581     break;
582   case WOODY:
583   case WAZIR:
584     strcpy(mStr, "W");
585     break;
586   case SQUIRREL:
587     strcpy(mStr, "O");
588     break;
589   case CENTAUR:
590     strcpy(mStr, "V");
591     break;
592   case HORSE:
593     strcpy(mStr, "H");
594     break;
595   case HONORABLEHORSE:
596     strcpy(mStr, "N");
597     break;
598   case DRAGONKING:
599     strcpy(mStr, "D");
600     break;
601   case DRAGONHORSE:
602     strcpy(mStr, "H");
603     break;
604   case LANCE:
605     strcpy(mStr, "L");
606     break;
607   case PRINCESS:
608   case SILVER:
609     strcpy(mStr, "S");
610     break;
611   case MASTODON:
612   case GOLD:
613     strcpy(mStr, "G");
614     break;
615   case MANDARIN:
616     strcpy(mStr, "A");
617     break;
618   case KING:
619     strcpy(mStr, "K");
620     break;
621   default:
622     strcpy(mStr, "");
623     break;
624   }
625
626   if (mt->fromFile == ALG_DROP) {
627     strcat(mStr, "@");
628   } else {
629   /* Checks for ambiguity in short notation ( Ncb3, R8e8 or so) */
630   if (piece != PAWN) {
631     ambig = r_ambig = f_ambig = 0;
632     for (r = 0; r < gs->ranks; r++)
633       for (f = 0; f < gs->files; f++) {
634         if ((gs->board[f][r] != NOPIECE) && iscolor(gs->board[f][r], gs->onMove)
635             && (piecetype(gs->board[f][r]) == piece) &&
636             ((f != mt->fromFile) || (r != mt->fromRank))) {
637           if (legal_move(gs, f, r, mt->toFile, mt->toRank)) {
638             fakeMove = *gs;
639             fakeMove.board[f][r] = NOPIECE;
640             fakeMove.board[mt->toFile][mt->toRank] = piece | gs->onMove;
641             fakeMove.onMove = CToggle(fakeMove.onMove);
642             gs->onMove = CToggle(gs->onMove);
643
644 #if 0
645             d_printf("possible move %c%d%c%d against %c%d%c%d\n",
646                      'a' + f, r+1,
647                      'a' + mt->toFile, mt->toRank+1,
648                      'a' + mt->fromFile, mt->fromRank+1,
649                      'a' + mt->toFile, mt->toRank+1);
650 #endif
651
652             if (!in_check(&fakeMove)) {
653                     ambig++;
654                     if (f == mt->fromFile) {
655                             ambig++;
656                             f_ambig++;
657                     }
658                     if (r == mt->fromRank) {
659                             ambig++;
660                             r_ambig++;
661                     }
662             }
663             gs->onMove = CToggle(gs->onMove);
664           }
665         }
666       }
667     if (ambig > 0) {
668       /* Ambiguity in short notation, need to add file,rank or _both_ in
669          notation */
670       if (f_ambig == 0) {
671         sprintf(tmp, "%c", mt->fromFile + 'a');
672         strcat(mStr, tmp);
673       } else if (r_ambig == 0) {
674         sprintf(tmp, "%d", mt->fromRank + 1 - (gs->ranks > 9));
675         strcat(mStr, tmp);
676       } else {
677         sprintf(tmp, "%c%d", mt->fromFile + 'a', mt->fromRank + 1 - (gs->ranks > 9));
678         strcat(mStr, tmp);
679       }
680     }
681   }
682   if ((gs->board[mt->toFile][mt->toRank] != NOPIECE) ||
683       ((piece == PAWN) && (mt->fromFile != mt->toFile))) {
684     strcat(mStr, "x");
685   }
686   }
687   sprintf(tmp, "%c%d", mt->toFile + 'a', mt->toRank + 1 - (gs->ranks > 9));
688   strcat(mStr, tmp);
689
690   if ((piece == PAWN || gs->promoType == 3) && (mt->piecePromotionTo != NOPIECE)) {
691     strcat(mStr, "=");          /* = before promoting piece */
692     switch (piecetype(mt->piecePromotionTo)) {
693     case KNIGHT:
694       strcat(mStr, "N");
695       break;
696     case BISHOP:
697       strcat(mStr, "B");
698       break;
699     case ROOK:
700       strcat(mStr, "R");
701       break;
702     case CARDINAL:
703       strcat(mStr, "A");
704       break;
705     case MARSHALL:
706       strcat(mStr, "C");
707       break;
708     case MAN:
709       strcat(mStr, "M");
710       break;
711     case QUEEN:
712       strcat(mStr, "Q");
713       break;
714     case FERZ2:
715       strcat(mStr, "F");
716       break;
717     case WOODY:
718       strcat(mStr, "W");
719       break;
720     case SELEPHANT:
721     case EMPRESS:
722       strcat(mStr, "E");
723       break;
724     case CENTAUR:
725       strcat(mStr, "V");
726       break;
727     case PRINCESS:
728       strcat(mStr, "S");
729       break;
730     case SQUIRREL:
731       strcat(mStr, "O");
732       break;
733     case MASTODON:
734       strcat(mStr, "G");
735       break;
736     case GOLD: // [HGM] Shogi promotions: avoid use of '+'
737       strcat(mStr, "G");
738       break;
739     case HAWK:
740     case DRAGONHORSE:
741       strcat(mStr, "H");
742       break;
743     case DRAGONKING:
744       strcat(mStr, "D");
745       break;
746     default:
747       break;
748     }
749   }
750 check:;
751   fakeMove = *gs;
752   execute_move(&fakeMove, mt, 0);
753   fakeMove.onMove = CToggle(fakeMove.onMove);
754   if (in_check(&fakeMove)) {
755     strcat(mStr, "+");
756   }
757   return mStr;
758 }