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