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