4 * Common display routines for GNU Shogi.
6 * ----------------------------------------------------------------------
7 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
10 * GNU SHOGI is based on GNU CHESS
12 * Copyright (c) 1988, 1989, 1990 John Stanback
13 * Copyright (c) 1992 Free Software Foundation
15 * This file is part of GNU SHOGI.
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 3 of the License,
20 * or (at your option) any later version.
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
27 * You should have received a copy of the GNU General Public License along
28 * with GNU Shogi; see the file COPYING. If not, see
29 * <http://www.gnu.org/licenses/>.
30 * ----------------------------------------------------------------------
34 #if defined HAVE_GETTIMEOFDAY
41 #include <sys/param.h>
42 #include <sys/types.h>
49 int InBackground = false;
55 movealgbr(short m, char *s)
58 short piece = 0, flag = 0;
71 piece = f - NO_SQUARES;
73 if (piece > NO_PIECES)
76 flag = (dropmask | piece);
123 #endif /* BOOKTEST */
129 * Generate move strings in different formats.
132 * - f piece to be moved
133 * - 0 < f < NO_SQUARES source square
134 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
135 * - t & 0x7f target square
136 * - t & 0x80 promotion flag
137 * - flag FIXME: must be zero ?
144 algbr(short f, short t, short flag)
150 piece = f - NO_SQUARES;
152 if (f > (NO_SQUARES + NO_PIECES))
155 flag = (dropmask | piece);
164 if ((f == t) && ((f != 0) || (t != 0)))
167 Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
170 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
172 else if ((flag & dropmask) != 0)
174 short piece = flag & pmask;
176 mvstr[0][0] = pxx[piece];
178 mvstr[0][2] = cxx[column(t)];
179 mvstr[0][3] = rxx[row(t)];
181 strcpy(mvstr[1], mvstr[0]);
182 strcpy(mvstr[2], mvstr[0]);
183 strcpy(mvstr[3], mvstr[0]);
185 else if ((f != 0) || (t != 0))
187 /* algebraic notation */
188 mvstr[0][0] = cxx[column(f)];
189 mvstr[0][1] = rxx[row(f)];
190 mvstr[0][2] = cxx[column(t)];
191 mvstr[0][3] = rxx[row(t)];
192 mvstr[0][4] = mvstr[3][0] = '\0';
193 mvstr[1][0] = pxx[board[f]];
195 mvstr[2][0] = mvstr[1][0];
196 mvstr[2][1] = mvstr[0][1];
198 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
199 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
200 mvstr[2][4] = mvstr[1][3] = '\0';
201 strcpy(mvstr[3], mvstr[2]);
202 mvstr[3][1] = mvstr[0][0];
206 strcat(mvstr[0], "+");
207 strcat(mvstr[1], "+");
208 strcat(mvstr[2], "+");
209 strcat(mvstr[3], "+");
214 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
221 * Compare the string 's' to the list of legal moves available for the
222 * opponent. If a match is found, make the move on the board.
226 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
228 static short pnt, tempb, tempc, tempsf, tempst, cnt;
229 static struct leaf xnode;
231 short i, l, local_flags;
234 /* check and remove quality flags */
235 for (i = local_flags = 0, l = strlen(s); i < l; i++)
240 local_flags |= badmove;
245 local_flags |= goodmove;
251 local_flags |= difficult;
260 if (iop == UNMAKE_MODE)
262 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
268 if (iop == VERIFY_AND_MAKE_MODE)
269 generate_move_flags = true;
271 MoveList(opponent, 2, -1, true);
272 generate_move_flags = false;
275 while (pnt < TrPnt[3])
278 algbr(node->f, node->t, (short) node->flags);
280 if ((strcmp(s, mvstr[0]) == 0)
281 || (strcmp(s, mvstr[1]) == 0)
282 || (strcmp(s, mvstr[2]) == 0)
283 || (strcmp(s, mvstr[3]) == 0))
290 if ((cnt == 1) && (xnode.score > DONTUSE))
294 MakeMove(opponent, &xnode, &tempb, &tempc,
295 &tempsf, &tempst, &INCscore);
297 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
299 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
300 AlwaysShowMessage("Illegal move (in check) %s", s);
305 if (iop == VERIFY_AND_TRY_MODE)
308 UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
309 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
310 GameList[GameCnt].nodes = 0;
311 ElapsedTime(COMPUTE_AND_INIT_MODE);
312 GameList[GameCnt].time = (short) (et + 50)/100;
313 GameList[GameCnt].flags |= local_flags;
317 TimeControl.clock[opponent] -= et;
318 timeopp[oppptr] = et;
319 --TimeControl.moves[opponent];
322 *mv = (xnode.f << 8) | xnode.t;
323 algbr(xnode.f, xnode.t, false);
325 /* in force mode, check for mate conditions */
328 if (IsCheckmate(opponent ^ 1, -1, -1))
332 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
342 AlwaysShowMessage("Illegal move (no match) %s", s);
344 if (!barebones && (cnt > 1))
346 sprintf(buffer, "Ambiguous Move %s!", s);
356 parser(char *f, int side, short *fpiece)
364 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
366 if (f[0] == pxx[i] || f[0] == qxx[i])
368 *fpiece = (p ? promoted[i] : unpromoted[i]);
373 if (f[1] == '*' || f[1] == '\'')
378 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
386 p = (f[5] == '+') ? 0x80 : 0;
388 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
396 while (*InPtr != ' ')
399 while (*InPtr == ' ')
408 while (*InPtr == ' ')
424 strcpy(fname, savefile);
426 ShowMessage("Enter file name: ");
427 RequestInputString(fname, sizeof(fname)-1);
430 if (fname[0] == '\0')
431 strcpy(fname, "shogi.000");
433 if ((fd = fopen(fname, "r")) != NULL)
436 fgets(fname, 256, fd);
437 computer = opponent = black;
446 /* FIXME: write a skipn() function so that we can get
447 * 3 skips by doing skipn(3) */
451 Game50 = atoi(InPtr);
453 flag.force = (*InPtr == 'f');
454 fgets(fname, 256, fd); /* empty */
455 fgets(fname, 256, fd);
458 TCflag = atoi(InPtr);
462 OperatorTime = atoi(InPtr);
463 fgets(fname, 256, fd);
466 TimeControl.clock[black] = atol(InPtr);
469 TimeControl.moves[black] = atoi(InPtr);
470 fgets(fname, 256, fd);
473 TimeControl.clock[white] = atol(InPtr);
476 TimeControl.moves[white] = atoi(InPtr);
477 fgets(fname, 256, fd); /* empty */
479 for (i = NO_ROWS - 1; i > -1; i--)
481 fgets(fname, 256, fd);
485 for (j = 0; j < NO_COLS; j++)
487 sq = i * NO_COLS + j;
493 board[sq] = no_piece;
498 for (c = 0; c < NO_PIECES; c++)
503 board[sq] = promoted[c];
505 board[sq] = unpromoted[c];
511 for (c = 0; c < NO_PIECES; c++)
516 board[sq] = promoted[c];
518 board[sq] = unpromoted[c];
526 Mvboard[sq] = atoi(InPtr);
531 fgets(fname, 256, fd); /* empty */
532 fgets(fname, 256, fd); /* 9 8 7 ... */
533 fgets(fname, 256, fd); /* empty */
534 fgets(fname, 256, fd); /* p l n ... */
537 for (side = 0; side <= 1; side++)
539 fgets(fname, 256, fd);
543 Captured[side][pawn] = atoi(InPtr);
546 Captured[side][lance] = atoi(InPtr);
548 Captured[side][knight] = atoi(InPtr);
551 Captured[side][silver] = atoi(InPtr);
553 Captured[side][gold] = atoi(InPtr);
555 Captured[side][bishop] = atoi(InPtr);
557 Captured[side][rook] = atoi(InPtr);
559 Captured[side][king] = atoi(InPtr);
563 flag.regularstart = true;
565 fgets(fname, 256, fd); /* empty */
566 fgets(fname, 256, fd); /* move score ... */
568 while (fgets(fname, 256, fd))
577 g = &GameList[GameCnt];
578 g->gmove = parser(InPtr, side, &g->fpiece);
580 g->score = atoi(InPtr);
582 g->depth = atoi(InPtr);
584 g->nodes = atol(InPtr);
586 g->time = atol(InPtr);
588 g->flags = c = atoi(InPtr);
590 g->hashkey = strtol(InPtr, (char **) NULL, 16);
592 g->hashbd = strtol(InPtr, (char **) NULL, 16);
600 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
602 if (pxx[i] == *InPtr)
610 g->color = ((*InPtr == 'W') ? white : black);
612 g->piece = (*InPtr == '+'
614 : unpromoted[piece]);
623 if (TimeControl.clock[black] > 0)
631 UpdateDisplay(0, 0, 1, 0);
643 short sq, i, c, f, t;
646 char empty[2] = "\n";
649 strcpy(fname, savefile);
651 ShowMessage("Enter file name: ");
652 RequestInputString(fname, sizeof(fname)-1);
655 if (fname[0] == '\0')
656 strcpy(fname, "shogi.000");
658 if ((fd = fopen(fname, "w")) != NULL)
663 if (computer == white)
666 if (computer == black)
669 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
670 flag.force ? "force" : "");
672 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
673 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
674 TimeControl.clock[black], TimeControl.moves[black],
675 TimeControl.clock[white], TimeControl.moves[white]);
678 for (i = NO_ROWS - 1; i > -1; i--)
680 fprintf(fd, "%c ", ROW_NAME(i));
682 for (c = 0; c < NO_COLS; c++)
684 sq = i * NO_COLS + c;
686 p = is_promoted[piece] ? '+' : ' ';
687 fprintf(fd, "%c", p);
703 fprintf(fd, "%c", p);
708 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
709 fprintf(fd, " %d", Mvboard[f]);
716 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
718 fprintf(fd, " p l n s g b r k\n");
720 fprintf(fd, " 5 4 3 2 1\n");
722 fprintf(fd, " p s g b r k\n");
725 for (side = 0; side <= 1; side++)
727 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
728 fprintf(fd, " %2d", Captured[side][pawn]);
730 fprintf(fd, " %2d", Captured[side][lance]);
731 fprintf(fd, " %2d", Captured[side][knight]);
733 fprintf(fd, " %2d", Captured[side][silver]);
734 fprintf(fd, " %2d", Captured[side][gold]);
735 fprintf(fd, " %2d", Captured[side][bishop]);
736 fprintf(fd, " %2d", Captured[side][rook]);
737 fprintf(fd, " %2d", Captured[side][king]);
742 fputs(" move score depth nodes time flags capture\n", fd);
744 for (i = 1; i <= GameCnt; i++)
746 struct GameRec *g = &GameList[i];
749 t = (g->gmove & 0xFF);
750 algbr(f, t, g->flags);
752 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
755 : (is_promoted[g->fpiece] ? '+' : ' ')),
757 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
759 g->nodes, g->time, g->flags,
760 g->hashkey, g->hashbd);
762 if (g->piece != no_piece)
764 fprintf(fd, " %c %s %c\n",
765 pxx[g->piece], ColorStr[g->color],
766 (is_promoted[g->piece] ? '+' : ' '));
776 ShowMessage("Game saved");
780 ShowMessage("Could not open file");
787 * GetXGame, SaveXGame and BookGame used to only be defined if
788 * xshogi wasn't defined -- wonder why?
800 ShowMessage("Enter file name: ");
801 RequestInputString(fname, sizeof(fname)-1);
803 if (fname[0] == '\0')
804 strcpy(fname, "xshogi.position.read");
806 if ((fd = fopen(fname, "r")) != NULL)
809 flag.regularstart = false;
812 /* xshogi position file ... */
813 fgets(fname, 256, fd);
818 if (strcmp(fname, "xshogi"))
822 /* -- empty line -- */
823 fgets(fname, 256, fd);
824 /* -- empty line -- */
825 fgets(fname, 256, fd);
827 for (i = NO_ROWS - 1; i > -1; i--)
829 fgets(fname, 256, fd);
832 for (j = 0; j < NO_COLS; j++)
834 sq = i * NO_COLS + j;
840 board[sq] = no_piece;
845 for (c = 0; c < NO_PIECES; c++)
850 board[sq] = promoted[c];
852 board[sq] = unpromoted[c];
858 for (c = 0; c < NO_PIECES; c++)
863 board[sq] = promoted[c];
865 board[sq] = unpromoted[c];
878 for (side = 0; side <= 1; side++)
880 fgets(fname, 256, fd);
882 Captured[side][pawn] = atoi(InPtr);
885 Captured[side][lance] = atoi(InPtr);
887 Captured[side][knight] = atoi(InPtr);
890 Captured[side][silver] = atoi(InPtr);
892 Captured[side][gold] = atoi(InPtr);
894 Captured[side][bishop] = atoi(InPtr);
896 Captured[side][rook] = atoi(InPtr);
898 Captured[side][king] = atoi(InPtr);
901 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
914 UpdateDisplay(0, 0, 1, 0);
929 ShowMessage("Enter file name: ");
930 RequestInputString(fname, sizeof(fname)-1);
932 if (fname[0] == '\0')
933 strcpy(fname, "xshogi.position.read");
935 if ((fd = fopen(fname, "w")) != NULL)
937 fputs("# xshogi position file -- \n", fd);
941 for (i = NO_ROWS - 1; i > -1; i--)
945 for (j = 0; j < NO_COLS; j++)
947 sq = i * NO_COLS + j;
949 isp = is_promoted[piece];
950 *p = (isp ? '+' : ' ');
953 if (piece == no_piece)
955 else if (color[sq] == white)
968 for (side = 0; side <= 1; side++)
972 "%d %d %d %d %d %d %d %d\n",
974 "%d %d %d %d %d %d\n",
976 Captured[side][pawn],
978 Captured[side][lance],
979 Captured[side][knight],
981 Captured[side][silver],
982 Captured[side][gold],
983 Captured[side][bishop],
984 Captured[side][rook],
985 Captured[side][king]);
990 if (computer == black)
991 fputs("white to play\n", fd);
993 fputs("black to play\n", fd);
1004 char fname[256], sflags[4];
1008 strcpy(fname, savefile);
1010 /* Enter file name */
1011 ShowMessage("Enter file name: ");
1012 RequestInputString(fname, sizeof(fname)-1);
1015 if (fname[0] == '\0')
1018 if ((fd = fopen(fname, "a")) != NULL)
1022 for (i = 1; i <= GameCnt; i++)
1024 struct GameRec *g = &GameList[i];
1025 char mvnr[20], mvs[20];
1028 sprintf(mvnr, "%d.", (i + 1)/2);
1033 t = (g->gmove & 0xFF);
1034 algbr(f, t, g->flags);
1037 /* determine move quality string */
1038 if (g->flags & goodmove)
1041 if (g->flags & badmove)
1044 #ifdef EASY_OPENINGS
1045 if (g->flags & difficult)
1051 /* determine move string */
1054 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1058 sprintf(mvs, "%c%c%c%c%c%s%s ",
1059 mvstr[0][0], mvstr[0][1],
1060 (g->flags & capture) ? 'x' : '-',
1061 mvstr[0][2], mvstr[0][3],
1062 (mvstr[0][4] == '+') ? "+" : "",
1066 fprintf(fd, "%s%s%c%s",
1070 : (is_promoted[g->fpiece] ? "+" : "")),
1083 ShowMessage("Game saved");
1087 ShowMessage("Could not open file");
1099 char fname[256], dbuf[256];
1103 strcpy(fname, listfile);
1108 strncpy(dbuf, ctime(&when), 20);
1115 /* use format "CL.Jan01-020304B" when
1118 program played white */
1120 sprintf(fname, "CL.%s%s-%s%s%s%c",
1121 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1122 dbuf + 17, ColorStr[computer][0]);
1124 /* replace space padding with 0 */
1125 for (i = 0; fname[i] != '\0'; i++)
1127 if (fname[i] == ' ')
1132 fd = fopen(fname, "w");
1136 printf("Open failure for file: %s", fname);
1140 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1141 fputs(" score depth nodes time ", fd);
1142 fputs(" score depth nodes time\n", fd);
1144 for (i = 1; i <= GameCnt; i++)
1146 f = GameList[i].gmove >> 8;
1147 t = (GameList[i].gmove & 0xFF);
1148 algbr(f, t, GameList[i].flags);
1150 if (GameList[i].flags & book)
1152 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1155 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1156 pxx[GameList[i].fpiece],
1158 ? &mvstr[0][1] : mvstr[0]),
1165 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1168 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1169 pxx[GameList[i].fpiece],
1170 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1171 GameList[i].score, GameList[i].depth,
1172 GameList[i].nodes, GameList[i].time);
1185 fprintf(fd, "\n\n");
1187 if (GameList[GameCnt].flags & draw)
1189 fprintf(fd, "Draw %s\n", DRAW);
1191 if (DRAW == DRAW_REPETITION)
1195 fprintf(fd, "repetition by positions ");
1197 for (j = GameCnt - 1; j >= Game50; j -= 2)
1199 if (GameList[j].hashkey == hashkey &&
1200 GameList[j].hashbd == hashbd)
1201 fprintf(fd, "%d ", j);
1207 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1209 fprintf(fd, "%s\n", ColorStr[player ]);
1211 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1213 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1227 GameList[GameCnt].flags |= badmove;
1231 GameList[GameCnt].flags |= goodmove;
1234 #ifdef EASY_OPENINGS
1236 GameList[GameCnt].flags |= difficult;
1246 * Undo the most recent half-move.
1254 f = GameList[GameCnt].gmove >> 8;
1255 t = GameList[GameCnt].gmove & 0x7F;
1259 /* the move was a drop */
1260 Captured[color[t]][board[t]]++;
1261 board[t] = no_piece;
1267 if (GameList[GameCnt].flags & promote)
1268 board[f] = unpromoted[board[t]];
1270 board[f] = board[t];
1272 color[f] = color[t];
1273 board[t] = GameList[GameCnt].piece;
1274 color[t] = GameList[GameCnt].color;
1276 if (board[t] != no_piece)
1277 Captured[color[f]][unpromoted[board[t]]]--;
1279 if (color[t] != neutral)
1287 if (TCflag && (TCmoves > 1))
1288 ++TimeControl.moves[color[f]];
1290 hashkey = GameList[GameCnt].hashkey;
1291 hashbd = GameList[GameCnt].hashbd;
1293 computer = computer ^ 1;
1294 opponent = opponent ^ 1;
1297 player = player ^ 1;
1299 UpdateDisplay(0, 0, 1, 0);
1301 if (flag.regularstart)
1308 FlagString(unsigned short flags, char *s)
1313 if (flags & promote)
1314 strcat(s, " promote");
1316 if (flags & dropmask)
1317 strcat(s, " drop:");
1319 if ((piece = (flags & pmask)))
1323 if (is_promoted[piece])
1326 s[l++] = pxx[piece];
1330 if (flags & capture)
1331 strcat(s, " capture");
1334 strcat(s, " exact");
1337 strcat(s, " tesuji");
1340 strcat(s, " check");
1346 strcat(s, " stupid");
1348 if (flags & questionable)
1349 strcat(s, " questionable");
1351 if (flags & kingattack)
1352 strcat(s, " kingattack");
1361 TestSpeed(void(*f)(short side, short ply,
1362 short in_check, short blockable),
1372 #ifdef HAVE_GETTIMEOFDAY
1376 #ifdef HAVE_GETTIMEOFDAY
1377 gettimeofday(&tv, NULL);
1378 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1383 for (i = 0; i < j; i++)
1385 f(opponent, 2, -1, true);
1388 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1390 if (!pick(jj, TrPnt[3] - 1))
1396 #ifdef HAVE_GETTIMEOFDAY
1397 gettimeofday(&tv, NULL);
1398 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1403 cnt = j * (TrPnt[3] - TrPnt[2]);
1416 TestPSpeed(short(*f) (short side), unsigned j)
1420 #ifdef HAVE_GETTIMEOFDAY
1424 #ifdef HAVE_GETTIMEOFDAY
1425 gettimeofday(&tv, NULL);
1426 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1431 for (i = 0; i < j; i++)
1434 #ifdef HAVE_GETTIMEOFDAY
1435 gettimeofday(&tv, NULL);
1436 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1460 time = &s[strlen("otime")];
1461 t = (int)strtol(time, &time, 10);
1466 sec = (int)strtol(time, &time, 10);
1469 m = (int)strtol(time, &time, 10);
1472 TimeControl.clock[opponent] = t;
1475 TimeControl.moves[opponent] = m;
1477 ElapsedTime(COMPUTE_AND_INIT_MODE);
1481 /* just to inform xshogi about availability of otime command */
1482 printf("otime %d %d\n", t, m);
1489 SetMachineTime(char *s)
1494 time = &s[strlen("time")];
1496 t = (int)strtol(time, &time, 10);
1501 sec = (int)strtol(time, &time, 10);
1504 m = (int)strtol(time, &time, 10);
1507 TimeControl.clock[computer] = t;
1510 TimeControl.moves[computer] = m;
1512 ElapsedTime(COMPUTE_AND_INIT_MODE);
1516 /* just to inform xshogi about availability of time command */
1517 printf("time %d %d\n", t, m);
1525 /* FIXME! This is truly the function from hell! */
1528 * Process the user's command. If easy mode is OFF (the computer is thinking
1529 * on opponents time) and the program is out of book, then make the 'hint'
1530 * move on the board and call SelectMove() to find a response. The user
1531 * terminates the search by entering ^C (quit siqnal) before entering a
1532 * command. If the opponent does not make the hint move, then set Sdepth to
1537 InputCommand(char *command)
1540 short have_shown_prompt = false;
1541 short ok, done, is_move = false;
1545 ok = flag.quit = done = false;
1549 if (TTadd > ttbllimit)
1553 if ((hint > 0) && !flag.easy && !flag.force)
1556 * A hint move for the player is available. Compute a move for the
1557 * opponent in background mode assuming that the hint move will be
1558 * selected by the player.
1561 ft = time0; /* Save reference time for the player. */
1563 algbr((short) hint >> 8, (short) hint & 0xff, false);
1564 strcpy(s, mvstr[0]);
1571 /* do the hint move */
1572 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1576 #ifdef QUIETBACKGROUND
1578 have_shown_prompt = true;
1579 #endif /* QUIETBACKGROUND */
1581 /* Start computing a move until the search is interrupted. */
1583 #ifdef INTERRUPT_TEST
1587 /* would love to put null move in here */
1588 /* after we make the hint move make a 2 ply search
1589 * with both plys our moves */
1590 /* think on opponents time */
1591 SelectMove(computer, BACKGROUND_MODE);
1593 #ifdef INTERRUPT_TEST
1594 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1598 printf("searching not terminated by interrupt!\n");
1602 printf("elapsed time from interrupt to "
1603 "terminating search: %ld\n", it);
1607 /* undo the hint and carry on */
1608 VerifyMove(s, UNMAKE_MODE, &mv);
1612 time0 = ft; /* Restore reference time for the player. */
1615 while(!(ok || flag.quit || done))
1619 #ifdef QUIETBACKGROUND
1620 if (!have_shown_prompt)
1622 #endif /* QUIETBACKGROUND */
1626 #ifdef QUIETBACKGROUND
1629 have_shown_prompt = false;
1630 #endif /* QUIETBACKGROUND */
1632 if (command == NULL) {
1636 eof = GetString(sx);
1638 strcpy(sx, command);
1642 sscanf(sx, "%s", s);
1650 if (strcmp(s, "bd") == 0) /* bd -- display board */
1652 /* FIXME: Hack alert! */
1653 short old_xshogi = XSHOGI;
1656 display_type = DISPLAY_RAW;
1659 UpdateDisplay(0, 0, 1, 0);
1662 display_type = DISPLAY_X;
1664 else if (strcmp(s, "post") == 0)
1666 flag.post = !flag.post;
1668 else if (strcmp(s, "alg") == 0)
1670 /* noop */ ; /* alg */
1672 else if ((strcmp(s, "quit") == 0)
1673 || (strcmp(s, "exit") == 0))
1678 else if (strcmp(s, "post") == 0)
1680 flag.post = !flag.post;
1683 else if ((strcmp(s, "set") == 0)
1684 || (strcmp(s, "edit") == 0))
1688 else if ((strcmp(s, "setup") == 0))
1692 else if (strcmp(s, "first") == 0)
1696 else if (strcmp(s, "go") == 0)
1701 if (computer == black)
1712 else if (strcmp(s, "help") == 0)
1716 else if (strcmp(s, "material") == 0)
1718 flag.material = !flag.material;
1720 else if (strcmp(s, "force") == 0)
1725 flag.bothsides = false;
1729 flag.force = !flag.force;
1730 flag.bothsides = false;
1733 else if (strcmp(s, "book") == 0)
1735 Book = Book ? 0 : BOOKFAIL;
1737 else if (strcmp(s, "new") == 0)
1740 UpdateDisplay(0, 0, 1, 0);
1742 else if (strcmp(s, "list") == 0)
1746 else if ((strcmp(s, "level") == 0)
1747 || (strcmp(s, "clock") == 0))
1751 else if (strcmp(s, "hash") == 0)
1753 flag.hash = !flag.hash;
1755 else if (strcmp(s, "gamein") == 0)
1757 flag.gamein = !flag.gamein;
1759 else if (strcmp(s, "beep") == 0)
1761 flag.beep = !flag.beep;
1763 else if (strcmp(s, "time") == 0)
1767 else if (strcmp(s, "otime") == 0)
1771 else if (strcmp(s, "Awindow") == 0)
1773 ChangeAlphaWindow();
1775 else if (strcmp(s, "Bwindow") == 0)
1779 else if (strcmp(s, "rcptr") == 0)
1781 flag.rcptr = !flag.rcptr;
1783 else if (strcmp(s, "hint") == 0)
1787 else if (strcmp(s, "both") == 0)
1789 flag.bothsides = !flag.bothsides;
1792 ElapsedTime(COMPUTE_AND_INIT_MODE);
1793 SelectMove(opponent, FOREGROUND_MODE);
1796 else if (strcmp(s, "reverse") == 0)
1798 flag.reverse = !flag.reverse;
1800 UpdateDisplay(0, 0, 1, 0);
1802 else if (strcmp(s, "switch") == 0)
1804 computer = computer ^ 1;
1805 opponent = opponent ^ 1;
1806 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1811 else if (strcmp(s, "black") == 0)
1820 * ok = true; don't automatically start with black command
1823 else if (strcmp(s, "white") == 0)
1832 * ok = true; don't automatically start with white command
1835 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1839 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1844 /* CHECKME: are these next three correct? */
1845 else if (!XSHOGI && strcmp(s, "xget") == 0)
1849 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1853 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1857 #ifdef EASY_OPENINGS
1858 else if ((strcmp(s, "?") == 0)
1859 || (strcmp(s, "!") == 0)
1860 || (strcmp(s, "~") == 0))
1862 else if ((strcmp(s, "?") == 0)
1863 || (strcmp(s, "!") == 0))
1868 else if (strcmp(s, "get") == 0)
1872 else if (strcmp(s, "save") == 0)
1876 else if (strcmp(s, "depth") == 0)
1878 ChangeSearchDepth();
1880 else if (strcmp(s, "hashdepth") == 0)
1884 else if (strcmp(s, "random") == 0)
1888 else if (strcmp(s, "hard") == 0)
1892 else if (strcmp(s, "easy") == 0)
1894 flag.easy = !flag.easy;
1896 else if (strcmp(s, "tsume") == 0)
1898 flag.tsume = !flag.tsume;
1900 else if (strcmp(s, "contempt") == 0)
1904 else if (strcmp(s, "xwndw") == 0)
1908 else if (strcmp(s, "rv") == 0)
1911 UpdateDisplay(0, 0, 1, 0);
1913 else if (strcmp(s, "coords") == 0)
1915 flag.coords = !flag.coords;
1916 UpdateDisplay(0, 0, 1, 0);
1918 else if (strcmp(s, "stars") == 0)
1920 flag.stars = !flag.stars;
1921 UpdateDisplay(0, 0, 1, 0);
1923 else if (!XSHOGI && strcmp(s, "moves") == 0)
1930 extern unsigned short PrVar[MAXDEPTH];
1932 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1939 ShowMessage("Testing MoveList Speed");
1940 temp = generate_move_flags;
1941 generate_move_flags = true;
1942 TestSpeed(MoveList, 1);
1943 generate_move_flags = temp;
1944 ShowMessage("Testing CaptureList Speed");
1945 TestSpeed(CaptureList, 1);
1946 ShowMessage("Testing Eval Speed");
1947 ExaminePosition(opponent);
1948 TestPSpeed(ScorePosition, 1);
1950 else if (!XSHOGI && strcmp(s, "test") == 0)
1953 ShowMessage("Testing MoveList Speed");
1954 TestSpeed(MoveList, 2000);
1955 ShowMessage("Testing CaptureList Speed");
1956 TestSpeed(CaptureList, 3000);
1957 ShowMessage("Testing Eval Speed");
1958 ExaminePosition(opponent);
1959 TestPSpeed(ScorePosition, 1500);
1961 ShowMessage("Testing MoveList Speed");
1962 TestSpeed(MoveList, 20000);
1963 ShowMessage("Testing CaptureList Speed");
1964 TestSpeed(CaptureList, 30000);
1965 ShowMessage("Testing Eval Speed");
1966 ExaminePosition(opponent);
1967 TestPSpeed(ScorePosition, 15000);
1970 else if (!XSHOGI && strcmp(s, "p") == 0)
1974 else if (!XSHOGI && strcmp(s, "debug") == 0)
1984 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
1986 /* check for repetition */
1987 short rpt = repetition();
1991 DRAW = DRAW_REPETITION;
1993 GameList[GameCnt].flags |= draw;
2007 ElapsedTime(COMPUTE_AND_INIT_MODE);
2011 computer = opponent;
2012 opponent = computer ^ 1;
2017 /* add remaining time in milliseconds for xshogi */
2020 printf("%d. %s %ld\n",
2021 ++mycnt2, s, TimeControl.clock[player] * 10);
2024 #ifdef notdef /* optional pass best line to frontend with move */
2025 # if !defined NOPOST
2027 if (flag.post && !flag.mate)
2031 printf(" %6d ", MSCORE);
2033 for (i = 1; MV[i] > 0; i++)
2035 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2036 printf("%5s ", mvstr[0]);
2049 SetTimeControl(void)
2053 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2054 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2055 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2059 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2060 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2063 flag.onemove = (TCmoves == 1);
2065 ElapsedTime(COMPUTE_AND_INIT_MODE);