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;
54 struct display *dsp = &raw_display;
60 movealgbr(short m, char *s)
63 short piece = 0, flag = 0;
76 piece = f - NO_SQUARES;
78 if (piece > NO_PIECES)
81 flag = (dropmask | piece);
128 #endif /* BOOKTEST */
132 * Generate move strings in different formats.
135 * - f piece to be moved
136 * - 0 < f < NO_SQUARES source square
137 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
138 * - t & 0x7f target square
139 * - t & 0x80 promotion flag
140 * - flag FIXME: must be zero ?
147 algbr(short f, short t, short flag)
153 piece = f - NO_SQUARES;
155 if (f > (NO_SQUARES + NO_PIECES))
158 flag = (dropmask | piece);
167 if ((f == t) && ((f != 0) || (t != 0)))
170 dsp->Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
173 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
175 else if ((flag & dropmask) != 0)
177 short piece = flag & pmask;
179 mvstr[0][0] = pxx[piece];
181 mvstr[0][2] = cxx[column(t)];
182 mvstr[0][3] = rxx[row(t)];
184 strcpy(mvstr[1], mvstr[0]);
185 strcpy(mvstr[2], mvstr[0]);
186 strcpy(mvstr[3], mvstr[0]);
188 else if ((f != 0) || (t != 0))
190 /* algebraic notation */
191 mvstr[0][0] = cxx[column(f)];
192 mvstr[0][1] = rxx[row(f)];
193 mvstr[0][2] = cxx[column(t)];
194 mvstr[0][3] = rxx[row(t)];
195 mvstr[0][4] = mvstr[3][0] = '\0';
196 mvstr[1][0] = pxx[board[f]];
198 mvstr[2][0] = mvstr[1][0];
199 mvstr[2][1] = mvstr[0][1];
201 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
202 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
203 mvstr[2][4] = mvstr[1][3] = '\0';
204 strcpy(mvstr[3], mvstr[2]);
205 mvstr[3][1] = mvstr[0][0];
209 strcat(mvstr[0], "+");
210 strcat(mvstr[1], "+");
211 strcat(mvstr[2], "+");
212 strcat(mvstr[3], "+");
217 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
223 * Compare the string 's' to the list of legal moves available for the
224 * opponent. If a match is found, make the move on the board.
228 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
230 static short pnt, tempb, tempc, tempsf, tempst, cnt;
231 static struct leaf xnode;
233 short i, l, local_flags;
236 /* check and remove quality flags */
237 for (i = local_flags = 0, l = strlen(s); i < l; i++)
242 local_flags |= badmove;
247 local_flags |= goodmove;
253 local_flags |= difficult;
262 if (iop == UNMAKE_MODE)
264 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
270 if (iop == VERIFY_AND_MAKE_MODE)
271 generate_move_flags = true;
273 MoveList(opponent, 2, -1, true);
274 generate_move_flags = false;
277 while (pnt < TrPnt[3])
280 algbr(node->f, node->t, (short) node->flags);
282 if ((strcmp(s, mvstr[0]) == 0)
283 || (strcmp(s, mvstr[1]) == 0)
284 || (strcmp(s, mvstr[2]) == 0)
285 || (strcmp(s, mvstr[3]) == 0))
292 if ((cnt == 1) && (xnode.score > DONTUSE))
296 MakeMove(opponent, &xnode, &tempb, &tempc,
297 &tempsf, &tempst, &INCscore);
299 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
301 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
302 dsp->AlwaysShowMessage("Illegal move (in check) %s", s);
307 if (iop == VERIFY_AND_TRY_MODE)
310 dsp->UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
311 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
312 GameList[GameCnt].nodes = 0;
313 ElapsedTime(COMPUTE_AND_INIT_MODE);
314 GameList[GameCnt].time = (short) (et + 50)/100;
315 GameList[GameCnt].flags |= local_flags;
319 TimeControl.clock[opponent] -= et;
320 timeopp[oppptr] = et;
321 --TimeControl.moves[opponent];
324 *mv = (xnode.f << 8) | xnode.t;
325 algbr(xnode.f, xnode.t, false);
327 /* in force mode, check for mate conditions */
330 if (IsCheckmate(opponent ^ 1, -1, -1))
334 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
335 dsp->ShowMessage(buf);
344 dsp->AlwaysShowMessage("Illegal move (no match) %s", s);
346 if (!barebones && (cnt > 1))
348 sprintf(buffer, "Ambiguous Move %s!", s);
349 dsp->ShowMessage(buffer);
357 parser(char *f, short *fpiece)
365 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
367 if (f[0] == pxx[i] || f[0] == qxx[i])
369 *fpiece = (p ? promoted[i] : unpromoted[i]);
374 if (f[1] == '*' || f[1] == '\'')
379 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
387 p = (f[5] == '+') ? 0x80 : 0;
389 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
397 while (*InPtr != ' ')
400 while (*InPtr == ' ')
408 while (*InPtr == ' ')
413 void RequestInputString(char* buffer, unsigned bufsize)
416 int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
418 perror("RequestInputString snprintf");
421 if (ret >= sizeof(fmt)) {
423 "Insufficient format-buffer size in %s for bufsize=%u\n",
424 __FUNCTION__, bufsize);
427 dsp->doRequestInputString(fmt, buffer);
441 strcpy(fname, savefile);
443 dsp->ShowMessage("Enter file name: ");
444 RequestInputString(fname, sizeof(fname)-1);
447 if (fname[0] == '\0')
448 strcpy(fname, "shogi.000");
450 if ((fd = fopen(fname, "r")) != NULL)
453 fgets(fname, 256, fd);
454 computer = opponent = black;
463 /* FIXME: write a skipn() function so that we can get
464 * 3 skips by doing skipn(3) */
468 Game50 = atoi(InPtr);
470 flag.force = (*InPtr == 'f');
471 fgets(fname, 256, fd); /* empty */
472 fgets(fname, 256, fd);
475 TCflag = atoi(InPtr);
479 OperatorTime = atoi(InPtr);
480 fgets(fname, 256, fd);
483 TimeControl.clock[black] = atol(InPtr);
486 TimeControl.moves[black] = atoi(InPtr);
487 fgets(fname, 256, fd);
490 TimeControl.clock[white] = atol(InPtr);
493 TimeControl.moves[white] = atoi(InPtr);
494 fgets(fname, 256, fd); /* empty */
496 for (i = NO_ROWS - 1; i > -1; i--)
498 fgets(fname, 256, fd);
502 for (j = 0; j < NO_COLS; j++)
504 sq = i * NO_COLS + j;
510 board[sq] = no_piece;
515 for (c = 0; c < NO_PIECES; c++)
520 board[sq] = promoted[c];
522 board[sq] = unpromoted[c];
528 for (c = 0; c < NO_PIECES; c++)
533 board[sq] = promoted[c];
535 board[sq] = unpromoted[c];
543 Mvboard[sq] = atoi(InPtr);
548 fgets(fname, 256, fd); /* empty */
549 fgets(fname, 256, fd); /* 9 8 7 ... */
550 fgets(fname, 256, fd); /* empty */
551 fgets(fname, 256, fd); /* p l n ... */
554 for (side = 0; side <= 1; side++)
556 fgets(fname, 256, fd);
560 Captured[side][pawn] = atoi(InPtr);
563 Captured[side][lance] = atoi(InPtr);
565 Captured[side][knight] = atoi(InPtr);
568 Captured[side][silver] = atoi(InPtr);
570 Captured[side][gold] = atoi(InPtr);
572 Captured[side][bishop] = atoi(InPtr);
574 Captured[side][rook] = atoi(InPtr);
576 Captured[side][king] = atoi(InPtr);
580 flag.regularstart = true;
582 fgets(fname, 256, fd); /* empty */
583 fgets(fname, 256, fd); /* move score ... */
585 while (fgets(fname, 256, fd))
594 g = &GameList[GameCnt];
595 g->gmove = parser(InPtr, &g->fpiece);
597 g->score = atoi(InPtr);
599 g->depth = atoi(InPtr);
601 g->nodes = atol(InPtr);
603 g->time = atol(InPtr);
605 g->flags = c = atoi(InPtr);
607 g->hashkey = strtol(InPtr, (char **) NULL, 16);
609 g->hashbd = strtol(InPtr, (char **) NULL, 16);
617 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
619 if (pxx[i] == *InPtr)
627 g->color = ((*InPtr == 'W') ? white : black);
629 g->piece = (*InPtr == '+'
631 : unpromoted[piece]);
640 if (TimeControl.clock[black] > 0)
648 dsp->UpdateDisplay(0, 0, 1, 0);
659 short sq, i, c, f, t;
662 char empty[2] = "\n";
665 strcpy(fname, savefile);
667 dsp->ShowMessage("Enter file name: ");
668 RequestInputString(fname, sizeof(fname)-1);
671 if (fname[0] == '\0')
672 strcpy(fname, "shogi.000");
674 if ((fd = fopen(fname, "w")) != NULL)
679 if (computer == white)
682 if (computer == black)
685 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
686 flag.force ? "force" : "");
688 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
689 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
690 TimeControl.clock[black], TimeControl.moves[black],
691 TimeControl.clock[white], TimeControl.moves[white]);
694 for (i = NO_ROWS - 1; i > -1; i--)
696 fprintf(fd, "%c ", ROW_NAME(i));
698 for (c = 0; c < NO_COLS; c++)
700 sq = i * NO_COLS + c;
702 p = is_promoted[piece] ? '+' : ' ';
703 fprintf(fd, "%c", p);
719 fprintf(fd, "%c", p);
724 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
725 fprintf(fd, " %d", Mvboard[f]);
732 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
734 fprintf(fd, " p l n s g b r k\n");
736 fprintf(fd, " 5 4 3 2 1\n");
738 fprintf(fd, " p s g b r k\n");
741 for (side = 0; side <= 1; side++)
743 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
744 fprintf(fd, " %2d", Captured[side][pawn]);
746 fprintf(fd, " %2d", Captured[side][lance]);
747 fprintf(fd, " %2d", Captured[side][knight]);
749 fprintf(fd, " %2d", Captured[side][silver]);
750 fprintf(fd, " %2d", Captured[side][gold]);
751 fprintf(fd, " %2d", Captured[side][bishop]);
752 fprintf(fd, " %2d", Captured[side][rook]);
753 fprintf(fd, " %2d", Captured[side][king]);
758 fputs(" move score depth nodes time flags capture\n", fd);
760 for (i = 1; i <= GameCnt; i++)
762 struct GameRec *g = &GameList[i];
765 t = (g->gmove & 0xFF);
766 algbr(f, t, g->flags);
768 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
771 : (is_promoted[g->fpiece] ? '+' : ' ')),
773 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
775 g->nodes, g->time, g->flags,
776 g->hashkey, g->hashbd);
778 if (g->piece != no_piece)
780 fprintf(fd, " %c %s %c\n",
781 pxx[g->piece], ColorStr[g->color],
782 (is_promoted[g->piece] ? '+' : ' '));
792 dsp->ShowMessage("Game saved");
796 dsp->ShowMessage("Could not open file");
802 * GetXGame, SaveXGame and BookGame used to only be defined if
803 * xshogi wasn't defined -- wonder why?
815 dsp->ShowMessage("Enter file name: ");
816 RequestInputString(fname, sizeof(fname)-1);
818 if (fname[0] == '\0')
819 strcpy(fname, "xshogi.position.read");
821 if ((fd = fopen(fname, "r")) != NULL)
824 flag.regularstart = false;
827 /* xshogi position file ... */
828 fgets(fname, 256, fd);
833 if (strcmp(fname, "xshogi"))
837 /* -- empty line -- */
838 fgets(fname, 256, fd);
839 /* -- empty line -- */
840 fgets(fname, 256, fd);
842 for (i = NO_ROWS - 1; i > -1; i--)
844 fgets(fname, 256, fd);
847 for (j = 0; j < NO_COLS; j++)
849 sq = i * NO_COLS + j;
855 board[sq] = no_piece;
860 for (c = 0; c < NO_PIECES; c++)
865 board[sq] = promoted[c];
867 board[sq] = unpromoted[c];
873 for (c = 0; c < NO_PIECES; c++)
878 board[sq] = promoted[c];
880 board[sq] = unpromoted[c];
893 for (side = 0; side <= 1; side++)
895 fgets(fname, 256, fd);
897 Captured[side][pawn] = atoi(InPtr);
900 Captured[side][lance] = atoi(InPtr);
902 Captured[side][knight] = atoi(InPtr);
905 Captured[side][silver] = atoi(InPtr);
907 Captured[side][gold] = atoi(InPtr);
909 Captured[side][bishop] = atoi(InPtr);
911 Captured[side][rook] = atoi(InPtr);
913 Captured[side][king] = atoi(InPtr);
916 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
929 dsp->UpdateDisplay(0, 0, 1, 0);
944 dsp->ShowMessage("Enter file name: ");
945 RequestInputString(fname, sizeof(fname)-1);
947 if (fname[0] == '\0')
948 strcpy(fname, "xshogi.position.read");
950 if ((fd = fopen(fname, "w")) != NULL)
952 fputs("# xshogi position file -- \n", fd);
956 for (i = NO_ROWS - 1; i > -1; i--)
960 for (j = 0; j < NO_COLS; j++)
962 sq = i * NO_COLS + j;
964 isp = is_promoted[piece];
965 *p = (isp ? '+' : ' ');
968 if (piece == no_piece)
970 else if (color[sq] == white)
983 for (side = 0; side <= 1; side++)
987 "%d %d %d %d %d %d %d %d\n",
989 "%d %d %d %d %d %d\n",
991 Captured[side][pawn],
993 Captured[side][lance],
994 Captured[side][knight],
996 Captured[side][silver],
997 Captured[side][gold],
998 Captured[side][bishop],
999 Captured[side][rook],
1000 Captured[side][king]);
1005 if (computer == black)
1006 fputs("white to play\n", fd);
1008 fputs("black to play\n", fd);
1019 char fname[256], sflags[4];
1023 strcpy(fname, savefile);
1025 /* Enter file name */
1026 dsp->ShowMessage("Enter file name: ");
1027 RequestInputString(fname, sizeof(fname)-1);
1030 if (fname[0] == '\0')
1033 if ((fd = fopen(fname, "a")) != NULL)
1037 for (i = 1; i <= GameCnt; i++)
1039 struct GameRec *g = &GameList[i];
1040 char mvnr[20], mvs[20];
1043 sprintf(mvnr, "%d.", (i + 1)/2);
1048 t = (g->gmove & 0xFF);
1049 algbr(f, t, g->flags);
1052 /* determine move quality string */
1053 if (g->flags & goodmove)
1056 if (g->flags & badmove)
1059 #ifdef EASY_OPENINGS
1060 if (g->flags & difficult)
1066 /* determine move string */
1069 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1073 sprintf(mvs, "%c%c%c%c%c%s%s ",
1074 mvstr[0][0], mvstr[0][1],
1075 (g->flags & capture) ? 'x' : '-',
1076 mvstr[0][2], mvstr[0][3],
1077 (mvstr[0][4] == '+') ? "+" : "",
1081 fprintf(fd, "%s%s%c%s",
1085 : (is_promoted[g->fpiece] ? "+" : "")),
1098 dsp->ShowMessage("Game saved");
1102 dsp->ShowMessage("Could not open file");
1113 char fname[256], dbuf[256];
1117 strcpy(fname, listfile);
1122 strncpy(dbuf, ctime(&when), 20);
1129 /* use format "CL.Jan01-020304B" when
1132 program played white */
1134 sprintf(fname, "CL.%s%s-%s%s%s%c",
1135 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1136 dbuf + 17, ColorStr[computer][0]);
1138 /* replace space padding with 0 */
1139 for (i = 0; fname[i] != '\0'; i++)
1141 if (fname[i] == ' ')
1146 fd = fopen(fname, "w");
1150 printf("Open failure for file: %s", fname);
1154 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1155 fputs(" score depth nodes time ", fd);
1156 fputs(" score depth nodes time\n", fd);
1158 for (i = 1; i <= GameCnt; i++)
1160 f = GameList[i].gmove >> 8;
1161 t = (GameList[i].gmove & 0xFF);
1162 algbr(f, t, GameList[i].flags);
1164 if (GameList[i].flags & book)
1166 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1169 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1170 pxx[GameList[i].fpiece],
1172 ? &mvstr[0][1] : mvstr[0]),
1179 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1182 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1183 pxx[GameList[i].fpiece],
1184 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1185 GameList[i].score, GameList[i].depth,
1186 GameList[i].nodes, GameList[i].time);
1199 fprintf(fd, "\n\n");
1201 if (GameList[GameCnt].flags & draw)
1203 fprintf(fd, "Draw %s\n", DRAW);
1205 if (DRAW == DRAW_REPETITION)
1209 fprintf(fd, "repetition by positions ");
1211 for (j = GameCnt - 1; j >= Game50; j -= 2)
1213 if (GameList[j].hashkey == hashkey &&
1214 GameList[j].hashbd == hashbd)
1215 fprintf(fd, "%d ", j);
1221 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1223 fprintf(fd, "%s\n", ColorStr[player ]);
1225 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1227 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1240 GameList[GameCnt].flags |= badmove;
1244 GameList[GameCnt].flags |= goodmove;
1247 #ifdef EASY_OPENINGS
1249 GameList[GameCnt].flags |= difficult;
1257 * Undo the most recent half-move.
1265 f = GameList[GameCnt].gmove >> 8;
1266 t = GameList[GameCnt].gmove & 0x7F;
1270 /* the move was a drop */
1271 Captured[color[t]][board[t]]++;
1272 board[t] = no_piece;
1278 if (GameList[GameCnt].flags & promote)
1279 board[f] = unpromoted[board[t]];
1281 board[f] = board[t];
1283 color[f] = color[t];
1284 board[t] = GameList[GameCnt].piece;
1285 color[t] = GameList[GameCnt].color;
1287 if (board[t] != no_piece)
1288 Captured[color[f]][unpromoted[board[t]]]--;
1290 if (color[t] != neutral)
1298 if (TCflag && (TCmoves > 1))
1299 ++TimeControl.moves[color[f]];
1301 hashkey = GameList[GameCnt].hashkey;
1302 hashbd = GameList[GameCnt].hashbd;
1304 computer = computer ^ 1;
1305 opponent = opponent ^ 1;
1308 player = player ^ 1;
1309 dsp->ShowSidetoMove();
1310 dsp->UpdateDisplay(0, 0, 1, 0);
1312 if (flag.regularstart)
1318 FlagString(unsigned short flags, char *s)
1323 if (flags & promote)
1324 strcat(s, " promote");
1326 if (flags & dropmask)
1327 strcat(s, " drop:");
1329 if ((piece = (flags & pmask)))
1333 if (is_promoted[piece])
1336 s[l++] = pxx[piece];
1340 if (flags & capture)
1341 strcat(s, " capture");
1344 strcat(s, " exact");
1347 strcat(s, " tesuji");
1350 strcat(s, " check");
1356 strcat(s, " stupid");
1358 if (flags & questionable)
1359 strcat(s, " questionable");
1361 if (flags & kingattack)
1362 strcat(s, " kingattack");
1370 TestSpeed(void(*f)(short side, short ply,
1371 short in_check, short blockable),
1381 #ifdef HAVE_GETTIMEOFDAY
1385 #ifdef HAVE_GETTIMEOFDAY
1386 gettimeofday(&tv, NULL);
1387 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1392 for (i = 0; i < j; i++)
1394 f(opponent, 2, -1, true);
1397 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1399 if (!pick(jj, TrPnt[3] - 1))
1405 #ifdef HAVE_GETTIMEOFDAY
1406 gettimeofday(&tv, NULL);
1407 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1412 cnt = j * (TrPnt[3] - TrPnt[2]);
1419 dsp->ShowNodeCnt(cnt);
1424 TestPSpeed(short(*f) (short side), unsigned j)
1428 #ifdef HAVE_GETTIMEOFDAY
1432 #ifdef HAVE_GETTIMEOFDAY
1433 gettimeofday(&tv, NULL);
1434 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1439 for (i = 0; i < j; i++)
1442 #ifdef HAVE_GETTIMEOFDAY
1443 gettimeofday(&tv, NULL);
1444 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1456 dsp->ShowNodeCnt(cnt);
1461 SetOppTime(char *time)
1466 t = (int)strtol(time, &time, 10);
1471 /* FIXME: sec is parsed but ignored */
1472 sec = (int)strtol(time, &time, 10);
1475 m = (int)strtol(time, &time, 10);
1478 TimeControl.clock[opponent] = t;
1481 TimeControl.moves[opponent] = m;
1483 ElapsedTime(COMPUTE_AND_INIT_MODE);
1487 /* just to inform xshogi about availability of otime command */
1488 printf("otime %d %d\n", t, m);
1494 SetMachineTime(char *time)
1499 t = (int)strtol(time, &time, 10);
1504 /* FIXME: sec is parsed but ignored */
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);
1526 /* FIXME! This is truly the function from hell! */
1529 * Process the user's command. If easy mode is OFF (the computer is thinking
1530 * on opponents time) and the program is out of book, then make the 'hint'
1531 * move on the board and call SelectMove() to find a response. The user
1532 * terminates the search by entering a command. If the opponent does not make
1533 * the hint move, then set Sdepth to zero.
1537 InputCommand(char *command)
1539 short have_shown_prompt = false;
1540 short ok, done, is_move = false;
1544 ok = flag.quit = done = false;
1548 if (TTadd > ttbllimit)
1552 if ((hint > 0) && !flag.easy && !flag.force)
1555 * A hint move for the player is available. Compute a move for the
1556 * opponent in background mode assuming that the hint move will be
1557 * selected by the player.
1560 ft = time0; /* Save reference time for the player. */
1562 algbr((short) hint >> 8, (short) hint & 0xff, false);
1563 strcpy(s, mvstr[0]);
1568 /* do the hint move */
1569 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1573 #ifdef QUIETBACKGROUND
1575 have_shown_prompt = true;
1576 #endif /* QUIETBACKGROUND */
1578 /* Start computing a move until the search is interrupted. */
1580 #ifdef INTERRUPT_TEST
1584 /* would love to put null move in here */
1585 /* after we make the hint move make a 2 ply search
1586 * with both plys our moves */
1587 /* think on opponents time */
1588 SelectMove(computer, BACKGROUND_MODE);
1590 #ifdef INTERRUPT_TEST
1591 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1595 printf("searching not terminated by interrupt!\n");
1599 printf("elapsed time from interrupt to "
1600 "terminating search: %ld\n", it);
1604 /* undo the hint and carry on */
1605 VerifyMove(s, UNMAKE_MODE, &mv);
1609 time0 = ft; /* Restore reference time for the player. */
1612 while(!(ok || flag.quit || done))
1616 #ifdef QUIETBACKGROUND
1617 if (!have_shown_prompt)
1619 #endif /* QUIETBACKGROUND */
1623 #ifdef QUIETBACKGROUND
1626 have_shown_prompt = false;
1627 #endif /* QUIETBACKGROUND */
1629 if (command == NULL) {
1630 int eof = dsp->GetString(sx);
1634 strcpy(sx, command);
1638 /* extract first word */
1639 if (sscanf(sx, "%s", s) < 1)
1642 if (strcmp(s, "bd") == 0) /* bd -- display board */
1644 /* FIXME: Hack alert! */
1645 short old_xshogi = XSHOGI;
1648 display_type = DISPLAY_RAW;
1651 dsp->UpdateDisplay(0, 0, 1, 0);
1654 display_type = DISPLAY_X;
1656 else if (strcmp(s, "post") == 0)
1658 flag.post = !flag.post;
1660 else if (strcmp(s, "alg") == 0)
1664 else if ((strcmp(s, "quit") == 0)
1665 || (strcmp(s, "exit") == 0))
1669 else if ((strcmp(s, "set") == 0)
1670 || (strcmp(s, "edit") == 0))
1674 else if (strcmp(s, "setup") == 0)
1678 else if (strcmp(s, "first") == 0)
1682 else if (strcmp(s, "go") == 0)
1687 if (computer == black)
1698 else if (strcmp(s, "help") == 0)
1702 else if (strcmp(s, "material") == 0)
1704 flag.material = !flag.material;
1706 else if (strcmp(s, "force") == 0)
1711 flag.bothsides = false;
1715 flag.force = !flag.force;
1716 flag.bothsides = false;
1719 else if (strcmp(s, "book") == 0)
1721 Book = Book ? 0 : BOOKFAIL;
1723 else if (strcmp(s, "new") == 0)
1726 dsp->UpdateDisplay(0, 0, 1, 0);
1728 else if (strcmp(s, "list") == 0)
1732 else if (strcmp(s, "level") == 0)
1734 dsp->SelectLevel(sx + strlen("level"));
1736 else if (strcmp(s, "clock") == 0)
1738 dsp->SelectLevel(sx + strlen("clock"));
1740 else if (strcmp(s, "hash") == 0)
1742 flag.hash = !flag.hash;
1744 else if (strcmp(s, "gamein") == 0)
1746 flag.gamein = !flag.gamein;
1748 else if (strcmp(s, "beep") == 0)
1750 flag.beep = !flag.beep;
1752 else if (strcmp(s, "time") == 0)
1754 SetMachineTime(sx + strlen("time"));
1756 else if (strcmp(s, "otime") == 0)
1758 SetOppTime(sx + strlen("otime"));
1760 else if (strcmp(s, "Awindow") == 0)
1762 dsp->ChangeAlphaWindow();
1764 else if (strcmp(s, "Bwindow") == 0)
1766 dsp->ChangeBetaWindow();
1768 else if (strcmp(s, "rcptr") == 0)
1770 flag.rcptr = !flag.rcptr;
1772 else if (strcmp(s, "hint") == 0)
1776 else if (strcmp(s, "both") == 0)
1778 flag.bothsides = !flag.bothsides;
1781 ElapsedTime(COMPUTE_AND_INIT_MODE);
1782 SelectMove(opponent, FOREGROUND_MODE);
1785 else if (strcmp(s, "reverse") == 0)
1787 flag.reverse = !flag.reverse;
1789 dsp->UpdateDisplay(0, 0, 1, 0);
1791 else if (strcmp(s, "switch") == 0)
1793 computer = computer ^ 1;
1794 opponent = opponent ^ 1;
1795 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1800 else if (strcmp(s, "black") == 0)
1809 * ok = true; don't automatically start with black command
1812 else if (strcmp(s, "white") == 0)
1821 * ok = true; don't automatically start with white command
1824 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1828 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1833 /* CHECKME: are these next three correct? */
1834 else if (!XSHOGI && strcmp(s, "xget") == 0)
1838 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1842 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1846 #ifdef EASY_OPENINGS
1847 else if ((strcmp(s, "?") == 0)
1848 || (strcmp(s, "!") == 0)
1849 || (strcmp(s, "~") == 0))
1851 else if ((strcmp(s, "?") == 0)
1852 || (strcmp(s, "!") == 0))
1857 else if (strcmp(s, "get") == 0)
1861 else if (strcmp(s, "save") == 0)
1865 else if (strcmp(s, "depth") == 0)
1867 dsp->ChangeSearchDepth(sx + strlen("depth"));
1869 else if (strcmp(s, "sd") == 0)
1871 dsp->ChangeSearchDepth(sx + strlen("sd"));
1873 else if (strcmp(s, "hashdepth") == 0)
1875 dsp->ChangeHashDepth();
1877 else if (strcmp(s, "random") == 0)
1881 else if (strcmp(s, "hard") == 0)
1885 else if (strcmp(s, "easy") == 0)
1887 flag.easy = !flag.easy;
1889 else if (strcmp(s, "tsume") == 0)
1891 flag.tsume = !flag.tsume;
1893 else if (strcmp(s, "contempt") == 0)
1897 else if (strcmp(s, "xwndw") == 0)
1899 dsp->ChangeXwindow();
1901 else if (strcmp(s, "rv") == 0)
1904 dsp->UpdateDisplay(0, 0, 1, 0);
1906 else if (strcmp(s, "coords") == 0)
1908 flag.coords = !flag.coords;
1909 dsp->UpdateDisplay(0, 0, 1, 0);
1911 else if (strcmp(s, "stars") == 0)
1913 flag.stars = !flag.stars;
1914 dsp->UpdateDisplay(0, 0, 1, 0);
1916 else if (!XSHOGI && strcmp(s, "moves") == 0)
1923 extern unsigned short PrVar[MAXDEPTH];
1925 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1932 dsp->ShowMessage("Testing MoveList Speed");
1933 temp = generate_move_flags;
1934 generate_move_flags = true;
1935 TestSpeed(MoveList, 1);
1936 generate_move_flags = temp;
1937 dsp->ShowMessage("Testing CaptureList Speed");
1938 TestSpeed(CaptureList, 1);
1939 dsp->ShowMessage("Testing Eval Speed");
1940 ExaminePosition(opponent);
1941 TestPSpeed(ScorePosition, 1);
1943 else if (!XSHOGI && strcmp(s, "test") == 0)
1946 dsp->ShowMessage("Testing MoveList Speed");
1947 TestSpeed(MoveList, 2000);
1948 dsp->ShowMessage("Testing CaptureList Speed");
1949 TestSpeed(CaptureList, 3000);
1950 dsp->ShowMessage("Testing Eval Speed");
1951 ExaminePosition(opponent);
1952 TestPSpeed(ScorePosition, 1500);
1954 dsp->ShowMessage("Testing MoveList Speed");
1955 TestSpeed(MoveList, 20000);
1956 dsp->ShowMessage("Testing CaptureList Speed");
1957 TestSpeed(CaptureList, 30000);
1958 dsp->ShowMessage("Testing Eval Speed");
1959 ExaminePosition(opponent);
1960 TestPSpeed(ScorePosition, 15000);
1963 else if (!XSHOGI && strcmp(s, "p") == 0)
1965 dsp->ShowPostnValues();
1967 else if (!XSHOGI && strcmp(s, "debug") == 0)
1977 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
1979 /* check for repetition */
1980 short rpt = repetition();
1984 DRAW = DRAW_REPETITION;
1985 dsp->ShowMessage(DRAW);
1986 GameList[GameCnt].flags |= draw;
2000 ElapsedTime(COMPUTE_AND_INIT_MODE);
2004 computer = opponent;
2005 opponent = computer ^ 1;
2010 /* add remaining time in milliseconds for xshogi */
2013 printf("%d. %s %ld\n",
2014 ++mycnt2, s, TimeControl.clock[player] * 10);
2017 #ifdef notdef /* optional pass best line to frontend with move */
2018 if (flag.post && !flag.mate)
2022 printf(" %6d ", MSCORE);
2024 for (i = 1; MV[i] > 0; i++)
2026 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2027 printf("%5s ", mvstr[0]);
2037 SetTimeControl(void)
2041 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2042 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2043 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2047 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2048 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2051 flag.onemove = (TCmoves == 1);
2053 ElapsedTime(COMPUTE_AND_INIT_MODE);