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;
50 struct display *dsp = &raw_display;
56 movealgbr(short m, char *s)
59 short piece = 0, flag = 0;
72 piece = f - NO_SQUARES;
74 if (piece > NO_PIECES)
77 flag = (dropmask | piece);
124 #endif /* BOOKTEST */
130 * Generate move strings in different formats.
133 * - f piece to be moved
134 * - 0 < f < NO_SQUARES source square
135 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
136 * - t & 0x7f target square
137 * - t & 0x80 promotion flag
138 * - flag FIXME: must be zero ?
145 algbr(short f, short t, short flag)
151 piece = f - NO_SQUARES;
153 if (f > (NO_SQUARES + NO_PIECES))
156 flag = (dropmask | piece);
165 if ((f == t) && ((f != 0) || (t != 0)))
168 dsp->Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
171 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
173 else if ((flag & dropmask) != 0)
175 short piece = flag & pmask;
177 mvstr[0][0] = pxx[piece];
179 mvstr[0][2] = cxx[column(t)];
180 mvstr[0][3] = rxx[row(t)];
182 strcpy(mvstr[1], mvstr[0]);
183 strcpy(mvstr[2], mvstr[0]);
184 strcpy(mvstr[3], mvstr[0]);
186 else if ((f != 0) || (t != 0))
188 /* algebraic notation */
189 mvstr[0][0] = cxx[column(f)];
190 mvstr[0][1] = rxx[row(f)];
191 mvstr[0][2] = cxx[column(t)];
192 mvstr[0][3] = rxx[row(t)];
193 mvstr[0][4] = mvstr[3][0] = '\0';
194 mvstr[1][0] = pxx[board[f]];
196 mvstr[2][0] = mvstr[1][0];
197 mvstr[2][1] = mvstr[0][1];
199 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
200 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
201 mvstr[2][4] = mvstr[1][3] = '\0';
202 strcpy(mvstr[3], mvstr[2]);
203 mvstr[3][1] = mvstr[0][0];
207 strcat(mvstr[0], "+");
208 strcat(mvstr[1], "+");
209 strcat(mvstr[2], "+");
210 strcat(mvstr[3], "+");
215 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
222 * Compare the string 's' to the list of legal moves available for the
223 * opponent. If a match is found, make the move on the board.
227 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
229 static short pnt, tempb, tempc, tempsf, tempst, cnt;
230 static struct leaf xnode;
232 short i, l, local_flags;
235 /* check and remove quality flags */
236 for (i = local_flags = 0, l = strlen(s); i < l; i++)
241 local_flags |= badmove;
246 local_flags |= goodmove;
252 local_flags |= difficult;
261 if (iop == UNMAKE_MODE)
263 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
269 if (iop == VERIFY_AND_MAKE_MODE)
270 generate_move_flags = true;
272 MoveList(opponent, 2, -1, true);
273 generate_move_flags = false;
276 while (pnt < TrPnt[3])
279 algbr(node->f, node->t, (short) node->flags);
281 if ((strcmp(s, mvstr[0]) == 0)
282 || (strcmp(s, mvstr[1]) == 0)
283 || (strcmp(s, mvstr[2]) == 0)
284 || (strcmp(s, mvstr[3]) == 0))
291 if ((cnt == 1) && (xnode.score > DONTUSE))
295 MakeMove(opponent, &xnode, &tempb, &tempc,
296 &tempsf, &tempst, &INCscore);
298 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
300 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
301 dsp->AlwaysShowMessage("Illegal move (in check) %s", s);
306 if (iop == VERIFY_AND_TRY_MODE)
309 dsp->UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
310 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
311 GameList[GameCnt].nodes = 0;
312 ElapsedTime(COMPUTE_AND_INIT_MODE);
313 GameList[GameCnt].time = (short) (et + 50)/100;
314 GameList[GameCnt].flags |= local_flags;
318 TimeControl.clock[opponent] -= et;
319 timeopp[oppptr] = et;
320 --TimeControl.moves[opponent];
323 *mv = (xnode.f << 8) | xnode.t;
324 algbr(xnode.f, xnode.t, false);
326 /* in force mode, check for mate conditions */
329 if (IsCheckmate(opponent ^ 1, -1, -1))
333 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
334 dsp->ShowMessage(buf);
343 dsp->AlwaysShowMessage("Illegal move (no match) %s", s);
345 if (!barebones && (cnt > 1))
347 sprintf(buffer, "Ambiguous Move %s!", s);
348 dsp->ShowMessage(buffer);
357 parser(char *f, int side, 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 == ' ')
409 while (*InPtr == ' ')
414 void RequestInputString(char* buffer, unsigned bufsize)
417 int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
419 perror("RequestInputString snprintf");
422 if (ret >= sizeof(fmt)) {
424 "Insufficient format-buffer size in %s for bufsize=%u\n",
425 __FUNCTION__, bufsize);
428 dsp->doRequestInputString(fmt, buffer);
442 strcpy(fname, savefile);
444 dsp->ShowMessage("Enter file name: ");
445 RequestInputString(fname, sizeof(fname)-1);
448 if (fname[0] == '\0')
449 strcpy(fname, "shogi.000");
451 if ((fd = fopen(fname, "r")) != NULL)
454 fgets(fname, 256, fd);
455 computer = opponent = black;
464 /* FIXME: write a skipn() function so that we can get
465 * 3 skips by doing skipn(3) */
469 Game50 = atoi(InPtr);
471 flag.force = (*InPtr == 'f');
472 fgets(fname, 256, fd); /* empty */
473 fgets(fname, 256, fd);
476 TCflag = atoi(InPtr);
480 OperatorTime = atoi(InPtr);
481 fgets(fname, 256, fd);
484 TimeControl.clock[black] = atol(InPtr);
487 TimeControl.moves[black] = atoi(InPtr);
488 fgets(fname, 256, fd);
491 TimeControl.clock[white] = atol(InPtr);
494 TimeControl.moves[white] = atoi(InPtr);
495 fgets(fname, 256, fd); /* empty */
497 for (i = NO_ROWS - 1; i > -1; i--)
499 fgets(fname, 256, fd);
503 for (j = 0; j < NO_COLS; j++)
505 sq = i * NO_COLS + j;
511 board[sq] = no_piece;
516 for (c = 0; c < NO_PIECES; c++)
521 board[sq] = promoted[c];
523 board[sq] = unpromoted[c];
529 for (c = 0; c < NO_PIECES; c++)
534 board[sq] = promoted[c];
536 board[sq] = unpromoted[c];
544 Mvboard[sq] = atoi(InPtr);
549 fgets(fname, 256, fd); /* empty */
550 fgets(fname, 256, fd); /* 9 8 7 ... */
551 fgets(fname, 256, fd); /* empty */
552 fgets(fname, 256, fd); /* p l n ... */
555 for (side = 0; side <= 1; side++)
557 fgets(fname, 256, fd);
561 Captured[side][pawn] = atoi(InPtr);
564 Captured[side][lance] = atoi(InPtr);
566 Captured[side][knight] = atoi(InPtr);
569 Captured[side][silver] = atoi(InPtr);
571 Captured[side][gold] = atoi(InPtr);
573 Captured[side][bishop] = atoi(InPtr);
575 Captured[side][rook] = atoi(InPtr);
577 Captured[side][king] = atoi(InPtr);
581 flag.regularstart = true;
583 fgets(fname, 256, fd); /* empty */
584 fgets(fname, 256, fd); /* move score ... */
586 while (fgets(fname, 256, fd))
595 g = &GameList[GameCnt];
596 g->gmove = parser(InPtr, side, &g->fpiece);
598 g->score = atoi(InPtr);
600 g->depth = atoi(InPtr);
602 g->nodes = atol(InPtr);
604 g->time = atol(InPtr);
606 g->flags = c = atoi(InPtr);
608 g->hashkey = strtol(InPtr, (char **) NULL, 16);
610 g->hashbd = strtol(InPtr, (char **) NULL, 16);
618 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
620 if (pxx[i] == *InPtr)
628 g->color = ((*InPtr == 'W') ? white : black);
630 g->piece = (*InPtr == '+'
632 : unpromoted[piece]);
641 if (TimeControl.clock[black] > 0)
649 dsp->UpdateDisplay(0, 0, 1, 0);
661 short sq, i, c, f, t;
664 char empty[2] = "\n";
667 strcpy(fname, savefile);
669 dsp->ShowMessage("Enter file name: ");
670 RequestInputString(fname, sizeof(fname)-1);
673 if (fname[0] == '\0')
674 strcpy(fname, "shogi.000");
676 if ((fd = fopen(fname, "w")) != NULL)
681 if (computer == white)
684 if (computer == black)
687 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
688 flag.force ? "force" : "");
690 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
691 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
692 TimeControl.clock[black], TimeControl.moves[black],
693 TimeControl.clock[white], TimeControl.moves[white]);
696 for (i = NO_ROWS - 1; i > -1; i--)
698 fprintf(fd, "%c ", ROW_NAME(i));
700 for (c = 0; c < NO_COLS; c++)
702 sq = i * NO_COLS + c;
704 p = is_promoted[piece] ? '+' : ' ';
705 fprintf(fd, "%c", p);
721 fprintf(fd, "%c", p);
726 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
727 fprintf(fd, " %d", Mvboard[f]);
734 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
736 fprintf(fd, " p l n s g b r k\n");
738 fprintf(fd, " 5 4 3 2 1\n");
740 fprintf(fd, " p s g b r k\n");
743 for (side = 0; side <= 1; side++)
745 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
746 fprintf(fd, " %2d", Captured[side][pawn]);
748 fprintf(fd, " %2d", Captured[side][lance]);
749 fprintf(fd, " %2d", Captured[side][knight]);
751 fprintf(fd, " %2d", Captured[side][silver]);
752 fprintf(fd, " %2d", Captured[side][gold]);
753 fprintf(fd, " %2d", Captured[side][bishop]);
754 fprintf(fd, " %2d", Captured[side][rook]);
755 fprintf(fd, " %2d", Captured[side][king]);
760 fputs(" move score depth nodes time flags capture\n", fd);
762 for (i = 1; i <= GameCnt; i++)
764 struct GameRec *g = &GameList[i];
767 t = (g->gmove & 0xFF);
768 algbr(f, t, g->flags);
770 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
773 : (is_promoted[g->fpiece] ? '+' : ' ')),
775 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
777 g->nodes, g->time, g->flags,
778 g->hashkey, g->hashbd);
780 if (g->piece != no_piece)
782 fprintf(fd, " %c %s %c\n",
783 pxx[g->piece], ColorStr[g->color],
784 (is_promoted[g->piece] ? '+' : ' '));
794 dsp->ShowMessage("Game saved");
798 dsp->ShowMessage("Could not open file");
805 * GetXGame, SaveXGame and BookGame used to only be defined if
806 * xshogi wasn't defined -- wonder why?
818 dsp->ShowMessage("Enter file name: ");
819 RequestInputString(fname, sizeof(fname)-1);
821 if (fname[0] == '\0')
822 strcpy(fname, "xshogi.position.read");
824 if ((fd = fopen(fname, "r")) != NULL)
827 flag.regularstart = false;
830 /* xshogi position file ... */
831 fgets(fname, 256, fd);
836 if (strcmp(fname, "xshogi"))
840 /* -- empty line -- */
841 fgets(fname, 256, fd);
842 /* -- empty line -- */
843 fgets(fname, 256, fd);
845 for (i = NO_ROWS - 1; i > -1; i--)
847 fgets(fname, 256, fd);
850 for (j = 0; j < NO_COLS; j++)
852 sq = i * NO_COLS + j;
858 board[sq] = no_piece;
863 for (c = 0; c < NO_PIECES; c++)
868 board[sq] = promoted[c];
870 board[sq] = unpromoted[c];
876 for (c = 0; c < NO_PIECES; c++)
881 board[sq] = promoted[c];
883 board[sq] = unpromoted[c];
896 for (side = 0; side <= 1; side++)
898 fgets(fname, 256, fd);
900 Captured[side][pawn] = atoi(InPtr);
903 Captured[side][lance] = atoi(InPtr);
905 Captured[side][knight] = atoi(InPtr);
908 Captured[side][silver] = atoi(InPtr);
910 Captured[side][gold] = atoi(InPtr);
912 Captured[side][bishop] = atoi(InPtr);
914 Captured[side][rook] = atoi(InPtr);
916 Captured[side][king] = atoi(InPtr);
919 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
932 dsp->UpdateDisplay(0, 0, 1, 0);
947 dsp->ShowMessage("Enter file name: ");
948 RequestInputString(fname, sizeof(fname)-1);
950 if (fname[0] == '\0')
951 strcpy(fname, "xshogi.position.read");
953 if ((fd = fopen(fname, "w")) != NULL)
955 fputs("# xshogi position file -- \n", fd);
959 for (i = NO_ROWS - 1; i > -1; i--)
963 for (j = 0; j < NO_COLS; j++)
965 sq = i * NO_COLS + j;
967 isp = is_promoted[piece];
968 *p = (isp ? '+' : ' ');
971 if (piece == no_piece)
973 else if (color[sq] == white)
986 for (side = 0; side <= 1; side++)
990 "%d %d %d %d %d %d %d %d\n",
992 "%d %d %d %d %d %d\n",
994 Captured[side][pawn],
996 Captured[side][lance],
997 Captured[side][knight],
999 Captured[side][silver],
1000 Captured[side][gold],
1001 Captured[side][bishop],
1002 Captured[side][rook],
1003 Captured[side][king]);
1008 if (computer == black)
1009 fputs("white to play\n", fd);
1011 fputs("black to play\n", fd);
1022 char fname[256], sflags[4];
1026 strcpy(fname, savefile);
1028 /* Enter file name */
1029 dsp->ShowMessage("Enter file name: ");
1030 RequestInputString(fname, sizeof(fname)-1);
1033 if (fname[0] == '\0')
1036 if ((fd = fopen(fname, "a")) != NULL)
1040 for (i = 1; i <= GameCnt; i++)
1042 struct GameRec *g = &GameList[i];
1043 char mvnr[20], mvs[20];
1046 sprintf(mvnr, "%d.", (i + 1)/2);
1051 t = (g->gmove & 0xFF);
1052 algbr(f, t, g->flags);
1055 /* determine move quality string */
1056 if (g->flags & goodmove)
1059 if (g->flags & badmove)
1062 #ifdef EASY_OPENINGS
1063 if (g->flags & difficult)
1069 /* determine move string */
1072 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1076 sprintf(mvs, "%c%c%c%c%c%s%s ",
1077 mvstr[0][0], mvstr[0][1],
1078 (g->flags & capture) ? 'x' : '-',
1079 mvstr[0][2], mvstr[0][3],
1080 (mvstr[0][4] == '+') ? "+" : "",
1084 fprintf(fd, "%s%s%c%s",
1088 : (is_promoted[g->fpiece] ? "+" : "")),
1101 dsp->ShowMessage("Game saved");
1105 dsp->ShowMessage("Could not open file");
1117 char fname[256], dbuf[256];
1121 strcpy(fname, listfile);
1126 strncpy(dbuf, ctime(&when), 20);
1133 /* use format "CL.Jan01-020304B" when
1136 program played white */
1138 sprintf(fname, "CL.%s%s-%s%s%s%c",
1139 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1140 dbuf + 17, ColorStr[computer][0]);
1142 /* replace space padding with 0 */
1143 for (i = 0; fname[i] != '\0'; i++)
1145 if (fname[i] == ' ')
1150 fd = fopen(fname, "w");
1154 printf("Open failure for file: %s", fname);
1158 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1159 fputs(" score depth nodes time ", fd);
1160 fputs(" score depth nodes time\n", fd);
1162 for (i = 1; i <= GameCnt; i++)
1164 f = GameList[i].gmove >> 8;
1165 t = (GameList[i].gmove & 0xFF);
1166 algbr(f, t, GameList[i].flags);
1168 if (GameList[i].flags & book)
1170 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1173 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1174 pxx[GameList[i].fpiece],
1176 ? &mvstr[0][1] : mvstr[0]),
1183 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1186 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1187 pxx[GameList[i].fpiece],
1188 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1189 GameList[i].score, GameList[i].depth,
1190 GameList[i].nodes, GameList[i].time);
1203 fprintf(fd, "\n\n");
1205 if (GameList[GameCnt].flags & draw)
1207 fprintf(fd, "Draw %s\n", DRAW);
1209 if (DRAW == DRAW_REPETITION)
1213 fprintf(fd, "repetition by positions ");
1215 for (j = GameCnt - 1; j >= Game50; j -= 2)
1217 if (GameList[j].hashkey == hashkey &&
1218 GameList[j].hashbd == hashbd)
1219 fprintf(fd, "%d ", j);
1225 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1227 fprintf(fd, "%s\n", ColorStr[player ]);
1229 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1231 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1245 GameList[GameCnt].flags |= badmove;
1249 GameList[GameCnt].flags |= goodmove;
1252 #ifdef EASY_OPENINGS
1254 GameList[GameCnt].flags |= difficult;
1264 * Undo the most recent half-move.
1272 f = GameList[GameCnt].gmove >> 8;
1273 t = GameList[GameCnt].gmove & 0x7F;
1277 /* the move was a drop */
1278 Captured[color[t]][board[t]]++;
1279 board[t] = no_piece;
1285 if (GameList[GameCnt].flags & promote)
1286 board[f] = unpromoted[board[t]];
1288 board[f] = board[t];
1290 color[f] = color[t];
1291 board[t] = GameList[GameCnt].piece;
1292 color[t] = GameList[GameCnt].color;
1294 if (board[t] != no_piece)
1295 Captured[color[f]][unpromoted[board[t]]]--;
1297 if (color[t] != neutral)
1305 if (TCflag && (TCmoves > 1))
1306 ++TimeControl.moves[color[f]];
1308 hashkey = GameList[GameCnt].hashkey;
1309 hashbd = GameList[GameCnt].hashbd;
1311 computer = computer ^ 1;
1312 opponent = opponent ^ 1;
1315 player = player ^ 1;
1316 dsp->ShowSidetoMove();
1317 dsp->UpdateDisplay(0, 0, 1, 0);
1319 if (flag.regularstart)
1326 FlagString(unsigned short flags, char *s)
1331 if (flags & promote)
1332 strcat(s, " promote");
1334 if (flags & dropmask)
1335 strcat(s, " drop:");
1337 if ((piece = (flags & pmask)))
1341 if (is_promoted[piece])
1344 s[l++] = pxx[piece];
1348 if (flags & capture)
1349 strcat(s, " capture");
1352 strcat(s, " exact");
1355 strcat(s, " tesuji");
1358 strcat(s, " check");
1364 strcat(s, " stupid");
1366 if (flags & questionable)
1367 strcat(s, " questionable");
1369 if (flags & kingattack)
1370 strcat(s, " kingattack");
1379 TestSpeed(void(*f)(short side, short ply,
1380 short in_check, short blockable),
1390 #ifdef HAVE_GETTIMEOFDAY
1394 #ifdef HAVE_GETTIMEOFDAY
1395 gettimeofday(&tv, NULL);
1396 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1401 for (i = 0; i < j; i++)
1403 f(opponent, 2, -1, true);
1406 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1408 if (!pick(jj, TrPnt[3] - 1))
1414 #ifdef HAVE_GETTIMEOFDAY
1415 gettimeofday(&tv, NULL);
1416 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1421 cnt = j * (TrPnt[3] - TrPnt[2]);
1428 dsp->ShowNodeCnt(cnt);
1434 TestPSpeed(short(*f) (short side), unsigned j)
1438 #ifdef HAVE_GETTIMEOFDAY
1442 #ifdef HAVE_GETTIMEOFDAY
1443 gettimeofday(&tv, NULL);
1444 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1449 for (i = 0; i < j; i++)
1452 #ifdef HAVE_GETTIMEOFDAY
1453 gettimeofday(&tv, NULL);
1454 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1466 dsp->ShowNodeCnt(cnt);
1478 time = &s[strlen("otime")];
1479 t = (int)strtol(time, &time, 10);
1484 sec = (int)strtol(time, &time, 10);
1487 m = (int)strtol(time, &time, 10);
1490 TimeControl.clock[opponent] = t;
1493 TimeControl.moves[opponent] = m;
1495 ElapsedTime(COMPUTE_AND_INIT_MODE);
1499 /* just to inform xshogi about availability of otime command */
1500 printf("otime %d %d\n", t, m);
1507 SetMachineTime(char *s)
1512 time = &s[strlen("time")];
1514 t = (int)strtol(time, &time, 10);
1519 sec = (int)strtol(time, &time, 10);
1522 m = (int)strtol(time, &time, 10);
1525 TimeControl.clock[computer] = t;
1528 TimeControl.moves[computer] = m;
1530 ElapsedTime(COMPUTE_AND_INIT_MODE);
1534 /* just to inform xshogi about availability of time command */
1535 printf("time %d %d\n", t, m);
1543 /* FIXME! This is truly the function from hell! */
1546 * Process the user's command. If easy mode is OFF (the computer is thinking
1547 * on opponents time) and the program is out of book, then make the 'hint'
1548 * move on the board and call SelectMove() to find a response. The user
1549 * terminates the search by entering ^C (quit siqnal) before entering a
1550 * command. If the opponent does not make the hint move, then set Sdepth to
1555 InputCommand(char *command)
1558 short have_shown_prompt = false;
1559 short ok, done, is_move = false;
1563 ok = flag.quit = done = false;
1567 if (TTadd > ttbllimit)
1571 if ((hint > 0) && !flag.easy && !flag.force)
1574 * A hint move for the player is available. Compute a move for the
1575 * opponent in background mode assuming that the hint move will be
1576 * selected by the player.
1579 ft = time0; /* Save reference time for the player. */
1581 algbr((short) hint >> 8, (short) hint & 0xff, false);
1582 strcpy(s, mvstr[0]);
1589 /* do the hint move */
1590 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1594 #ifdef QUIETBACKGROUND
1596 have_shown_prompt = true;
1597 #endif /* QUIETBACKGROUND */
1599 /* Start computing a move until the search is interrupted. */
1601 #ifdef INTERRUPT_TEST
1605 /* would love to put null move in here */
1606 /* after we make the hint move make a 2 ply search
1607 * with both plys our moves */
1608 /* think on opponents time */
1609 SelectMove(computer, BACKGROUND_MODE);
1611 #ifdef INTERRUPT_TEST
1612 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1616 printf("searching not terminated by interrupt!\n");
1620 printf("elapsed time from interrupt to "
1621 "terminating search: %ld\n", it);
1625 /* undo the hint and carry on */
1626 VerifyMove(s, UNMAKE_MODE, &mv);
1630 time0 = ft; /* Restore reference time for the player. */
1633 while(!(ok || flag.quit || done))
1637 #ifdef QUIETBACKGROUND
1638 if (!have_shown_prompt)
1640 #endif /* QUIETBACKGROUND */
1644 #ifdef QUIETBACKGROUND
1647 have_shown_prompt = false;
1648 #endif /* QUIETBACKGROUND */
1650 if (command == NULL) {
1654 eof = dsp->GetString(sx);
1656 strcpy(sx, command);
1660 sscanf(sx, "%s", s);
1668 if (strcmp(s, "bd") == 0) /* bd -- display board */
1670 /* FIXME: Hack alert! */
1671 short old_xshogi = XSHOGI;
1674 display_type = DISPLAY_RAW;
1677 dsp->UpdateDisplay(0, 0, 1, 0);
1680 display_type = DISPLAY_X;
1682 else if (strcmp(s, "post") == 0)
1684 flag.post = !flag.post;
1686 else if (strcmp(s, "alg") == 0)
1688 /* noop */ ; /* alg */
1690 else if ((strcmp(s, "quit") == 0)
1691 || (strcmp(s, "exit") == 0))
1696 else if (strcmp(s, "post") == 0)
1698 flag.post = !flag.post;
1701 else if ((strcmp(s, "set") == 0)
1702 || (strcmp(s, "edit") == 0))
1706 else if ((strcmp(s, "setup") == 0))
1710 else if (strcmp(s, "first") == 0)
1714 else if (strcmp(s, "go") == 0)
1719 if (computer == black)
1730 else if (strcmp(s, "help") == 0)
1734 else if (strcmp(s, "material") == 0)
1736 flag.material = !flag.material;
1738 else if (strcmp(s, "force") == 0)
1743 flag.bothsides = false;
1747 flag.force = !flag.force;
1748 flag.bothsides = false;
1751 else if (strcmp(s, "book") == 0)
1753 Book = Book ? 0 : BOOKFAIL;
1755 else if (strcmp(s, "new") == 0)
1758 dsp->UpdateDisplay(0, 0, 1, 0);
1760 else if (strcmp(s, "list") == 0)
1764 else if ((strcmp(s, "level") == 0)
1765 || (strcmp(s, "clock") == 0))
1767 dsp->SelectLevel(sx);
1769 else if (strcmp(s, "hash") == 0)
1771 flag.hash = !flag.hash;
1773 else if (strcmp(s, "gamein") == 0)
1775 flag.gamein = !flag.gamein;
1777 else if (strcmp(s, "beep") == 0)
1779 flag.beep = !flag.beep;
1781 else if (strcmp(s, "time") == 0)
1785 else if (strcmp(s, "otime") == 0)
1789 else if (strcmp(s, "Awindow") == 0)
1791 dsp->ChangeAlphaWindow();
1793 else if (strcmp(s, "Bwindow") == 0)
1795 dsp->ChangeBetaWindow();
1797 else if (strcmp(s, "rcptr") == 0)
1799 flag.rcptr = !flag.rcptr;
1801 else if (strcmp(s, "hint") == 0)
1805 else if (strcmp(s, "both") == 0)
1807 flag.bothsides = !flag.bothsides;
1810 ElapsedTime(COMPUTE_AND_INIT_MODE);
1811 SelectMove(opponent, FOREGROUND_MODE);
1814 else if (strcmp(s, "reverse") == 0)
1816 flag.reverse = !flag.reverse;
1818 dsp->UpdateDisplay(0, 0, 1, 0);
1820 else if (strcmp(s, "switch") == 0)
1822 computer = computer ^ 1;
1823 opponent = opponent ^ 1;
1824 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1829 else if (strcmp(s, "black") == 0)
1838 * ok = true; don't automatically start with black command
1841 else if (strcmp(s, "white") == 0)
1850 * ok = true; don't automatically start with white command
1853 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1857 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1862 /* CHECKME: are these next three correct? */
1863 else if (!XSHOGI && strcmp(s, "xget") == 0)
1867 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1871 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1875 #ifdef EASY_OPENINGS
1876 else if ((strcmp(s, "?") == 0)
1877 || (strcmp(s, "!") == 0)
1878 || (strcmp(s, "~") == 0))
1880 else if ((strcmp(s, "?") == 0)
1881 || (strcmp(s, "!") == 0))
1886 else if (strcmp(s, "get") == 0)
1890 else if (strcmp(s, "save") == 0)
1894 else if (strcmp(s, "depth") == 0)
1896 dsp->ChangeSearchDepth();
1898 else if (strcmp(s, "hashdepth") == 0)
1900 dsp->ChangeHashDepth();
1902 else if (strcmp(s, "random") == 0)
1906 else if (strcmp(s, "hard") == 0)
1910 else if (strcmp(s, "easy") == 0)
1912 flag.easy = !flag.easy;
1914 else if (strcmp(s, "tsume") == 0)
1916 flag.tsume = !flag.tsume;
1918 else if (strcmp(s, "contempt") == 0)
1922 else if (strcmp(s, "xwndw") == 0)
1924 dsp->ChangeXwindow();
1926 else if (strcmp(s, "rv") == 0)
1929 dsp->UpdateDisplay(0, 0, 1, 0);
1931 else if (strcmp(s, "coords") == 0)
1933 flag.coords = !flag.coords;
1934 dsp->UpdateDisplay(0, 0, 1, 0);
1936 else if (strcmp(s, "stars") == 0)
1938 flag.stars = !flag.stars;
1939 dsp->UpdateDisplay(0, 0, 1, 0);
1941 else if (!XSHOGI && strcmp(s, "moves") == 0)
1948 extern unsigned short PrVar[MAXDEPTH];
1950 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1957 dsp->ShowMessage("Testing MoveList Speed");
1958 temp = generate_move_flags;
1959 generate_move_flags = true;
1960 TestSpeed(MoveList, 1);
1961 generate_move_flags = temp;
1962 dsp->ShowMessage("Testing CaptureList Speed");
1963 TestSpeed(CaptureList, 1);
1964 dsp->ShowMessage("Testing Eval Speed");
1965 ExaminePosition(opponent);
1966 TestPSpeed(ScorePosition, 1);
1968 else if (!XSHOGI && strcmp(s, "test") == 0)
1971 dsp->ShowMessage("Testing MoveList Speed");
1972 TestSpeed(MoveList, 2000);
1973 dsp->ShowMessage("Testing CaptureList Speed");
1974 TestSpeed(CaptureList, 3000);
1975 dsp->ShowMessage("Testing Eval Speed");
1976 ExaminePosition(opponent);
1977 TestPSpeed(ScorePosition, 1500);
1979 dsp->ShowMessage("Testing MoveList Speed");
1980 TestSpeed(MoveList, 20000);
1981 dsp->ShowMessage("Testing CaptureList Speed");
1982 TestSpeed(CaptureList, 30000);
1983 dsp->ShowMessage("Testing Eval Speed");
1984 ExaminePosition(opponent);
1985 TestPSpeed(ScorePosition, 15000);
1988 else if (!XSHOGI && strcmp(s, "p") == 0)
1990 dsp->ShowPostnValues();
1992 else if (!XSHOGI && strcmp(s, "debug") == 0)
2002 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2004 /* check for repetition */
2005 short rpt = repetition();
2009 DRAW = DRAW_REPETITION;
2010 dsp->ShowMessage(DRAW);
2011 GameList[GameCnt].flags |= draw;
2025 ElapsedTime(COMPUTE_AND_INIT_MODE);
2029 computer = opponent;
2030 opponent = computer ^ 1;
2035 /* add remaining time in milliseconds for xshogi */
2038 printf("%d. %s %ld\n",
2039 ++mycnt2, s, TimeControl.clock[player] * 10);
2042 #ifdef notdef /* optional pass best line to frontend with move */
2043 # if !defined NOPOST
2045 if (flag.post && !flag.mate)
2049 printf(" %6d ", MSCORE);
2051 for (i = 1; MV[i] > 0; i++)
2053 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2054 printf("%5s ", mvstr[0]);
2067 SetTimeControl(void)
2071 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2072 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2073 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2077 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2078 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2081 flag.onemove = (TCmoves == 1);
2083 ElapsedTime(COMPUTE_AND_INIT_MODE);