4 * Common display routines for GNU Shogi.
6 * ----------------------------------------------------------------------
8 * Copyright (c) 2012 Free Software Foundation
10 * GNU SHOGI is based on GNU CHESS
12 * This file is part of GNU SHOGI.
14 * GNU Shogi is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 3 of the License,
17 * or (at your option) any later version.
19 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with GNU Shogi; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
27 * ----------------------------------------------------------------------
31 #if defined HAVE_GETTIMEOFDAY
38 #include <sys/param.h>
39 #include <sys/types.h>
47 int InBackground = false;
53 movealgbr(short m, char *s)
56 short piece = 0, flag = 0;
69 piece = f - NO_SQUARES;
71 if (piece > NO_PIECES)
74 flag = (dropmask | piece);
121 #endif /* BOOKTEST */
127 * Generate move strings in different formats.
130 * - f piece to be moved
131 * - 0 < f < NO_SQUARES source square
132 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
133 * - t & 0x7f target square
134 * - t & 0x80 promotion flag
135 * - flag FIXME: must be zero ?
142 algbr(short f, short t, short flag)
148 piece = f - NO_SQUARES;
150 if (f > (NO_SQUARES + NO_PIECES))
153 flag = (dropmask | piece);
162 if ((f == t) && ((f != 0) || (t != 0)))
167 printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
169 printw("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
172 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
174 else if ((flag & dropmask) != 0)
176 short piece = flag & pmask;
178 mvstr[0][0] = pxx[piece];
180 mvstr[0][2] = cxx[column(t)];
181 mvstr[0][3] = rxx[row(t)];
183 strcpy(mvstr[1], mvstr[0]);
184 strcpy(mvstr[2], mvstr[0]);
185 strcpy(mvstr[3], mvstr[0]);
187 else if ((f != 0) || (t != 0))
189 /* algebraic notation */
190 mvstr[0][0] = cxx[column(f)];
191 mvstr[0][1] = rxx[row(f)];
192 mvstr[0][2] = cxx[column(t)];
193 mvstr[0][3] = rxx[row(t)];
194 mvstr[0][4] = mvstr[3][0] = '\0';
195 mvstr[1][0] = pxx[board[f]];
197 mvstr[2][0] = mvstr[1][0];
198 mvstr[2][1] = mvstr[0][1];
200 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
201 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
202 mvstr[2][4] = mvstr[1][3] = '\0';
203 strcpy(mvstr[3], mvstr[2]);
204 mvstr[3][1] = mvstr[0][0];
208 strcat(mvstr[0], "+");
209 strcat(mvstr[1], "+");
210 strcat(mvstr[2], "+");
211 strcat(mvstr[3], "+");
216 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
223 * Compare the string 's' to the list of legal moves available for the
224 * opponent. If a match is found, make the move on the board.
228 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
230 static short pnt, tempb, tempc, tempsf, tempst, cnt;
231 static struct leaf xnode;
233 short i, l, local_flags;
236 /* check and remove quality flags */
237 for (i = local_flags = 0, l = strlen(s); i < l; i++)
242 local_flags |= badmove;
247 local_flags |= goodmove;
253 local_flags |= difficult;
262 if (iop == UNMAKE_MODE)
264 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
270 if (iop == VERIFY_AND_MAKE_MODE)
271 generate_move_flags = true;
273 MoveList(opponent, 2, -1, true);
274 generate_move_flags = false;
277 while (pnt < TrPnt[3])
280 algbr(node->f, node->t, (short) node->flags);
282 if ((strcmp(s, mvstr[0]) == 0)
283 || (strcmp(s, mvstr[1]) == 0)
284 || (strcmp(s, mvstr[2]) == 0)
285 || (strcmp(s, mvstr[3]) == 0))
292 if ((cnt == 1) && (xnode.score > DONTUSE))
296 MakeMove(opponent, &xnode, &tempb, &tempc,
297 &tempsf, &tempst, &INCscore);
299 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
301 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
305 /* Illegal move in check */
306 printf(CP[77], mvstr[0]);
311 /* Illegal move in check */
312 sprintf(buffer, CP[77], s);
320 if (iop == VERIFY_AND_TRY_MODE)
323 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]);
365 sprintf(buffer, CP[76], s);
369 if (!barebones && (cnt > 1))
371 sprintf(buffer, CP[32], s);
381 parser(char *f, int side, short *fpiece)
389 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
391 if (f[0] == pxx[i] || f[0] == qxx[i])
393 *fpiece = (p ? promoted[i] : unpromoted[i]);
398 if (f[1] == '*' || f[1] == '\'')
403 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
411 p = (f[5] == '+') ? 0x80 : 0;
413 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
421 while (*InPtr != ' ')
424 while (*InPtr == ' ')
433 while (*InPtr == ' ')
450 strcpy(fname, savefile);
454 /* Enter file name */
469 if (fname[0] == '\0')
470 strcpy(fname, CP[137]);
472 if ((fd = fopen(fname, "r")) != NULL)
475 fgets(fname, 256, fd);
476 computer = opponent = black;
485 /* FIXME: write a skipn() function so that we can get
486 * 3 skips by doing skipn(3) */
490 Game50 = atoi(InPtr);
492 flag.force = (*InPtr == 'f');
493 fgets(fname, 256, fd); /* empty */
494 fgets(fname, 256, fd);
497 TCflag = atoi(InPtr);
501 OperatorTime = atoi(InPtr);
502 fgets(fname, 256, fd);
505 TimeControl.clock[black] = atol(InPtr);
508 TimeControl.moves[black] = atoi(InPtr);
509 fgets(fname, 256, fd);
512 TimeControl.clock[white] = atol(InPtr);
515 TimeControl.moves[white] = atoi(InPtr);
516 fgets(fname, 256, fd); /* empty */
518 for (i = NO_ROWS - 1; i > -1; i--)
520 fgets(fname, 256, fd);
524 for (j = 0; j < NO_COLS; j++)
526 sq = i * NO_COLS + j;
532 board[sq] = no_piece;
537 for (c = 0; c < NO_PIECES; c++)
542 board[sq] = promoted[c];
544 board[sq] = unpromoted[c];
550 for (c = 0; c < NO_PIECES; c++)
555 board[sq] = promoted[c];
557 board[sq] = unpromoted[c];
565 Mvboard[sq] = atoi(InPtr);
570 fgets(fname, 256, fd); /* empty */
571 fgets(fname, 256, fd); /* 9 8 7 ... */
572 fgets(fname, 256, fd); /* empty */
573 fgets(fname, 256, fd); /* p l n ... */
576 for (side = 0; side <= 1; side++)
578 fgets(fname, 256, fd);
582 Captured[side][pawn] = atoi(InPtr);
584 Captured[side][lance] = atoi(InPtr);
586 Captured[side][knight] = atoi(InPtr);
588 Captured[side][silver] = atoi(InPtr);
590 Captured[side][gold] = atoi(InPtr);
592 Captured[side][bishop] = atoi(InPtr);
594 Captured[side][rook] = atoi(InPtr);
596 Captured[side][king] = atoi(InPtr);
600 flag.regularstart = true;
602 fgets(fname, 256, fd); /* empty */
603 fgets(fname, 256, fd); /* move score ... */
605 while (fgets(fname, 256, fd))
614 g = &GameList[GameCnt];
615 g->gmove = parser(InPtr, side, &g->fpiece);
617 g->score = atoi(InPtr);
619 g->depth = atoi(InPtr);
621 g->nodes = atol(InPtr);
623 g->time = atol(InPtr);
625 g->flags = c = atoi(InPtr);
627 g->hashkey = strtol(InPtr, (char **) NULL, 16);
629 g->hashbd = strtol(InPtr, (char **) NULL, 16);
637 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
639 if (pxx[i] == *InPtr)
647 g->color = ((*InPtr == CP[119][0]) ? white : black);
649 g->piece = (*InPtr == '+'
651 : unpromoted[piece]);
660 if (TimeControl.clock[black] > 0)
668 UpdateDisplay(0, 0, 1, 0);
680 short sq, i, c, f, t;
683 char empty[2] = "\n";
687 strcpy(fname, savefile);
691 /* Enter file name */
705 if (fname[0] == '\0') /* shogi.000 */
706 strcpy(fname, CP[137]);
708 if ((fd = fopen(fname, "w")) != NULL)
713 if (computer == white)
716 if (computer == black)
719 fprintf(fd, CP[37], w, b, Game50,
720 flag.force ? "force" : "");
722 fprintf(fd, CP[111], TCflag, OperatorTime);
724 TimeControl.clock[black], TimeControl.moves[black],
725 TimeControl.clock[white], TimeControl.moves[white]);
728 for (i = NO_ROWS - 1; i > -1; i--)
730 fprintf(fd, "%c ", ROW_NAME(i));
732 for (c = 0; c < NO_COLS; c++)
734 sq = i * NO_COLS + c;
736 p = is_promoted[piece] ? '+' : ' ';
737 fprintf(fd, "%c", p);
753 fprintf(fd, "%c", p);
758 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
759 fprintf(fd, " %d", Mvboard[f]);
765 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n"); /* FIXME */
767 fprintf(fd, " p l n s g b r k\n");
769 for (side = 0; side <= 1; side++)
771 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
772 fprintf(fd, " %2d", Captured[side][pawn]);
773 fprintf(fd, " %2d", Captured[side][lance]);
774 fprintf(fd, " %2d", Captured[side][knight]);
775 fprintf(fd, " %2d", Captured[side][silver]);
776 fprintf(fd, " %2d", Captured[side][gold]);
777 fprintf(fd, " %2d", Captured[side][bishop]);
778 fprintf(fd, " %2d", Captured[side][rook]);
779 fprintf(fd, " %2d", Captured[side][king]);
786 for (i = 1; i <= GameCnt; i++)
788 struct GameRec *g = &GameList[i];
791 t = (g->gmove & 0xFF);
792 algbr(f, t, g->flags);
794 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
797 : (is_promoted[g->fpiece] ? '+' : ' ')),
799 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
801 g->nodes, g->time, g->flags,
802 g->hashkey, g->hashbd);
804 if (g->piece != no_piece)
806 fprintf(fd, " %c %s %c\n",
807 pxx[g->piece], ColorStr[g->color],
808 (is_promoted[g->piece] ? '+' : ' '));
823 /* ShowMessage("Could not open file"); */
831 * GetXGame, SaveXGame and BookGame used to only be defined if
832 * xshogi wasn't defined -- wonder why?
844 /* Enter file name */
857 if (fname[0] == '\0') /* XSHOGI.position.read */
858 strcpy(fname, CP[205]);
860 if ((fd = fopen(fname, "r")) != NULL)
863 flag.regularstart = false;
866 /* xshogi position file ... */
867 fgets(fname, 256, fd);
872 if (strcmp(fname, CP[206]))
876 /* -- empty line -- */
877 fgets(fname, 256, fd);
878 /* -- empty line -- */
879 fgets(fname, 256, fd);
881 for (i = NO_ROWS - 1; i > -1; i--)
883 fgets(fname, 256, fd);
886 for (j = 0; j < NO_COLS; j++)
888 sq = i * NO_COLS + j;
894 board[sq] = no_piece;
899 for (c = 0; c < NO_PIECES; c++)
904 board[sq] = promoted[c];
906 board[sq] = unpromoted[c];
912 for (c = 0; c < NO_PIECES; c++)
917 board[sq] = promoted[c];
919 board[sq] = unpromoted[c];
932 for (side = 0; side <= 1; side++)
934 fgets(fname, 256, fd);
936 Captured[side][pawn] = atoi(InPtr);
938 Captured[side][lance] = atoi(InPtr);
940 Captured[side][knight] = atoi(InPtr);
942 Captured[side][silver] = atoi(InPtr);
944 Captured[side][gold] = atoi(InPtr);
946 Captured[side][bishop] = atoi(InPtr);
948 Captured[side][rook] = atoi(InPtr);
950 Captured[side][king] = atoi(InPtr);
953 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
966 UpdateDisplay(0, 0, 1, 0);
981 /* Enter file name */
994 if (fname[0] == '\0') /* XSHOGI.position.read */
995 strcpy(fname, CP[205]);
997 if ((fd = fopen(fname, "w")) != NULL)
999 /* xshogi position file ... */
1000 fputs("# xshogi position file -- \n", fd);
1001 /* -- empty line -- */
1003 /* -- empty line -- */
1006 for (i = NO_ROWS - 1; i > -1; i--)
1010 for (j = 0; j < NO_COLS; j++)
1012 sq = i * NO_COLS + j;
1014 isp = is_promoted[piece];
1015 *p = (isp ? '+' : ' ');
1018 if (piece == no_piece)
1020 else if (color[sq] == white)
1033 for (side = 0; side <= 1; side++)
1035 sprintf(fname, "%d %d %d %d %d %d %d %d\n",
1036 Captured[side][pawn],
1037 Captured[side][lance],
1038 Captured[side][knight],
1039 Captured[side][silver],
1040 Captured[side][gold],
1041 Captured[side][bishop],
1042 Captured[side][rook],
1043 Captured[side][king]);
1048 if (computer == black)
1049 fputs("white to play\n", fd);
1051 fputs("black to play\n", fd);
1062 char fname[256], sflags[4];
1067 strcpy(fname, savefile);
1071 /* Enter file name */
1072 ShowMessage(CP[63]);
1085 if (fname[0] == '\0')
1088 if ((fd = fopen(fname, "a")) != NULL)
1092 for (i = 1; i <= GameCnt; i++)
1094 struct GameRec *g = &GameList[i];
1095 char mvnr[20], mvs[20];
1098 sprintf(mvnr, "%d.", (i + 1)/2);
1103 t = (g->gmove & 0xFF);
1104 algbr(f, t, g->flags);
1107 /* determine move quality string */
1108 if (g->flags & goodmove)
1111 if (g->flags & badmove)
1114 #ifdef EASY_OPENINGS
1115 if (g->flags & difficult)
1121 /* determine move string */
1124 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1128 sprintf(mvs, "%c%c%c%c%c%s%s ",
1129 mvstr[0][0], mvstr[0][1],
1130 (g->flags & capture) ? 'x' : '-',
1131 mvstr[0][2], mvstr[0][3],
1132 (mvstr[0][4] == '+') ? "+" : "",
1136 fprintf(fd, "%s%s%c%s",
1140 : (is_promoted[g->fpiece] ? "+" : "")),
1154 ShowMessage(CP[70]);
1158 /* ShowMessage("Could not open file"); */
1159 ShowMessage(CP[48]);
1171 char fname[256], dbuf[256];
1175 strcpy(fname, listfile);
1180 strncpy(dbuf, ctime(&when), 20);
1187 /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
1190 program played white */
1192 sprintf(fname, "CLp%s.%s%s-%s%s%s%c",
1193 patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1194 dbuf + 17, ColorStr[computer][0]);
1196 /* replace space padding with 0 */
1197 for (i = 0; fname[i] != '\0'; i++)
1199 if (fname[i] == ' ')
1204 fd = fopen(fname, "w");
1208 printf(CP[219], fname);
1212 /* fprintf(fd, "gnushogi game %d\n", u); */
1213 fprintf(fd, CP[161], version, patchlevel);
1217 for (i = 1; i <= GameCnt; i++)
1219 f = GameList[i].gmove >> 8;
1220 t = (GameList[i].gmove & 0xFF);
1221 algbr(f, t, GameList[i].flags);
1223 if (GameList[i].flags & book)
1225 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1228 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1229 pxx[GameList[i].fpiece],
1231 ? &mvstr[0][1] : mvstr[0]),
1238 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1241 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1242 pxx[GameList[i].fpiece],
1243 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1244 GameList[i].score, GameList[i].depth,
1245 GameList[i].nodes, GameList[i].time);
1258 fprintf(fd, "\n\n");
1260 if (GameList[GameCnt].flags & draw)
1262 fprintf(fd, CP[54], DRAW);
1264 if (DRAW == CP[101])
1268 fprintf(fd, "repetition by positions ");
1270 for (j = GameCnt - 1; j >= Game50; j -= 2)
1272 if (GameList[j].hashkey == hashkey &&
1273 GameList[j].hashbd == hashbd)
1274 fprintf(fd, "%d ", j);
1280 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1282 fprintf(fd, "%s\n", ColorStr[player ]);
1284 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1286 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1300 GameList[GameCnt].flags |= badmove;
1304 GameList[GameCnt].flags |= goodmove;
1307 #ifdef EASY_OPENINGS
1309 GameList[GameCnt].flags |= difficult;
1319 * Undo the most recent half-move.
1327 f = GameList[GameCnt].gmove >> 8;
1328 t = GameList[GameCnt].gmove & 0x7F;
1332 /* the move was a drop */
1333 Captured[color[t]][board[t]]++;
1334 board[t] = no_piece;
1340 if (GameList[GameCnt].flags & promote)
1341 board[f] = unpromoted[board[t]];
1343 board[f] = board[t];
1345 color[f] = color[t];
1346 board[t] = GameList[GameCnt].piece;
1347 color[t] = GameList[GameCnt].color;
1349 if (board[t] != no_piece)
1350 Captured[color[f]][unpromoted[board[t]]]--;
1352 if (color[t] != neutral)
1360 if (TCflag && (TCmoves > 1))
1361 ++TimeControl.moves[color[f]];
1363 hashkey = GameList[GameCnt].hashkey;
1364 hashbd = GameList[GameCnt].hashbd;
1366 computer = computer ^ 1;
1367 opponent = opponent ^ 1;
1370 player = player ^ 1;
1372 UpdateDisplay(0, 0, 1, 0);
1374 if (flag.regularstart)
1381 FlagString(unsigned short flags, char *s)
1386 if (flags & promote)
1387 strcat(s, " promote");
1389 if (flags & dropmask)
1390 strcat(s, " drop:");
1392 if ((piece = (flags & pmask)))
1396 if (is_promoted[piece])
1399 s[l++] = pxx[piece];
1403 if (flags & capture)
1404 strcat(s, " capture");
1407 strcat(s, " exact");
1410 strcat(s, " tesuji");
1413 strcat(s, " check");
1419 strcat(s, " stupid");
1421 if (flags & questionable)
1422 strcat(s, " questionable");
1424 if (flags & kingattack)
1425 strcat(s, " kingattack");
1434 TestSpeed(void(*f)(short side, short ply,
1435 short in_check, short blockable),
1443 long cnt, rate, t1, t2;
1445 #ifdef HAVE_GETTIMEOFDAY
1449 #ifdef HAVE_GETTIMEOFDAY
1450 gettimeofday(&tv, NULL);
1451 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1456 for (i = 0; i < j; i++)
1458 f(opponent, 2, -1, true);
1461 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1463 if (!pick(jj, TrPnt[3] - 1))
1469 #ifdef HAVE_GETTIMEOFDAY
1470 gettimeofday(&tv, NULL);
1471 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1476 cnt = j * (TrPnt[3] - TrPnt[2]);
1483 rate = (((et) ? ((cnt * 100) / et) : 0));
1485 #ifdef DYNAMIC_ZNODES
1491 printf(CP[91], cnt, rate);
1499 TestPSpeed(short(*f) (short side), unsigned j)
1502 long cnt, rate, t1, t2;
1503 #ifdef HAVE_GETTIMEOFDAY
1507 #ifdef HAVE_GETTIMEOFDAY
1508 gettimeofday(&tv, NULL);
1509 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1514 for (i = 0; i < j; i++)
1517 #ifdef HAVE_GETTIMEOFDAY
1518 gettimeofday(&tv, NULL);
1519 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1531 rate = (et) ? ((cnt * 100) / et) : 0;
1533 /* printf("Nodes= %ld Nodes/sec= %ld\n", cnt, rate); */
1536 printf(CP[91], cnt, rate);
1550 time = &s[strlen(CP[228])];
1551 t = (int)strtol(time, &time, 10);
1556 sec = (int)strtol(time, &time, 10);
1559 m = (int)strtol(time, &time, 10);
1562 TimeControl.clock[opponent] = t;
1565 TimeControl.moves[opponent] = m;
1567 ElapsedTime(COMPUTE_AND_INIT_MODE);
1571 /* just to inform xshogi about availability of otime command */
1572 printf("otime %d %d\n", t, m);
1579 SetMachineTime(char *s)
1584 time = &s[strlen(CP[197])];
1586 t = (int)strtol(time, &time, 10);
1591 sec = (int)strtol(time, &time, 10);
1594 m = (int)strtol(time, &time, 10);
1597 TimeControl.clock[computer] = t;
1600 TimeControl.moves[computer] = m;
1602 ElapsedTime(COMPUTE_AND_INIT_MODE);
1606 /* just to inform xshogi about availability of time command */
1607 printf("time %d %d\n", t, m);
1615 /* FIXME! This is truly the function from hell! */
1618 * Process the user's command. If easy mode is OFF (the computer is thinking
1619 * on opponents time) and the program is out of book, then make the 'hint'
1620 * move on the board and call SelectMove() to find a response. The user
1621 * terminates the search by entering ^C (quit siqnal) before entering a
1622 * command. If the opponent does not make the hint move, then set Sdepth to
1627 InputCommand(char *command)
1630 short have_shown_prompt = false;
1631 short ok, done, is_move = false;
1635 ok = flag.quit = done = false;
1639 if (TTadd > ttbllimit)
1643 if ((hint > 0) && !flag.easy && !flag.force)
1646 * A hint move for the player is available. Compute a move for the
1647 * opponent in background mode assuming that the hint move will be
1648 * selected by the player.
1651 ft = time0; /* Save reference time for the player. */
1653 algbr((short) hint >> 8, (short) hint & 0xff, false);
1654 strcpy(s, mvstr[0]);
1661 /* do the hint move */
1662 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1666 #ifdef QUIETBACKGROUND
1677 have_shown_prompt = true;
1678 #endif /* QUIETBACKGROUND */
1680 /* Start computing a move until the search is interrupted. */
1682 #ifdef INTERRUPT_TEST
1686 /* would love to put null move in here */
1687 /* after we make the hint move make a 2 ply search
1688 * with both plys our moves */
1689 /* think on opponents time */
1690 SelectMove(computer, BACKGROUND_MODE);
1692 #ifdef INTERRUPT_TEST
1693 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1697 printf("searching not terminated by interrupt!\n");
1701 printf("elapsed time from interrupt to "
1702 "terminating search: %ld\n", it);
1706 /* undo the hint and carry on */
1707 VerifyMove(s, UNMAKE_MODE, &mv);
1711 time0 = ft; /* Restore reference time for the player. */
1714 while(!(ok || flag.quit || done))
1718 #ifdef QUIETBACKGROUND
1719 if (!have_shown_prompt)
1721 #endif /* QUIETBACKGROUND */
1733 #ifdef QUIETBACKGROUND
1736 have_shown_prompt = false;
1737 #endif /* QUIETBACKGROUND */
1739 if (command == NULL)
1743 s[0] = sx[0] = '\0';
1746 (void)fgets(sx, 80, stdin);
1751 eof = (getstr(sx) == ERR);
1756 strcpy(sx, command);
1760 sscanf(sx, "%s", s);
1768 if (strcmp(s, CP[131]) == 0) /* bd -- display board */
1770 /* FIXME: Hack alert! */
1771 short old_xshogi = XSHOGI;
1774 display_type = DISPLAY_RAW;
1777 UpdateDisplay(0, 0, 1, 0);
1780 display_type = DISPLAY_X;
1782 else if (strcmp(s, "post") == 0)
1784 flag.post = !flag.post;
1786 else if (strcmp(s, CP[129]) == 0)
1788 /* noop */ ; /* alg */
1790 else if ((strcmp(s, CP[180]) == 0)
1791 || (strcmp(s, CP[216]) == 0)) /* quit exit */
1796 else if (strcmp(s, CP[178]) == 0) /* post */
1798 flag.post = !flag.post;
1801 else if ((strcmp(s, CP[191]) == 0)
1802 || (strcmp(s, CP[154]) == 0)) /* set edit */
1806 else if (NOT_CURSES && (strcmp(s, CP[190]) == 0)) /* setup */
1810 else if (strcmp(s, CP[156]) == 0) /* first */
1814 else if (strcmp(s, CP[162]) == 0) /* go */
1819 if (computer == black)
1830 else if (strcmp(s, CP[166]) == 0) /* help */
1834 else if (strcmp(s, CP[221]) == 0) /* material */
1836 flag.material = !flag.material;
1838 else if (strcmp(s, CP[157]) == 0) /* force */
1843 flag.bothsides = false;
1847 flag.force = !flag.force;
1848 flag.bothsides = false;
1851 else if (strcmp(s, CP[134]) == 0) /* book */
1853 Book = Book ? 0 : BOOKFAIL;
1855 else if (strcmp(s, CP[172]) == 0) /* new */
1858 UpdateDisplay(0, 0, 1, 0);
1860 else if (strcmp(s, CP[171]) == 0) /* list */
1864 else if ((strcmp(s, CP[169]) == 0)
1865 || (strcmp(s, CP[217]) == 0)) /* level clock */
1869 else if (strcmp(s, CP[165]) == 0) /* hash */
1871 flag.hash = !flag.hash;
1873 else if (strcmp(s, CP[227]) == 0) /* gamein */
1875 flag.gamein = !flag.gamein;
1877 else if (strcmp(s, CP[226]) == 0) /* beep */
1879 flag.beep = !flag.beep;
1881 else if (strcmp(s, CP[197]) == 0) /* time */
1885 else if (strcmp(s, CP[228]) == 0) /* otime */
1889 else if (strcmp(s, CP[33]) == 0) /* Awindow */
1891 ChangeAlphaWindow();
1893 else if (strcmp(s, CP[39]) == 0) /* Bwindow */
1897 else if (strcmp(s, CP[183]) == 0) /* rcptr */
1899 flag.rcptr = !flag.rcptr;
1901 else if (strcmp(s, CP[168]) == 0) /* hint */
1905 else if (strcmp(s, CP[135]) == 0) /* both */
1907 flag.bothsides = !flag.bothsides;
1910 ElapsedTime(COMPUTE_AND_INIT_MODE);
1911 SelectMove(opponent, FOREGROUND_MODE);
1914 else if (strcmp(s, CP[185]) == 0) /* reverse */
1916 flag.reverse = !flag.reverse;
1918 UpdateDisplay(0, 0, 1, 0);
1920 else if (strcmp(s, CP[195]) == 0) /* switch */
1922 computer = computer ^ 1;
1923 opponent = opponent ^ 1;
1924 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1929 else if (strcmp(s, CP[203]) == 0) /* black */
1938 * ok = true; don't automatically start with black command
1941 else if (strcmp(s, CP[133]) == 0) /* white */
1950 * ok = true; don't automatically start with white command
1953 else if (strcmp(s, CP[201]) == 0 && GameCnt > 0) /* undo */
1957 else if (strcmp(s, CP[184]) == 0 && GameCnt > 1) /* remove */
1962 /* CHECKME: are these next three correct? */
1963 else if (!XSHOGI && strcmp(s, CP[207]) == 0) /* xget */
1967 else if (!XSHOGI && strcmp(s, "xsave") == 0) /* xsave */
1971 else if (!XSHOGI && strcmp(s, "bsave") == 0) /* bsave */
1975 #ifdef EASY_OPENINGS
1976 else if ((strcmp(s, "?") == 0)
1977 || (strcmp(s, "!") == 0)
1978 || (strcmp(s, "~") == 0))
1980 else if ((strcmp(s, "?") == 0)
1981 || (strcmp(s, "!") == 0))
1986 else if (strcmp(s, CP[160]) == 0) /* get */
1990 else if (strcmp(s, CP[189]) == 0) /* save */
1994 else if (strcmp(s, CP[151]) == 0) /* depth */
1996 ChangeSearchDepth();
1998 else if (strcmp(s, CP[164]) == 0) /* hashdepth */
2002 else if (strcmp(s, CP[182]) == 0) /* random */
2006 else if (strcmp(s, CP[229]) == 0) /* hard */
2010 else if (strcmp(s, CP[152]) == 0) /* easy */
2012 flag.easy = !flag.easy;
2014 else if (strcmp(s, CP[230]) == 0) /* tsume */
2016 flag.tsume = !flag.tsume;
2018 else if (strcmp(s, CP[143]) == 0) /* contempt */
2022 else if (strcmp(s, CP[209]) == 0) /* xwndw */
2026 else if (strcmp(s, CP[186]) == 0) /* rv */
2029 UpdateDisplay(0, 0, 1, 0);
2031 else if (strcmp(s, CP[145]) == 0) /* coords */
2033 flag.coords = !flag.coords;
2034 UpdateDisplay(0, 0, 1, 0);
2036 else if (strcmp(s, CP[193]) == 0) /* stars */
2038 flag.stars = !flag.stars;
2039 UpdateDisplay(0, 0, 1, 0);
2041 else if (!XSHOGI && strcmp(s, CP[5]) == 0) /* moves */
2048 extern unsigned short PrVar[MAXDEPTH];
2050 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
2057 ShowMessage(CP[108]); /* test movelist */
2058 temp = generate_move_flags;
2059 generate_move_flags = true;
2060 TestSpeed(MoveList, 1);
2061 generate_move_flags = temp;
2062 ShowMessage(CP[107]); /* test capturelist */
2063 TestSpeed(CaptureList, 1);
2064 ShowMessage(CP[85]); /* test score position */
2065 ExaminePosition(opponent);
2066 TestPSpeed(ScorePosition, 1);
2068 else if (!XSHOGI && strcmp(s, CP[196]) == 0) /* test */
2071 ShowMessage(CP[108]); /* test movelist */
2072 TestSpeed(MoveList, 2000);
2073 ShowMessage(CP[107]); /* test capturelist */
2074 TestSpeed(CaptureList, 3000);
2075 ShowMessage(CP[85]); /* test score position */
2076 ExaminePosition(opponent);
2077 TestPSpeed(ScorePosition, 1500);
2079 ShowMessage(CP[108]); /* test movelist */
2080 TestSpeed(MoveList, 20000);
2081 ShowMessage(CP[107]); /* test capturelist */
2082 TestSpeed(CaptureList, 30000);
2083 ShowMessage(CP[85]); /* test score position */
2084 ExaminePosition(opponent);
2085 TestPSpeed(ScorePosition, 15000);
2088 else if (!XSHOGI && strcmp(s, CP[179]) == 0) /* p */
2092 else if (!XSHOGI && strcmp(s, CP[148]) == 0) /* debug */
2102 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2104 /* check for repetition */
2105 short rpt = repetition();
2111 GameList[GameCnt].flags |= draw;
2125 ElapsedTime(COMPUTE_AND_INIT_MODE);
2129 computer = opponent;
2130 opponent = computer ^ 1;
2135 /* add remaining time in milliseconds for xshogi */
2138 printf("%d. %s %ld\n",
2139 ++mycnt2, s, TimeControl.clock[player] * 10);
2142 #ifdef notdef /* optional pass best line to frontend with move */
2143 # if !defined NOPOST
2145 if (flag.post && !flag.mate)
2149 printf(" %6d ", MSCORE);
2151 for (i = 1; MV[i] > 0; i++)
2153 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2154 printf("%5s ", mvstr[0]);
2162 signal(SIGUSR1, TerminateSearch);
2169 SetTimeControl(void)
2173 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2174 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2175 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2179 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2180 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2183 flag.onemove = (TCmoves == 1);
2185 ElapsedTime(COMPUTE_AND_INIT_MODE);