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;
288 if(s[4] == '=') s[4] = '\0'; /* deferral is implied */
290 while (pnt < TrPnt[3])
293 algbr(node->f, node->t, (short) node->flags);
295 if ((strcmp(s, mvstr[0]) == 0)
296 || (strcmp(s, mvstr[1]) == 0)
297 || (strcmp(s, mvstr[2]) == 0)
298 || (strcmp(s, mvstr[3]) == 0))
305 if ((cnt == 1) && (xnode.score > DONTUSE))
309 MakeMove(opponent, &xnode, &tempb, &tempc,
310 &tempsf, &tempst, &INCscore);
312 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
314 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
315 dsp->AlwaysShowMessage("Illegal move (in check): %s", s);
320 if (iop == VERIFY_AND_TRY_MODE)
323 dsp->UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
324 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
325 GameList[GameCnt].nodes = 0;
326 ElapsedTime(COMPUTE_AND_INIT_MODE);
327 GameList[GameCnt].time = (short) (et + 50)/100;
328 GameList[GameCnt].flags |= local_flags;
332 TimeControl.clock[opponent] -= et;
333 timeopp[oppptr] = et;
334 --TimeControl.moves[opponent];
337 *mv = (xnode.f << 8) | xnode.t;
338 algbr(xnode.f, xnode.t, false);
340 /* in force mode, check for mate conditions */
343 if (IsCheckmate(opponent ^ 1, -1, -1))
347 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
348 dsp->ShowMessage(buf);
357 dsp->AlwaysShowMessage("Illegal move (no match): %s", s);
359 if (!XSHOGI && (cnt > 1))
361 sprintf(buffer, "Ambiguous Move %s!", s);
362 dsp->ShowMessage(buffer);
370 parser(char *f, short *fpiece)
378 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
380 if (f[0] == pxx[i] || f[0] == qxx[i])
382 *fpiece = (p ? promoted[i] : unpromoted[i]);
387 if (f[1] == '*' || f[1] == '\'')
392 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
400 p = (f[5] == '+') ? 0x80 : 0;
402 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
410 while (*InPtr != ' ')
413 while (*InPtr == ' ')
421 while (*InPtr == ' ')
426 void RequestInputString(char* buffer, unsigned bufsize)
429 int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
431 perror("RequestInputString snprintf");
434 if (ret >= sizeof(fmt)) {
436 "Insufficient format-buffer size in %s for bufsize=%u\n",
437 __FUNCTION__, bufsize);
440 dsp->doRequestInputString(fmt, buffer);
454 strcpy(fname, savefile);
456 dsp->ShowMessage("Enter file name: ");
457 RequestInputString(fname, sizeof(fname)-1);
460 if (fname[0] == '\0')
461 strcpy(fname, "shogi.000");
463 if ((fd = fopen(fname, "r")) != NULL)
466 fgets(fname, 256, fd);
467 computer = opponent = black;
476 /* FIXME: write a skipn() function so that we can get
477 * 3 skips by doing skipn(3) */
481 Game50 = atoi(InPtr);
483 flag.force = (*InPtr == 'f');
484 fgets(fname, 256, fd); /* empty */
485 fgets(fname, 256, fd);
488 TCflag = atoi(InPtr);
492 OperatorTime = atoi(InPtr);
493 fgets(fname, 256, fd);
496 TimeControl.clock[black] = atol(InPtr);
499 TimeControl.moves[black] = atoi(InPtr);
500 fgets(fname, 256, fd);
503 TimeControl.clock[white] = atol(InPtr);
506 TimeControl.moves[white] = atoi(InPtr);
507 fgets(fname, 256, fd); /* empty */
509 for (i = NO_ROWS - 1; i > -1; i--)
511 fgets(fname, 256, fd);
515 for (j = 0; j < NO_COLS; j++)
517 sq = i * NO_COLS + j;
523 board[sq] = no_piece;
528 for (c = 0; c < NO_PIECES; c++)
533 board[sq] = promoted[c];
535 board[sq] = unpromoted[c];
541 for (c = 0; c < NO_PIECES; c++)
546 board[sq] = promoted[c];
548 board[sq] = unpromoted[c];
556 Mvboard[sq] = atoi(InPtr);
561 fgets(fname, 256, fd); /* empty */
562 fgets(fname, 256, fd); /* 9 8 7 ... */
563 fgets(fname, 256, fd); /* empty */
564 fgets(fname, 256, fd); /* p l n ... */
567 for (side = 0; side <= 1; side++)
569 fgets(fname, 256, fd);
573 Captured[side][pawn] = atoi(InPtr);
576 Captured[side][lance] = atoi(InPtr);
578 Captured[side][knight] = atoi(InPtr);
581 Captured[side][silver] = atoi(InPtr);
583 Captured[side][gold] = atoi(InPtr);
585 Captured[side][bishop] = atoi(InPtr);
587 Captured[side][rook] = atoi(InPtr);
589 Captured[side][king] = atoi(InPtr);
593 flag.regularstart = true;
595 fgets(fname, 256, fd); /* empty */
596 fgets(fname, 256, fd); /* move score ... */
598 while (fgets(fname, 256, fd))
607 g = &GameList[GameCnt];
608 g->gmove = parser(InPtr, &g->fpiece);
610 g->score = atoi(InPtr);
612 g->depth = atoi(InPtr);
614 g->nodes = atol(InPtr);
616 g->time = atol(InPtr);
618 g->flags = c = atoi(InPtr);
620 g->hashkey = strtol(InPtr, (char **) NULL, 16);
622 g->hashbd = strtol(InPtr, (char **) NULL, 16);
630 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
632 if (pxx[i] == *InPtr)
640 g->color = ((*InPtr == 'W') ? white : black);
642 g->piece = (*InPtr == '+'
644 : unpromoted[piece]);
653 if (TimeControl.clock[black] > 0)
661 dsp->UpdateDisplay(0, 0, 1, 0);
672 short sq, i, c, f, t;
675 char empty[2] = "\n";
678 strcpy(fname, savefile);
680 dsp->ShowMessage("Enter file name: ");
681 RequestInputString(fname, sizeof(fname)-1);
684 if (fname[0] == '\0')
685 strcpy(fname, "shogi.000");
687 if ((fd = fopen(fname, "w")) != NULL)
692 if (computer == white)
695 if (computer == black)
698 fprintf(fd, "White %s Black %s %d %s\n", w, b, Game50,
699 flag.force ? "force" : "");
701 fprintf(fd, "TimeControl %d Operator Time %d\n", TCflag, OperatorTime);
702 fprintf(fd, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
703 TimeControl.clock[black], TimeControl.moves[black],
704 TimeControl.clock[white], TimeControl.moves[white]);
707 for (i = NO_ROWS - 1; i > -1; i--)
709 fprintf(fd, "%c ", ROW_NAME(i));
711 for (c = 0; c < NO_COLS; c++)
713 sq = i * NO_COLS + c;
715 p = is_promoted[piece] ? '+' : ' ';
716 fprintf(fd, "%c", p);
732 fprintf(fd, "%c", p);
737 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
738 fprintf(fd, " %d", Mvboard[f]);
745 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
747 fprintf(fd, " p l n s g b r k\n");
749 fprintf(fd, " 5 4 3 2 1\n");
751 fprintf(fd, " p s g b r k\n");
754 for (side = 0; side <= 1; side++)
756 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
757 fprintf(fd, " %2d", Captured[side][pawn]);
759 fprintf(fd, " %2d", Captured[side][lance]);
760 fprintf(fd, " %2d", Captured[side][knight]);
762 fprintf(fd, " %2d", Captured[side][silver]);
763 fprintf(fd, " %2d", Captured[side][gold]);
764 fprintf(fd, " %2d", Captured[side][bishop]);
765 fprintf(fd, " %2d", Captured[side][rook]);
766 fprintf(fd, " %2d", Captured[side][king]);
771 fputs(" move score depth nodes time flags capture\n", fd);
773 for (i = 1; i <= GameCnt; i++)
775 struct GameRec *g = &GameList[i];
778 t = (g->gmove & 0xFF);
779 algbr(f, t, g->flags);
781 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
784 : (is_promoted[g->fpiece] ? '+' : ' ')),
786 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
788 g->nodes, g->time, g->flags,
789 g->hashkey, g->hashbd);
791 if (g->piece != no_piece)
793 fprintf(fd, " %c %s %c\n",
794 pxx[g->piece], ColorStr[g->color],
795 (is_promoted[g->piece] ? '+' : ' '));
805 dsp->ShowMessage("Game saved");
809 dsp->ShowMessage("Could not open file");
815 * GetXGame, SaveXGame and BookGame used to only be defined if
816 * xshogi wasn't defined -- wonder why?
828 dsp->ShowMessage("Enter file name: ");
829 RequestInputString(fname, sizeof(fname)-1);
831 if (fname[0] == '\0')
832 strcpy(fname, "xshogi.position.read");
834 if ((fd = fopen(fname, "r")) != NULL)
837 flag.regularstart = false;
840 /* xshogi position file ... */
841 fgets(fname, 256, fd);
846 if (strcmp(fname, "xshogi"))
850 /* -- empty line -- */
851 fgets(fname, 256, fd);
852 /* -- empty line -- */
853 fgets(fname, 256, fd);
855 for (i = NO_ROWS - 1; i > -1; i--)
857 fgets(fname, 256, fd);
860 for (j = 0; j < NO_COLS; j++)
862 sq = i * NO_COLS + j;
868 board[sq] = no_piece;
873 for (c = 0; c < NO_PIECES; c++)
878 board[sq] = promoted[c];
880 board[sq] = unpromoted[c];
886 for (c = 0; c < NO_PIECES; c++)
891 board[sq] = promoted[c];
893 board[sq] = unpromoted[c];
906 for (side = 0; side <= 1; side++)
908 fgets(fname, 256, fd);
910 Captured[side][pawn] = atoi(InPtr);
913 Captured[side][lance] = atoi(InPtr);
915 Captured[side][knight] = atoi(InPtr);
918 Captured[side][silver] = atoi(InPtr);
920 Captured[side][gold] = atoi(InPtr);
922 Captured[side][bishop] = atoi(InPtr);
924 Captured[side][rook] = atoi(InPtr);
926 Captured[side][king] = atoi(InPtr);
929 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
942 dsp->UpdateDisplay(0, 0, 1, 0);
957 dsp->ShowMessage("Enter file name: ");
958 RequestInputString(fname, sizeof(fname)-1);
960 if (fname[0] == '\0')
961 strcpy(fname, "xshogi.position.read");
963 if ((fd = fopen(fname, "w")) != NULL)
965 fputs("# xshogi position file -- \n", fd);
969 for (i = NO_ROWS - 1; i > -1; i--)
973 for (j = 0; j < NO_COLS; j++)
975 sq = i * NO_COLS + j;
977 isp = is_promoted[piece];
978 *p = (isp ? '+' : ' ');
981 if (piece == no_piece)
983 else if (color[sq] == white)
996 for (side = 0; side <= 1; side++)
1000 "%d %d %d %d %d %d %d %d\n",
1002 "%d %d %d %d %d %d\n",
1004 Captured[side][pawn],
1006 Captured[side][lance],
1007 Captured[side][knight],
1009 Captured[side][silver],
1010 Captured[side][gold],
1011 Captured[side][bishop],
1012 Captured[side][rook],
1013 Captured[side][king]);
1018 if (computer == black)
1019 fputs("white to play\n", fd);
1021 fputs("black to play\n", fd);
1032 char fname[256], sflags[4];
1036 strcpy(fname, savefile);
1038 /* Enter file name */
1039 dsp->ShowMessage("Enter file name: ");
1040 RequestInputString(fname, sizeof(fname)-1);
1043 if (fname[0] == '\0') {
1044 dsp->AlwaysShowMessage("aborting book save");
1048 if ((fd = fopen(fname, "a")) != NULL)
1052 for (i = 1; i <= GameCnt; i++)
1054 struct GameRec *g = &GameList[i];
1055 char mvnr[20], mvs[20];
1058 sprintf(mvnr, "%d.", (i + 1)/2);
1063 t = (g->gmove & 0xFF);
1064 algbr(f, t, g->flags);
1067 /* determine move quality string */
1068 if (g->flags & goodmove)
1071 if (g->flags & badmove)
1074 #ifdef EASY_OPENINGS
1075 if (g->flags & difficult)
1081 /* determine move string */
1084 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1088 sprintf(mvs, "%c%c%c%c%c%s%s ",
1089 mvstr[0][0], mvstr[0][1],
1090 (g->flags & capture) ? 'x' : '-',
1091 mvstr[0][2], mvstr[0][3],
1092 (mvstr[0][4] == '+') ? "+" : "",
1096 fprintf(fd, "%s%s%c%s",
1100 : (is_promoted[g->fpiece] ? "+" : "")),
1113 dsp->ShowMessage("Game saved");
1117 dsp->ShowMessage("Could not open file");
1128 char fname[256], dbuf[256];
1132 strcpy(fname, listfile);
1137 strncpy(dbuf, ctime(&when), 20);
1144 /* use format "CL.Jan01-020304B" when
1147 program played white */
1149 sprintf(fname, "CL.%s%s-%s%s%s%c",
1150 dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1151 dbuf + 17, ColorStr[computer][0]);
1153 /* replace space padding with 0 */
1154 for (i = 0; fname[i] != '\0'; i++)
1156 if (fname[i] == ' ')
1161 fd = fopen(fname, "w");
1165 printf("Open failure for file: %s", fname);
1169 fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
1170 fputs(" score depth nodes time ", fd);
1171 fputs(" score depth nodes time\n", fd);
1173 for (i = 1; i <= GameCnt; i++)
1175 f = GameList[i].gmove >> 8;
1176 t = (GameList[i].gmove & 0xFF);
1177 algbr(f, t, GameList[i].flags);
1179 if (GameList[i].flags & book)
1181 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1184 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1185 pxx[GameList[i].fpiece],
1187 ? &mvstr[0][1] : mvstr[0]),
1194 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1197 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1198 pxx[GameList[i].fpiece],
1199 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1200 GameList[i].score, GameList[i].depth,
1201 GameList[i].nodes, GameList[i].time);
1214 fprintf(fd, "\n\n");
1216 if (GameList[GameCnt].flags & draw)
1218 fprintf(fd, "Draw %s\n", DRAW);
1220 if (DRAW == DRAW_REPETITION)
1224 fprintf(fd, "repetition by positions ");
1226 for (j = GameCnt - 1; j >= Game50; j -= 2)
1228 if (GameList[j].hashkey == hashkey &&
1229 GameList[j].hashbd == hashbd)
1230 fprintf(fd, "%d ", j);
1236 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1238 fprintf(fd, "%s\n", ColorStr[player ]);
1240 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1242 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1255 GameList[GameCnt].flags |= badmove;
1259 GameList[GameCnt].flags |= goodmove;
1262 #ifdef EASY_OPENINGS
1264 GameList[GameCnt].flags |= difficult;
1272 * Undo the most recent half-move.
1280 f = GameList[GameCnt].gmove >> 8;
1281 t = GameList[GameCnt].gmove & 0x7F;
1285 /* the move was a drop */
1286 Captured[color[t]][board[t]]++;
1287 board[t] = no_piece;
1293 if (GameList[GameCnt].flags & promote)
1294 board[f] = unpromoted[board[t]];
1296 board[f] = board[t];
1298 color[f] = color[t];
1299 board[t] = GameList[GameCnt].piece;
1300 color[t] = GameList[GameCnt].color;
1302 if (board[t] != no_piece)
1303 Captured[color[f]][unpromoted[board[t]]]--;
1305 if (color[t] != neutral)
1313 if (TCflag && (TCmoves > 1))
1314 ++TimeControl.moves[color[f]];
1316 hashkey = GameList[GameCnt].hashkey;
1317 hashbd = GameList[GameCnt].hashbd;
1319 computer = computer ^ 1;
1320 opponent = opponent ^ 1;
1323 player = player ^ 1;
1324 dsp->ShowSidetoMove();
1325 dsp->UpdateDisplay(0, 0, 1, 0);
1327 if (flag.regularstart)
1333 TestSpeed(void(*f)(short side, short ply,
1334 short in_check, short blockable),
1344 #ifdef HAVE_GETTIMEOFDAY
1348 #ifdef HAVE_GETTIMEOFDAY
1349 gettimeofday(&tv, NULL);
1350 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1355 for (i = 0; i < j; i++)
1357 f(opponent, 2, -1, true);
1360 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1362 if (!pick(jj, TrPnt[3] - 1))
1368 #ifdef HAVE_GETTIMEOFDAY
1369 gettimeofday(&tv, NULL);
1370 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1375 cnt = j * (TrPnt[3] - TrPnt[2]);
1382 dsp->ShowNodeCnt(cnt);
1387 TestPSpeed(short(*f) (short side), unsigned j)
1391 #ifdef HAVE_GETTIMEOFDAY
1395 #ifdef HAVE_GETTIMEOFDAY
1396 gettimeofday(&tv, NULL);
1397 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1402 for (i = 0; i < j; i++)
1405 #ifdef HAVE_GETTIMEOFDAY
1406 gettimeofday(&tv, NULL);
1407 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1419 dsp->ShowNodeCnt(cnt);
1424 SetOppTime(char *time)
1428 t = (int)strtol(time, &time, 10);
1433 /* FIXME: sec is parsed but ignored */
1434 (void)strtol(time, &time, 10);
1437 m = (int)strtol(time, &time, 10);
1440 TimeControl.clock[opponent] = t;
1443 TimeControl.moves[opponent] = m;
1445 ElapsedTime(COMPUTE_AND_INIT_MODE);
1449 /* just to inform xshogi about availability of otime command */
1450 printf("otime %d %d\n", t, m);
1456 SetMachineTime(char *time)
1460 t = (int)strtol(time, &time, 10);
1465 /* FIXME: sec is parsed but ignored */
1466 (void)strtol(time, &time, 10);
1469 m = (int)strtol(time, &time, 10);
1472 TimeControl.clock[computer] = t;
1475 TimeControl.moves[computer] = m;
1477 ElapsedTime(COMPUTE_AND_INIT_MODE);
1481 /* just to inform xshogi about availability of time command */
1482 printf("time %d %d\n", t, m);
1487 * Set up a board position. Pieces are entered by typing the piece followed
1488 * by the location. For example, Nf3 will place a knight on square f3.
1493 short a = white, r, c, sq, i, error = 0;
1496 flag.regularstart = true;
1499 for (sq = 0; sq < NO_SQUARES; sq++)
1501 board[sq] = no_piece;
1502 color[sq] = neutral;
1508 r = NO_ROWS-1; c = 0;
1513 c += *fen++ - '0'; /* assumes single digit! */
1515 else if (*fen == '/')
1517 if (c != NO_COLS) error++;
1523 int promo = 0, found = 0;
1529 if (!isalpha(*fen)) break;
1531 for (i = no_piece; i <= king; i++)
1533 if ((*fen == pxx[i]) || (*fen == qxx[i]))
1536 color[sq] = (islower(*fen) ? white : black);
1538 board[sq] = promoted[i];
1547 if (!found) error++;
1551 if(r || c != NO_COLS) error++;
1553 while (*fen == ' ') fen++;
1556 if(!strncmp(fen, "[-]", 3)) fen += 3; /* empty holdings */
1557 else if(*fen == '[')
1560 while(isalpha(*fen))
1563 for (i = pawn; i <= king; i++)
1565 if ((*fen == pxx[i]) || (*fen == qxx[i]))
1567 Captured[islower(*fen) ? white : black][i]++;
1572 if (!found) error++;
1575 if(*fen == ']') fen++; else error++;
1578 while (*fen == ' ') fen++;
1582 else if (*fen == 'b')
1587 if (error) printf("tellusererror bad FEN\n");
1589 for (sq = 0; sq < NO_SQUARES; sq++)
1590 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
1592 computer = otherside[a];
1603 /* FIXME! This is truly the function from hell! */
1606 * Process the user's command. If easy mode is OFF (the computer is thinking
1607 * on opponents time) and the program is out of book, then make the 'hint'
1608 * move on the board and call SelectMove() to find a response. The user
1609 * terminates the search by entering a command. If the opponent does not make
1610 * the hint move, then set Sdepth to zero.
1614 InputCommand(char *command, int root)
1616 #ifdef QUIETBACKGROUND
1617 short have_shown_prompt = false;
1619 short ok, done, is_move = false;
1621 char s[200], sx[200], s2[200];
1622 static char backlog[200], ponderString[20];
1624 ok = flag.quit = done = false;
1628 if (TTadd > ttbllimit)
1632 while ((hint > 0) && !flag.easy && !flag.force && !command && !backlog[0] && root)
1635 * A hint move for the player is available. Compute a move for the
1636 * opponent in background mode assuming that the hint move will be
1637 * selected by the player.
1640 ft = time0; /* Save reference time for the player. */
1642 algbr((short) hint >> 8, (short) hint & 0xff, false);
1643 strcpy(ponderString, mvstr[0]);
1648 /* do the hint move */
1649 if (VerifyMove(ponderString, VERIFY_AND_TRY_MODE, &mv))
1653 #ifdef QUIETBACKGROUND
1655 have_shown_prompt = true;
1656 #endif /* QUIETBACKGROUND */
1658 /* Start computing a move until the search is interrupted. */
1660 #ifdef INTERRUPT_TEST
1664 /* would love to put null move in here */
1665 /* after we make the hint move make a 2 ply search
1666 * with both plys our moves */
1667 /* think on opponents time */
1668 SelectMove(computer, BACKGROUND_MODE);
1670 #ifdef INTERRUPT_TEST
1671 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1675 printf("searching not terminated by interrupt!\n");
1679 printf("elapsed time from interrupt to "
1680 "terminating search: %ld\n", it);
1684 if (strcmp(ponderString, "hit"))
1685 { /* undo the hint and carry on */
1686 VerifyMove(ponderString, UNMAKE_MODE, &mv);
1689 { /* otherwise SelectMove will have played the computer's reply */
1690 /* update ponder-move stats, which was skipped in TRY_MODE */
1691 GameList[GameCnt-1].depth = GameList[GameCnt].score = 0;
1692 GameList[GameCnt-1].nodes = 0;
1693 ElapsedTime(COMPUTE_AND_INIT_MODE);
1694 GameList[GameCnt-1].time = (short) (et + 50)/100; /* FIXME: this is wrong */
1696 if(TCflag && TimeControl.moves[computer] == 0)
1697 SetTimeControl(); /* add time for next session */
1702 time0 = ft; /* Restore reference time for the player. */
1703 ponderString[0] = '\0';
1704 /* on a ponder miss or other command, loop terminates because of backlog */
1707 while(!(ok || flag.quit || done))
1711 if (flag.analyze && !command && !backlog[0] && root) {
1712 SelectMove(opponent, BACKGROUND_MODE);
1715 #ifdef QUIETBACKGROUND
1716 if (!have_shown_prompt)
1718 #endif /* QUIETBACKGROUND */
1722 #ifdef QUIETBACKGROUND
1725 have_shown_prompt = false;
1726 #endif /* QUIETBACKGROUND */
1728 if (!command && backlog[0]) command = backlog; /* pick up backlogged command */
1730 if (command == NULL) {
1731 int eof = dsp->GetString(sx);
1735 strcpy(sx, command);
1736 backlog[0]= '\0'; /* make sure no backlog is left */
1740 /* extract first word */
1741 if (sscanf(sx, "%s %s", s, s2) < 1)
1744 if (!root && (strcmp(s, "usermove") == 0)
1745 && (strcmp(s2, ponderString) == 0))
1746 { /* ponder hit; switch to normal search */
1750 { /* account opponent time and moves */
1751 TimeControl.clock[opponent] -= et;
1752 timeopp[oppptr] = et;
1753 --TimeControl.moves[opponent];
1754 if(TimeControl.moves[computer] == 0) SetTimeControl();
1756 SetResponseTime(computer);
1757 strcpy(ponderString, "hit");
1758 return false; /* no search abort */
1761 if (!root && strcmp(s, ".") && strcmp(s, "time") && strcmp(s, "otim"))
1762 { /* during search most commands can only be done after abort */
1763 strcpy(backlog, sx); /* backlog the command */
1764 return true; /* and order search abort */
1767 if (strcmp(s, "bd") == 0) /* bd -- display board */
1769 /* FIXME: Hack alert! */
1770 short old_xshogi = XSHOGI;
1773 display_type = DISPLAY_RAW;
1776 dsp->UpdateDisplay(0, 0, 1, 0);
1779 display_type = DISPLAY_X;
1781 else if (strcmp(s, "post") == 0)
1783 flag.post = (xboard ? 1 : !flag.post);
1785 else if (strcmp(s, "nopost") == 0)
1790 else if (strcmp(s, "variant") == 0)
1791 { /* only variant we play is minishogi */
1792 printf("setup (P.BR.S...G.+.++.+Kp.br.s...g.+.++.+k) 5x5+5_shogi rbsgk/4p/5/P4/KGSBR [-] w 0 1\n");
1795 else if (strcmp(s, "alg") == 0 ||
1796 strcmp(s, "accepted") == 0 || strcmp(s, "rejected") == 0 ||
1797 strcmp(s, "variant") == 0 || strcmp(s, "computer") == 0)
1801 else if ((strcmp(s, "quit") == 0) ||
1802 (strcmp(s, "exit") == 0) && !xboard)
1806 else if (strcmp(s, "xboard") == 0)
1809 strcpy(ColorStr[0], "White");
1810 strcpy(ColorStr[1], "Black");
1812 else if (strcmp(s, "protover") == 0)
1814 printf("feature option=\"tsume -check 0\"\n");
1815 printf("feature option=\"contempt -spin %d -1000 1000\"\n", contempt);
1816 printf("feature option=\"Hash-file search depth -spin %d 0 100\"\n", HashDepth);
1817 printf("feature option=\"Hash-file move number -spin %d 0 100\"\n", HashMoveLimit);
1818 printf("feature myname=\"GNU %s %s\" ",
1826 printf("variants=\"%s\" ",
1828 "5x5+5_shogi,minishogi"
1833 printf("debug=1 setboard=1 sigint=0 memory=1 usermove=1 done=1\n");
1835 else if (strcmp(s, ".") == 0)
1836 { // periodic update request of analysis info: send stat01 info
1838 algbr((short)(currentMove >> 8), (short)(currentMove & 0xFF), 0);
1839 printf("stat01: %4ld %8ld %2d %2d %2d %s\n",
1840 et, NodeCnt, Sdepth, movesLeft, TrPnt[2]-TrPnt[1], mvstr[0]);
1842 if (!root) return false; /* signal no abort needed */
1844 else if (strcmp(s, "exit") == 0)
1846 flag.analyze = false;
1849 else if (strcmp(s, "analyze") == 0)
1851 flag.analyze = true;
1854 else if ((strcmp(s, "set") == 0) ||
1855 (strcmp(s, "edit") == 0))
1859 else if (strcmp(s, "setup") == 0)
1863 else if (strcmp(s, "first") == 0)
1868 else if (strcmp(s, "memory") == 0)
1870 unsigned int mem, size, t = 1;
1871 sscanf(sx, "memory %d", &mem);
1872 if(mem > 2048) mem = 2048; /* prevent integer overflow for > 2GB hash */
1873 size = (mem << 20) / sizeof(struct hashentry) - rehash;
1874 while(t <= size/4) t <<= 1;
1878 else if (strcmp(s, "go") == 0)
1883 if (computer == black)
1894 else if (strcmp(s, "help") == 0)
1898 else if (strcmp(s, "material") == 0)
1900 flag.material = !flag.material;
1902 else if (strcmp(s, "force") == 0)
1907 flag.bothsides = false;
1911 flag.force = !flag.force;
1912 flag.bothsides = false;
1915 else if (strcmp(s, "book") == 0)
1917 Book = Book ? 0 : BOOKFAIL;
1919 else if (strcmp(s, "new") == 0)
1922 dsp->UpdateDisplay(0, 0, 1, 0);
1924 else if (strcmp(s, "setboard") == 0)
1928 else if (strcmp(s, "list") == 0)
1932 else if (strcmp(s, "level") == 0)
1934 dsp->SelectLevel(sx + strlen("level"));
1936 else if (strcmp(s, "clock") == 0)
1938 dsp->SelectLevel(sx + strlen("clock"));
1940 else if (strcmp(s, "hash") == 0)
1942 flag.hash = !flag.hash;
1944 else if (strcmp(s, "gamein") == 0)
1946 flag.gamein = !flag.gamein;
1948 else if (strcmp(s, "beep") == 0)
1950 flag.beep = !flag.beep;
1952 else if (strcmp(s, "time") == 0)
1954 SetMachineTime(sx + strlen("time"));
1956 else if ((strcmp(s, "otime") == 0) ||
1957 (xboard && (strcmp(s, "otim")) == 0))
1959 SetOppTime(sx + strlen("otime"));
1961 else if (strcmp(s, "Awindow") == 0)
1963 dsp->ChangeAlphaWindow();
1965 else if (strcmp(s, "Bwindow") == 0)
1967 dsp->ChangeBetaWindow();
1969 else if (strcmp(s, "rcptr") == 0)
1971 flag.rcptr = !flag.rcptr;
1973 else if (strcmp(s, "hint") == 0)
1977 else if (strcmp(s, "both") == 0)
1979 flag.bothsides = !flag.bothsides;
1982 ElapsedTime(COMPUTE_AND_INIT_MODE);
1983 SelectMove(opponent, FOREGROUND_MODE);
1986 else if (strcmp(s, "reverse") == 0)
1988 flag.reverse = !flag.reverse;
1990 dsp->UpdateDisplay(0, 0, 1, 0);
1992 else if (strcmp(s, "switch") == 0)
1994 computer = computer ^ 1;
1995 opponent = opponent ^ 1;
1996 xwndw = (computer == black) ? WXWNDW : BXWNDW;
2000 dsp->UpdateDisplay(0, 0, 1, 0);
2002 else if (xboard ? strcmp(s, "white") == 0 : strcmp(s, "black") == 0)
2011 * ok = true; don't automatically start with black command
2014 else if (xboard ? strcmp(s, "black") == 0 : strcmp(s, "white") == 0)
2023 * ok = true; don't automatically start with white command
2026 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
2030 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
2035 /* CHECKME: are these next three correct? */
2036 else if (!XSHOGI && strcmp(s, "xget") == 0)
2040 else if (!XSHOGI && strcmp(s, "xsave") == 0)
2044 else if (!XSHOGI && strcmp(s, "bsave") == 0)
2048 #ifdef EASY_OPENINGS
2049 else if ((strcmp(s, "?") == 0) ||
2050 (strcmp(s, "!") == 0) ||
2051 (strcmp(s, "~") == 0))
2053 else if ((strcmp(s, "?") == 0) ||
2054 (strcmp(s, "!") == 0))
2059 else if (strcmp(s, "get") == 0)
2063 else if (strcmp(s, "save") == 0)
2067 else if (strcmp(s, "depth") == 0)
2069 dsp->ChangeSearchDepth(sx + strlen("depth"));
2071 else if (strcmp(s, "sd") == 0)
2073 dsp->ChangeSearchDepth(sx + strlen("sd"));
2075 else if (strcmp(s, "hashdepth") == 0)
2077 dsp->ChangeHashDepth();
2079 else if (strcmp(s, "random") == 0)
2083 else if (strcmp(s, "hard") == 0)
2087 else if (strcmp(s, "easy") == 0)
2089 flag.easy = !flag.easy;
2091 else if (strcmp(s, "option") == 0)
2093 sscanf(sx, "option tsume=%hd", &flag.tsume) ||
2094 sscanf(sx, "option hash=%hd", &flag.hash) ||
2095 sscanf(sx, "option Hash-file search depth=%hd", &HashDepth) ||
2096 sscanf(sx, "option Hash-file move number=%hd", &HashMoveLimit) ||
2097 sscanf(sx, "option contempt=%hd", &contempt);
2099 else if (strcmp(s, "tsume") == 0)
2101 flag.tsume = !flag.tsume;
2103 else if (strcmp(s, "contempt") == 0)
2107 else if (strcmp(s, "xwndw") == 0)
2109 dsp->ChangeXwindow();
2111 else if (strcmp(s, "rv") == 0)
2114 dsp->UpdateDisplay(0, 0, 1, 0);
2116 else if (strcmp(s, "coords") == 0)
2118 flag.coords = !flag.coords;
2119 dsp->UpdateDisplay(0, 0, 1, 0);
2121 else if (strcmp(s, "stars") == 0)
2123 flag.stars = !flag.stars;
2124 dsp->UpdateDisplay(0, 0, 1, 0);
2126 else if (!XSHOGI && strcmp(s, "moves") == 0)
2133 extern unsigned short PrVar[MAXDEPTH];
2135 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
2142 dsp->ShowMessage("Testing MoveList Speed");
2143 temp = generate_move_flags;
2144 generate_move_flags = true;
2145 TestSpeed(MoveList, 1);
2146 generate_move_flags = temp;
2147 dsp->ShowMessage("Testing CaptureList Speed");
2148 TestSpeed(CaptureList, 1);
2149 dsp->ShowMessage("Testing Eval Speed");
2150 ExaminePosition(opponent);
2151 TestPSpeed(ScorePosition, 1);
2153 else if (!XSHOGI && strcmp(s, "test") == 0)
2156 dsp->ShowMessage("Testing MoveList Speed");
2157 TestSpeed(MoveList, 2000);
2158 dsp->ShowMessage("Testing CaptureList Speed");
2159 TestSpeed(CaptureList, 3000);
2160 dsp->ShowMessage("Testing Eval Speed");
2161 ExaminePosition(opponent);
2162 TestPSpeed(ScorePosition, 1500);
2164 dsp->ShowMessage("Testing MoveList Speed");
2165 TestSpeed(MoveList, 20000);
2166 dsp->ShowMessage("Testing CaptureList Speed");
2167 TestSpeed(CaptureList, 30000);
2168 dsp->ShowMessage("Testing Eval Speed");
2169 ExaminePosition(opponent);
2170 TestPSpeed(ScorePosition, 15000);
2173 else if (!XSHOGI && strcmp(s, "p") == 0)
2175 dsp->ShowPostnValues();
2177 else if (!XSHOGI && strcmp(s, "debug") == 0)
2183 if (strcmp(s, "usermove") == 0)
2184 sscanf(sx + 9, "%s", s);
2190 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2192 /* check for repetition */
2193 short rpt = repetition();
2197 DRAW = DRAW_REPETITION;
2198 dsp->ShowMessage(DRAW);
2199 GameList[GameCnt].flags |= draw;
2213 ElapsedTime(COMPUTE_AND_INIT_MODE);
2217 computer = opponent;
2218 opponent = computer ^ 1;
2223 /* add remaining time in milliseconds for xshogi */
2226 printf("%d. %s %ld\n",
2227 ++mycnt2, s, TimeControl.clock[player] * 10);