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);
545 Captured[side][lance] = atoi(InPtr);
547 Captured[side][knight] = atoi(InPtr);
549 Captured[side][silver] = atoi(InPtr);
551 Captured[side][gold] = atoi(InPtr);
553 Captured[side][bishop] = atoi(InPtr);
555 Captured[side][rook] = atoi(InPtr);
557 Captured[side][king] = atoi(InPtr);
561 flag.regularstart = true;
563 fgets(fname, 256, fd); /* empty */
564 fgets(fname, 256, fd); /* move score ... */
566 while (fgets(fname, 256, fd))
575 g = &GameList[GameCnt];
576 g->gmove = parser(InPtr, side, &g->fpiece);
578 g->score = atoi(InPtr);
580 g->depth = atoi(InPtr);
582 g->nodes = atol(InPtr);
584 g->time = atol(InPtr);
586 g->flags = c = atoi(InPtr);
588 g->hashkey = strtol(InPtr, (char **) NULL, 16);
590 g->hashbd = strtol(InPtr, (char **) NULL, 16);
598 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
600 if (pxx[i] == *InPtr)
608 g->color = ((*InPtr == 'W') ? white : black);
610 g->piece = (*InPtr == '+'
612 : unpromoted[piece]);
621 if (TimeControl.clock[black] > 0)
629 UpdateDisplay(0, 0, 1, 0);
641 short sq, i, c, f, t;
644 char empty[2] = "\n";
647 strcpy(fname, savefile);
649 ShowMessage("Enter file name: ");
650 RequestInputString(fname, sizeof(fname)-1);
653 if (fname[0] == '\0')
654 strcpy(fname, "shogi.000");
656 if ((fd = fopen(fname, "w")) != NULL)
661 if (computer == white)
664 if (computer == black)
667 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
668 flag.force ? "force" : "");
670 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
671 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
672 TimeControl.clock[black], TimeControl.moves[black],
673 TimeControl.clock[white], TimeControl.moves[white]);
676 for (i = NO_ROWS - 1; i > -1; i--)
678 fprintf(fd, "%c ", ROW_NAME(i));
680 for (c = 0; c < NO_COLS; c++)
682 sq = i * NO_COLS + c;
684 p = is_promoted[piece] ? '+' : ' ';
685 fprintf(fd, "%c", p);
701 fprintf(fd, "%c", p);
706 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
707 fprintf(fd, " %d", Mvboard[f]);
713 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n"); /* FIXME */
715 fprintf(fd, " p l n s g b r k\n");
717 for (side = 0; side <= 1; side++)
719 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
720 fprintf(fd, " %2d", Captured[side][pawn]);
721 fprintf(fd, " %2d", Captured[side][lance]);
722 fprintf(fd, " %2d", Captured[side][knight]);
723 fprintf(fd, " %2d", Captured[side][silver]);
724 fprintf(fd, " %2d", Captured[side][gold]);
725 fprintf(fd, " %2d", Captured[side][bishop]);
726 fprintf(fd, " %2d", Captured[side][rook]);
727 fprintf(fd, " %2d", Captured[side][king]);
732 fputs(" move score depth nodes time flags capture\n", fd);
734 for (i = 1; i <= GameCnt; i++)
736 struct GameRec *g = &GameList[i];
739 t = (g->gmove & 0xFF);
740 algbr(f, t, g->flags);
742 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
745 : (is_promoted[g->fpiece] ? '+' : ' ')),
747 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
749 g->nodes, g->time, g->flags,
750 g->hashkey, g->hashbd);
752 if (g->piece != no_piece)
754 fprintf(fd, " %c %s %c\n",
755 pxx[g->piece], ColorStr[g->color],
756 (is_promoted[g->piece] ? '+' : ' '));
766 ShowMessage("Game saved");
770 ShowMessage("Could not open file");
777 * GetXGame, SaveXGame and BookGame used to only be defined if
778 * xshogi wasn't defined -- wonder why?
790 ShowMessage("Enter file name: ");
791 RequestInputString(fname, sizeof(fname)-1);
793 if (fname[0] == '\0')
794 strcpy(fname, "xshogi.position.read");
796 if ((fd = fopen(fname, "r")) != NULL)
799 flag.regularstart = false;
802 /* xshogi position file ... */
803 fgets(fname, 256, fd);
808 if (strcmp(fname, "xshogi"))
812 /* -- empty line -- */
813 fgets(fname, 256, fd);
814 /* -- empty line -- */
815 fgets(fname, 256, fd);
817 for (i = NO_ROWS - 1; i > -1; i--)
819 fgets(fname, 256, fd);
822 for (j = 0; j < NO_COLS; j++)
824 sq = i * NO_COLS + j;
830 board[sq] = no_piece;
835 for (c = 0; c < NO_PIECES; c++)
840 board[sq] = promoted[c];
842 board[sq] = unpromoted[c];
848 for (c = 0; c < NO_PIECES; c++)
853 board[sq] = promoted[c];
855 board[sq] = unpromoted[c];
868 for (side = 0; side <= 1; side++)
870 fgets(fname, 256, fd);
872 Captured[side][pawn] = atoi(InPtr);
874 Captured[side][lance] = atoi(InPtr);
876 Captured[side][knight] = atoi(InPtr);
878 Captured[side][silver] = atoi(InPtr);
880 Captured[side][gold] = atoi(InPtr);
882 Captured[side][bishop] = atoi(InPtr);
884 Captured[side][rook] = atoi(InPtr);
886 Captured[side][king] = atoi(InPtr);
889 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
902 UpdateDisplay(0, 0, 1, 0);
917 ShowMessage("Enter file name: ");
918 RequestInputString(fname, sizeof(fname)-1);
920 if (fname[0] == '\0')
921 strcpy(fname, "xshogi.position.read");
923 if ((fd = fopen(fname, "w")) != NULL)
925 fputs("# xshogi position file -- \n", fd);
929 for (i = NO_ROWS - 1; i > -1; i--)
933 for (j = 0; j < NO_COLS; j++)
935 sq = i * NO_COLS + j;
937 isp = is_promoted[piece];
938 *p = (isp ? '+' : ' ');
941 if (piece == no_piece)
943 else if (color[sq] == white)
956 for (side = 0; side <= 1; side++)
958 sprintf(fname, "%d %d %d %d %d %d %d %d\n",
959 Captured[side][pawn],
960 Captured[side][lance],
961 Captured[side][knight],
962 Captured[side][silver],
963 Captured[side][gold],
964 Captured[side][bishop],
965 Captured[side][rook],
966 Captured[side][king]);
971 if (computer == black)
972 fputs("white to play\n", fd);
974 fputs("black to play\n", fd);
985 char fname[256], sflags[4];
989 strcpy(fname, savefile);
991 /* Enter file name */
992 ShowMessage("Enter file name: ");
993 RequestInputString(fname, sizeof(fname)-1);
996 if (fname[0] == '\0')
999 if ((fd = fopen(fname, "a")) != NULL)
1003 for (i = 1; i <= GameCnt; i++)
1005 struct GameRec *g = &GameList[i];
1006 char mvnr[20], mvs[20];
1009 sprintf(mvnr, "%d.", (i + 1)/2);
1014 t = (g->gmove & 0xFF);
1015 algbr(f, t, g->flags);
1018 /* determine move quality string */
1019 if (g->flags & goodmove)
1022 if (g->flags & badmove)
1025 #ifdef EASY_OPENINGS
1026 if (g->flags & difficult)
1032 /* determine move string */
1035 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1039 sprintf(mvs, "%c%c%c%c%c%s%s ",
1040 mvstr[0][0], mvstr[0][1],
1041 (g->flags & capture) ? 'x' : '-',
1042 mvstr[0][2], mvstr[0][3],
1043 (mvstr[0][4] == '+') ? "+" : "",
1047 fprintf(fd, "%s%s%c%s",
1051 : (is_promoted[g->fpiece] ? "+" : "")),
1064 ShowMessage("Game saved");
1068 ShowMessage("Could not open file");
1080 char fname[256], dbuf[256];
1084 strcpy(fname, listfile);
1089 strncpy(dbuf, ctime(&when), 20);
1096 /* use format "CL.Jan01-020304B" when
1099 program played white */
1101 sprintf(fname, "CL.%s%s-%s%s%s%c",
1102 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1103 dbuf + 17, ColorStr[computer][0]);
1105 /* replace space padding with 0 */
1106 for (i = 0; fname[i] != '\0'; i++)
1108 if (fname[i] == ' ')
1113 fd = fopen(fname, "w");
1117 printf("Open failure for file: %s", fname);
1121 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1122 fputs(" score depth nodes time ", fd);
1123 fputs(" score depth nodes time\n", fd);
1125 for (i = 1; i <= GameCnt; i++)
1127 f = GameList[i].gmove >> 8;
1128 t = (GameList[i].gmove & 0xFF);
1129 algbr(f, t, GameList[i].flags);
1131 if (GameList[i].flags & book)
1133 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1136 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1137 pxx[GameList[i].fpiece],
1139 ? &mvstr[0][1] : mvstr[0]),
1146 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1149 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1150 pxx[GameList[i].fpiece],
1151 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1152 GameList[i].score, GameList[i].depth,
1153 GameList[i].nodes, GameList[i].time);
1166 fprintf(fd, "\n\n");
1168 if (GameList[GameCnt].flags & draw)
1170 fprintf(fd, "Draw %s\n", DRAW);
1172 if (DRAW == DRAW_REPETITION)
1176 fprintf(fd, "repetition by positions ");
1178 for (j = GameCnt - 1; j >= Game50; j -= 2)
1180 if (GameList[j].hashkey == hashkey &&
1181 GameList[j].hashbd == hashbd)
1182 fprintf(fd, "%d ", j);
1188 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1190 fprintf(fd, "%s\n", ColorStr[player ]);
1192 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1194 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1208 GameList[GameCnt].flags |= badmove;
1212 GameList[GameCnt].flags |= goodmove;
1215 #ifdef EASY_OPENINGS
1217 GameList[GameCnt].flags |= difficult;
1227 * Undo the most recent half-move.
1235 f = GameList[GameCnt].gmove >> 8;
1236 t = GameList[GameCnt].gmove & 0x7F;
1240 /* the move was a drop */
1241 Captured[color[t]][board[t]]++;
1242 board[t] = no_piece;
1248 if (GameList[GameCnt].flags & promote)
1249 board[f] = unpromoted[board[t]];
1251 board[f] = board[t];
1253 color[f] = color[t];
1254 board[t] = GameList[GameCnt].piece;
1255 color[t] = GameList[GameCnt].color;
1257 if (board[t] != no_piece)
1258 Captured[color[f]][unpromoted[board[t]]]--;
1260 if (color[t] != neutral)
1268 if (TCflag && (TCmoves > 1))
1269 ++TimeControl.moves[color[f]];
1271 hashkey = GameList[GameCnt].hashkey;
1272 hashbd = GameList[GameCnt].hashbd;
1274 computer = computer ^ 1;
1275 opponent = opponent ^ 1;
1278 player = player ^ 1;
1280 UpdateDisplay(0, 0, 1, 0);
1282 if (flag.regularstart)
1289 FlagString(unsigned short flags, char *s)
1294 if (flags & promote)
1295 strcat(s, " promote");
1297 if (flags & dropmask)
1298 strcat(s, " drop:");
1300 if ((piece = (flags & pmask)))
1304 if (is_promoted[piece])
1307 s[l++] = pxx[piece];
1311 if (flags & capture)
1312 strcat(s, " capture");
1315 strcat(s, " exact");
1318 strcat(s, " tesuji");
1321 strcat(s, " check");
1327 strcat(s, " stupid");
1329 if (flags & questionable)
1330 strcat(s, " questionable");
1332 if (flags & kingattack)
1333 strcat(s, " kingattack");
1342 TestSpeed(void(*f)(short side, short ply,
1343 short in_check, short blockable),
1353 #ifdef HAVE_GETTIMEOFDAY
1357 #ifdef HAVE_GETTIMEOFDAY
1358 gettimeofday(&tv, NULL);
1359 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1364 for (i = 0; i < j; i++)
1366 f(opponent, 2, -1, true);
1369 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1371 if (!pick(jj, TrPnt[3] - 1))
1377 #ifdef HAVE_GETTIMEOFDAY
1378 gettimeofday(&tv, NULL);
1379 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1384 cnt = j * (TrPnt[3] - TrPnt[2]);
1397 TestPSpeed(short(*f) (short side), unsigned j)
1401 #ifdef HAVE_GETTIMEOFDAY
1405 #ifdef HAVE_GETTIMEOFDAY
1406 gettimeofday(&tv, NULL);
1407 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1412 for (i = 0; i < j; i++)
1415 #ifdef HAVE_GETTIMEOFDAY
1416 gettimeofday(&tv, NULL);
1417 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1441 time = &s[strlen("otime")];
1442 t = (int)strtol(time, &time, 10);
1447 sec = (int)strtol(time, &time, 10);
1450 m = (int)strtol(time, &time, 10);
1453 TimeControl.clock[opponent] = t;
1456 TimeControl.moves[opponent] = m;
1458 ElapsedTime(COMPUTE_AND_INIT_MODE);
1462 /* just to inform xshogi about availability of otime command */
1463 printf("otime %d %d\n", t, m);
1470 SetMachineTime(char *s)
1475 time = &s[strlen("time")];
1477 t = (int)strtol(time, &time, 10);
1482 sec = (int)strtol(time, &time, 10);
1485 m = (int)strtol(time, &time, 10);
1488 TimeControl.clock[computer] = t;
1491 TimeControl.moves[computer] = m;
1493 ElapsedTime(COMPUTE_AND_INIT_MODE);
1497 /* just to inform xshogi about availability of time command */
1498 printf("time %d %d\n", t, m);
1506 /* FIXME! This is truly the function from hell! */
1509 * Process the user's command. If easy mode is OFF (the computer is thinking
1510 * on opponents time) and the program is out of book, then make the 'hint'
1511 * move on the board and call SelectMove() to find a response. The user
1512 * terminates the search by entering ^C (quit siqnal) before entering a
1513 * command. If the opponent does not make the hint move, then set Sdepth to
1518 InputCommand(char *command)
1521 short have_shown_prompt = false;
1522 short ok, done, is_move = false;
1526 ok = flag.quit = done = false;
1530 if (TTadd > ttbllimit)
1534 if ((hint > 0) && !flag.easy && !flag.force)
1537 * A hint move for the player is available. Compute a move for the
1538 * opponent in background mode assuming that the hint move will be
1539 * selected by the player.
1542 ft = time0; /* Save reference time for the player. */
1544 algbr((short) hint >> 8, (short) hint & 0xff, false);
1545 strcpy(s, mvstr[0]);
1552 /* do the hint move */
1553 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1557 #ifdef QUIETBACKGROUND
1559 have_shown_prompt = true;
1560 #endif /* QUIETBACKGROUND */
1562 /* Start computing a move until the search is interrupted. */
1564 #ifdef INTERRUPT_TEST
1568 /* would love to put null move in here */
1569 /* after we make the hint move make a 2 ply search
1570 * with both plys our moves */
1571 /* think on opponents time */
1572 SelectMove(computer, BACKGROUND_MODE);
1574 #ifdef INTERRUPT_TEST
1575 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1579 printf("searching not terminated by interrupt!\n");
1583 printf("elapsed time from interrupt to "
1584 "terminating search: %ld\n", it);
1588 /* undo the hint and carry on */
1589 VerifyMove(s, UNMAKE_MODE, &mv);
1593 time0 = ft; /* Restore reference time for the player. */
1596 while(!(ok || flag.quit || done))
1600 #ifdef QUIETBACKGROUND
1601 if (!have_shown_prompt)
1603 #endif /* QUIETBACKGROUND */
1607 #ifdef QUIETBACKGROUND
1610 have_shown_prompt = false;
1611 #endif /* QUIETBACKGROUND */
1613 if (command == NULL) {
1617 eof = GetString(sx);
1619 strcpy(sx, command);
1623 sscanf(sx, "%s", s);
1631 if (strcmp(s, "bd") == 0) /* bd -- display board */
1633 /* FIXME: Hack alert! */
1634 short old_xshogi = XSHOGI;
1637 display_type = DISPLAY_RAW;
1640 UpdateDisplay(0, 0, 1, 0);
1643 display_type = DISPLAY_X;
1645 else if (strcmp(s, "post") == 0)
1647 flag.post = !flag.post;
1649 else if (strcmp(s, "alg") == 0)
1651 /* noop */ ; /* alg */
1653 else if ((strcmp(s, "quit") == 0)
1654 || (strcmp(s, "exit") == 0))
1659 else if (strcmp(s, "post") == 0)
1661 flag.post = !flag.post;
1664 else if ((strcmp(s, "set") == 0)
1665 || (strcmp(s, "edit") == 0))
1669 else if ((strcmp(s, "setup") == 0))
1673 else if (strcmp(s, "first") == 0)
1677 else if (strcmp(s, "go") == 0)
1682 if (computer == black)
1693 else if (strcmp(s, "help") == 0)
1697 else if (strcmp(s, "material") == 0)
1699 flag.material = !flag.material;
1701 else if (strcmp(s, "force") == 0)
1706 flag.bothsides = false;
1710 flag.force = !flag.force;
1711 flag.bothsides = false;
1714 else if (strcmp(s, "book") == 0)
1716 Book = Book ? 0 : BOOKFAIL;
1718 else if (strcmp(s, "new") == 0)
1721 UpdateDisplay(0, 0, 1, 0);
1723 else if (strcmp(s, "list") == 0)
1727 else if ((strcmp(s, "level") == 0)
1728 || (strcmp(s, "clock") == 0))
1732 else if (strcmp(s, "hash") == 0)
1734 flag.hash = !flag.hash;
1736 else if (strcmp(s, "gamein") == 0)
1738 flag.gamein = !flag.gamein;
1740 else if (strcmp(s, "beep") == 0)
1742 flag.beep = !flag.beep;
1744 else if (strcmp(s, "time") == 0)
1748 else if (strcmp(s, "otime") == 0)
1752 else if (strcmp(s, "Awindow") == 0)
1754 ChangeAlphaWindow();
1756 else if (strcmp(s, "Bwindow") == 0)
1760 else if (strcmp(s, "rcptr") == 0)
1762 flag.rcptr = !flag.rcptr;
1764 else if (strcmp(s, "hint") == 0)
1768 else if (strcmp(s, "both") == 0)
1770 flag.bothsides = !flag.bothsides;
1773 ElapsedTime(COMPUTE_AND_INIT_MODE);
1774 SelectMove(opponent, FOREGROUND_MODE);
1777 else if (strcmp(s, "reverse") == 0)
1779 flag.reverse = !flag.reverse;
1781 UpdateDisplay(0, 0, 1, 0);
1783 else if (strcmp(s, "switch") == 0)
1785 computer = computer ^ 1;
1786 opponent = opponent ^ 1;
1787 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1792 else if (strcmp(s, "black") == 0)
1801 * ok = true; don't automatically start with black command
1804 else if (strcmp(s, "white") == 0)
1813 * ok = true; don't automatically start with white command
1816 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1820 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1825 /* CHECKME: are these next three correct? */
1826 else if (!XSHOGI && strcmp(s, "xget") == 0)
1830 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1834 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1838 #ifdef EASY_OPENINGS
1839 else if ((strcmp(s, "?") == 0)
1840 || (strcmp(s, "!") == 0)
1841 || (strcmp(s, "~") == 0))
1843 else if ((strcmp(s, "?") == 0)
1844 || (strcmp(s, "!") == 0))
1849 else if (strcmp(s, "get") == 0)
1853 else if (strcmp(s, "save") == 0)
1857 else if (strcmp(s, "depth") == 0)
1859 ChangeSearchDepth();
1861 else if (strcmp(s, "hashdepth") == 0)
1865 else if (strcmp(s, "random") == 0)
1869 else if (strcmp(s, "hard") == 0)
1873 else if (strcmp(s, "easy") == 0)
1875 flag.easy = !flag.easy;
1877 else if (strcmp(s, "tsume") == 0)
1879 flag.tsume = !flag.tsume;
1881 else if (strcmp(s, "contempt") == 0)
1885 else if (strcmp(s, "xwndw") == 0)
1889 else if (strcmp(s, "rv") == 0)
1892 UpdateDisplay(0, 0, 1, 0);
1894 else if (strcmp(s, "coords") == 0)
1896 flag.coords = !flag.coords;
1897 UpdateDisplay(0, 0, 1, 0);
1899 else if (strcmp(s, "stars") == 0)
1901 flag.stars = !flag.stars;
1902 UpdateDisplay(0, 0, 1, 0);
1904 else if (!XSHOGI && strcmp(s, "moves") == 0)
1911 extern unsigned short PrVar[MAXDEPTH];
1913 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1920 ShowMessage("Testing MoveList Speed");
1921 temp = generate_move_flags;
1922 generate_move_flags = true;
1923 TestSpeed(MoveList, 1);
1924 generate_move_flags = temp;
1925 ShowMessage("Testing CaptureList Speed");
1926 TestSpeed(CaptureList, 1);
1927 ShowMessage("Testing Eval Speed");
1928 ExaminePosition(opponent);
1929 TestPSpeed(ScorePosition, 1);
1931 else if (!XSHOGI && strcmp(s, "test") == 0)
1934 ShowMessage("Testing MoveList Speed");
1935 TestSpeed(MoveList, 2000);
1936 ShowMessage("Testing CaptureList Speed");
1937 TestSpeed(CaptureList, 3000);
1938 ShowMessage("Testing Eval Speed");
1939 ExaminePosition(opponent);
1940 TestPSpeed(ScorePosition, 1500);
1942 ShowMessage("Testing MoveList Speed");
1943 TestSpeed(MoveList, 20000);
1944 ShowMessage("Testing CaptureList Speed");
1945 TestSpeed(CaptureList, 30000);
1946 ShowMessage("Testing Eval Speed");
1947 ExaminePosition(opponent);
1948 TestPSpeed(ScorePosition, 15000);
1951 else if (!XSHOGI && strcmp(s, "p") == 0)
1955 else if (!XSHOGI && strcmp(s, "debug") == 0)
1965 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
1967 /* check for repetition */
1968 short rpt = repetition();
1972 DRAW = DRAW_REPETITION;
1974 GameList[GameCnt].flags |= draw;
1988 ElapsedTime(COMPUTE_AND_INIT_MODE);
1992 computer = opponent;
1993 opponent = computer ^ 1;
1998 /* add remaining time in milliseconds for xshogi */
2001 printf("%d. %s %ld\n",
2002 ++mycnt2, s, TimeControl.clock[player] * 10);
2005 #ifdef notdef /* optional pass best line to frontend with move */
2006 # if !defined NOPOST
2008 if (flag.post && !flag.mate)
2012 printf(" %6d ", MSCORE);
2014 for (i = 1; MV[i] > 0; i++)
2016 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2017 printf("%5s ", mvstr[0]);
2030 SetTimeControl(void)
2034 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2035 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2036 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2040 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2041 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2044 flag.onemove = (TCmoves == 1);
2046 ElapsedTime(COMPUTE_AND_INIT_MODE);