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 /* request *snprintf prototypes */
35 #define _POSIX_C_SOURCE 200112L
38 #if defined HAVE_GETTIMEOFDAY
45 #include <sys/param.h>
46 #include <sys/types.h>
53 int InBackground = false;
59 movealgbr(short m, char *s)
62 short piece = 0, flag = 0;
75 piece = f - NO_SQUARES;
77 if (piece > NO_PIECES)
80 flag = (dropmask | piece);
127 #endif /* BOOKTEST */
133 * Generate move strings in different formats.
136 * - f piece to be moved
137 * - 0 < f < NO_SQUARES source square
138 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
139 * - t & 0x7f target square
140 * - t & 0x80 promotion flag
141 * - flag FIXME: must be zero ?
148 algbr(short f, short t, short flag)
154 piece = f - NO_SQUARES;
156 if (f > (NO_SQUARES + NO_PIECES))
159 flag = (dropmask | piece);
168 if ((f == t) && ((f != 0) || (t != 0)))
171 Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
174 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
176 else if ((flag & dropmask) != 0)
178 short piece = flag & pmask;
180 mvstr[0][0] = pxx[piece];
182 mvstr[0][2] = cxx[column(t)];
183 mvstr[0][3] = rxx[row(t)];
185 strcpy(mvstr[1], mvstr[0]);
186 strcpy(mvstr[2], mvstr[0]);
187 strcpy(mvstr[3], mvstr[0]);
189 else if ((f != 0) || (t != 0))
191 /* algebraic notation */
192 mvstr[0][0] = cxx[column(f)];
193 mvstr[0][1] = rxx[row(f)];
194 mvstr[0][2] = cxx[column(t)];
195 mvstr[0][3] = rxx[row(t)];
196 mvstr[0][4] = mvstr[3][0] = '\0';
197 mvstr[1][0] = pxx[board[f]];
199 mvstr[2][0] = mvstr[1][0];
200 mvstr[2][1] = mvstr[0][1];
202 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
203 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
204 mvstr[2][4] = mvstr[1][3] = '\0';
205 strcpy(mvstr[3], mvstr[2]);
206 mvstr[3][1] = mvstr[0][0];
210 strcat(mvstr[0], "+");
211 strcat(mvstr[1], "+");
212 strcat(mvstr[2], "+");
213 strcat(mvstr[3], "+");
218 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
225 * Compare the string 's' to the list of legal moves available for the
226 * opponent. If a match is found, make the move on the board.
230 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
232 static short pnt, tempb, tempc, tempsf, tempst, cnt;
233 static struct leaf xnode;
235 short i, l, local_flags;
238 /* check and remove quality flags */
239 for (i = local_flags = 0, l = strlen(s); i < l; i++)
244 local_flags |= badmove;
249 local_flags |= goodmove;
255 local_flags |= difficult;
264 if (iop == UNMAKE_MODE)
266 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
272 if (iop == VERIFY_AND_MAKE_MODE)
273 generate_move_flags = true;
275 MoveList(opponent, 2, -1, true);
276 generate_move_flags = false;
279 while (pnt < TrPnt[3])
282 algbr(node->f, node->t, (short) node->flags);
284 if ((strcmp(s, mvstr[0]) == 0)
285 || (strcmp(s, mvstr[1]) == 0)
286 || (strcmp(s, mvstr[2]) == 0)
287 || (strcmp(s, mvstr[3]) == 0))
294 if ((cnt == 1) && (xnode.score > DONTUSE))
298 MakeMove(opponent, &xnode, &tempb, &tempc,
299 &tempsf, &tempst, &INCscore);
301 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
303 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
304 AlwaysShowMessage("Illegal move (in check) %s", s);
309 if (iop == VERIFY_AND_TRY_MODE)
312 UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
313 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
314 GameList[GameCnt].nodes = 0;
315 ElapsedTime(COMPUTE_AND_INIT_MODE);
316 GameList[GameCnt].time = (short) (et + 50)/100;
317 GameList[GameCnt].flags |= local_flags;
321 TimeControl.clock[opponent] -= et;
322 timeopp[oppptr] = et;
323 --TimeControl.moves[opponent];
326 *mv = (xnode.f << 8) | xnode.t;
327 algbr(xnode.f, xnode.t, false);
329 /* in force mode, check for mate conditions */
332 if (IsCheckmate(opponent ^ 1, -1, -1))
336 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
346 AlwaysShowMessage("Illegal move (no match) %s", s);
348 if (!barebones && (cnt > 1))
350 sprintf(buffer, "Ambiguous Move %s!", s);
360 parser(char *f, int side, short *fpiece)
368 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
370 if (f[0] == pxx[i] || f[0] == qxx[i])
372 *fpiece = (p ? promoted[i] : unpromoted[i]);
377 if (f[1] == '*' || f[1] == '\'')
382 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
390 p = (f[5] == '+') ? 0x80 : 0;
392 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
400 while (*InPtr != ' ')
403 while (*InPtr == ' ')
412 while (*InPtr == ' ')
428 strcpy(fname, savefile);
430 ShowMessage("Enter file name: ");
431 RequestInputString(fname, sizeof(fname)-1);
434 if (fname[0] == '\0')
435 strcpy(fname, "shogi.000");
437 if ((fd = fopen(fname, "r")) != NULL)
440 fgets(fname, 256, fd);
441 computer = opponent = black;
450 /* FIXME: write a skipn() function so that we can get
451 * 3 skips by doing skipn(3) */
455 Game50 = atoi(InPtr);
457 flag.force = (*InPtr == 'f');
458 fgets(fname, 256, fd); /* empty */
459 fgets(fname, 256, fd);
462 TCflag = atoi(InPtr);
466 OperatorTime = atoi(InPtr);
467 fgets(fname, 256, fd);
470 TimeControl.clock[black] = atol(InPtr);
473 TimeControl.moves[black] = atoi(InPtr);
474 fgets(fname, 256, fd);
477 TimeControl.clock[white] = atol(InPtr);
480 TimeControl.moves[white] = atoi(InPtr);
481 fgets(fname, 256, fd); /* empty */
483 for (i = NO_ROWS - 1; i > -1; i--)
485 fgets(fname, 256, fd);
489 for (j = 0; j < NO_COLS; j++)
491 sq = i * NO_COLS + j;
497 board[sq] = no_piece;
502 for (c = 0; c < NO_PIECES; c++)
507 board[sq] = promoted[c];
509 board[sq] = unpromoted[c];
515 for (c = 0; c < NO_PIECES; c++)
520 board[sq] = promoted[c];
522 board[sq] = unpromoted[c];
530 Mvboard[sq] = atoi(InPtr);
535 fgets(fname, 256, fd); /* empty */
536 fgets(fname, 256, fd); /* 9 8 7 ... */
537 fgets(fname, 256, fd); /* empty */
538 fgets(fname, 256, fd); /* p l n ... */
541 for (side = 0; side <= 1; side++)
543 fgets(fname, 256, fd);
547 Captured[side][pawn] = atoi(InPtr);
550 Captured[side][lance] = atoi(InPtr);
552 Captured[side][knight] = atoi(InPtr);
555 Captured[side][silver] = atoi(InPtr);
557 Captured[side][gold] = atoi(InPtr);
559 Captured[side][bishop] = atoi(InPtr);
561 Captured[side][rook] = atoi(InPtr);
563 Captured[side][king] = atoi(InPtr);
567 flag.regularstart = true;
569 fgets(fname, 256, fd); /* empty */
570 fgets(fname, 256, fd); /* move score ... */
572 while (fgets(fname, 256, fd))
581 g = &GameList[GameCnt];
582 g->gmove = parser(InPtr, side, &g->fpiece);
584 g->score = atoi(InPtr);
586 g->depth = atoi(InPtr);
588 g->nodes = atol(InPtr);
590 g->time = atol(InPtr);
592 g->flags = c = atoi(InPtr);
594 g->hashkey = strtol(InPtr, (char **) NULL, 16);
596 g->hashbd = strtol(InPtr, (char **) NULL, 16);
604 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
606 if (pxx[i] == *InPtr)
614 g->color = ((*InPtr == 'W') ? white : black);
616 g->piece = (*InPtr == '+'
618 : unpromoted[piece]);
627 if (TimeControl.clock[black] > 0)
635 UpdateDisplay(0, 0, 1, 0);
647 short sq, i, c, f, t;
650 char empty[2] = "\n";
653 strcpy(fname, savefile);
655 ShowMessage("Enter file name: ");
656 RequestInputString(fname, sizeof(fname)-1);
659 if (fname[0] == '\0')
660 strcpy(fname, "shogi.000");
662 if ((fd = fopen(fname, "w")) != NULL)
667 if (computer == white)
670 if (computer == black)
673 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
674 flag.force ? "force" : "");
676 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
677 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
678 TimeControl.clock[black], TimeControl.moves[black],
679 TimeControl.clock[white], TimeControl.moves[white]);
682 for (i = NO_ROWS - 1; i > -1; i--)
684 fprintf(fd, "%c ", ROW_NAME(i));
686 for (c = 0; c < NO_COLS; c++)
688 sq = i * NO_COLS + c;
690 p = is_promoted[piece] ? '+' : ' ';
691 fprintf(fd, "%c", p);
707 fprintf(fd, "%c", p);
712 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
713 fprintf(fd, " %d", Mvboard[f]);
720 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
722 fprintf(fd, " p l n s g b r k\n");
724 fprintf(fd, " 5 4 3 2 1\n");
726 fprintf(fd, " p s g b r k\n");
729 for (side = 0; side <= 1; side++)
731 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
732 fprintf(fd, " %2d", Captured[side][pawn]);
734 fprintf(fd, " %2d", Captured[side][lance]);
735 fprintf(fd, " %2d", Captured[side][knight]);
737 fprintf(fd, " %2d", Captured[side][silver]);
738 fprintf(fd, " %2d", Captured[side][gold]);
739 fprintf(fd, " %2d", Captured[side][bishop]);
740 fprintf(fd, " %2d", Captured[side][rook]);
741 fprintf(fd, " %2d", Captured[side][king]);
746 fputs(" move score depth nodes time flags capture\n", fd);
748 for (i = 1; i <= GameCnt; i++)
750 struct GameRec *g = &GameList[i];
753 t = (g->gmove & 0xFF);
754 algbr(f, t, g->flags);
756 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
759 : (is_promoted[g->fpiece] ? '+' : ' ')),
761 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
763 g->nodes, g->time, g->flags,
764 g->hashkey, g->hashbd);
766 if (g->piece != no_piece)
768 fprintf(fd, " %c %s %c\n",
769 pxx[g->piece], ColorStr[g->color],
770 (is_promoted[g->piece] ? '+' : ' '));
780 ShowMessage("Game saved");
784 ShowMessage("Could not open file");
791 * GetXGame, SaveXGame and BookGame used to only be defined if
792 * xshogi wasn't defined -- wonder why?
804 ShowMessage("Enter file name: ");
805 RequestInputString(fname, sizeof(fname)-1);
807 if (fname[0] == '\0')
808 strcpy(fname, "xshogi.position.read");
810 if ((fd = fopen(fname, "r")) != NULL)
813 flag.regularstart = false;
816 /* xshogi position file ... */
817 fgets(fname, 256, fd);
822 if (strcmp(fname, "xshogi"))
826 /* -- empty line -- */
827 fgets(fname, 256, fd);
828 /* -- empty line -- */
829 fgets(fname, 256, fd);
831 for (i = NO_ROWS - 1; i > -1; i--)
833 fgets(fname, 256, fd);
836 for (j = 0; j < NO_COLS; j++)
838 sq = i * NO_COLS + j;
844 board[sq] = no_piece;
849 for (c = 0; c < NO_PIECES; c++)
854 board[sq] = promoted[c];
856 board[sq] = unpromoted[c];
862 for (c = 0; c < NO_PIECES; c++)
867 board[sq] = promoted[c];
869 board[sq] = unpromoted[c];
882 for (side = 0; side <= 1; side++)
884 fgets(fname, 256, fd);
886 Captured[side][pawn] = atoi(InPtr);
889 Captured[side][lance] = atoi(InPtr);
891 Captured[side][knight] = atoi(InPtr);
894 Captured[side][silver] = atoi(InPtr);
896 Captured[side][gold] = atoi(InPtr);
898 Captured[side][bishop] = atoi(InPtr);
900 Captured[side][rook] = atoi(InPtr);
902 Captured[side][king] = atoi(InPtr);
905 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
918 UpdateDisplay(0, 0, 1, 0);
933 ShowMessage("Enter file name: ");
934 RequestInputString(fname, sizeof(fname)-1);
936 if (fname[0] == '\0')
937 strcpy(fname, "xshogi.position.read");
939 if ((fd = fopen(fname, "w")) != NULL)
941 fputs("# xshogi position file -- \n", fd);
945 for (i = NO_ROWS - 1; i > -1; i--)
949 for (j = 0; j < NO_COLS; j++)
951 sq = i * NO_COLS + j;
953 isp = is_promoted[piece];
954 *p = (isp ? '+' : ' ');
957 if (piece == no_piece)
959 else if (color[sq] == white)
972 for (side = 0; side <= 1; side++)
976 "%d %d %d %d %d %d %d %d\n",
978 "%d %d %d %d %d %d\n",
980 Captured[side][pawn],
982 Captured[side][lance],
983 Captured[side][knight],
985 Captured[side][silver],
986 Captured[side][gold],
987 Captured[side][bishop],
988 Captured[side][rook],
989 Captured[side][king]);
994 if (computer == black)
995 fputs("white to play\n", fd);
997 fputs("black to play\n", fd);
1008 char fname[256], sflags[4];
1012 strcpy(fname, savefile);
1014 /* Enter file name */
1015 ShowMessage("Enter file name: ");
1016 RequestInputString(fname, sizeof(fname)-1);
1019 if (fname[0] == '\0')
1022 if ((fd = fopen(fname, "a")) != NULL)
1026 for (i = 1; i <= GameCnt; i++)
1028 struct GameRec *g = &GameList[i];
1029 char mvnr[20], mvs[20];
1032 sprintf(mvnr, "%d.", (i + 1)/2);
1037 t = (g->gmove & 0xFF);
1038 algbr(f, t, g->flags);
1041 /* determine move quality string */
1042 if (g->flags & goodmove)
1045 if (g->flags & badmove)
1048 #ifdef EASY_OPENINGS
1049 if (g->flags & difficult)
1055 /* determine move string */
1058 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1062 sprintf(mvs, "%c%c%c%c%c%s%s ",
1063 mvstr[0][0], mvstr[0][1],
1064 (g->flags & capture) ? 'x' : '-',
1065 mvstr[0][2], mvstr[0][3],
1066 (mvstr[0][4] == '+') ? "+" : "",
1070 fprintf(fd, "%s%s%c%s",
1074 : (is_promoted[g->fpiece] ? "+" : "")),
1087 ShowMessage("Game saved");
1091 ShowMessage("Could not open file");
1103 char fname[256], dbuf[256];
1107 strcpy(fname, listfile);
1112 strncpy(dbuf, ctime(&when), 20);
1119 /* use format "CL.Jan01-020304B" when
1122 program played white */
1124 sprintf(fname, "CL.%s%s-%s%s%s%c",
1125 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1126 dbuf + 17, ColorStr[computer][0]);
1128 /* replace space padding with 0 */
1129 for (i = 0; fname[i] != '\0'; i++)
1131 if (fname[i] == ' ')
1136 fd = fopen(fname, "w");
1140 printf("Open failure for file: %s", fname);
1144 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1145 fputs(" score depth nodes time ", fd);
1146 fputs(" score depth nodes time\n", fd);
1148 for (i = 1; i <= GameCnt; i++)
1150 f = GameList[i].gmove >> 8;
1151 t = (GameList[i].gmove & 0xFF);
1152 algbr(f, t, GameList[i].flags);
1154 if (GameList[i].flags & book)
1156 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1159 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1160 pxx[GameList[i].fpiece],
1162 ? &mvstr[0][1] : mvstr[0]),
1169 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1172 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1173 pxx[GameList[i].fpiece],
1174 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1175 GameList[i].score, GameList[i].depth,
1176 GameList[i].nodes, GameList[i].time);
1189 fprintf(fd, "\n\n");
1191 if (GameList[GameCnt].flags & draw)
1193 fprintf(fd, "Draw %s\n", DRAW);
1195 if (DRAW == DRAW_REPETITION)
1199 fprintf(fd, "repetition by positions ");
1201 for (j = GameCnt - 1; j >= Game50; j -= 2)
1203 if (GameList[j].hashkey == hashkey &&
1204 GameList[j].hashbd == hashbd)
1205 fprintf(fd, "%d ", j);
1211 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1213 fprintf(fd, "%s\n", ColorStr[player ]);
1215 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1217 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1231 GameList[GameCnt].flags |= badmove;
1235 GameList[GameCnt].flags |= goodmove;
1238 #ifdef EASY_OPENINGS
1240 GameList[GameCnt].flags |= difficult;
1250 * Undo the most recent half-move.
1258 f = GameList[GameCnt].gmove >> 8;
1259 t = GameList[GameCnt].gmove & 0x7F;
1263 /* the move was a drop */
1264 Captured[color[t]][board[t]]++;
1265 board[t] = no_piece;
1271 if (GameList[GameCnt].flags & promote)
1272 board[f] = unpromoted[board[t]];
1274 board[f] = board[t];
1276 color[f] = color[t];
1277 board[t] = GameList[GameCnt].piece;
1278 color[t] = GameList[GameCnt].color;
1280 if (board[t] != no_piece)
1281 Captured[color[f]][unpromoted[board[t]]]--;
1283 if (color[t] != neutral)
1291 if (TCflag && (TCmoves > 1))
1292 ++TimeControl.moves[color[f]];
1294 hashkey = GameList[GameCnt].hashkey;
1295 hashbd = GameList[GameCnt].hashbd;
1297 computer = computer ^ 1;
1298 opponent = opponent ^ 1;
1301 player = player ^ 1;
1303 UpdateDisplay(0, 0, 1, 0);
1305 if (flag.regularstart)
1312 FlagString(unsigned short flags, char *s)
1317 if (flags & promote)
1318 strcat(s, " promote");
1320 if (flags & dropmask)
1321 strcat(s, " drop:");
1323 if ((piece = (flags & pmask)))
1327 if (is_promoted[piece])
1330 s[l++] = pxx[piece];
1334 if (flags & capture)
1335 strcat(s, " capture");
1338 strcat(s, " exact");
1341 strcat(s, " tesuji");
1344 strcat(s, " check");
1350 strcat(s, " stupid");
1352 if (flags & questionable)
1353 strcat(s, " questionable");
1355 if (flags & kingattack)
1356 strcat(s, " kingattack");
1365 TestSpeed(void(*f)(short side, short ply,
1366 short in_check, short blockable),
1376 #ifdef HAVE_GETTIMEOFDAY
1380 #ifdef HAVE_GETTIMEOFDAY
1381 gettimeofday(&tv, NULL);
1382 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1387 for (i = 0; i < j; i++)
1389 f(opponent, 2, -1, true);
1392 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1394 if (!pick(jj, TrPnt[3] - 1))
1400 #ifdef HAVE_GETTIMEOFDAY
1401 gettimeofday(&tv, NULL);
1402 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1407 cnt = j * (TrPnt[3] - TrPnt[2]);
1420 TestPSpeed(short(*f) (short side), unsigned j)
1424 #ifdef HAVE_GETTIMEOFDAY
1428 #ifdef HAVE_GETTIMEOFDAY
1429 gettimeofday(&tv, NULL);
1430 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1435 for (i = 0; i < j; i++)
1438 #ifdef HAVE_GETTIMEOFDAY
1439 gettimeofday(&tv, NULL);
1440 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1464 time = &s[strlen("otime")];
1465 t = (int)strtol(time, &time, 10);
1470 sec = (int)strtol(time, &time, 10);
1473 m = (int)strtol(time, &time, 10);
1476 TimeControl.clock[opponent] = t;
1479 TimeControl.moves[opponent] = m;
1481 ElapsedTime(COMPUTE_AND_INIT_MODE);
1485 /* just to inform xshogi about availability of otime command */
1486 printf("otime %d %d\n", t, m);
1493 SetMachineTime(char *s)
1498 time = &s[strlen("time")];
1500 t = (int)strtol(time, &time, 10);
1505 sec = (int)strtol(time, &time, 10);
1508 m = (int)strtol(time, &time, 10);
1511 TimeControl.clock[computer] = t;
1514 TimeControl.moves[computer] = m;
1516 ElapsedTime(COMPUTE_AND_INIT_MODE);
1520 /* just to inform xshogi about availability of time command */
1521 printf("time %d %d\n", t, m);
1529 /* FIXME! This is truly the function from hell! */
1532 * Process the user's command. If easy mode is OFF (the computer is thinking
1533 * on opponents time) and the program is out of book, then make the 'hint'
1534 * move on the board and call SelectMove() to find a response. The user
1535 * terminates the search by entering ^C (quit siqnal) before entering a
1536 * command. If the opponent does not make the hint move, then set Sdepth to
1541 InputCommand(char *command)
1544 short have_shown_prompt = false;
1545 short ok, done, is_move = false;
1549 ok = flag.quit = done = false;
1553 if (TTadd > ttbllimit)
1557 if ((hint > 0) && !flag.easy && !flag.force)
1560 * A hint move for the player is available. Compute a move for the
1561 * opponent in background mode assuming that the hint move will be
1562 * selected by the player.
1565 ft = time0; /* Save reference time for the player. */
1567 algbr((short) hint >> 8, (short) hint & 0xff, false);
1568 strcpy(s, mvstr[0]);
1575 /* do the hint move */
1576 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1580 #ifdef QUIETBACKGROUND
1582 have_shown_prompt = true;
1583 #endif /* QUIETBACKGROUND */
1585 /* Start computing a move until the search is interrupted. */
1587 #ifdef INTERRUPT_TEST
1591 /* would love to put null move in here */
1592 /* after we make the hint move make a 2 ply search
1593 * with both plys our moves */
1594 /* think on opponents time */
1595 SelectMove(computer, BACKGROUND_MODE);
1597 #ifdef INTERRUPT_TEST
1598 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1602 printf("searching not terminated by interrupt!\n");
1606 printf("elapsed time from interrupt to "
1607 "terminating search: %ld\n", it);
1611 /* undo the hint and carry on */
1612 VerifyMove(s, UNMAKE_MODE, &mv);
1616 time0 = ft; /* Restore reference time for the player. */
1619 while(!(ok || flag.quit || done))
1623 #ifdef QUIETBACKGROUND
1624 if (!have_shown_prompt)
1626 #endif /* QUIETBACKGROUND */
1630 #ifdef QUIETBACKGROUND
1633 have_shown_prompt = false;
1634 #endif /* QUIETBACKGROUND */
1636 if (command == NULL) {
1640 eof = GetString(sx);
1642 strcpy(sx, command);
1646 sscanf(sx, "%s", s);
1654 if (strcmp(s, "bd") == 0) /* bd -- display board */
1656 /* FIXME: Hack alert! */
1657 short old_xshogi = XSHOGI;
1660 display_type = DISPLAY_RAW;
1663 UpdateDisplay(0, 0, 1, 0);
1666 display_type = DISPLAY_X;
1668 else if (strcmp(s, "post") == 0)
1670 flag.post = !flag.post;
1672 else if (strcmp(s, "alg") == 0)
1674 /* noop */ ; /* alg */
1676 else if ((strcmp(s, "quit") == 0)
1677 || (strcmp(s, "exit") == 0))
1682 else if (strcmp(s, "post") == 0)
1684 flag.post = !flag.post;
1687 else if ((strcmp(s, "set") == 0)
1688 || (strcmp(s, "edit") == 0))
1692 else if ((strcmp(s, "setup") == 0))
1696 else if (strcmp(s, "first") == 0)
1700 else if (strcmp(s, "go") == 0)
1705 if (computer == black)
1716 else if (strcmp(s, "help") == 0)
1720 else if (strcmp(s, "material") == 0)
1722 flag.material = !flag.material;
1724 else if (strcmp(s, "force") == 0)
1729 flag.bothsides = false;
1733 flag.force = !flag.force;
1734 flag.bothsides = false;
1737 else if (strcmp(s, "book") == 0)
1739 Book = Book ? 0 : BOOKFAIL;
1741 else if (strcmp(s, "new") == 0)
1744 UpdateDisplay(0, 0, 1, 0);
1746 else if (strcmp(s, "list") == 0)
1750 else if ((strcmp(s, "level") == 0)
1751 || (strcmp(s, "clock") == 0))
1755 else if (strcmp(s, "hash") == 0)
1757 flag.hash = !flag.hash;
1759 else if (strcmp(s, "gamein") == 0)
1761 flag.gamein = !flag.gamein;
1763 else if (strcmp(s, "beep") == 0)
1765 flag.beep = !flag.beep;
1767 else if (strcmp(s, "time") == 0)
1771 else if (strcmp(s, "otime") == 0)
1775 else if (strcmp(s, "Awindow") == 0)
1777 ChangeAlphaWindow();
1779 else if (strcmp(s, "Bwindow") == 0)
1783 else if (strcmp(s, "rcptr") == 0)
1785 flag.rcptr = !flag.rcptr;
1787 else if (strcmp(s, "hint") == 0)
1791 else if (strcmp(s, "both") == 0)
1793 flag.bothsides = !flag.bothsides;
1796 ElapsedTime(COMPUTE_AND_INIT_MODE);
1797 SelectMove(opponent, FOREGROUND_MODE);
1800 else if (strcmp(s, "reverse") == 0)
1802 flag.reverse = !flag.reverse;
1804 UpdateDisplay(0, 0, 1, 0);
1806 else if (strcmp(s, "switch") == 0)
1808 computer = computer ^ 1;
1809 opponent = opponent ^ 1;
1810 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1815 else if (strcmp(s, "black") == 0)
1824 * ok = true; don't automatically start with black command
1827 else if (strcmp(s, "white") == 0)
1836 * ok = true; don't automatically start with white command
1839 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1843 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1848 /* CHECKME: are these next three correct? */
1849 else if (!XSHOGI && strcmp(s, "xget") == 0)
1853 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1857 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1861 #ifdef EASY_OPENINGS
1862 else if ((strcmp(s, "?") == 0)
1863 || (strcmp(s, "!") == 0)
1864 || (strcmp(s, "~") == 0))
1866 else if ((strcmp(s, "?") == 0)
1867 || (strcmp(s, "!") == 0))
1872 else if (strcmp(s, "get") == 0)
1876 else if (strcmp(s, "save") == 0)
1880 else if (strcmp(s, "depth") == 0)
1882 ChangeSearchDepth();
1884 else if (strcmp(s, "hashdepth") == 0)
1888 else if (strcmp(s, "random") == 0)
1892 else if (strcmp(s, "hard") == 0)
1896 else if (strcmp(s, "easy") == 0)
1898 flag.easy = !flag.easy;
1900 else if (strcmp(s, "tsume") == 0)
1902 flag.tsume = !flag.tsume;
1904 else if (strcmp(s, "contempt") == 0)
1908 else if (strcmp(s, "xwndw") == 0)
1912 else if (strcmp(s, "rv") == 0)
1915 UpdateDisplay(0, 0, 1, 0);
1917 else if (strcmp(s, "coords") == 0)
1919 flag.coords = !flag.coords;
1920 UpdateDisplay(0, 0, 1, 0);
1922 else if (strcmp(s, "stars") == 0)
1924 flag.stars = !flag.stars;
1925 UpdateDisplay(0, 0, 1, 0);
1927 else if (!XSHOGI && strcmp(s, "moves") == 0)
1934 extern unsigned short PrVar[MAXDEPTH];
1936 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1943 ShowMessage("Testing MoveList Speed");
1944 temp = generate_move_flags;
1945 generate_move_flags = true;
1946 TestSpeed(MoveList, 1);
1947 generate_move_flags = temp;
1948 ShowMessage("Testing CaptureList Speed");
1949 TestSpeed(CaptureList, 1);
1950 ShowMessage("Testing Eval Speed");
1951 ExaminePosition(opponent);
1952 TestPSpeed(ScorePosition, 1);
1954 else if (!XSHOGI && strcmp(s, "test") == 0)
1957 ShowMessage("Testing MoveList Speed");
1958 TestSpeed(MoveList, 2000);
1959 ShowMessage("Testing CaptureList Speed");
1960 TestSpeed(CaptureList, 3000);
1961 ShowMessage("Testing Eval Speed");
1962 ExaminePosition(opponent);
1963 TestPSpeed(ScorePosition, 1500);
1965 ShowMessage("Testing MoveList Speed");
1966 TestSpeed(MoveList, 20000);
1967 ShowMessage("Testing CaptureList Speed");
1968 TestSpeed(CaptureList, 30000);
1969 ShowMessage("Testing Eval Speed");
1970 ExaminePosition(opponent);
1971 TestPSpeed(ScorePosition, 15000);
1974 else if (!XSHOGI && strcmp(s, "p") == 0)
1978 else if (!XSHOGI && strcmp(s, "debug") == 0)
1988 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
1990 /* check for repetition */
1991 short rpt = repetition();
1995 DRAW = DRAW_REPETITION;
1997 GameList[GameCnt].flags |= draw;
2011 ElapsedTime(COMPUTE_AND_INIT_MODE);
2015 computer = opponent;
2016 opponent = computer ^ 1;
2021 /* add remaining time in milliseconds for xshogi */
2024 printf("%d. %s %ld\n",
2025 ++mycnt2, s, TimeControl.clock[player] * 10);
2028 #ifdef notdef /* optional pass best line to frontend with move */
2029 # if !defined NOPOST
2031 if (flag.post && !flag.mate)
2035 printf(" %6d ", MSCORE);
2037 for (i = 1; MV[i] > 0; i++)
2039 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2040 printf("%5s ", mvstr[0]);
2053 SetTimeControl(void)
2057 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2058 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2059 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2063 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2064 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2067 flag.onemove = (TCmoves == 1);
2069 ElapsedTime(COMPUTE_AND_INIT_MODE);