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