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
9 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
11 * GNU SHOGI is based on GNU CHESS
13 * Copyright (c) 1988, 1989, 1990 John Stanback
14 * Copyright (c) 1992 Free Software Foundation
16 * This file is part of GNU SHOGI.
18 * GNU Shogi is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 3 of the License,
21 * or (at your option) any later version.
23 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 * You should have received a copy of the GNU General Public License along
29 * with GNU Shogi; see the file COPYING. If not, see
30 * <http://www.gnu.org/licenses/>.
31 * ----------------------------------------------------------------------
35 /* request *snprintf prototypes */
36 #define _POSIX_C_SOURCE 200112L
39 #if defined HAVE_GETTIMEOFDAY
46 #include <sys/param.h>
47 #include <sys/types.h>
55 struct display *dsp = &raw_display;
62 movealgbr(short m, char *s)
65 short piece = 0, flag = 0;
78 piece = f - NO_SQUARES;
80 if (piece > NO_PIECES)
83 flag = (dropmask | piece);
98 *s = COL_NAME(column(t));
100 *s = ROW_NAME(row(t));
105 *s = COL_NAME(column(f));
107 *s = ROW_NAME(row(f));
109 *s = COL_NAME(column(t));
111 *s = ROW_NAME(row(t));
130 #endif /* BOOKTEST */
134 * Generate move strings in different formats.
137 * - f piece to be moved
138 * - 0 < f < NO_SQUARES source square
139 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
140 * - t & 0x7f target square
141 * - t & 0x80 promotion flag
143 * - if flag & dropmask, piece type encoded in flag & pmask
145 * FIXME: that makes 2 ways to specify drops and promotions, why ?
152 algbr(short f, short t, short flag)
158 piece = f - NO_SQUARES;
160 if (f > (NO_SQUARES + NO_PIECES))
163 flag = (dropmask | piece);
172 if ((f == t) && ((f != 0) || (t != 0)))
175 dsp->Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
178 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
180 else if ((flag & dropmask) != 0)
182 short piece = flag & pmask;
184 mvstr[0][0] = pxx[piece];
185 mvstr[0][1] = xboard ? '@' : '*';
186 mvstr[0][2] = COL_NAME(column(t));
187 mvstr[0][3] = ROW_NAME(row(t));
189 strcpy(mvstr[1], mvstr[0]);
190 strcpy(mvstr[2], mvstr[0]);
191 strcpy(mvstr[3], mvstr[0]);
193 else if ((f != 0) || (t != 0))
195 /* pure coordinates notation */
196 mvstr[0][0] = COL_NAME(column(f));
197 mvstr[0][1] = ROW_NAME(row(f));
198 mvstr[0][2] = COL_NAME(column(t));
199 mvstr[0][3] = ROW_NAME(row(t));
202 /* algebraic notation without disambiguation */
203 mvstr[1][0] = pxx[board[f]];
204 mvstr[1][1] = mvstr[0][2]; /* to column */
205 mvstr[1][2] = mvstr[0][3]; /* to row */
208 /* algebraic notation with row disambiguation */
209 mvstr[2][0] = mvstr[1][0];
210 mvstr[2][1] = mvstr[0][1];
211 mvstr[2][2] = mvstr[0][2]; /* to column */
212 mvstr[2][3] = mvstr[0][3]; /* to row */
215 /* algebraic notation with column disambiguation */
216 strcpy(mvstr[3], mvstr[2]);
217 mvstr[3][1] = mvstr[0][0];
221 strcat(mvstr[0], "+");
222 strcat(mvstr[1], "+");
223 strcat(mvstr[2], "+");
224 strcat(mvstr[3], "+");
229 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
235 * Compare the string 's' to the list of legal moves available for the
236 * opponent. If a match is found, make the move on the board.
240 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
242 static short pnt, tempb, tempc, tempsf, tempst, cnt;
243 static struct leaf xnode;
245 short i, l, local_flags;
248 /* check and remove quality flags */
249 for (i = local_flags = 0, l = strlen(s); i < l; i++)
254 local_flags |= badmove;
259 local_flags |= goodmove;
265 local_flags |= difficult;
274 if (iop == UNMAKE_MODE)
276 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
282 if (iop == VERIFY_AND_MAKE_MODE)
283 generate_move_flags = true;
285 MoveList(opponent, 2, -1, true);
286 generate_move_flags = false;
289 while (pnt < TrPnt[3])
292 algbr(node->f, node->t, (short) node->flags);
294 if ((strcmp(s, mvstr[0]) == 0)
295 || (strcmp(s, mvstr[1]) == 0)
296 || (strcmp(s, mvstr[2]) == 0)
297 || (strcmp(s, mvstr[3]) == 0))
304 if ((cnt == 1) && (xnode.score > DONTUSE))
308 MakeMove(opponent, &xnode, &tempb, &tempc,
309 &tempsf, &tempst, &INCscore);
311 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
313 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
314 dsp->AlwaysShowMessage("Illegal move (in check): %s", s);
319 if (iop == VERIFY_AND_TRY_MODE)
322 dsp->UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
323 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
324 GameList[GameCnt].nodes = 0;
325 ElapsedTime(COMPUTE_AND_INIT_MODE);
326 GameList[GameCnt].time = (short) (et + 50)/100;
327 GameList[GameCnt].flags |= local_flags;
331 TimeControl.clock[opponent] -= et;
332 timeopp[oppptr] = et;
333 --TimeControl.moves[opponent];
336 *mv = (xnode.f << 8) | xnode.t;
337 algbr(xnode.f, xnode.t, false);
339 /* in force mode, check for mate conditions */
342 if (IsCheckmate(opponent ^ 1, -1, -1))
346 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
347 dsp->ShowMessage(buf);
356 dsp->AlwaysShowMessage("Illegal move (no match): %s", s);
358 if (!XSHOGI && (cnt > 1))
360 sprintf(buffer, "Ambiguous Move %s!", s);
361 dsp->ShowMessage(buffer);
369 parser(char *f, short *fpiece)
377 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
379 if (f[0] == pxx[i] || f[0] == qxx[i])
381 *fpiece = (p ? promoted[i] : unpromoted[i]);
386 if (f[1] == '*' || f[1] == '\'')
391 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
399 p = (f[5] == '+') ? 0x80 : 0;
401 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
409 while (*InPtr != ' ')
412 while (*InPtr == ' ')
420 while (*InPtr == ' ')
425 void RequestInputString(char* buffer, unsigned bufsize)
428 int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
430 perror("RequestInputString snprintf");
433 if (ret >= sizeof(fmt)) {
435 "Insufficient format-buffer size in %s for bufsize=%u\n",
436 __FUNCTION__, bufsize);
439 dsp->doRequestInputString(fmt, buffer);
453 strcpy(fname, savefile);
455 dsp->ShowMessage("Enter file name: ");
456 RequestInputString(fname, sizeof(fname)-1);
459 if (fname[0] == '\0')
460 strcpy(fname, "shogi.000");
462 if ((fd = fopen(fname, "r")) != NULL)
465 fgets(fname, 256, fd);
466 computer = opponent = black;
475 /* FIXME: write a skipn() function so that we can get
476 * 3 skips by doing skipn(3) */
480 Game50 = atoi(InPtr);
482 flag.force = (*InPtr == 'f');
483 fgets(fname, 256, fd); /* empty */
484 fgets(fname, 256, fd);
487 TCflag = atoi(InPtr);
491 OperatorTime = atoi(InPtr);
492 fgets(fname, 256, fd);
495 TimeControl.clock[black] = atol(InPtr);
498 TimeControl.moves[black] = atoi(InPtr);
499 fgets(fname, 256, fd);
502 TimeControl.clock[white] = atol(InPtr);
505 TimeControl.moves[white] = atoi(InPtr);
506 fgets(fname, 256, fd); /* empty */
508 for (i = NO_ROWS - 1; i > -1; i--)
510 fgets(fname, 256, fd);
514 for (j = 0; j < NO_COLS; j++)
516 sq = i * NO_COLS + j;
522 board[sq] = no_piece;
527 for (c = 0; c < NO_PIECES; c++)
532 board[sq] = promoted[c];
534 board[sq] = unpromoted[c];
540 for (c = 0; c < NO_PIECES; c++)
545 board[sq] = promoted[c];
547 board[sq] = unpromoted[c];
555 Mvboard[sq] = atoi(InPtr);
560 fgets(fname, 256, fd); /* empty */
561 fgets(fname, 256, fd); /* 9 8 7 ... */
562 fgets(fname, 256, fd); /* empty */
563 fgets(fname, 256, fd); /* p l n ... */
566 for (side = 0; side <= 1; side++)
568 fgets(fname, 256, fd);
572 Captured[side][pawn] = atoi(InPtr);
575 Captured[side][lance] = atoi(InPtr);
577 Captured[side][knight] = atoi(InPtr);
580 Captured[side][silver] = atoi(InPtr);
582 Captured[side][gold] = atoi(InPtr);
584 Captured[side][bishop] = atoi(InPtr);
586 Captured[side][rook] = atoi(InPtr);
588 Captured[side][king] = atoi(InPtr);
592 flag.regularstart = true;
594 fgets(fname, 256, fd); /* empty */
595 fgets(fname, 256, fd); /* move score ... */
597 while (fgets(fname, 256, fd))
606 g = &GameList[GameCnt];
607 g->gmove = parser(InPtr, &g->fpiece);
609 g->score = atoi(InPtr);
611 g->depth = atoi(InPtr);
613 g->nodes = atol(InPtr);
615 g->time = atol(InPtr);
617 g->flags = c = atoi(InPtr);
619 g->hashkey = strtol(InPtr, (char **) NULL, 16);
621 g->hashbd = strtol(InPtr, (char **) NULL, 16);
629 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
631 if (pxx[i] == *InPtr)
639 g->color = ((*InPtr == 'W') ? white : black);
641 g->piece = (*InPtr == '+'
643 : unpromoted[piece]);
652 if (TimeControl.clock[black] > 0)
660 dsp->UpdateDisplay(0, 0, 1, 0);
671 short sq, i, c, f, t;
674 char empty[2] = "\n";
677 strcpy(fname, savefile);
679 dsp->ShowMessage("Enter file name: ");
680 RequestInputString(fname, sizeof(fname)-1);
683 if (fname[0] == '\0')
684 strcpy(fname, "shogi.000");
686 if ((fd = fopen(fname, "w")) != NULL)
691 if (computer == white)
694 if (computer == black)
697 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
698 flag.force ? "force" : "");
700 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
701 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
702 TimeControl.clock[black], TimeControl.moves[black],
703 TimeControl.clock[white], TimeControl.moves[white]);
706 for (i = NO_ROWS - 1; i > -1; i--)
708 fprintf(fd, "%c ", ROW_NAME(i));
710 for (c = 0; c < NO_COLS; c++)
712 sq = i * NO_COLS + c;
714 p = is_promoted[piece] ? '+' : ' ';
715 fprintf(fd, "%c", p);
731 fprintf(fd, "%c", p);
736 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
737 fprintf(fd, " %d", Mvboard[f]);
744 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
746 fprintf(fd, " p l n s g b r k\n");
748 fprintf(fd, " 5 4 3 2 1\n");
750 fprintf(fd, " p s g b r k\n");
753 for (side = 0; side <= 1; side++)
755 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
756 fprintf(fd, " %2d", Captured[side][pawn]);
758 fprintf(fd, " %2d", Captured[side][lance]);
759 fprintf(fd, " %2d", Captured[side][knight]);
761 fprintf(fd, " %2d", Captured[side][silver]);
762 fprintf(fd, " %2d", Captured[side][gold]);
763 fprintf(fd, " %2d", Captured[side][bishop]);
764 fprintf(fd, " %2d", Captured[side][rook]);
765 fprintf(fd, " %2d", Captured[side][king]);
770 fputs(" move score depth nodes time flags capture\n", fd);
772 for (i = 1; i <= GameCnt; i++)
774 struct GameRec *g = &GameList[i];
777 t = (g->gmove & 0xFF);
778 algbr(f, t, g->flags);
780 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
783 : (is_promoted[g->fpiece] ? '+' : ' ')),
785 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
787 g->nodes, g->time, g->flags,
788 g->hashkey, g->hashbd);
790 if (g->piece != no_piece)
792 fprintf(fd, " %c %s %c\n",
793 pxx[g->piece], ColorStr[g->color],
794 (is_promoted[g->piece] ? '+' : ' '));
804 dsp->ShowMessage("Game saved");
808 dsp->ShowMessage("Could not open file");
814 * GetXGame, SaveXGame and BookGame used to only be defined if
815 * xshogi wasn't defined -- wonder why?
827 dsp->ShowMessage("Enter file name: ");
828 RequestInputString(fname, sizeof(fname)-1);
830 if (fname[0] == '\0')
831 strcpy(fname, "xshogi.position.read");
833 if ((fd = fopen(fname, "r")) != NULL)
836 flag.regularstart = false;
839 /* xshogi position file ... */
840 fgets(fname, 256, fd);
845 if (strcmp(fname, "xshogi"))
849 /* -- empty line -- */
850 fgets(fname, 256, fd);
851 /* -- empty line -- */
852 fgets(fname, 256, fd);
854 for (i = NO_ROWS - 1; i > -1; i--)
856 fgets(fname, 256, fd);
859 for (j = 0; j < NO_COLS; j++)
861 sq = i * NO_COLS + j;
867 board[sq] = no_piece;
872 for (c = 0; c < NO_PIECES; c++)
877 board[sq] = promoted[c];
879 board[sq] = unpromoted[c];
885 for (c = 0; c < NO_PIECES; c++)
890 board[sq] = promoted[c];
892 board[sq] = unpromoted[c];
905 for (side = 0; side <= 1; side++)
907 fgets(fname, 256, fd);
909 Captured[side][pawn] = atoi(InPtr);
912 Captured[side][lance] = atoi(InPtr);
914 Captured[side][knight] = atoi(InPtr);
917 Captured[side][silver] = atoi(InPtr);
919 Captured[side][gold] = atoi(InPtr);
921 Captured[side][bishop] = atoi(InPtr);
923 Captured[side][rook] = atoi(InPtr);
925 Captured[side][king] = atoi(InPtr);
928 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
941 dsp->UpdateDisplay(0, 0, 1, 0);
956 dsp->ShowMessage("Enter file name: ");
957 RequestInputString(fname, sizeof(fname)-1);
959 if (fname[0] == '\0')
960 strcpy(fname, "xshogi.position.read");
962 if ((fd = fopen(fname, "w")) != NULL)
964 fputs("# xshogi position file -- \n", fd);
968 for (i = NO_ROWS - 1; i > -1; i--)
972 for (j = 0; j < NO_COLS; j++)
974 sq = i * NO_COLS + j;
976 isp = is_promoted[piece];
977 *p = (isp ? '+' : ' ');
980 if (piece == no_piece)
982 else if (color[sq] == white)
995 for (side = 0; side <= 1; side++)
999 "%d %d %d %d %d %d %d %d\n",
1001 "%d %d %d %d %d %d\n",
1003 Captured[side][pawn],
1005 Captured[side][lance],
1006 Captured[side][knight],
1008 Captured[side][silver],
1009 Captured[side][gold],
1010 Captured[side][bishop],
1011 Captured[side][rook],
1012 Captured[side][king]);
1017 if (computer == black)
1018 fputs("white to play\n", fd);
1020 fputs("black to play\n", fd);
1031 char fname[256], sflags[4];
1035 strcpy(fname, savefile);
1037 /* Enter file name */
1038 dsp->ShowMessage("Enter file name: ");
1039 RequestInputString(fname, sizeof(fname)-1);
1042 if (fname[0] == '\0') {
1043 dsp->AlwaysShowMessage("aborting book save");
1047 if ((fd = fopen(fname, "a")) != NULL)
1051 for (i = 1; i <= GameCnt; i++)
1053 struct GameRec *g = &GameList[i];
1054 char mvnr[20], mvs[20];
1057 sprintf(mvnr, "%d.", (i + 1)/2);
1062 t = (g->gmove & 0xFF);
1063 algbr(f, t, g->flags);
1066 /* determine move quality string */
1067 if (g->flags & goodmove)
1070 if (g->flags & badmove)
1073 #ifdef EASY_OPENINGS
1074 if (g->flags & difficult)
1080 /* determine move string */
1083 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1087 sprintf(mvs, "%c%c%c%c%c%s%s ",
1088 mvstr[0][0], mvstr[0][1],
1089 (g->flags & capture) ? 'x' : '-',
1090 mvstr[0][2], mvstr[0][3],
1091 (mvstr[0][4] == '+') ? "+" : "",
1095 fprintf(fd, "%s%s%c%s",
1099 : (is_promoted[g->fpiece] ? "+" : "")),
1112 dsp->ShowMessage("Game saved");
1116 dsp->ShowMessage("Could not open file");
1127 char fname[256], dbuf[256];
1131 strcpy(fname, listfile);
1136 strncpy(dbuf, ctime(&when), 20);
1143 /* use format "CL.Jan01-020304B" when
1146 program played white */
1148 sprintf(fname, "CL.%s%s-%s%s%s%c",
1149 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1150 dbuf + 17, ColorStr[computer][0]);
1152 /* replace space padding with 0 */
1153 for (i = 0; fname[i] != '\0'; i++)
1155 if (fname[i] == ' ')
1160 fd = fopen(fname, "w");
1164 printf("Open failure for file: %s", fname);
1168 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1169 fputs(" score depth nodes time ", fd);
1170 fputs(" score depth nodes time\n", fd);
1172 for (i = 1; i <= GameCnt; i++)
1174 f = GameList[i].gmove >> 8;
1175 t = (GameList[i].gmove & 0xFF);
1176 algbr(f, t, GameList[i].flags);
1178 if (GameList[i].flags & book)
1180 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1183 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1184 pxx[GameList[i].fpiece],
1186 ? &mvstr[0][1] : mvstr[0]),
1193 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1196 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1197 pxx[GameList[i].fpiece],
1198 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1199 GameList[i].score, GameList[i].depth,
1200 GameList[i].nodes, GameList[i].time);
1213 fprintf(fd, "\n\n");
1215 if (GameList[GameCnt].flags & draw)
1217 fprintf(fd, "Draw %s\n", DRAW);
1219 if (DRAW == DRAW_REPETITION)
1223 fprintf(fd, "repetition by positions ");
1225 for (j = GameCnt - 1; j >= Game50; j -= 2)
1227 if (GameList[j].hashkey == hashkey &&
1228 GameList[j].hashbd == hashbd)
1229 fprintf(fd, "%d ", j);
1235 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1237 fprintf(fd, "%s\n", ColorStr[player ]);
1239 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1241 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1254 GameList[GameCnt].flags |= badmove;
1258 GameList[GameCnt].flags |= goodmove;
1261 #ifdef EASY_OPENINGS
1263 GameList[GameCnt].flags |= difficult;
1271 * Undo the most recent half-move.
1279 f = GameList[GameCnt].gmove >> 8;
1280 t = GameList[GameCnt].gmove & 0x7F;
1284 /* the move was a drop */
1285 Captured[color[t]][board[t]]++;
1286 board[t] = no_piece;
1292 if (GameList[GameCnt].flags & promote)
1293 board[f] = unpromoted[board[t]];
1295 board[f] = board[t];
1297 color[f] = color[t];
1298 board[t] = GameList[GameCnt].piece;
1299 color[t] = GameList[GameCnt].color;
1301 if (board[t] != no_piece)
1302 Captured[color[f]][unpromoted[board[t]]]--;
1304 if (color[t] != neutral)
1312 if (TCflag && (TCmoves > 1))
1313 ++TimeControl.moves[color[f]];
1315 hashkey = GameList[GameCnt].hashkey;
1316 hashbd = GameList[GameCnt].hashbd;
1318 computer = computer ^ 1;
1319 opponent = opponent ^ 1;
1322 player = player ^ 1;
1323 dsp->ShowSidetoMove();
1324 dsp->UpdateDisplay(0, 0, 1, 0);
1326 if (flag.regularstart)
1332 TestSpeed(void(*f)(short side, short ply,
1333 short in_check, short blockable),
1343 #ifdef HAVE_GETTIMEOFDAY
1347 #ifdef HAVE_GETTIMEOFDAY
1348 gettimeofday(&tv, NULL);
1349 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1354 for (i = 0; i < j; i++)
1356 f(opponent, 2, -1, true);
1359 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1361 if (!pick(jj, TrPnt[3] - 1))
1367 #ifdef HAVE_GETTIMEOFDAY
1368 gettimeofday(&tv, NULL);
1369 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1374 cnt = j * (TrPnt[3] - TrPnt[2]);
1381 dsp->ShowNodeCnt(cnt);
1386 TestPSpeed(short(*f) (short side), unsigned j)
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++)
1404 #ifdef HAVE_GETTIMEOFDAY
1405 gettimeofday(&tv, NULL);
1406 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1418 dsp->ShowNodeCnt(cnt);
1423 SetOppTime(char *time)
1427 t = (int)strtol(time, &time, 10);
1432 /* FIXME: sec is parsed but ignored */
1433 (void)strtol(time, &time, 10);
1436 m = (int)strtol(time, &time, 10);
1439 TimeControl.clock[opponent] = t;
1442 TimeControl.moves[opponent] = m;
1444 ElapsedTime(COMPUTE_AND_INIT_MODE);
1448 /* just to inform xshogi about availability of otime command */
1449 printf("otime %d %d\n", t, m);
1455 SetMachineTime(char *time)
1459 t = (int)strtol(time, &time, 10);
1464 /* FIXME: sec is parsed but ignored */
1465 (void)strtol(time, &time, 10);
1468 m = (int)strtol(time, &time, 10);
1471 TimeControl.clock[computer] = t;
1474 TimeControl.moves[computer] = m;
1476 ElapsedTime(COMPUTE_AND_INIT_MODE);
1480 /* just to inform xshogi about availability of time command */
1481 printf("time %d %d\n", t, m);
1486 /* FIXME! This is truly the function from hell! */
1489 * Process the user's command. If easy mode is OFF (the computer is thinking
1490 * on opponents time) and the program is out of book, then make the 'hint'
1491 * move on the board and call SelectMove() to find a response. The user
1492 * terminates the search by entering a command. If the opponent does not make
1493 * the hint move, then set Sdepth to zero.
1497 InputCommand(char *command, int root)
1499 #ifdef QUIETBACKGROUND
1500 short have_shown_prompt = false;
1502 short ok, done, is_move = false;
1505 static char backlog[80];
1507 ok = flag.quit = done = false;
1511 if (TTadd > ttbllimit)
1515 if ((hint > 0) && !flag.easy && !flag.force && !command && !backlog[0] && root)
1518 * A hint move for the player is available. Compute a move for the
1519 * opponent in background mode assuming that the hint move will be
1520 * selected by the player.
1523 ft = time0; /* Save reference time for the player. */
1525 algbr((short) hint >> 8, (short) hint & 0xff, false);
1526 strcpy(s, mvstr[0]);
1531 /* do the hint move */
1532 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1536 #ifdef QUIETBACKGROUND
1538 have_shown_prompt = true;
1539 #endif /* QUIETBACKGROUND */
1541 /* Start computing a move until the search is interrupted. */
1543 #ifdef INTERRUPT_TEST
1547 /* would love to put null move in here */
1548 /* after we make the hint move make a 2 ply search
1549 * with both plys our moves */
1550 /* think on opponents time */
1551 SelectMove(computer, BACKGROUND_MODE);
1553 #ifdef INTERRUPT_TEST
1554 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1558 printf("searching not terminated by interrupt!\n");
1562 printf("elapsed time from interrupt to "
1563 "terminating search: %ld\n", it);
1567 /* undo the hint and carry on */
1568 VerifyMove(s, UNMAKE_MODE, &mv);
1572 time0 = ft; /* Restore reference time for the player. */
1575 while(!(ok || flag.quit || done))
1579 if (flag.analyze && !command && !backlog[0] && root) {
1580 SelectMove(opponent, BACKGROUND_MODE);
1583 #ifdef QUIETBACKGROUND
1584 if (!have_shown_prompt)
1586 #endif /* QUIETBACKGROUND */
1590 #ifdef QUIETBACKGROUND
1593 have_shown_prompt = false;
1594 #endif /* QUIETBACKGROUND */
1596 if (!command && backlog[0]) command = backlog; /* pick up backlogged command */
1598 if (command == NULL) {
1599 int eof = dsp->GetString(sx);
1603 strcpy(sx, command);
1604 backlog[0]= '\0'; /* make sure no backlog is left */
1608 /* extract first word */
1609 if (sscanf(sx, "%s", s) < 1)
1612 if (!root && strcmp(s, "."))
1613 { /* during search most commands can only be done after abort */
1614 strcpy(backlog, sx); /* backlog the command */
1615 return true; /* and order search abort */
1618 if (strcmp(s, "bd") == 0) /* bd -- display board */
1620 /* FIXME: Hack alert! */
1621 short old_xshogi = XSHOGI;
1624 display_type = DISPLAY_RAW;
1627 dsp->UpdateDisplay(0, 0, 1, 0);
1630 display_type = DISPLAY_X;
1632 else if (strcmp(s, "post") == 0)
1634 flag.post = (xboard ? 1 : !flag.post);
1636 else if (strcmp(s, "nopost") == 0)
1641 else if (strcmp(s, "variant") == 0)
1642 { /* only variant we play is minishogi */
1643 printf("setup (P.BR.S...G.+.++.+Kp.br.s...g.+.++.+k) 5x5+5_shogi rbsgk/4p/5/P4/KGSBR [-] w 0 1\n");
1646 else if (strcmp(s, "alg") == 0 ||
1647 strcmp(s, "accepted") == 0 || strcmp(s, "rejected") == 0 ||
1648 strcmp(s, "variant") == 0 || strcmp(s, "computer") == 0)
1652 else if ((strcmp(s, "quit") == 0) ||
1653 (strcmp(s, "exit") == 0) && !xboard)
1657 else if (strcmp(s, "xboard") == 0)
1660 strcpy(ColorStr[0], "White");
1661 strcpy(ColorStr[1], "Black");
1663 else if (strcmp(s, "protover") == 0)
1665 printf("feature myname=\"GNU %s %s\" ",
1673 printf("variants=\"%s\" ",
1675 "5x5+5_shogi,minishogi"
1680 printf("debug=1 setboard=0 sigint=0 done=1\n");
1682 else if (strcmp(s, ".") == 0)
1683 { // periodic update request of analysis info: send stat01 info
1685 algbr((short)(currentMove >> 8), (short)(currentMove & 0xFF), 0);
1686 printf("stat01: %4ld %8ld %2d %2d %2d %s\n",
1687 et, NodeCnt, Sdepth, movesLeft, TrPnt[2]-TrPnt[1], mvstr[0]);
1689 if (!root) return false; /* signal no abort needed */
1691 else if (strcmp(s, "exit") == 0)
1693 flag.analyze = false;
1696 else if (strcmp(s, "analyze") == 0)
1698 flag.analyze = true;
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)
1766 dsp->SelectLevel(sx + strlen("level"));
1768 else if (strcmp(s, "clock") == 0)
1770 dsp->SelectLevel(sx + strlen("clock"));
1772 else if (strcmp(s, "hash") == 0)
1774 flag.hash = !flag.hash;
1776 else if (strcmp(s, "gamein") == 0)
1778 flag.gamein = !flag.gamein;
1780 else if (strcmp(s, "beep") == 0)
1782 flag.beep = !flag.beep;
1784 else if (strcmp(s, "time") == 0)
1786 SetMachineTime(sx + strlen("time"));
1788 else if ((strcmp(s, "otime") == 0) ||
1789 (xboard && (strcmp(s, "otim")) == 0))
1791 SetOppTime(sx + strlen("otime"));
1793 else if (strcmp(s, "Awindow") == 0)
1795 dsp->ChangeAlphaWindow();
1797 else if (strcmp(s, "Bwindow") == 0)
1799 dsp->ChangeBetaWindow();
1801 else if (strcmp(s, "rcptr") == 0)
1803 flag.rcptr = !flag.rcptr;
1805 else if (strcmp(s, "hint") == 0)
1809 else if (strcmp(s, "both") == 0)
1811 flag.bothsides = !flag.bothsides;
1814 ElapsedTime(COMPUTE_AND_INIT_MODE);
1815 SelectMove(opponent, FOREGROUND_MODE);
1818 else if (strcmp(s, "reverse") == 0)
1820 flag.reverse = !flag.reverse;
1822 dsp->UpdateDisplay(0, 0, 1, 0);
1824 else if (strcmp(s, "switch") == 0)
1826 computer = computer ^ 1;
1827 opponent = opponent ^ 1;
1828 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1832 dsp->UpdateDisplay(0, 0, 1, 0);
1834 else if (xboard ? strcmp(s, "white") == 0 : strcmp(s, "black") == 0)
1843 * ok = true; don't automatically start with black command
1846 else if (xboard ? strcmp(s, "black") == 0 : strcmp(s, "white") == 0)
1855 * ok = true; don't automatically start with white command
1858 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1862 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1867 /* CHECKME: are these next three correct? */
1868 else if (!XSHOGI && strcmp(s, "xget") == 0)
1872 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1876 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1880 #ifdef EASY_OPENINGS
1881 else if ((strcmp(s, "?") == 0) ||
1882 (strcmp(s, "!") == 0) ||
1883 (strcmp(s, "~") == 0))
1885 else if ((strcmp(s, "?") == 0) ||
1886 (strcmp(s, "!") == 0))
1891 else if (strcmp(s, "get") == 0)
1895 else if (strcmp(s, "save") == 0)
1899 else if (strcmp(s, "depth") == 0)
1901 dsp->ChangeSearchDepth(sx + strlen("depth"));
1903 else if (strcmp(s, "sd") == 0)
1905 dsp->ChangeSearchDepth(sx + strlen("sd"));
1907 else if (strcmp(s, "hashdepth") == 0)
1909 dsp->ChangeHashDepth();
1911 else if (strcmp(s, "random") == 0)
1915 else if (strcmp(s, "hard") == 0)
1919 else if (strcmp(s, "easy") == 0)
1921 flag.easy = !flag.easy;
1923 else if (strcmp(s, "tsume") == 0)
1925 flag.tsume = !flag.tsume;
1927 else if (strcmp(s, "contempt") == 0)
1931 else if (strcmp(s, "xwndw") == 0)
1933 dsp->ChangeXwindow();
1935 else if (strcmp(s, "rv") == 0)
1938 dsp->UpdateDisplay(0, 0, 1, 0);
1940 else if (strcmp(s, "coords") == 0)
1942 flag.coords = !flag.coords;
1943 dsp->UpdateDisplay(0, 0, 1, 0);
1945 else if (strcmp(s, "stars") == 0)
1947 flag.stars = !flag.stars;
1948 dsp->UpdateDisplay(0, 0, 1, 0);
1950 else if (!XSHOGI && strcmp(s, "moves") == 0)
1957 extern unsigned short PrVar[MAXDEPTH];
1959 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
1966 dsp->ShowMessage("Testing MoveList Speed");
1967 temp = generate_move_flags;
1968 generate_move_flags = true;
1969 TestSpeed(MoveList, 1);
1970 generate_move_flags = temp;
1971 dsp->ShowMessage("Testing CaptureList Speed");
1972 TestSpeed(CaptureList, 1);
1973 dsp->ShowMessage("Testing Eval Speed");
1974 ExaminePosition(opponent);
1975 TestPSpeed(ScorePosition, 1);
1977 else if (!XSHOGI && strcmp(s, "test") == 0)
1980 dsp->ShowMessage("Testing MoveList Speed");
1981 TestSpeed(MoveList, 2000);
1982 dsp->ShowMessage("Testing CaptureList Speed");
1983 TestSpeed(CaptureList, 3000);
1984 dsp->ShowMessage("Testing Eval Speed");
1985 ExaminePosition(opponent);
1986 TestPSpeed(ScorePosition, 1500);
1988 dsp->ShowMessage("Testing MoveList Speed");
1989 TestSpeed(MoveList, 20000);
1990 dsp->ShowMessage("Testing CaptureList Speed");
1991 TestSpeed(CaptureList, 30000);
1992 dsp->ShowMessage("Testing Eval Speed");
1993 ExaminePosition(opponent);
1994 TestPSpeed(ScorePosition, 15000);
1997 else if (!XSHOGI && strcmp(s, "p") == 0)
1999 dsp->ShowPostnValues();
2001 else if (!XSHOGI && strcmp(s, "debug") == 0)
2011 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2013 /* check for repetition */
2014 short rpt = repetition();
2018 DRAW = DRAW_REPETITION;
2019 dsp->ShowMessage(DRAW);
2020 GameList[GameCnt].flags |= draw;
2034 ElapsedTime(COMPUTE_AND_INIT_MODE);
2038 computer = opponent;
2039 opponent = computer ^ 1;
2044 /* add remaining time in milliseconds for xshogi */
2047 printf("%d. %s %ld\n",
2048 ++mycnt2, s, TimeControl.clock[player] * 10);