Remove more duplicate variable declarations.
[gnushogi.git] / gnushogi / commondsp.c
1 /*
2  * FILE: commondsp.c
3  *
4  *     Common display routines for GNU Shogi.
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
10  *
11  * GNU SHOGI is based on GNU CHESS
12  *
13  * Copyright (c) 1988, 1989, 1990 John Stanback
14  * Copyright (c) 1992 Free Software Foundation
15  *
16  * This file is part of GNU SHOGI.
17  *
18  * GNU Shogi is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 3 of the License,
21  * or (at your option) any later version.
22  *
23  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with GNU Shogi; see the file COPYING. If not, see
30  * <http://www.gnu.org/licenses/>.
31  * ----------------------------------------------------------------------
32  *
33  */
34
35 /* request *snprintf prototypes */
36 #define _POSIX_C_SOURCE 200112L
37 #include <stdio.h>
38
39 #if defined HAVE_GETTIMEOFDAY
40 #include <sys/time.h>
41 #endif
42
43 #include <ctype.h>
44 #include <signal.h>
45
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/file.h>
49
50 #include "gnushogi.h"
51
52 char mvstr[4][6];
53 char *InPtr;
54 int mycnt1, mycnt2;
55 struct display *dsp = &raw_display;
56
57
58 #if defined(BOOKTEST)
59
60 void
61 movealgbr(short m, char *s)
62 {
63     unsigned int f, t;
64     short piece = 0, flag = 0;
65
66     if (m == 0)
67     {
68         strcpy(s, "none");
69         return;
70     }
71
72     f = (m >> 8) & 0x7f;
73     t = m & 0xff;
74
75     if (f > NO_SQUARES)
76     {
77         piece = f - NO_SQUARES;
78
79         if (piece > NO_PIECES)
80             piece -= NO_PIECES;
81
82         flag = (dropmask | piece);
83     }
84
85     if (t & 0x80)
86     {
87         flag |= promote;
88         t &= 0x7f;
89     }
90
91     if (flag & dropmask)
92     {
93         *s = pxx[piece];
94         s++;
95         *s = '*';
96         s++;
97         *s = cxx[column(t)];
98         s++;
99         *s = rxx[row(t)];
100         s++;
101     }
102     else
103     {
104         *s = cxx[column(f)];
105         s++;
106         *s = rxx[row(f)];
107         s++;
108         *s = cxx[column(t)];
109         s++;
110         *s = rxx[row(t)];
111         s++;
112
113         if (flag & promote)
114         {
115             *s = '+';
116             s++;
117         }
118     }
119
120     if (m & 0x8000)
121     {
122         *s = '?';
123         s++;
124     }
125
126     *s = '\0';
127 }
128
129 #endif /* BOOKTEST */
130
131
132 /*
133  * Generate move strings in different formats.
134  *
135  * INPUT:
136  * - f                                  piece to be moved
137  *   - 0 < f < NO_SQUARES                               source square
138  *   - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES         dropped piece modulo NO_PIECES
139  * - t & 0x7f                           target square
140  * - t & 0x80                           promotion flag
141  * - flag                               FIXME: must be zero ?
142  *
143  * OUTPUT:
144  * - GLOBAL mvstr
145  */
146
147 void
148 algbr(short f, short t, short flag)
149 {
150     if (f > NO_SQUARES)
151     {
152         short piece;
153
154         piece = f - NO_SQUARES;
155
156         if (f > (NO_SQUARES + NO_PIECES))
157             piece -= NO_PIECES;
158
159         flag = (dropmask | piece);
160     }
161
162     if ((t & 0x80) != 0)
163     {
164         flag |= promote;
165         t &= 0x7f;
166     }
167
168     if ((f == t) && ((f != 0) || (t != 0)))
169     {
170         if (!XSHOGI) {
171             dsp->Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
172         }
173
174         mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
175     }
176     else if ((flag & dropmask) != 0)
177     {
178         short piece = flag & pmask;
179
180         mvstr[0][0] = pxx[piece];
181         mvstr[0][1] = '*';
182         mvstr[0][2] = cxx[column(t)];
183         mvstr[0][3] = rxx[row(t)];
184         mvstr[0][4] = '\0';
185         strcpy(mvstr[1], mvstr[0]);
186         strcpy(mvstr[2], mvstr[0]);
187         strcpy(mvstr[3], mvstr[0]);
188     }
189     else if ((f != 0) || (t != 0))
190     {
191         /* algebraic notation */
192         mvstr[0][0] = cxx[column(f)];
193         mvstr[0][1] = rxx[row(f)];
194         mvstr[0][2] = cxx[column(t)];
195         mvstr[0][3] = rxx[row(t)];
196         mvstr[0][4] = mvstr[3][0] = '\0';
197         mvstr[1][0] = pxx[board[f]];
198
199         mvstr[2][0] = mvstr[1][0];
200         mvstr[2][1] = mvstr[0][1];
201
202         mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
203         mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
204         mvstr[2][4] = mvstr[1][3] = '\0';
205         strcpy(mvstr[3], mvstr[2]);
206         mvstr[3][1] = mvstr[0][0];
207
208         if (flag & promote)
209         {
210             strcat(mvstr[0], "+");
211             strcat(mvstr[1], "+");
212             strcat(mvstr[2], "+");
213             strcat(mvstr[3], "+");
214         }
215     }
216     else
217     {
218         mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
219     }
220 }
221
222
223 /*
224  * Compare the string 's' to the list of legal moves available for the
225  * opponent. If a match is found, make the move on the board.
226  */
227
228 int
229 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
230 {
231     static short pnt, tempb, tempc, tempsf, tempst, cnt;
232     static struct leaf xnode;
233     struct leaf  *node;
234     short i, l, local_flags;
235     char buffer[60];
236
237     /* check and remove quality flags */
238     for (i = local_flags = 0, l = strlen(s); i < l; i++)
239     {
240         switch(s[i])
241         {
242         case '?':
243             local_flags |= badmove;
244             s[i] = '\0';
245             break;
246
247         case '!':
248             local_flags |= goodmove;
249             s[i] = '\0';
250             break;
251
252 #ifdef EASY_OPENINGS
253         case '~':
254             local_flags |= difficult;
255             s[i] = '\0';
256             break;
257 #endif
258         }
259     }
260
261     *mv = 0;
262
263     if (iop == UNMAKE_MODE)
264     {
265         UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
266         return false;
267     }
268
269     cnt = 0;
270
271     if (iop == VERIFY_AND_MAKE_MODE)
272         generate_move_flags = true;
273
274     MoveList(opponent, 2, -1, true);
275     generate_move_flags = false;
276     pnt = TrPnt[2];
277
278     while (pnt < TrPnt[3])
279     {
280         node = &Tree[pnt++];
281         algbr(node->f, node->t, (short) node->flags);
282
283         if ((strcmp(s, mvstr[0]) == 0)
284             || (strcmp(s, mvstr[1]) == 0)
285             || (strcmp(s, mvstr[2]) == 0)
286             || (strcmp(s, mvstr[3]) == 0))
287         {
288             cnt++;
289             xnode = *node;
290         }
291     }
292
293     if ((cnt == 1) && (xnode.score > DONTUSE))
294     {
295         short blocked;
296
297         MakeMove(opponent, &xnode, &tempb, &tempc,
298                  &tempsf, &tempst, &INCscore);
299
300         if (SqAttacked(PieceList[opponent][0], computer, &blocked))
301         {
302             UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
303             dsp->AlwaysShowMessage("Illegal move (in check) %s", s);
304             return false;
305         }
306         else
307         {
308             if (iop == VERIFY_AND_TRY_MODE)
309                 return true;
310
311             dsp->UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
312             GameList[GameCnt].depth = GameList[GameCnt].score = 0;
313             GameList[GameCnt].nodes = 0;
314             ElapsedTime(COMPUTE_AND_INIT_MODE);
315             GameList[GameCnt].time = (short) (et + 50)/100;
316             GameList[GameCnt].flags |= local_flags;
317
318             if (TCflag)
319             {
320                 TimeControl.clock[opponent] -= et;
321                 timeopp[oppptr] = et;
322                 --TimeControl.moves[opponent];
323             }
324
325             *mv = (xnode.f << 8) | xnode.t;
326             algbr(xnode.f, xnode.t, false);
327
328             /* in force mode, check for mate conditions */
329             if (flag.force)
330             {
331                 if (IsCheckmate(opponent ^ 1, -1, -1))
332                 {
333                     char buf[20];
334
335                     sprintf(buf, "%s mates!\n", ColorStr[opponent]);
336                     dsp->ShowMessage(buf);
337                     flag.mate = true;
338                 }
339             }
340
341             return true;
342         }
343     }
344
345     dsp->AlwaysShowMessage("Illegal move (no match) %s", s);
346
347     if (!XSHOGI && (cnt > 1))
348     {
349         sprintf(buffer, "Ambiguous Move %s!", s);
350         dsp->ShowMessage(buffer);
351     }
352
353     return false;
354 }
355
356
357 static int
358 parser(char *f, short *fpiece)
359 {
360     int c1, r1, c2, r2;
361     short i, p = false;
362
363     if (*f == '+')
364         f++, p = true;
365
366     for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
367     {
368         if (f[0] == pxx[i] || f[0] == qxx[i])
369         {
370             *fpiece = (p ? promoted[i] : unpromoted[i]);
371             break;
372         }
373     }
374
375     if (f[1] == '*' || f[1] == '\'')
376     {
377         c2 = COL_NAME(f[2]);
378         r2 = ROW_NAME(f[3]);
379
380         return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
381     }
382     else
383     {
384         c1 = COL_NAME(f[1]);
385         r1 = ROW_NAME(f[2]);
386         c2 = COL_NAME(f[3]);
387         r2 = ROW_NAME(f[4]);
388         p = (f[5] == '+') ? 0x80 : 0;
389
390         return (locn(r1, c1) << 8) | locn(r2, c2) | p;
391     }
392 }
393
394
395 void
396 skip()
397 {
398     while (*InPtr != ' ')
399         InPtr++;
400
401     while (*InPtr == ' ')
402         InPtr++;
403 }
404
405
406 void
407 skipb()
408 {
409     while (*InPtr == ' ')
410         InPtr++;
411 }
412
413
414 void RequestInputString(char* buffer, unsigned bufsize)
415 {
416     static char fmt[10];
417     int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
418     if (ret < 0 ) {
419         perror("RequestInputString snprintf");
420         exit(1);
421     }
422     if (ret >= sizeof(fmt)) {
423         fprintf(stderr,
424                 "Insufficient format-buffer size in %s for bufsize=%u\n",
425                 __FUNCTION__, bufsize);
426         exit(1);
427     }
428     dsp->doRequestInputString(fmt, buffer);
429 }
430
431
432 static void
433 GetGame(void)
434 {
435     FILE *fd;
436     char fname[256], *p;
437     int c, i, j;
438     short sq;
439     short side, isp;
440
441     if (savefile[0]) {
442         strcpy(fname, savefile);
443     } else {
444         dsp->ShowMessage("Enter file name: ");
445         RequestInputString(fname, sizeof(fname)-1);
446     }
447
448     if (fname[0] == '\0')
449         strcpy(fname, "shogi.000");
450
451     if ((fd = fopen(fname, "r")) != NULL)
452     {
453         NewGame();
454         fgets(fname, 256, fd);
455         computer = opponent = black;
456         InPtr = fname;
457         skip();
458
459         if (*InPtr == 'c')
460             computer = white;
461         else
462             opponent = white;
463
464         /* FIXME: write a skipn() function so that we can get
465          * 3 skips by doing skipn(3) */
466         skip();
467         skip();
468         skip();
469         Game50 = atoi(InPtr);
470         skip();
471         flag.force = (*InPtr == 'f');
472         fgets(fname, 256, fd); /* empty */
473         fgets(fname, 256, fd);
474         InPtr = &fname[11];
475         skipb();
476         TCflag = atoi(InPtr);
477         skip();
478         InPtr += 14;
479         skipb();
480         OperatorTime = atoi(InPtr);
481         fgets(fname, 256, fd);
482         InPtr = &fname[11];
483         skipb();
484         TimeControl.clock[black] = atol(InPtr);
485         skip();
486         skip();
487         TimeControl.moves[black] = atoi(InPtr);
488         fgets(fname, 256, fd);
489         InPtr = &fname[11];
490         skipb();
491         TimeControl.clock[white] = atol(InPtr);
492         skip();
493         skip();
494         TimeControl.moves[white] = atoi(InPtr);
495         fgets(fname, 256, fd); /* empty */
496
497         for (i = NO_ROWS - 1; i > -1; i--)
498         {
499             fgets(fname, 256, fd);
500             p = &fname[2];
501             InPtr = &fname[23];
502
503             for (j = 0; j < NO_COLS; j++)
504             {
505                 sq = i * NO_COLS + j;
506                 isp = (*p == '+');
507                 p++;
508
509                 if (*p == '-')
510                 {
511                     board[sq] = no_piece;
512                     color[sq] = neutral;
513                 }
514                 else
515                 {
516                     for (c = 0; c < NO_PIECES; c++)
517                     {
518                         if (*p == pxx[c])
519                         {
520                             if (isp)
521                                 board[sq] = promoted[c];
522                             else
523                                 board[sq] = unpromoted[c];
524
525                             color[sq] = white;
526                         }
527                     }
528
529                     for (c = 0; c < NO_PIECES; c++)
530                     {
531                         if (*p == qxx[c])
532                         {
533                             if (isp)
534                                 board[sq] = promoted[c];
535                             else
536                                 board[sq] = unpromoted[c];
537
538                             color[sq] = black;
539                         }
540                     }
541                 }
542
543                 p++;
544                 Mvboard[sq] = atoi(InPtr);
545                 skip();
546             }
547         }
548
549         fgets(fname, 256, fd);  /* empty */
550         fgets(fname, 256, fd);  /* 9 8 7 ... */
551         fgets(fname, 256, fd);  /* empty */
552         fgets(fname, 256, fd);  /* p l n ... */
553         ClearCaptured();
554
555         for (side = 0; side <= 1; side++)
556         {
557             fgets(fname, 256, fd);
558             InPtr = fname;
559             skip();
560             skipb();
561             Captured[side][pawn] = atoi(InPtr);
562             skip();
563 #ifndef MINISHOGI
564             Captured[side][lance] = atoi(InPtr);
565             skip();
566             Captured[side][knight] = atoi(InPtr);
567             skip();
568 #endif
569             Captured[side][silver] = atoi(InPtr);
570             skip();
571             Captured[side][gold] = atoi(InPtr);
572             skip();
573             Captured[side][bishop] = atoi(InPtr);
574             skip();
575             Captured[side][rook] = atoi(InPtr);
576             skip();
577             Captured[side][king] = atoi(InPtr);
578         }
579
580         GameCnt = 0;
581         flag.regularstart = true;
582         Book = BOOKFAIL;
583         fgets(fname, 256, fd); /* empty */
584         fgets(fname, 256, fd);   /*  move score ... */
585
586         while (fgets(fname, 256, fd))
587         {
588             struct GameRec  *g;
589             int side = computer;
590
591             side = side ^ 1;
592             ++GameCnt;
593             InPtr = fname;
594             skipb();
595             g = &GameList[GameCnt];
596             g->gmove = parser(InPtr, &g->fpiece);
597             skip();
598             g->score = atoi(InPtr);
599             skip();
600             g->depth = atoi(InPtr);
601             skip();
602             g->nodes = atol(InPtr);
603             skip();
604             g->time = atol(InPtr);
605             skip();
606             g->flags = c = atoi(InPtr);
607             skip();
608             g->hashkey = strtol(InPtr, (char **) NULL, 16);
609             skip();
610             g->hashbd = strtol(InPtr, (char **) NULL, 16);
611
612             if (c & capture)
613             {
614                 short i, piece;
615
616                 skip();
617
618                 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
619                 {
620                     if (pxx[i] == *InPtr)
621                     {
622                         piece = i;
623                         break;
624                     }
625                 }
626
627                 skip();
628                 g->color = ((*InPtr == 'W') ? white : black);
629                 skip();
630                 g->piece = (*InPtr == '+'
631                             ? promoted[piece]
632                             : unpromoted[piece]);
633             }
634             else
635             {
636                 g->color = neutral;
637                 g->piece = no_piece;
638             }
639         }
640
641         if (TimeControl.clock[black] > 0)
642             TCflag = true;
643
644         fclose(fd);
645     }
646
647     ZeroRPT();
648     InitializeStats();
649     dsp->UpdateDisplay(0, 0, 1, 0);
650     Sdepth = 0;
651     hint = 0;
652 }
653
654
655 static void
656 SaveGame(void)
657 {
658     FILE *fd;
659     char fname[256];
660     short sq, i, c, f, t;
661     char p;
662     short side, piece;
663     char empty[2] = "\n";
664
665     if (savefile[0]) {
666         strcpy(fname, savefile);
667     } else {
668         dsp->ShowMessage("Enter file name: ");
669         RequestInputString(fname, sizeof(fname)-1);
670     }
671
672     if (fname[0] == '\0')
673         strcpy(fname, "shogi.000");
674
675     if ((fd = fopen(fname, "w")) != NULL)
676     {
677         char *b, *w;
678         b = w = "Human   ";
679
680         if (computer == white)
681             w = "computer";
682
683         if (computer == black)
684             b = "computer";
685
686         fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
687                 flag.force ? "force" : "");
688         fputs(empty, fd);
689         fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
690         fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
691                 TimeControl.clock[black], TimeControl.moves[black],
692                 TimeControl.clock[white], TimeControl.moves[white]);
693         fputs(empty, fd);
694
695         for (i = NO_ROWS - 1; i > -1; i--)
696         {
697             fprintf(fd, "%c ", ROW_NAME(i));
698
699             for (c = 0; c < NO_COLS; c++)
700             {
701                 sq = i * NO_COLS + c;
702                 piece = board[sq];
703                 p = is_promoted[piece] ? '+' : ' ';
704                 fprintf(fd, "%c", p);
705
706                 switch(color[sq])
707                 {
708                 case white:
709                     p = pxx[piece];
710                     break;
711
712                 case black:
713                     p = qxx[piece];
714                     break;
715
716                 default:
717                     p = '-';
718                 }
719
720                 fprintf(fd, "%c", p);
721             }
722
723             fprintf(fd, "  ");
724
725             for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
726                 fprintf(fd, " %d", Mvboard[f]);
727
728             fprintf(fd, "\n");
729         }
730
731         fputs(empty, fd);
732 #ifndef MINISHOGI
733         fprintf(fd, "   9 8 7 6 5 4 3 2 1\n");
734         fputs(empty, fd);
735         fprintf(fd, "   p  l  n  s  g  b  r  k\n");
736 #else
737         fprintf(fd, "   5 4 3 2 1\n");
738         fputs(empty, fd);
739         fprintf(fd, "   p  s  g  b  r  k\n");
740 #endif
741
742         for (side = 0; side <= 1; side++)
743         {
744             fprintf(fd, "%c", (side == black) ? 'B' : 'W');
745             fprintf(fd, " %2d", Captured[side][pawn]);
746 #ifndef MINISHOGI
747             fprintf(fd, " %2d", Captured[side][lance]);
748             fprintf(fd, " %2d", Captured[side][knight]);
749 #endif
750             fprintf(fd, " %2d", Captured[side][silver]);
751             fprintf(fd, " %2d", Captured[side][gold]);
752             fprintf(fd, " %2d", Captured[side][bishop]);
753             fprintf(fd, " %2d", Captured[side][rook]);
754             fprintf(fd, " %2d", Captured[side][king]);
755             fprintf(fd, "\n");
756         }
757
758         fputs(empty, fd);
759         fputs("  move   score depth   nodes   time flags                         capture\n", fd);
760
761         for (i = 1; i <= GameCnt; i++)
762         {
763             struct GameRec  *g = &GameList[i];
764
765             f = g->gmove >> 8;
766             t = (g->gmove & 0xFF);
767             algbr(f, t, g->flags);
768
769             fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d  0x%08lx 0x%08lx",
770                     ((f > NO_SQUARES)
771                      ? ' '
772                      : (is_promoted[g->fpiece] ? '+' : ' ')),
773                     pxx[g->fpiece],
774                     ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
775                     g->score, g->depth,
776                     g->nodes, g->time, g->flags,
777                     g->hashkey, g->hashbd);
778
779             if (g->piece != no_piece)
780             {
781                 fprintf(fd, "  %c %s %c\n",
782                         pxx[g->piece], ColorStr[g->color],
783                         (is_promoted[g->piece] ? '+' : ' '));
784             }
785             else
786             {
787                 fprintf(fd, "\n");
788             }
789         }
790
791         fclose(fd);
792
793         dsp->ShowMessage("Game saved");
794     }
795     else
796     {
797         dsp->ShowMessage("Could not open file");
798     }
799 }
800
801
802 /*
803  * GetXGame, SaveXGame and BookGame used to only be defined if
804  * xshogi wasn't defined -- wonder why?
805  */
806
807 static void
808 GetXGame(void)
809 {
810     FILE *fd;
811     char fname[256], *p;
812     int c, i, j;
813     short sq;
814     short side, isp;
815
816     dsp->ShowMessage("Enter file name: ");
817     RequestInputString(fname, sizeof(fname)-1);
818
819     if (fname[0] == '\0')
820         strcpy(fname, "xshogi.position.read");
821
822     if ((fd = fopen(fname, "r")) != NULL)
823     {
824         NewGame();
825         flag.regularstart = false;
826         Book = false;
827
828         /* xshogi position file ... */
829         fgets(fname, 256, fd);
830
831 #ifdef notdef
832         fname[6] = '\0';
833
834         if (strcmp(fname, "xshogi"))
835             return;
836 #endif
837
838         /* -- empty line -- */
839         fgets(fname, 256, fd);
840         /* -- empty line -- */
841         fgets(fname, 256, fd);
842
843         for (i = NO_ROWS - 1; i > -1; i--)
844         {
845             fgets(fname, 256, fd);
846             p = fname;
847
848             for (j = 0; j < NO_COLS; j++)
849             {
850                 sq = i * NO_COLS + j;
851                 isp = (*p == '+');
852                 p++;
853
854                 if (*p == '.')
855                 {
856                     board[sq] = no_piece;
857                     color[sq] = neutral;
858                 }
859                 else
860                 {
861                     for (c = 0; c < NO_PIECES; c++)
862                     {
863                         if (*p == qxx[c])
864                         {
865                             if (isp)
866                                 board[sq] = promoted[c];
867                             else
868                                 board[sq] = unpromoted[c];
869
870                             color[sq] = white;
871                         }
872                     }
873
874                     for (c = 0; c < NO_PIECES; c++)
875                     {
876                         if (*p == pxx[c])
877                         {
878                             if (isp)
879                                 board[sq] = promoted[c];
880                             else
881                                 board[sq] = unpromoted[c];
882
883                             color[sq] = black;
884                         }
885                     }
886                 }
887
888                 p++;
889             }
890         }
891
892         ClearCaptured();
893
894         for (side = 0; side <= 1; side++)
895         {
896             fgets(fname, 256, fd);
897             InPtr = fname;
898             Captured[side][pawn]   = atoi(InPtr);
899             skip();
900 #ifndef MINISHOGI
901             Captured[side][lance]  = atoi(InPtr);
902             skip();
903             Captured[side][knight] = atoi(InPtr);
904             skip();
905 #endif
906             Captured[side][silver] = atoi(InPtr);
907             skip();
908             Captured[side][gold]   = atoi(InPtr);
909             skip();
910             Captured[side][bishop] = atoi(InPtr);
911             skip();
912             Captured[side][rook]   = atoi(InPtr);
913             skip();
914             Captured[side][king]   = atoi(InPtr);
915         }
916
917         if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
918         {
919             computer = black;
920             opponent = white;
921             xwndw = BXWNDW;
922         }
923
924         fclose(fd);
925     }
926
927     Game50 = 1;
928     ZeroRPT();
929     InitializeStats();
930     dsp->UpdateDisplay(0, 0, 1, 0);
931     Sdepth = 0;
932     hint = 0;
933 }
934
935
936 static void
937 SaveXGame(void)
938 {
939     FILE *fd;
940     char fname[256], *p;
941     int i, j;
942     short sq, piece;
943     short side, isp;
944
945     dsp->ShowMessage("Enter file name: ");
946     RequestInputString(fname, sizeof(fname)-1);
947
948     if (fname[0] == '\0')
949         strcpy(fname, "xshogi.position.read");
950
951     if ((fd = fopen(fname, "w")) != NULL)
952     {
953         fputs("# xshogi position file -- \n", fd);
954         fputs("\n", fd);
955         fputs("\n", fd);
956
957         for (i = NO_ROWS - 1; i > -1; i--)
958         {
959             p = fname;
960
961             for (j = 0; j < NO_COLS; j++)
962             {
963                 sq = i * NO_COLS + j;
964                 piece = board[sq];
965                 isp = is_promoted[piece];
966                 *p = (isp ? '+' : ' ');
967                 p++;
968
969                 if (piece == no_piece)
970                     *p = '.';
971                 else if (color[sq] == white)
972                     *p = qxx[piece];
973                 else
974                     *p = pxx[piece];
975
976                 p++;
977             }
978
979             *p++ = '\n';
980             *p++ = '\0';
981             fputs(fname, fd);
982         }
983
984         for (side = 0; side <= 1; side++)
985         {
986             sprintf(fname,
987 #ifndef MINISHOGI
988                     "%d %d %d %d %d %d %d %d\n",
989 #else
990                     "%d %d %d %d %d %d\n",
991 #endif
992                     Captured[side][pawn],
993 #ifndef MINISHOGI
994                     Captured[side][lance],
995                     Captured[side][knight],
996 #endif
997                     Captured[side][silver],
998                     Captured[side][gold],
999                     Captured[side][bishop],
1000                     Captured[side][rook],
1001                     Captured[side][king]);
1002
1003             fputs(fname, fd);
1004         }
1005
1006         if (computer == black)
1007             fputs("white to play\n", fd);
1008         else
1009             fputs("black to play\n", fd);
1010
1011         fclose(fd);
1012     }
1013 }
1014
1015
1016 static void
1017 BookSave(void)
1018 {
1019     FILE *fd;
1020     char fname[256], sflags[4];
1021     short i, j, f, t;
1022
1023     if (savefile[0]) {
1024         strcpy(fname, savefile);
1025     } else {
1026         /* Enter file name */
1027         dsp->ShowMessage("Enter file name: ");
1028         RequestInputString(fname, sizeof(fname)-1);
1029     }
1030
1031     if (fname[0] == '\0')
1032         return;
1033
1034     if ((fd = fopen(fname, "a")) != NULL)
1035     {
1036         fprintf(fd, "#\n");
1037
1038         for (i = 1; i <= GameCnt; i++)
1039         {
1040             struct GameRec  *g = &GameList[i];
1041             char mvnr[20], mvs[20];
1042
1043             if (i % 2)
1044                 sprintf(mvnr, "%d.", (i + 1)/2);
1045             else
1046                 strcpy(mvnr, "");
1047
1048             f = g->gmove >> 8;
1049             t = (g->gmove & 0xFF);
1050             algbr(f, t, g->flags);
1051             j = 0;
1052
1053             /* determine move quality string */
1054             if (g->flags & goodmove)
1055                 sflags[j++] = '!';
1056
1057             if (g->flags & badmove)
1058                 sflags[j++] = '?';
1059
1060 #ifdef EASY_OPENINGS
1061             if (g->flags & difficult)
1062                 sflags[j++] = '~';
1063 #endif
1064
1065             sflags[j] = '\0';
1066
1067             /* determine move string */
1068             if (f > NO_SQUARES)
1069             {
1070                 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1071             }
1072             else
1073             {
1074                 sprintf(mvs, "%c%c%c%c%c%s%s ",
1075                         mvstr[0][0], mvstr[0][1],
1076                         (g->flags & capture) ? 'x' : '-',
1077                         mvstr[0][2], mvstr[0][3],
1078                         (mvstr[0][4] == '+') ? "+" : "",
1079                         sflags);
1080             }
1081
1082             fprintf(fd, "%s%s%c%s",
1083                     mvnr,
1084                     (f > NO_SQUARES
1085                      ? ""
1086                      : (is_promoted[g->fpiece] ? "+" : "")),
1087                     pxx[g->fpiece],
1088                     mvs);
1089
1090             if ((i % 10) == 0)
1091                 fprintf(fd, "\n");
1092         }
1093
1094         if ((i % 10) != 1)
1095             fprintf(fd, "\n");
1096
1097         fclose(fd);
1098
1099         dsp->ShowMessage("Game saved");
1100     }
1101     else
1102     {
1103         dsp->ShowMessage("Could not open file");
1104     }
1105 }
1106
1107
1108 void
1109 ListGame(void)
1110 {
1111     FILE *fd;
1112     short i, f, t;
1113     time_t when;
1114     char fname[256], dbuf[256];
1115
1116     if (listfile[0])
1117     {
1118         strcpy(fname, listfile);
1119     }
1120     else
1121     {
1122         time(&when);
1123         strncpy(dbuf, ctime(&when), 20);
1124         dbuf[7]  = '\0';
1125         dbuf[10] = '\0';
1126         dbuf[13] = '\0';
1127         dbuf[16] = '\0';
1128         dbuf[19] = '\0';
1129
1130         /* use format "CL.Jan01-020304B" when
1131            date is Jan 1
1132            time is 02:03:04
1133            program played white */
1134
1135         sprintf(fname, "CL.%s%s-%s%s%s%c",
1136                 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1137                 dbuf + 17, ColorStr[computer][0]);
1138
1139         /* replace space padding with 0 */
1140         for (i = 0; fname[i] != '\0'; i++)
1141         {
1142             if (fname[i] == ' ')
1143                 fname[i] = '0';
1144         }
1145     }
1146
1147     fd = fopen(fname, "w");
1148
1149     if (!fd)
1150     {
1151         printf("Open failure for file: %s", fname);
1152         exit(1);
1153     }
1154
1155     fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1156     fputs("         score  depth   nodes  time         ", fd);
1157     fputs("         score  depth   nodes  time\n", fd);
1158
1159     for (i = 1; i <= GameCnt; i++)
1160     {
1161         f = GameList[i].gmove >> 8;
1162         t = (GameList[i].gmove & 0xFF);
1163         algbr(f, t, GameList[i].flags);
1164
1165         if (GameList[i].flags & book)
1166         {
1167             fprintf(fd, "%c%c%-5s  %5d    Book%7ld %5ld",
1168                     ((f > NO_SQUARES)
1169                      ? ' '
1170                      : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1171                     pxx[GameList[i].fpiece],
1172                     ((f > NO_SQUARES)
1173                      ? &mvstr[0][1] : mvstr[0]),
1174                     GameList[i].score,
1175                     GameList[i].nodes,
1176                     GameList[i].time);
1177         }
1178         else
1179         {
1180             fprintf(fd, "%c%c%-5s  %5d     %2d %7ld %5ld",
1181                     (f > NO_SQUARES
1182                      ? ' '
1183                      : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1184                     pxx[GameList[i].fpiece],
1185                     (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1186                     GameList[i].score, GameList[i].depth,
1187                     GameList[i].nodes, GameList[i].time);
1188         }
1189
1190         if ((i % 2) == 0)
1191         {
1192             fprintf(fd, "\n");
1193         }
1194         else
1195         {
1196             fprintf(fd, "         ");
1197         }
1198     }
1199
1200     fprintf(fd, "\n\n");
1201
1202     if (GameList[GameCnt].flags & draw)
1203     {
1204         fprintf(fd, "Draw %s\n", DRAW);
1205
1206         if (DRAW == DRAW_REPETITION)
1207         {
1208             short j;
1209
1210             fprintf(fd, "repetition by positions ");
1211
1212             for (j = GameCnt - 1; j >= Game50; j -= 2)
1213             {
1214                 if (GameList[j].hashkey == hashkey &&
1215                     GameList[j].hashbd == hashbd)
1216                     fprintf(fd, "%d ", j);
1217             }
1218
1219             fprintf(fd, "\n");
1220         }
1221     }
1222     else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1223     {
1224         fprintf(fd, "%s\n", ColorStr[player ]);
1225     }
1226     else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1227     {
1228         fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1229     }
1230
1231     fclose(fd);
1232 }
1233
1234
1235 static void
1236 FlagMove(char c)
1237 {
1238     switch(c)
1239     {
1240     case '?' :
1241         GameList[GameCnt].flags |= badmove;
1242         break;
1243
1244     case '!' :
1245         GameList[GameCnt].flags |= goodmove;
1246         break;
1247
1248 #ifdef EASY_OPENINGS
1249     case '~' :
1250         GameList[GameCnt].flags |= difficult;
1251         break;
1252 #endif
1253     }
1254 }
1255
1256
1257 /*
1258  * Undo the most recent half-move.
1259  */
1260
1261 static void
1262 Undo(void)
1263 {
1264     short f, t;
1265
1266     f = GameList[GameCnt].gmove >> 8;
1267     t = GameList[GameCnt].gmove & 0x7F;
1268
1269     if (f > NO_SQUARES)
1270     {
1271         /* the move was a drop */
1272         Captured[color[t]][board[t]]++;
1273         board[t] = no_piece;
1274         color[t] = neutral;
1275         Mvboard[t]--;
1276     }
1277     else
1278     {
1279         if (GameList[GameCnt].flags & promote)
1280             board[f] = unpromoted[board[t]];
1281         else
1282             board[f] = board[t];
1283
1284         color[f] = color[t];
1285         board[t] = GameList[GameCnt].piece;
1286         color[t] = GameList[GameCnt].color;
1287
1288         if (board[t] != no_piece)
1289             Captured[color[f]][unpromoted[board[t]]]--;
1290
1291         if (color[t] != neutral)
1292             Mvboard[t]--;
1293
1294         Mvboard[f]--;
1295     }
1296
1297     InitializeStats();
1298
1299     if (TCflag && (TCmoves > 1))
1300         ++TimeControl.moves[color[f]];
1301
1302     hashkey = GameList[GameCnt].hashkey;
1303     hashbd = GameList[GameCnt].hashbd;
1304     GameCnt--;
1305     computer = computer ^ 1;
1306     opponent = opponent ^ 1;
1307     flag.mate = false;
1308     Sdepth = 0;
1309     player = player ^ 1;
1310     dsp->ShowSidetoMove();
1311     dsp->UpdateDisplay(0, 0, 1, 0);
1312
1313     if (flag.regularstart)
1314         Book = false;
1315 }
1316
1317
1318 static void
1319 TestSpeed(void(*f)(short side, short ply,
1320                    short in_check, short blockable),
1321           unsigned j)
1322 {
1323 #ifdef test
1324     unsigned jj;
1325 #endif
1326
1327     unsigned i;
1328     long cnt, t1, t2;
1329
1330 #ifdef HAVE_GETTIMEOFDAY
1331     struct timeval tv;
1332 #endif
1333
1334 #ifdef HAVE_GETTIMEOFDAY
1335     gettimeofday(&tv, NULL);
1336     t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1337 #else
1338     t1 = time(0);
1339 #endif
1340
1341     for (i = 0; i < j; i++)
1342     {
1343         f(opponent, 2, -1, true);
1344
1345 #ifdef test
1346         for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1347         {
1348             if (!pick(jj, TrPnt[3] - 1))
1349                 break;
1350         }
1351 #endif
1352     }
1353
1354 #ifdef HAVE_GETTIMEOFDAY
1355     gettimeofday(&tv, NULL);
1356     t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1357 #else
1358     t2 = time(0);
1359 #endif
1360
1361     cnt = j * (TrPnt[3] - TrPnt[2]);
1362
1363     if (t2 - t1)
1364         et = (t2 - t1);
1365     else
1366         et = 1;
1367
1368     dsp->ShowNodeCnt(cnt);
1369 }
1370
1371
1372 static void
1373 TestPSpeed(short(*f) (short side), unsigned j)
1374 {
1375     unsigned i;
1376     long cnt, t1, t2;
1377 #ifdef HAVE_GETTIMEOFDAY
1378     struct timeval tv;
1379 #endif
1380
1381 #ifdef HAVE_GETTIMEOFDAY
1382     gettimeofday(&tv, NULL);
1383     t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1384 #else
1385     t1 = time(0);
1386 #endif
1387
1388     for (i = 0; i < j; i++)
1389         (void) f(opponent);
1390
1391 #ifdef HAVE_GETTIMEOFDAY
1392     gettimeofday(&tv, NULL);
1393     t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1394 #else
1395     t2 = time(0);
1396 #endif
1397
1398     cnt = j;
1399
1400     if (t2 - t1)
1401         et = (t2 - t1);
1402     else
1403         et = 1;
1404
1405     dsp->ShowNodeCnt(cnt);
1406 }
1407
1408
1409 static void
1410 SetOppTime(char *time)
1411 {
1412     int m, t, sec;
1413
1414     sec = 0;
1415     t = (int)strtol(time, &time, 10);
1416
1417     if (*time == ':')
1418     {
1419         time++;
1420         /* FIXME: sec is parsed but ignored */
1421         sec = (int)strtol(time, &time, 10);
1422     }
1423
1424     m = (int)strtol(time, &time, 10);
1425
1426     if (t)
1427         TimeControl.clock[opponent] = t;
1428
1429     if (m)
1430         TimeControl.moves[opponent] = m;
1431
1432     ElapsedTime(COMPUTE_AND_INIT_MODE);
1433
1434     if (XSHOGI)
1435     {
1436         /* just to inform xshogi about availability of otime command */
1437         printf("otime %d %d\n", t, m);
1438     }
1439 }
1440
1441
1442 static void
1443 SetMachineTime(char *time)
1444 {
1445     int m, t, sec;
1446
1447     sec = 0;
1448     t = (int)strtol(time, &time, 10);
1449
1450     if (*time == ':')
1451     {
1452         time++;
1453         /* FIXME: sec is parsed but ignored */
1454         sec = (int)strtol(time, &time, 10);
1455     }
1456
1457     m = (int)strtol(time, &time, 10);
1458
1459     if (t)
1460         TimeControl.clock[computer] = t;
1461
1462     if (m)
1463         TimeControl.moves[computer] = m;
1464
1465     ElapsedTime(COMPUTE_AND_INIT_MODE);
1466
1467     if (XSHOGI)
1468     {
1469         /* just to inform xshogi about availability of time command */
1470         printf("time %d %d\n", t, m);
1471     }
1472 }
1473
1474
1475 /* FIXME!  This is truly the function from hell! */
1476
1477 /*
1478  * Process the user's command. If easy mode is OFF (the computer is thinking
1479  * on opponents time) and the program is out of book, then make the 'hint'
1480  * move on the board and call SelectMove() to find a response. The user
1481  * terminates the search by entering a command. If the opponent does not make
1482  * the hint move, then set Sdepth to zero.
1483  */
1484
1485 void
1486 InputCommand(char *command)
1487 {
1488     short have_shown_prompt = false;
1489     short ok, done, is_move = false;
1490     unsigned short mv;
1491     char s[80], sx[80];
1492
1493     ok = flag.quit = done = false;
1494     player = opponent;
1495
1496 #if ttblsz
1497     if (TTadd > ttbllimit)
1498         ZeroTTable();
1499 #endif
1500
1501     if ((hint > 0) && !flag.easy && !flag.force)
1502     {
1503         /*
1504          * A hint move for the player is available.  Compute a move for the
1505          * opponent in background mode assuming that the hint move will be
1506          * selected by the player.
1507          */
1508
1509         ft = time0; /* Save reference time for the player. */
1510         fflush(stdout);
1511         algbr((short) hint >> 8, (short) hint & 0xff, false);
1512         strcpy(s, mvstr[0]);
1513
1514         if (flag.post)
1515             dsp->GiveHint();
1516
1517         /* do the hint move */
1518         if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1519         {
1520             Sdepth = 0;
1521
1522 #ifdef QUIETBACKGROUND
1523             dsp->ShowPrompt();
1524             have_shown_prompt = true;
1525 #endif /* QUIETBACKGROUND */
1526
1527             /* Start computing a move until the search is interrupted. */
1528
1529 #ifdef INTERRUPT_TEST
1530             itime0 = 0;
1531 #endif
1532
1533             /* would love to put null move in here */
1534             /* after we make the hint move make a 2 ply search
1535              * with both plys our moves */
1536             /* think on opponents time */
1537             SelectMove(computer, BACKGROUND_MODE);
1538
1539 #ifdef INTERRUPT_TEST
1540             ElapsedTime(COMPUTE_INTERRUPT_MODE);
1541
1542             if (itime0 == 0)
1543             {
1544                 printf("searching not terminated by interrupt!\n");
1545             }
1546             else
1547             {
1548                 printf("elapsed time from interrupt to "
1549                        "terminating search: %ld\n", it);
1550             }
1551 #endif
1552
1553             /* undo the hint and carry on */
1554             VerifyMove(s, UNMAKE_MODE, &mv);
1555             Sdepth = 0;
1556         }
1557
1558         time0 = ft; /* Restore reference time for the player. */
1559     }
1560
1561     while(!(ok || flag.quit || done))
1562     {
1563         player = opponent;
1564
1565 #ifdef QUIETBACKGROUND
1566         if (!have_shown_prompt)
1567         {
1568 #endif /* QUIETBACKGROUND */
1569
1570             dsp->ShowPrompt();
1571
1572 #ifdef QUIETBACKGROUND
1573         }
1574
1575         have_shown_prompt = false;
1576 #endif /* QUIETBACKGROUND */
1577
1578         if (command == NULL) {
1579             int eof = dsp->GetString(sx);
1580             if (eof)
1581                 dsp->ExitShogi();
1582         } else {
1583             strcpy(sx, command);
1584             done = true;
1585         }
1586
1587         /* extract first word */
1588         if (sscanf(sx, "%s", s) < 1)
1589             continue;
1590
1591         if (strcmp(s, "bd") == 0)   /* bd -- display board */
1592         {
1593             /* FIXME: Hack alert! */
1594             short old_xshogi = XSHOGI;
1595
1596             if (old_xshogi)
1597                 display_type = DISPLAY_RAW;
1598
1599             dsp->ClearScreen();
1600             dsp->UpdateDisplay(0, 0, 1, 0);
1601
1602             if (old_xshogi)
1603                 display_type = DISPLAY_X;
1604         }
1605         else if (strcmp(s, "post") == 0)
1606         {
1607             flag.post = !flag.post;
1608         }
1609         else if (strcmp(s, "alg") == 0)
1610         {
1611             /* noop */ ;
1612         }
1613         else if ((strcmp(s, "quit") == 0)
1614                  || (strcmp(s, "exit") == 0))
1615         {
1616             flag.quit = true;
1617         }
1618         else if ((strcmp(s, "set") == 0)
1619                  || (strcmp(s, "edit") == 0))
1620         {
1621             dsp->EditBoard();
1622         }
1623         else if (strcmp(s, "setup") == 0)
1624         {
1625             dsp->SetupBoard();
1626         }
1627         else if (strcmp(s, "first") == 0)
1628         {
1629             ok = true;
1630         }
1631         else if (strcmp(s, "go") == 0)
1632         {
1633             ok = true;
1634             flag.force = false;
1635
1636             if (computer == black)
1637             {
1638                 computer = white;
1639                 opponent = black;
1640             }
1641             else
1642             {
1643                 computer = black;
1644                 opponent = white;
1645             }
1646         }
1647         else if (strcmp(s, "help") == 0)
1648         {
1649             dsp->help();
1650         }
1651         else if (strcmp(s, "material") == 0)
1652         {
1653             flag.material = !flag.material;
1654         }
1655         else if (strcmp(s, "force") == 0)
1656         {
1657             if (XSHOGI)
1658             {
1659                 flag.force = true;
1660                 flag.bothsides = false;
1661             }
1662             else
1663             {
1664                 flag.force = !flag.force;
1665                 flag.bothsides = false;
1666             }
1667         }
1668         else if (strcmp(s, "book") == 0)
1669         {
1670             Book = Book ? 0 : BOOKFAIL;
1671         }
1672         else if (strcmp(s, "new") == 0)
1673         {
1674             NewGame();
1675             dsp->UpdateDisplay(0, 0, 1, 0);
1676         }
1677         else if (strcmp(s, "list") == 0)
1678         {
1679             ListGame();
1680         }
1681         else if (strcmp(s, "level") == 0)
1682         {
1683             dsp->SelectLevel(sx + strlen("level"));
1684         }
1685         else if (strcmp(s, "clock") == 0)
1686         {
1687             dsp->SelectLevel(sx + strlen("clock"));
1688         }
1689         else if (strcmp(s, "hash") == 0)
1690         {
1691             flag.hash = !flag.hash;
1692         }
1693         else if (strcmp(s, "gamein") == 0)
1694         {
1695             flag.gamein = !flag.gamein;
1696         }
1697         else if (strcmp(s, "beep") == 0)
1698         {
1699             flag.beep = !flag.beep;
1700         }
1701         else if (strcmp(s, "time") == 0)
1702         {
1703             SetMachineTime(sx + strlen("time"));
1704         }
1705         else if (strcmp(s, "otime") == 0)
1706         {
1707             SetOppTime(sx + strlen("otime"));
1708         }
1709         else if (strcmp(s, "Awindow") == 0)
1710         {
1711             dsp->ChangeAlphaWindow();
1712         }
1713         else if (strcmp(s, "Bwindow") == 0)
1714         {
1715             dsp->ChangeBetaWindow();
1716         }
1717         else if (strcmp(s, "rcptr") == 0)
1718         {
1719             flag.rcptr = !flag.rcptr;
1720         }
1721         else if (strcmp(s, "hint") == 0)
1722         {
1723             dsp->GiveHint();
1724         }
1725         else if (strcmp(s, "both") == 0)
1726         {
1727             flag.bothsides = !flag.bothsides;
1728             flag.force = false;
1729             Sdepth = 0;
1730             ElapsedTime(COMPUTE_AND_INIT_MODE);
1731             SelectMove(opponent, FOREGROUND_MODE);
1732             ok = true;
1733         }
1734         else if (strcmp(s, "reverse") == 0)
1735         {
1736             flag.reverse = !flag.reverse;
1737             dsp->ClearScreen();
1738             dsp->UpdateDisplay(0, 0, 1, 0);
1739         }
1740         else if (strcmp(s, "switch") == 0)
1741         {
1742             computer = computer ^ 1;
1743             opponent = opponent ^ 1;
1744             xwndw = (computer == black) ? WXWNDW : BXWNDW;
1745             flag.force = false;
1746             Sdepth = 0;
1747             ok = true;
1748         }
1749         else if (strcmp(s, "black") == 0)
1750         {
1751             computer = white;
1752             opponent = black;
1753             xwndw = WXWNDW;
1754             flag.force = false;
1755             Sdepth = 0;
1756
1757             /*
1758              * ok = true; don't automatically start with black command
1759              */
1760         }
1761         else if (strcmp(s, "white") == 0)
1762         {
1763             computer = black;
1764             opponent = white;
1765             xwndw = BXWNDW;
1766             flag.force = false;
1767             Sdepth = 0;
1768
1769             /*
1770              * ok = true; don't automatically start with white command
1771              */
1772         }
1773         else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1774         {
1775             Undo();
1776         }
1777         else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1778         {
1779             Undo();
1780             Undo();
1781         }
1782         /* CHECKME: are these next three correct? */
1783         else if (!XSHOGI && strcmp(s, "xget") == 0)
1784         {
1785             GetXGame();
1786         }
1787         else if (!XSHOGI && strcmp(s, "xsave") == 0)
1788         {
1789             SaveXGame();
1790         }
1791         else if (!XSHOGI && strcmp(s, "bsave") == 0)
1792         {
1793             BookSave();
1794         }
1795 #ifdef EASY_OPENINGS
1796         else if ((strcmp(s, "?") == 0)
1797                  || (strcmp(s, "!") == 0)
1798                  || (strcmp(s, "~") == 0))
1799 #else
1800         else if ((strcmp(s, "?") == 0)
1801                  || (strcmp(s, "!") == 0))
1802 #endif
1803         {
1804             FlagMove(*s);
1805         }
1806         else if (strcmp(s, "get") == 0)
1807         {
1808             GetGame();
1809         }
1810         else if (strcmp(s, "save") == 0)
1811         {
1812             SaveGame();
1813         }
1814         else if (strcmp(s, "depth") == 0)
1815         {
1816             dsp->ChangeSearchDepth(sx + strlen("depth"));
1817         }
1818         else if (strcmp(s, "sd") == 0)
1819         {
1820             dsp->ChangeSearchDepth(sx + strlen("sd"));
1821         }
1822         else if (strcmp(s, "hashdepth") == 0)
1823         {
1824             dsp->ChangeHashDepth();
1825         }
1826         else if (strcmp(s, "random") == 0)
1827         {
1828             dither = DITHER;
1829         }
1830         else if (strcmp(s, "hard") == 0)
1831         {
1832             flag.easy = false;
1833         }
1834         else if (strcmp(s, "easy") == 0)
1835         {
1836             flag.easy = !flag.easy;
1837         }
1838         else if (strcmp(s, "tsume") == 0)
1839         {
1840             flag.tsume = !flag.tsume;
1841         }
1842         else if (strcmp(s, "contempt") == 0)
1843         {
1844             dsp->SetContempt();
1845         }
1846         else if (strcmp(s, "xwndw") == 0)
1847         {
1848             dsp->ChangeXwindow();
1849         }
1850         else if (strcmp(s, "rv") == 0)
1851         {
1852             flag.rv = !flag.rv;
1853             dsp->UpdateDisplay(0, 0, 1, 0);
1854         }
1855         else if (strcmp(s, "coords") == 0)
1856         {
1857             flag.coords = !flag.coords;
1858             dsp->UpdateDisplay(0, 0, 1, 0);
1859         }
1860         else if (strcmp(s, "stars") == 0)
1861         {
1862             flag.stars = !flag.stars;
1863             dsp->UpdateDisplay(0, 0, 1, 0);
1864         }
1865         else if (!XSHOGI && strcmp(s, "moves") == 0)
1866         {
1867             short temp;
1868
1869 #if MAXDEPTH > 3
1870             if (GameCnt > 0)
1871             {
1872                 extern unsigned short PrVar[MAXDEPTH];
1873
1874                 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1875                     ? PrVar[2] : 0;
1876             }
1877             else
1878 #endif
1879                 SwagHt = 0;
1880
1881             dsp->ShowMessage("Testing MoveList Speed");
1882             temp = generate_move_flags;
1883             generate_move_flags = true;
1884             TestSpeed(MoveList, 1);
1885             generate_move_flags = temp;
1886             dsp->ShowMessage("Testing CaptureList Speed");
1887             TestSpeed(CaptureList, 1);
1888             dsp->ShowMessage("Testing Eval Speed");
1889             ExaminePosition(opponent);
1890             TestPSpeed(ScorePosition, 1);
1891         }
1892         else if (!XSHOGI && strcmp(s, "test") == 0)
1893         {
1894 #ifdef SLOW_CPU
1895             dsp->ShowMessage("Testing MoveList Speed");
1896             TestSpeed(MoveList, 2000);
1897             dsp->ShowMessage("Testing CaptureList Speed");
1898             TestSpeed(CaptureList, 3000);
1899             dsp->ShowMessage("Testing Eval Speed");
1900             ExaminePosition(opponent);
1901             TestPSpeed(ScorePosition, 1500);
1902 #else
1903             dsp->ShowMessage("Testing MoveList Speed");
1904             TestSpeed(MoveList, 20000);
1905             dsp->ShowMessage("Testing CaptureList Speed");
1906             TestSpeed(CaptureList, 30000);
1907             dsp->ShowMessage("Testing Eval Speed");
1908             ExaminePosition(opponent);
1909             TestPSpeed(ScorePosition, 15000);
1910 #endif
1911         }
1912         else if (!XSHOGI && strcmp(s, "p") == 0)
1913         {
1914             dsp->ShowPostnValues();
1915         }
1916         else if (!XSHOGI && strcmp(s, "debug") == 0)
1917         {
1918             dsp->DoDebug();
1919         }
1920         else
1921         {
1922             if (flag.mate)
1923             {
1924                 ok = true;
1925             }
1926             else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
1927             {
1928                 /* check for repetition */
1929                 short rpt = repetition();
1930
1931                 if (rpt >= 3)
1932                 {
1933                     DRAW = DRAW_REPETITION;
1934                     dsp->ShowMessage(DRAW);
1935                     GameList[GameCnt].flags |= draw;
1936
1937                         flag.mate = true;
1938                 }
1939                 else
1940                 {
1941                     is_move = true;
1942                 }
1943             }
1944
1945             Sdepth = 0;
1946         }
1947     }
1948
1949     ElapsedTime(COMPUTE_AND_INIT_MODE);
1950
1951     if (flag.force)
1952     {
1953         computer = opponent;
1954         opponent = computer ^ 1;
1955     }
1956
1957     if (XSHOGI)
1958     {
1959         /* add remaining time in milliseconds for xshogi */
1960         if (is_move)
1961         {
1962             printf("%d. %s %ld\n",
1963                    ++mycnt2, s, TimeControl.clock[player] * 10);
1964         }
1965     }
1966 }