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];
1604 * Process the user's command. If easy mode is OFF (the computer is thinking
1605 * on opponents time) and the program is out of book, then make the 'hint'
1606 * move on the board and call SelectMove() to find a response. The user
1607 * terminates the search by entering a command. If the opponent does not make
1608 * the hint move, then set Sdepth to zero.
1611 static char ponderString[20];
1614 PonderOnHintMove(void)
1616 #ifdef QUIETBACKGROUND
1617 short have_shown_prompt = false;
1622 * A hint move for the player is available. Compute a move for the
1623 * opponent in background mode assuming that the hint move will be
1624 * selected by the player.
1627 ft = time0; /* Save reference time for the player. */
1629 algbr((short) hint >> 8, (short) hint & 0xff, false);
1630 strcpy(ponderString, mvstr[0]);
1635 /* do the hint move */
1636 if (VerifyMove(ponderString, VERIFY_AND_TRY_MODE, &mv))
1640 #ifdef QUIETBACKGROUND
1642 have_shown_prompt = true;
1643 #endif /* QUIETBACKGROUND */
1645 /* Start computing a move until the search is interrupted. */
1647 #ifdef INTERRUPT_TEST
1651 /* would love to put null move in here */
1652 /* after we make the hint move make a 2 ply search
1653 * with both plys our moves */
1654 /* think on opponents time */
1655 SelectMove(computer, BACKGROUND_MODE);
1657 #ifdef INTERRUPT_TEST
1658 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1662 printf("searching not terminated by interrupt!\n");
1666 printf("elapsed time from interrupt to "
1667 "terminating search: %ld\n", it);
1671 if (strcmp(ponderString, "hit"))
1672 { /* undo the hint and carry on */
1673 VerifyMove(ponderString, UNMAKE_MODE, &mv);
1676 { /* otherwise SelectMove will have played the computer's reply */
1677 /* update ponder-move stats, which was skipped in TRY_MODE */
1678 GameList[GameCnt-1].depth = GameList[GameCnt].score = 0;
1679 GameList[GameCnt-1].nodes = 0;
1680 ElapsedTime(COMPUTE_AND_INIT_MODE);
1681 GameList[GameCnt-1].time = (short) (et + 50)/100; /* FIXME: this is wrong */
1683 RenewTimeControl(computer); /* add time for next session */
1688 time0 = ft; /* Restore reference time for the player. */
1689 ponderString[0] = '\0';
1693 * Recognize the command s from input line sx, and perform the action it specifies.
1694 * Returns whether the command could cause it to be out turn to move.
1697 ParseAndExecuteCommand(char *s, char *sx)
1702 if (strcmp(s, "bd") == 0) /* bd -- display board */
1704 /* FIXME: Hack alert! */
1705 short old_xshogi = XSHOGI;
1708 display_type = DISPLAY_RAW;
1711 dsp->UpdateDisplay(0, 0, 1, 0);
1714 display_type = DISPLAY_X;
1716 else if (strcmp(s, "post") == 0)
1718 flag.post = (xboard ? 1 : !flag.post);
1720 else if (strcmp(s, "nopost") == 0)
1725 else if (strcmp(s, "variant") == 0)
1726 { /* only variant we play is minishogi */
1727 printf("setup (P.BR.S...G.+.++.+Kp.br.s...g.+.++.+k) 5x5+5_shogi rbsgk/4p/5/P4/KGSBR [-] w 0 1\n");
1730 else if (strcmp(s, "alg") == 0 ||
1731 strcmp(s, "accepted") == 0 || strcmp(s, "rejected") == 0 ||
1732 strcmp(s, "variant") == 0 || strcmp(s, "computer") == 0)
1736 else if ((strcmp(s, "quit") == 0) ||
1737 (strcmp(s, "exit") == 0) && !xboard)
1741 else if (strcmp(s, "xboard") == 0)
1744 strcpy(ColorStr[0], "White");
1745 strcpy(ColorStr[1], "Black");
1747 else if (strcmp(s, "protover") == 0)
1749 printf("feature option=\"tsume -check 0\"\n");
1750 printf("feature option=\"contempt -spin %d -1000 1000\"\n", contempt);
1751 printf("feature option=\"Hash-file search depth -spin %d 0 100\"\n", HashDepth);
1752 printf("feature option=\"Hash-file move number -spin %d 0 100\"\n", HashMoveLimit);
1753 printf("feature myname=\"GNU %s %s\" ",
1761 printf("variants=\"%s\" ",
1763 "5x5+5_shogi,minishogi"
1768 printf("debug=1 setboard=1 sigint=0 memory=1 usermove=1 done=1\n");
1770 else if (strcmp(s, ".") == 0)
1771 { // periodic update request of analysis info: send stat01 info
1773 algbr((short)(currentMove >> 8), (short)(currentMove & 0xFF), 0);
1774 printf("stat01: %4ld %8ld %2d %2d %2d %s\n",
1775 et, NodeCnt, Sdepth, movesLeft, TrPnt[2]-TrPnt[1], mvstr[0]);
1777 if (!root) return false; /* signal no abort needed */
1779 else if (strcmp(s, "exit") == 0)
1781 flag.analyze = false;
1784 else if (strcmp(s, "analyze") == 0)
1786 flag.analyze = true;
1789 else if ((strcmp(s, "set") == 0) ||
1790 (strcmp(s, "edit") == 0))
1794 else if (strcmp(s, "setup") == 0)
1798 else if (strcmp(s, "first") == 0)
1803 else if (strcmp(s, "memory") == 0)
1805 unsigned int mem, size, t = 1;
1806 sscanf(sx, "memory %d", &mem);
1807 if(mem > 2048) mem = 2048; /* prevent integer overflow for > 2GB hash */
1808 size = (mem << 20) / sizeof(struct hashentry) - rehash;
1809 while(t <= size/4) t <<= 1;
1813 else if (strcmp(s, "go") == 0)
1818 if (computer == black)
1829 else if (strcmp(s, "help") == 0)
1833 else if (strcmp(s, "material") == 0)
1835 flag.material = !flag.material;
1837 else if (strcmp(s, "force") == 0)
1842 flag.bothsides = false;
1846 flag.force = !flag.force;
1847 flag.bothsides = false;
1850 else if (strcmp(s, "book") == 0)
1852 Book = Book ? 0 : BOOKFAIL;
1854 else if (strcmp(s, "new") == 0)
1857 dsp->UpdateDisplay(0, 0, 1, 0);
1859 else if (strcmp(s, "setboard") == 0)
1863 else if (strcmp(s, "list") == 0)
1867 else if (strcmp(s, "level") == 0)
1869 dsp->SelectLevel(sx + strlen("level"));
1871 else if (strcmp(s, "clock") == 0)
1873 dsp->SelectLevel(sx + strlen("clock"));
1875 else if (strcmp(s, "hash") == 0)
1877 flag.hash = !flag.hash;
1879 else if (strcmp(s, "gamein") == 0)
1881 flag.gamein = !flag.gamein;
1883 else if (strcmp(s, "beep") == 0)
1885 flag.beep = !flag.beep;
1887 else if (strcmp(s, "time") == 0)
1889 SetMachineTime(sx + strlen("time"));
1891 else if ((strcmp(s, "otime") == 0) ||
1892 (xboard && (strcmp(s, "otim")) == 0))
1894 SetOppTime(sx + strlen("otime"));
1896 else if (strcmp(s, "Awindow") == 0)
1898 dsp->ChangeAlphaWindow();
1900 else if (strcmp(s, "Bwindow") == 0)
1902 dsp->ChangeBetaWindow();
1904 else if (strcmp(s, "rcptr") == 0)
1906 flag.rcptr = !flag.rcptr;
1908 else if (strcmp(s, "hint") == 0)
1912 else if (strcmp(s, "both") == 0)
1914 flag.bothsides = !flag.bothsides;
1917 ElapsedTime(COMPUTE_AND_INIT_MODE);
1918 SelectMove(opponent, FOREGROUND_MODE);
1921 else if (strcmp(s, "reverse") == 0)
1923 flag.reverse = !flag.reverse;
1925 dsp->UpdateDisplay(0, 0, 1, 0);
1927 else if (strcmp(s, "switch") == 0)
1929 computer = computer ^ 1;
1930 opponent = opponent ^ 1;
1931 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1935 dsp->UpdateDisplay(0, 0, 1, 0);
1937 else if (xboard ? strcmp(s, "white") == 0 : strcmp(s, "black") == 0)
1946 * ok = true; don't automatically start with black command
1949 else if (xboard ? strcmp(s, "black") == 0 : strcmp(s, "white") == 0)
1958 * ok = true; don't automatically start with white command
1961 else if (strcmp(s, "undo") == 0 && GameCnt > 0)
1965 else if (strcmp(s, "remove") == 0 && GameCnt > 1)
1970 /* CHECKME: are these next three correct? */
1971 else if (!XSHOGI && strcmp(s, "xget") == 0)
1975 else if (!XSHOGI && strcmp(s, "xsave") == 0)
1979 else if (!XSHOGI && strcmp(s, "bsave") == 0)
1983 #ifdef EASY_OPENINGS
1984 else if ((strcmp(s, "?") == 0) ||
1985 (strcmp(s, "!") == 0) ||
1986 (strcmp(s, "~") == 0))
1988 else if ((strcmp(s, "?") == 0) ||
1989 (strcmp(s, "!") == 0))
1994 else if (strcmp(s, "get") == 0)
1998 else if (strcmp(s, "save") == 0)
2002 else if (strcmp(s, "depth") == 0)
2004 dsp->ChangeSearchDepth(sx + strlen("depth"));
2006 else if (strcmp(s, "sd") == 0)
2008 dsp->ChangeSearchDepth(sx + strlen("sd"));
2010 else if (strcmp(s, "hashdepth") == 0)
2012 dsp->ChangeHashDepth();
2014 else if (strcmp(s, "random") == 0)
2018 else if (strcmp(s, "hard") == 0)
2022 else if (strcmp(s, "easy") == 0)
2024 flag.easy = !flag.easy;
2026 else if (strcmp(s, "option") == 0)
2028 sscanf(sx, "option tsume=%hd", &flag.tsume) ||
2029 sscanf(sx, "option hash=%hd", &flag.hash) ||
2030 sscanf(sx, "option Hash-file search depth=%hd", &HashDepth) ||
2031 sscanf(sx, "option Hash-file move number=%hd", &HashMoveLimit) ||
2032 sscanf(sx, "option contempt=%hd", &contempt);
2034 else if (strcmp(s, "tsume") == 0)
2036 flag.tsume = !flag.tsume;
2038 else if (strcmp(s, "contempt") == 0)
2042 else if (strcmp(s, "xwndw") == 0)
2044 dsp->ChangeXwindow();
2046 else if (strcmp(s, "rv") == 0)
2049 dsp->UpdateDisplay(0, 0, 1, 0);
2051 else if (strcmp(s, "coords") == 0)
2053 flag.coords = !flag.coords;
2054 dsp->UpdateDisplay(0, 0, 1, 0);
2056 else if (strcmp(s, "stars") == 0)
2058 flag.stars = !flag.stars;
2059 dsp->UpdateDisplay(0, 0, 1, 0);
2061 else if (!XSHOGI && strcmp(s, "moves") == 0)
2068 extern unsigned short PrVar[MAXDEPTH];
2070 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
2077 dsp->ShowMessage("Testing MoveList Speed");
2078 temp = generate_move_flags;
2079 generate_move_flags = true;
2080 TestSpeed(MoveList, 1);
2081 generate_move_flags = temp;
2082 dsp->ShowMessage("Testing CaptureList Speed");
2083 TestSpeed(CaptureList, 1);
2084 dsp->ShowMessage("Testing Eval Speed");
2085 ExaminePosition(opponent);
2086 TestPSpeed(ScorePosition, 1);
2088 else if (!XSHOGI && strcmp(s, "test") == 0)
2091 dsp->ShowMessage("Testing MoveList Speed");
2092 TestSpeed(MoveList, 2000);
2093 dsp->ShowMessage("Testing CaptureList Speed");
2094 TestSpeed(CaptureList, 3000);
2095 dsp->ShowMessage("Testing Eval Speed");
2096 ExaminePosition(opponent);
2097 TestPSpeed(ScorePosition, 1500);
2099 dsp->ShowMessage("Testing MoveList Speed");
2100 TestSpeed(MoveList, 20000);
2101 dsp->ShowMessage("Testing CaptureList Speed");
2102 TestSpeed(CaptureList, 30000);
2103 dsp->ShowMessage("Testing Eval Speed");
2104 ExaminePosition(opponent);
2105 TestPSpeed(ScorePosition, 15000);
2108 else if (!XSHOGI && strcmp(s, "p") == 0)
2110 dsp->ShowPostnValues();
2112 else if (!XSHOGI && strcmp(s, "debug") == 0)
2118 if (strcmp(s, "usermove") == 0)
2119 sscanf(sx + 9, "%s", s);
2125 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2127 /* check for repetition */
2128 short rpt = repetition();
2132 DRAW = DRAW_REPETITION;
2133 dsp->ShowMessage(DRAW);
2134 GameList[GameCnt].flags |= draw;
2140 /* add remaining time in milliseconds for xshogi */
2141 printf("%d. %s %ld\n",
2142 ++mycnt2, s, TimeControl.clock[player] * 10);
2153 * Read commands from input, and execute them, until it becomes our turn to move.
2154 * When called during a background search (root = false) it just backlogs the
2155 * input command without executing it, and returns immediately. Unless the command
2156 * was the move on which the search was pondering. In that case we turn the ongoing
2157 * search into a foreground search. To judge this, it is also necessary to process
2158 * the 'time' and 'otim' commands that preceed the move. The '.' command is also
2159 * always processed, to prevent it from aborting an analysis search.
2160 * The time spent waiting for input can be filled with background searches for
2161 * pondering or analysis. (In !root mode input is guaranteed to be pending already!)
2164 InputCommand(int root)
2166 #ifdef QUIETBACKGROUND
2167 short have_shown_prompt = false;
2170 char s[200], sx[200], s2[200];
2171 static char backlog[200];
2173 ok = flag.quit = false;
2177 /* CHECKME: should this also be done in the following while loop? */
2178 if (TTadd > ttbllimit)
2182 while ((hint > 0) && !flag.easy && !flag.force && !backlog[0] && root)
2185 * A hint move for the player is available. Compute a move for the
2186 * opponent in background mode assuming that the hint move will be
2187 * selected by the player.
2188 * Terminate this search on input, which will then be saved in backlog[].
2189 * Unless the input was the hint move ('ponder hit'). Then that move will
2190 * be played (after the search times out) in addition to the hint. There
2191 * will then be no backlog, and we start pondering on the new hint move.
2197 while(!(ok || flag.quit))
2198 { /* process input commands until our it becomes our turn to move */
2201 /* in analysis mode we do a background search while waiting for input */
2202 if (flag.analyze && !backlog[0] && root) {
2203 SelectMove(opponent, BACKGROUND_MODE);
2206 #ifdef QUIETBACKGROUND
2207 if (!have_shown_prompt)
2209 #endif /* QUIETBACKGROUND */
2213 #ifdef QUIETBACKGROUND
2216 have_shown_prompt = false;
2217 #endif /* QUIETBACKGROUND */
2219 if (!backlog[0]) { /* read new input line */
2220 int eof = dsp->GetString(sx);
2223 } else { /* or use backlogged input line */
2224 strcpy(sx, backlog);
2225 backlog[0]= '\0'; /* make sure no backlog is left */
2228 /* extract first word */
2229 if (sscanf(sx, "%s %s", s, s2) < 1)
2232 if (!root && (strcmp(s, "usermove") == 0)
2233 && (strcmp(s2, ponderString) == 0))
2234 { /* ponder hit; switch to normal search */
2238 { /* account opponent time and moves */
2239 TimeControl.clock[opponent] -= et;
2240 timeopp[oppptr] = et;
2241 if (--TimeControl.moves[opponent] == 0)
2242 TimeControl.moves[opponent] = TCmoves; /* assumes uni-TC! */
2244 SetResponseTime(computer);
2245 strcpy(ponderString, "hit");
2246 return false; /* no search abort */
2249 if (!root && strcmp(s, ".") && strcmp(s, "time") && strcmp(s, "otim"))
2250 { /* during search most commands can only be done after abort */
2251 strcpy(backlog, sx); /* backlog the command */
2252 return true; /* and order search abort */
2255 ok = ParseAndExecuteCommand(s, sx); /* returns whether turn changed */
2258 ElapsedTime(COMPUTE_AND_INIT_MODE);
2260 /* kludge alert: change the side we play to prevent starting a search */
2263 computer = opponent;
2264 opponent = computer ^ 1;