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.
131 algbr(short f, short t, short flag)
137 piece = f - NO_SQUARES;
139 if (f > (NO_SQUARES + NO_PIECES))
142 flag = (dropmask | piece);
151 if ((f == t) && ((f != 0) || (t != 0)))
156 printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
158 printw("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
161 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
163 else if ((flag & dropmask) != 0)
165 short piece = flag & pmask;
167 mvstr[0][0] = pxx[piece];
169 mvstr[0][2] = cxx[column(t)];
170 mvstr[0][3] = rxx[row(t)];
172 strcpy(mvstr[1], mvstr[0]);
173 strcpy(mvstr[2], mvstr[0]);
174 strcpy(mvstr[3], mvstr[0]);
176 else if ((f != 0) || (t != 0))
178 /* algebraic notation */
179 mvstr[0][0] = cxx[column(f)];
180 mvstr[0][1] = rxx[row(f)];
181 mvstr[0][2] = cxx[column(t)];
182 mvstr[0][3] = rxx[row(t)];
183 mvstr[0][4] = mvstr[3][0] = '\0';
184 mvstr[1][0] = pxx[board[f]];
186 mvstr[2][0] = mvstr[1][0];
187 mvstr[2][1] = mvstr[0][1];
189 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
190 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
191 mvstr[2][4] = mvstr[1][3] = '\0';
192 strcpy(mvstr[3], mvstr[2]);
193 mvstr[3][1] = mvstr[0][0];
197 strcat(mvstr[0], "+");
198 strcat(mvstr[1], "+");
199 strcat(mvstr[2], "+");
200 strcat(mvstr[3], "+");
205 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
212 * Compare the string 's' to the list of legal moves available for the
213 * opponent. If a match is found, make the move on the board.
217 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
219 static short pnt, tempb, tempc, tempsf, tempst, cnt;
220 static struct leaf xnode;
222 short i, l, local_flags;
225 /* check and remove quality flags */
226 for (i = local_flags = 0, l = strlen(s); i < l; i++)
231 local_flags |= badmove;
236 local_flags |= goodmove;
242 local_flags |= difficult;
251 if (iop == UNMAKE_MODE)
253 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
259 if (iop == VERIFY_AND_MAKE_MODE)
260 generate_move_flags = true;
262 MoveList(opponent, 2, -1, true);
263 generate_move_flags = false;
266 while (pnt < TrPnt[3])
269 algbr(node->f, node->t, (short) node->flags);
271 if ((strcmp(s, mvstr[0]) == 0)
272 || (strcmp(s, mvstr[1]) == 0)
273 || (strcmp(s, mvstr[2]) == 0)
274 || (strcmp(s, mvstr[3]) == 0))
281 if ((cnt == 1) && (xnode.score > DONTUSE))
285 MakeMove(opponent, &xnode, &tempb, &tempc,
286 &tempsf, &tempst, &INCscore);
288 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
290 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
294 /* Illegal move in check */
295 printf(CP[77], mvstr[0]);
300 /* Illegal move in check */
301 sprintf(buffer, CP[77], s);
309 if (iop == VERIFY_AND_TRY_MODE)
312 UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
313 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
314 GameList[GameCnt].nodes = 0;
315 ElapsedTime(COMPUTE_AND_INIT_MODE);
316 GameList[GameCnt].time = (short) (et + 50)/100;
317 GameList[GameCnt].flags |= local_flags;
321 TimeControl.clock[opponent] -= et;
322 timeopp[oppptr] = et;
323 --TimeControl.moves[opponent];
326 *mv = (xnode.f << 8) | xnode.t;
327 algbr(xnode.f, xnode.t, false);
329 /* in force mode, check for mate conditions */
332 if (IsCheckmate(opponent ^ 1, -1, -1))
336 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
354 sprintf(buffer, CP[76], s);
358 if (!barebones && (cnt > 1))
360 sprintf(buffer, CP[32], s);
370 parser(char *f, int side, 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 == ' ')
422 while (*InPtr == ' ')
439 strcpy(fname, savefile);
443 /* Enter file name */
458 if (fname[0] == '\0')
459 strcpy(fname, CP[137]);
461 if ((fd = fopen(fname, "r")) != NULL)
464 fgets(fname, 256, fd);
465 computer = opponent = black;
474 /* FIXME: write a skipn() function so that we can get
475 * 3 skips by doing skipn(3) */
479 Game50 = atoi(InPtr);
481 flag.force = (*InPtr == 'f');
482 fgets(fname, 256, fd); /* empty */
483 fgets(fname, 256, fd);
486 TCflag = atoi(InPtr);
490 OperatorTime = atoi(InPtr);
491 fgets(fname, 256, fd);
494 TimeControl.clock[black] = atol(InPtr);
497 TimeControl.moves[black] = atoi(InPtr);
498 fgets(fname, 256, fd);
501 TimeControl.clock[white] = atol(InPtr);
504 TimeControl.moves[white] = atoi(InPtr);
505 fgets(fname, 256, fd); /* empty */
507 for (i = NO_ROWS - 1; i > -1; i--)
509 fgets(fname, 256, fd);
513 for (j = 0; j < NO_COLS; j++)
515 sq = i * NO_COLS + j;
521 board[sq] = no_piece;
526 for (c = 0; c < NO_PIECES; c++)
531 board[sq] = promoted[c];
533 board[sq] = unpromoted[c];
539 for (c = 0; c < NO_PIECES; c++)
544 board[sq] = promoted[c];
546 board[sq] = unpromoted[c];
554 Mvboard[sq] = atoi(InPtr);
559 fgets(fname, 256, fd); /* empty */
560 fgets(fname, 256, fd); /* 9 8 7 ... */
561 fgets(fname, 256, fd); /* empty */
562 fgets(fname, 256, fd); /* p l n ... */
565 for (side = 0; side <= 1; side++)
567 fgets(fname, 256, fd);
571 Captured[side][pawn] = atoi(InPtr);
573 Captured[side][lance] = atoi(InPtr);
575 Captured[side][knight] = atoi(InPtr);
577 Captured[side][silver] = atoi(InPtr);
579 Captured[side][gold] = atoi(InPtr);
581 Captured[side][bishop] = atoi(InPtr);
583 Captured[side][rook] = atoi(InPtr);
585 Captured[side][king] = atoi(InPtr);
589 flag.regularstart = true;
591 fgets(fname, 256, fd); /* empty */
592 fgets(fname, 256, fd); /* move score ... */
594 while (fgets(fname, 256, fd))
603 g = &GameList[GameCnt];
604 g->gmove = parser(InPtr, side, &g->fpiece);
606 g->score = atoi(InPtr);
608 g->depth = atoi(InPtr);
610 g->nodes = atol(InPtr);
612 g->time = atol(InPtr);
614 g->flags = c = atoi(InPtr);
616 g->hashkey = strtol(InPtr, (char **) NULL, 16);
618 g->hashbd = strtol(InPtr, (char **) NULL, 16);
626 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
628 if (pxx[i] == *InPtr)
636 g->color = ((*InPtr == CP[119][0]) ? white : black);
638 g->piece = (*InPtr == '+'
640 : unpromoted[piece]);
649 if (TimeControl.clock[black] > 0)
657 UpdateDisplay(0, 0, 1, 0);
669 short sq, i, c, f, t;
672 char empty[2] = "\n";
676 strcpy(fname, savefile);
680 /* Enter file name */
694 if (fname[0] == '\0') /* shogi.000 */
695 strcpy(fname, CP[137]);
697 if ((fd = fopen(fname, "w")) != NULL)
702 if (computer == white)
705 if (computer == black)
708 fprintf(fd, CP[37], w, b, Game50,
709 flag.force ? "force" : "");
711 fprintf(fd, CP[111], TCflag, OperatorTime);
713 TimeControl.clock[black], TimeControl.moves[black],
714 TimeControl.clock[white], TimeControl.moves[white]);
717 for (i = NO_ROWS - 1; i > -1; i--)
719 fprintf(fd, "%c ", 'i' - i);
721 for (c = 0; c < NO_COLS; c++)
723 sq = i * NO_COLS + c;
725 p = is_promoted[piece] ? '+' : ' ';
726 fprintf(fd, "%c", p);
742 fprintf(fd, "%c", p);
747 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
748 fprintf(fd, " %d", Mvboard[f]);
754 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
756 fprintf(fd, " p l n s g b r k\n");
758 for (side = 0; side <= 1; side++)
760 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
761 fprintf(fd, " %2d", Captured[side][pawn]);
762 fprintf(fd, " %2d", Captured[side][lance]);
763 fprintf(fd, " %2d", Captured[side][knight]);
764 fprintf(fd, " %2d", Captured[side][silver]);
765 fprintf(fd, " %2d", Captured[side][gold]);
766 fprintf(fd, " %2d", Captured[side][bishop]);
767 fprintf(fd, " %2d", Captured[side][rook]);
768 fprintf(fd, " %2d", Captured[side][king]);
773 fprintf(fd, CP[126]);
775 for (i = 1; i <= GameCnt; i++)
777 struct GameRec *g = &GameList[i];
780 t = (g->gmove & 0xFF);
781 algbr(f, t, g->flags);
783 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
786 : (is_promoted[g->fpiece] ? '+' : ' ')),
788 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
790 g->nodes, g->time, g->flags,
791 g->hashkey, g->hashbd);
793 if (g->piece != no_piece)
795 fprintf(fd, " %c %s %c\n",
796 pxx[g->piece], ColorStr[g->color],
797 (is_promoted[g->piece] ? '+' : ' '));
812 /* ShowMessage("Could not open file"); */
820 * GetXGame, SaveXGame and BookGame used to only be defined if
821 * xshogi wasn't defined -- wonder why?
833 /* Enter file name */
846 if (fname[0] == '\0') /* XSHOGI.position.read */
847 strcpy(fname, CP[205]);
849 if ((fd = fopen(fname, "r")) != NULL)
852 flag.regularstart = false;
855 /* xshogi position file ... */
856 fgets(fname, 256, fd);
861 if (strcmp(fname, CP[206]))
865 /* -- empty line -- */
866 fgets(fname, 256, fd);
867 /* -- empty line -- */
868 fgets(fname, 256, fd);
870 for (i = NO_ROWS - 1; i > -1; i--)
872 fgets(fname, 256, fd);
875 for (j = 0; j < NO_COLS; j++)
877 sq = i * NO_COLS + j;
883 board[sq] = no_piece;
888 for (c = 0; c < NO_PIECES; c++)
893 board[sq] = promoted[c];
895 board[sq] = unpromoted[c];
901 for (c = 0; c < NO_PIECES; c++)
906 board[sq] = promoted[c];
908 board[sq] = unpromoted[c];
921 for (side = 0; side <= 1; side++)
923 fgets(fname, 256, fd);
925 Captured[side][pawn] = atoi(InPtr);
927 Captured[side][lance] = atoi(InPtr);
929 Captured[side][knight] = atoi(InPtr);
931 Captured[side][silver] = atoi(InPtr);
933 Captured[side][gold] = atoi(InPtr);
935 Captured[side][bishop] = atoi(InPtr);
937 Captured[side][rook] = atoi(InPtr);
939 Captured[side][king] = atoi(InPtr);
942 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
955 UpdateDisplay(0, 0, 1, 0);
970 /* Enter file name */
983 if (fname[0] == '\0') /* XSHOGI.position.read */
984 strcpy(fname, CP[205]);
986 if ((fd = fopen(fname, "w")) != NULL)
988 /* xshogi position file ... */
989 fputs("# xshogi position file -- \n", fd);
990 /* -- empty line -- */
992 /* -- empty line -- */
995 for (i = NO_ROWS - 1; i > -1; i--)
999 for (j = 0; j < NO_COLS; j++)
1001 sq = i * NO_COLS + j;
1003 isp = is_promoted[piece];
1004 *p = (isp ? '+' : ' ');
1007 if (piece == no_piece)
1009 else if (color[sq] == white)
1022 for (side = 0; side <= 1; side++)
1024 sprintf(fname, "%d %d %d %d %d %d %d %d\n",
1025 Captured[side][pawn],
1026 Captured[side][lance],
1027 Captured[side][knight],
1028 Captured[side][silver],
1029 Captured[side][gold],
1030 Captured[side][bishop],
1031 Captured[side][rook],
1032 Captured[side][king]);
1037 if (computer == black)
1038 fputs("white to play\n", fd);
1040 fputs("black to play\n", fd);
1051 char fname[256], sflags[4];
1056 strcpy(fname, savefile);
1060 /* Enter file name */
1061 ShowMessage(CP[63]);
1074 if (fname[0] == '\0')
1077 if ((fd = fopen(fname, "a")) != NULL)
1081 for (i = 1; i <= GameCnt; i++)
1083 struct GameRec *g = &GameList[i];
1084 char mvnr[20], mvs[20];
1087 sprintf(mvnr, "%d.", (i + 1)/2);
1092 t = (g->gmove & 0xFF);
1093 algbr(f, t, g->flags);
1096 /* determine move quality string */
1097 if (g->flags & goodmove)
1100 if (g->flags & badmove)
1103 #ifdef EASY_OPENINGS
1104 if (g->flags & difficult)
1110 /* determine move string */
1113 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1117 sprintf(mvs, "%c%c%c%c%c%s%s ",
1118 mvstr[0][0], mvstr[0][1],
1119 (g->flags & capture) ? 'x' : '-',
1120 mvstr[0][2], mvstr[0][3],
1121 (mvstr[0][4] == '+') ? "+" : "",
1125 fprintf(fd, "%s%s%c%s",
1129 : (is_promoted[g->fpiece] ? "+" : "")),
1143 ShowMessage(CP[70]);
1147 /* ShowMessage("Could not open file"); */
1148 ShowMessage(CP[48]);
1160 char fname[256], dbuf[256];
1164 strcpy(fname, listfile);
1169 strncpy(dbuf, ctime(&when), 20);
1176 /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
1179 program played white */
1181 sprintf(fname, "CLp%s.%s%s-%s%s%s%c",
1182 patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1183 dbuf + 17, ColorStr[computer][0]);
1185 /* replace space padding with 0 */
1186 for (i = 0; fname[i] != '\0'; i++)
1188 if (fname[i] == ' ')
1193 fd = fopen(fname, "w");
1197 printf(CP[219], fname);
1201 /* fprintf(fd, "gnushogi game %d\n", u); */
1202 fprintf(fd, CP[161], version, patchlevel);
1203 fprintf(fd, CP[10]);
1204 fprintf(fd, CP[11]);
1206 for (i = 1; i <= GameCnt; i++)
1208 f = GameList[i].gmove >> 8;
1209 t = (GameList[i].gmove & 0xFF);
1210 algbr(f, t, GameList[i].flags);
1212 if (GameList[i].flags & book)
1214 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1217 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1218 pxx[GameList[i].fpiece],
1220 ? &mvstr[0][1] : mvstr[0]),
1227 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1230 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1231 pxx[GameList[i].fpiece],
1232 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1233 GameList[i].score, GameList[i].depth,
1234 GameList[i].nodes, GameList[i].time);
1247 fprintf(fd, "\n\n");
1249 if (GameList[GameCnt].flags & draw)
1251 fprintf(fd, CP[54], DRAW);
1253 if (DRAW == CP[101])
1257 fprintf(fd, "repetition by positions ");
1259 for (j = GameCnt - 1; j >= Game50; j -= 2)
1261 if (GameList[j].hashkey == hashkey &&
1262 GameList[j].hashbd == hashbd)
1263 fprintf(fd, "%d ", j);
1269 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1271 fprintf(fd, "%s\n", ColorStr[player ]);
1273 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1275 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1289 GameList[GameCnt].flags |= badmove;
1293 GameList[GameCnt].flags |= goodmove;
1296 #ifdef EASY_OPENINGS
1298 GameList[GameCnt].flags |= difficult;
1308 * Undo the most recent half-move.
1316 f = GameList[GameCnt].gmove >> 8;
1317 t = GameList[GameCnt].gmove & 0x7F;
1321 /* the move was a drop */
1322 Captured[color[t]][board[t]]++;
1323 board[t] = no_piece;
1329 if (GameList[GameCnt].flags & promote)
1330 board[f] = unpromoted[board[t]];
1332 board[f] = board[t];
1334 color[f] = color[t];
1335 board[t] = GameList[GameCnt].piece;
1336 color[t] = GameList[GameCnt].color;
1338 if (board[t] != no_piece)
1339 Captured[color[f]][unpromoted[board[t]]]--;
1341 if (color[t] != neutral)
1349 if (TCflag && (TCmoves > 1))
1350 ++TimeControl.moves[color[f]];
1352 hashkey = GameList[GameCnt].hashkey;
1353 hashbd = GameList[GameCnt].hashbd;
1355 computer = computer ^ 1;
1356 opponent = opponent ^ 1;
1359 player = player ^ 1;
1361 UpdateDisplay(0, 0, 1, 0);
1363 if (flag.regularstart)
1370 FlagString(unsigned short flags, char *s)
1375 if (flags & promote)
1376 strcat(s, " promote");
1378 if (flags & dropmask)
1379 strcat(s, " drop:");
1381 if ((piece = (flags & pmask)))
1385 if (is_promoted[piece])
1388 s[l++] = pxx[piece];
1392 if (flags & capture)
1393 strcat(s, " capture");
1396 strcat(s, " exact");
1399 strcat(s, " tesuji");
1402 strcat(s, " check");
1408 strcat(s, " stupid");
1410 if (flags & questionable)
1411 strcat(s, " questionable");
1413 if (flags & kingattack)
1414 strcat(s, " kingattack");
1423 TestSpeed(void(*f)(short side, short ply,
1424 short in_check, short blockable),
1432 long cnt, rate, t1, t2;
1434 #ifdef HAVE_GETTIMEOFDAY
1438 #ifdef HAVE_GETTIMEOFDAY
1439 gettimeofday(&tv, NULL);
1440 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1445 for (i = 0; i < j; i++)
1447 f(opponent, 2, -1, true);
1450 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1452 if (!pick(jj, TrPnt[3] - 1))
1458 #ifdef HAVE_GETTIMEOFDAY
1459 gettimeofday(&tv, NULL);
1460 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1465 cnt = j * (TrPnt[3] - TrPnt[2]);
1472 rate = (((et) ? ((cnt * 100) / et) : 0));
1474 #ifdef DYNAMIC_ZNODES
1480 printf(CP[91], cnt, rate);
1488 TestPSpeed(short(*f) (short side), unsigned j)
1491 long cnt, rate, t1, t2;
1492 #ifdef HAVE_GETTIMEOFDAY
1496 #ifdef HAVE_GETTIMEOFDAY
1497 gettimeofday(&tv, NULL);
1498 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1503 for (i = 0; i < j; i++)
1506 #ifdef HAVE_GETTIMEOFDAY
1507 gettimeofday(&tv, NULL);
1508 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1520 rate = (et) ? ((cnt * 100) / et) : 0;
1522 /* printf("Nodes= %ld Nodes/sec= %ld\n", cnt, rate); */
1525 printf(CP[91], cnt, rate);
1539 time = &s[strlen(CP[228])];
1540 t = (int)strtol(time, &time, 10);
1545 sec = (int)strtol(time, &time, 10);
1548 m = (int)strtol(time, &time, 10);
1551 TimeControl.clock[opponent] = t;
1554 TimeControl.moves[opponent] = m;
1556 ElapsedTime(COMPUTE_AND_INIT_MODE);
1560 /* just to inform xshogi about availability of otime command */
1561 printf("otime %d %d\n", t, m);
1568 SetMachineTime(char *s)
1573 time = &s[strlen(CP[197])];
1575 t = (int)strtol(time, &time, 10);
1580 sec = (int)strtol(time, &time, 10);
1583 m = (int)strtol(time, &time, 10);
1586 TimeControl.clock[computer] = t;
1589 TimeControl.moves[computer] = m;
1591 ElapsedTime(COMPUTE_AND_INIT_MODE);
1595 /* just to inform xshogi about availability of time command */
1596 printf("time %d %d\n", t, m);
1604 /* FIXME! This is truly the function from hell! */
1607 * Process the user's command. If easy mode is OFF (the computer is thinking
1608 * on opponents time) and the program is out of book, then make the 'hint'
1609 * move on the board and call SelectMove() to find a response. The user
1610 * terminates the search by entering ^C (quit siqnal) before entering a
1611 * command. If the opponent does not make the hint move, then set Sdepth to
1616 InputCommand(char *command)
1619 short have_shown_prompt = false;
1620 short ok, done, is_move = false;
1624 ok = flag.quit = done = false;
1628 if (TTadd > ttbllimit)
1632 if ((hint > 0) && !flag.easy && !flag.force)
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(s, mvstr[0]);
1650 /* do the hint move */
1651 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1655 #ifdef QUIETBACKGROUND
1666 have_shown_prompt = true;
1667 #endif /* QUIETBACKGROUND */
1669 /* Start computing a move until the search is interrupted. */
1671 #ifdef INTERRUPT_TEST
1675 /* would love to put null move in here */
1676 /* after we make the hint move make a 2 ply search
1677 * with both plys our moves */
1678 /* think on opponents time */
1679 SelectMove(computer, BACKGROUND_MODE);
1681 #ifdef INTERRUPT_TEST
1682 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1686 printf("searching not terminated by interrupt!\n");
1690 printf("elapsed time from interrupt to "
1691 "terminating search: %ld\n", it);
1695 /* undo the hint and carry on */
1696 VerifyMove(s, UNMAKE_MODE, &mv);
1700 time0 = ft; /* Restore reference time for the player. */
1703 while(!(ok || flag.quit || done))
1707 #ifdef QUIETBACKGROUND
1708 if (!have_shown_prompt)
1710 #endif /* QUIETBACKGROUND */
1722 #ifdef QUIETBACKGROUND
1725 have_shown_prompt = false;
1726 #endif /* QUIETBACKGROUND */
1728 if (command == NULL)
1732 s[0] = sx[0] = '\0';
1735 (void)fgets(sx, 80, stdin);
1740 eof = (getstr(sx) == ERR);
1745 strcpy(sx, command);
1749 sscanf(sx, "%s", s);
1757 if (strcmp(s, CP[131]) == 0) /* bd -- display board */
1759 /* FIXME: Hack alert! */
1760 short old_xshogi = XSHOGI;
1763 display_type = DISPLAY_RAW;
1766 UpdateDisplay(0, 0, 1, 0);
1769 display_type = DISPLAY_X;
1771 else if (strcmp(s, "post") == 0)
1773 flag.post = !flag.post;
1775 else if (strcmp(s, CP[129]) == 0)
1777 /* noop */ ; /* alg */
1779 else if ((strcmp(s, CP[180]) == 0)
1780 || (strcmp(s, CP[216]) == 0)) /* quit exit */
1785 else if (strcmp(s, CP[178]) == 0) /* post */
1787 flag.post = !flag.post;
1790 else if ((strcmp(s, CP[191]) == 0)
1791 || (strcmp(s, CP[154]) == 0)) /* set edit */
1795 else if (NOT_CURSES && (strcmp(s, CP[190]) == 0)) /* setup */
1799 else if (strcmp(s, CP[156]) == 0) /* first */
1803 else if (strcmp(s, CP[162]) == 0) /* go */
1808 if (computer == black)
1819 else if (strcmp(s, CP[166]) == 0) /* help */
1823 else if (strcmp(s, CP[221]) == 0) /* material */
1825 flag.material = !flag.material;
1827 else if (strcmp(s, CP[157]) == 0) /* force */
1832 flag.bothsides = false;
1836 flag.force = !flag.force;
1837 flag.bothsides = false;
1840 else if (strcmp(s, CP[134]) == 0) /* book */
1842 Book = Book ? 0 : BOOKFAIL;
1844 else if (strcmp(s, CP[172]) == 0) /* new */
1847 UpdateDisplay(0, 0, 1, 0);
1849 else if (strcmp(s, CP[171]) == 0) /* list */
1853 else if ((strcmp(s, CP[169]) == 0)
1854 || (strcmp(s, CP[217]) == 0)) /* level clock */
1858 else if (strcmp(s, CP[165]) == 0) /* hash */
1860 flag.hash = !flag.hash;
1862 else if (strcmp(s, CP[227]) == 0) /* gamein */
1864 flag.gamein = !flag.gamein;
1866 else if (strcmp(s, CP[226]) == 0) /* beep */
1868 flag.beep = !flag.beep;
1870 else if (strcmp(s, CP[197]) == 0) /* time */
1874 else if (strcmp(s, CP[228]) == 0) /* otime */
1878 else if (strcmp(s, CP[33]) == 0) /* Awindow */
1880 ChangeAlphaWindow();
1882 else if (strcmp(s, CP[39]) == 0) /* Bwindow */
1886 else if (strcmp(s, CP[183]) == 0) /* rcptr */
1888 flag.rcptr = !flag.rcptr;
1890 else if (strcmp(s, CP[168]) == 0) /* hint */
1894 else if (strcmp(s, CP[135]) == 0) /* both */
1896 flag.bothsides = !flag.bothsides;
1899 ElapsedTime(COMPUTE_AND_INIT_MODE);
1900 SelectMove(opponent, FOREGROUND_MODE);
1903 else if (strcmp(s, CP[185]) == 0) /* reverse */
1905 flag.reverse = !flag.reverse;
1907 UpdateDisplay(0, 0, 1, 0);
1909 else if (strcmp(s, CP[195]) == 0) /* switch */
1911 computer = computer ^ 1;
1912 opponent = opponent ^ 1;
1913 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1918 else if (strcmp(s, CP[203]) == 0) /* black */
1927 * ok = true; don't automatically start with black command
1930 else if (strcmp(s, CP[133]) == 0) /* white */
1939 * ok = true; don't automatically start with white command
1942 else if (strcmp(s, CP[201]) == 0 && GameCnt > 0) /* undo */
1946 else if (strcmp(s, CP[184]) == 0 && GameCnt > 1) /* remove */
1951 /* CHECKME: are these next three correct? */
1952 else if (!XSHOGI && strcmp(s, CP[207]) == 0) /* xget */
1956 else if (!XSHOGI && strcmp(s, "xsave") == 0) /* xsave */
1960 else if (!XSHOGI && strcmp(s, "bsave") == 0) /* bsave */
1964 #ifdef EASY_OPENINGS
1965 else if ((strcmp(s, "?") == 0)
1966 || (strcmp(s, "!") == 0)
1967 || (strcmp(s, "~") == 0))
1969 else if ((strcmp(s, "?") == 0)
1970 || (strcmp(s, "!") == 0))
1975 else if (strcmp(s, CP[160]) == 0) /* get */
1979 else if (strcmp(s, CP[189]) == 0) /* save */
1983 else if (strcmp(s, CP[151]) == 0) /* depth */
1985 ChangeSearchDepth();
1987 else if (strcmp(s, CP[164]) == 0) /* hashdepth */
1991 else if (strcmp(s, CP[182]) == 0) /* random */
1995 else if (strcmp(s, CP[229]) == 0) /* hard */
1999 else if (strcmp(s, CP[152]) == 0) /* easy */
2001 flag.easy = !flag.easy;
2003 else if (strcmp(s, CP[230]) == 0) /* tsume */
2005 flag.tsume = !flag.tsume;
2007 else if (strcmp(s, CP[143]) == 0) /* contempt */
2011 else if (strcmp(s, CP[209]) == 0) /* xwndw */
2015 else if (strcmp(s, CP[186]) == 0) /* rv */
2018 UpdateDisplay(0, 0, 1, 0);
2020 else if (strcmp(s, CP[145]) == 0) /* coords */
2022 flag.coords = !flag.coords;
2023 UpdateDisplay(0, 0, 1, 0);
2025 else if (strcmp(s, CP[193]) == 0) /* stars */
2027 flag.stars = !flag.stars;
2028 UpdateDisplay(0, 0, 1, 0);
2030 else if (!XSHOGI && strcmp(s, CP[5]) == 0) /* moves */
2037 extern unsigned short PrVar[MAXDEPTH];
2039 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
2046 ShowMessage(CP[108]); /* test movelist */
2047 temp = generate_move_flags;
2048 generate_move_flags = true;
2049 TestSpeed(MoveList, 1);
2050 generate_move_flags = temp;
2051 ShowMessage(CP[107]); /* test capturelist */
2052 TestSpeed(CaptureList, 1);
2053 ShowMessage(CP[85]); /* test score position */
2054 ExaminePosition(opponent);
2055 TestPSpeed(ScorePosition, 1);
2057 else if (!XSHOGI && strcmp(s, CP[196]) == 0) /* test */
2060 ShowMessage(CP[108]); /* test movelist */
2061 TestSpeed(MoveList, 2000);
2062 ShowMessage(CP[107]); /* test capturelist */
2063 TestSpeed(CaptureList, 3000);
2064 ShowMessage(CP[85]); /* test score position */
2065 ExaminePosition(opponent);
2066 TestPSpeed(ScorePosition, 1500);
2068 ShowMessage(CP[108]); /* test movelist */
2069 TestSpeed(MoveList, 20000);
2070 ShowMessage(CP[107]); /* test capturelist */
2071 TestSpeed(CaptureList, 30000);
2072 ShowMessage(CP[85]); /* test score position */
2073 ExaminePosition(opponent);
2074 TestPSpeed(ScorePosition, 15000);
2077 else if (!XSHOGI && strcmp(s, CP[179]) == 0) /* p */
2081 else if (!XSHOGI && strcmp(s, CP[148]) == 0) /* debug */
2091 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2093 /* check for repetition */
2094 short rpt = repetition();
2100 GameList[GameCnt].flags |= draw;
2114 ElapsedTime(COMPUTE_AND_INIT_MODE);
2118 computer = opponent;
2119 opponent = computer ^ 1;
2124 /* add remaining time in milliseconds for xshogi */
2127 printf("%d. %s %ld\n",
2128 ++mycnt2, s, TimeControl.clock[player] * 10);
2131 #ifdef notdef /* optional pass best line to frontend with move */
2132 # if !defined NOPOST
2134 if (flag.post && !flag.mate)
2138 printf(" %6d ", MSCORE);
2140 for (i = 1; MV[i] > 0; i++)
2142 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2143 printf("%5s ", mvstr[0]);
2151 signal(SIGUSR1, TerminateSearch);
2158 SetTimeControl(void)
2162 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2163 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2164 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2168 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2169 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2172 flag.onemove = (TCmoves == 1);
2174 ElapsedTime(COMPUTE_AND_INIT_MODE);