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