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
10 * GNU SHOGI is based on GNU CHESS
12 * Copyright (c) 1988, 1989, 1990 John Stanback
13 * Copyright (c) 1992 Free Software Foundation
15 * This file is part of GNU SHOGI.
17 * GNU Shogi is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 1, or (at your option) any
22 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License along
28 * with GNU Shogi; see the file COPYING. If not, write to the Free
29 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 * ----------------------------------------------------------------------
36 #if defined HAVE_GETTIMEOFDAY
42 int InBackground = false;
48 #include <sys/param.h>
49 #include <sys/types.h>
58 movealgbr(short m, char *s)
61 short piece = 0, flag = 0;
74 piece = f - NO_SQUARES;
76 if (piece > NO_PIECES)
79 flag = (dropmask | piece);
126 #endif /* BOOKTEST */
132 * Generate move strings in different formats.
136 algbr(short f, short t, short flag)
142 piece = f - NO_SQUARES;
144 if (f > (NO_SQUARES + NO_PIECES))
147 flag = (dropmask | piece);
156 if ((f == t) && ((f != 0) || (t != 0)))
161 printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
163 printw("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
166 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
168 else if ((flag & dropmask) != 0)
170 short piece = flag & pmask;
172 mvstr[0][0] = pxx[piece];
174 mvstr[0][2] = cxx[column(t)];
175 mvstr[0][3] = rxx[row(t)];
177 strcpy(mvstr[1], mvstr[0]);
178 strcpy(mvstr[2], mvstr[0]);
179 strcpy(mvstr[3], mvstr[0]);
181 else if ((f != 0) || (t != 0))
183 /* algebraic notation */
184 mvstr[0][0] = cxx[column(f)];
185 mvstr[0][1] = rxx[row(f)];
186 mvstr[0][2] = cxx[column(t)];
187 mvstr[0][3] = rxx[row(t)];
188 mvstr[0][4] = mvstr[3][0] = '\0';
189 mvstr[1][0] = pxx[board[f]];
191 mvstr[2][0] = mvstr[1][0];
192 mvstr[2][1] = mvstr[0][1];
194 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
195 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
196 mvstr[2][4] = mvstr[1][3] = '\0';
197 strcpy(mvstr[3], mvstr[2]);
198 mvstr[3][1] = mvstr[0][0];
202 strcat(mvstr[0], "+");
203 strcat(mvstr[1], "+");
204 strcat(mvstr[2], "+");
205 strcat(mvstr[3], "+");
210 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
217 * Compare the string 's' to the list of legal moves available for the
218 * opponent. If a match is found, make the move on the board.
222 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
224 static short pnt, tempb, tempc, tempsf, tempst, cnt;
225 static struct leaf xnode;
227 short i, l, local_flags;
230 /* check and remove quality flags */
231 for (i = local_flags = 0, l = strlen(s); i < l; i++)
236 local_flags |= badmove;
241 local_flags |= goodmove;
247 local_flags |= difficult;
256 if (iop == UNMAKE_MODE)
258 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
264 if (iop == VERIFY_AND_MAKE_MODE)
265 generate_move_flags = true;
267 MoveList(opponent, 2, -1, true);
268 generate_move_flags = false;
271 while (pnt < TrPnt[3])
274 algbr(node->f, node->t, (short) node->flags);
276 if ((strcmp(s, mvstr[0]) == 0)
277 || (strcmp(s, mvstr[1]) == 0)
278 || (strcmp(s, mvstr[2]) == 0)
279 || (strcmp(s, mvstr[3]) == 0))
286 if ((cnt == 1) && (xnode.score > DONTUSE))
290 MakeMove(opponent, &xnode, &tempb, &tempc,
291 &tempsf, &tempst, &INCscore);
293 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
295 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
299 /* Illegal move in check */
300 printf(CP[77], mvstr[0]);
305 /* Illegal move in check */
306 sprintf(buffer, CP[77], s);
314 if (iop == VERIFY_AND_TRY_MODE)
317 UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
318 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
319 GameList[GameCnt].nodes = 0;
320 ElapsedTime(COMPUTE_AND_INIT_MODE);
321 GameList[GameCnt].time = (short) (et + 50)/100;
322 GameList[GameCnt].flags |= local_flags;
326 TimeControl.clock[opponent] -= et;
327 timeopp[oppptr] = et;
328 --TimeControl.moves[opponent];
331 *mv = (xnode.f << 8) | xnode.t;
332 algbr(xnode.f, xnode.t, false);
334 /* in force mode, check for mate conditions */
337 if (IsCheckmate(opponent ^ 1, -1, -1))
341 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
359 sprintf(buffer, CP[76], s);
363 if (!barebones && (cnt > 1))
365 sprintf(buffer, CP[32], s);
375 parser(char *f, int side, short *fpiece)
383 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
385 if (f[0] == pxx[i] || f[0] == qxx[i])
387 *fpiece = (p ? promoted[i] : unpromoted[i]);
392 if (f[1] == '*' || f[1] == '\'')
397 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
405 p = (f[5] == '+') ? 0x80 : 0;
407 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
415 while (*InPtr != ' ')
418 while (*InPtr == ' ')
427 while (*InPtr == ' ')
444 strcpy(fname, savefile);
448 /* Enter file name */
463 if (fname[0] == '\0')
464 strcpy(fname, CP[137]);
466 if ((fd = fopen(fname, "r")) != NULL)
469 fgets(fname, 256, fd);
470 computer = opponent = black;
479 /* FIXME: write a skipn() function so that we can get
480 * 3 skips by doing skipn(3) */
484 Game50 = atoi(InPtr);
486 flag.force = (*InPtr == 'f');
487 fgets(fname, 256, fd); /* empty */
488 fgets(fname, 256, fd);
491 TCflag = atoi(InPtr);
495 OperatorTime = atoi(InPtr);
496 fgets(fname, 256, fd);
499 TimeControl.clock[black] = atol(InPtr);
502 TimeControl.moves[black] = atoi(InPtr);
503 fgets(fname, 256, fd);
506 TimeControl.clock[white] = atol(InPtr);
509 TimeControl.moves[white] = atoi(InPtr);
510 fgets(fname, 256, fd); /* empty */
512 for (i = NO_ROWS - 1; i > -1; i--)
514 fgets(fname, 256, fd);
518 for (j = 0; j < NO_COLS; j++)
520 sq = i * NO_COLS + j;
526 board[sq] = no_piece;
531 for (c = 0; c < NO_PIECES; c++)
536 board[sq] = promoted[c];
538 board[sq] = unpromoted[c];
544 for (c = 0; c < NO_PIECES; c++)
549 board[sq] = promoted[c];
551 board[sq] = unpromoted[c];
559 Mvboard[sq] = atoi(InPtr);
564 fgets(fname, 256, fd); /* empty */
565 fgets(fname, 256, fd); /* 9 8 7 ... */
566 fgets(fname, 256, fd); /* empty */
567 fgets(fname, 256, fd); /* p l n ... */
570 for (side = 0; side <= 1; side++)
572 fgets(fname, 256, fd);
576 Captured[side][pawn] = atoi(InPtr);
578 Captured[side][lance] = atoi(InPtr);
580 Captured[side][knight] = atoi(InPtr);
582 Captured[side][silver] = atoi(InPtr);
584 Captured[side][gold] = atoi(InPtr);
586 Captured[side][bishop] = atoi(InPtr);
588 Captured[side][rook] = atoi(InPtr);
590 Captured[side][king] = atoi(InPtr);
594 flag.regularstart = true;
596 fgets(fname, 256, fd); /* empty */
597 fgets(fname, 256, fd); /* move score ... */
599 while (fgets(fname, 256, fd))
608 g = &GameList[GameCnt];
609 g->gmove = parser(InPtr, side, &g->fpiece);
611 g->score = atoi(InPtr);
613 g->depth = atoi(InPtr);
615 g->nodes = atol(InPtr);
617 g->time = atol(InPtr);
619 g->flags = c = atoi(InPtr);
621 g->hashkey = strtol(InPtr, (char **) NULL, 16);
623 g->hashbd = strtol(InPtr, (char **) NULL, 16);
631 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
633 if (pxx[i] == *InPtr)
641 g->color = ((*InPtr == CP[119][0]) ? white : black);
643 g->piece = (*InPtr == '+'
645 : unpromoted[piece]);
654 if (TimeControl.clock[black] > 0)
662 UpdateDisplay(0, 0, 1, 0);
674 short sq, i, c, f, t;
677 char empty[2] = "\n";
681 strcpy(fname, savefile);
685 /* Enter file name */
699 if (fname[0] == '\0') /* shogi.000 */
700 strcpy(fname, CP[137]);
702 if ((fd = fopen(fname, "w")) != NULL)
707 if (computer == white)
710 if (computer == black)
713 fprintf(fd, CP[37], w, b, Game50,
714 flag.force ? "force" : "");
716 fprintf(fd, CP[111], TCflag, OperatorTime);
718 TimeControl.clock[black], TimeControl.moves[black],
719 TimeControl.clock[white], TimeControl.moves[white]);
722 for (i = NO_ROWS - 1; i > -1; i--)
724 fprintf(fd, "%c ", 'i' - i);
726 for (c = 0; c < NO_COLS; c++)
728 sq = i * NO_COLS + c;
730 p = is_promoted[piece] ? '+' : ' ';
731 fprintf(fd, "%c", p);
747 fprintf(fd, "%c", p);
752 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
753 fprintf(fd, " %d", Mvboard[f]);
759 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
761 fprintf(fd, " p l n s g b r k\n");
763 for (side = 0; side <= 1; side++)
765 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
766 fprintf(fd, " %2d", Captured[side][pawn]);
767 fprintf(fd, " %2d", Captured[side][lance]);
768 fprintf(fd, " %2d", Captured[side][knight]);
769 fprintf(fd, " %2d", Captured[side][silver]);
770 fprintf(fd, " %2d", Captured[side][gold]);
771 fprintf(fd, " %2d", Captured[side][bishop]);
772 fprintf(fd, " %2d", Captured[side][rook]);
773 fprintf(fd, " %2d", Captured[side][king]);
778 fprintf(fd, CP[126]);
780 for (i = 1; i <= GameCnt; i++)
782 struct GameRec *g = &GameList[i];
785 t = (g->gmove & 0xFF);
786 algbr(f, t, g->flags);
788 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
791 : (is_promoted[g->fpiece] ? '+' : ' ')),
793 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
795 g->nodes, g->time, g->flags,
796 g->hashkey, g->hashbd);
798 if (g->piece != no_piece)
800 fprintf(fd, " %c %s %c\n",
801 pxx[g->piece], ColorStr[g->color],
802 (is_promoted[g->piece] ? '+' : ' '));
817 /* ShowMessage("Could not open file"); */
825 * GetXGame, SaveXGame and BookGame used to only be defined if
826 * xshogi wasn't defined -- wonder why?
838 /* Enter file name */
851 if (fname[0] == '\0') /* XSHOGI.position.read */
852 strcpy(fname, CP[205]);
854 if ((fd = fopen(fname, "r")) != NULL)
857 flag.regularstart = false;
860 /* xshogi position file ... */
861 fgets(fname, 256, fd);
866 if (strcmp(fname, CP[206]))
870 /* -- empty line -- */
871 fgets(fname, 256, fd);
872 /* -- empty line -- */
873 fgets(fname, 256, fd);
875 for (i = NO_ROWS - 1; i > -1; i--)
877 fgets(fname, 256, fd);
880 for (j = 0; j < NO_COLS; j++)
882 sq = i * NO_COLS + j;
888 board[sq] = no_piece;
893 for (c = 0; c < NO_PIECES; c++)
898 board[sq] = promoted[c];
900 board[sq] = unpromoted[c];
906 for (c = 0; c < NO_PIECES; c++)
911 board[sq] = promoted[c];
913 board[sq] = unpromoted[c];
926 for (side = 0; side <= 1; side++)
928 fgets(fname, 256, fd);
930 Captured[side][pawn] = atoi(InPtr);
932 Captured[side][lance] = atoi(InPtr);
934 Captured[side][knight] = atoi(InPtr);
936 Captured[side][silver] = atoi(InPtr);
938 Captured[side][gold] = atoi(InPtr);
940 Captured[side][bishop] = atoi(InPtr);
942 Captured[side][rook] = atoi(InPtr);
944 Captured[side][king] = atoi(InPtr);
947 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
960 UpdateDisplay(0, 0, 1, 0);
975 /* Enter file name */
988 if (fname[0] == '\0') /* XSHOGI.position.read */
989 strcpy(fname, CP[205]);
991 if ((fd = fopen(fname, "w")) != NULL)
993 /* xshogi position file ... */
994 fputs("# xshogi position file -- \n", fd);
995 /* -- empty line -- */
997 /* -- empty line -- */
1000 for (i = NO_ROWS - 1; i > -1; i--)
1004 for (j = 0; j < NO_COLS; j++)
1006 sq = i * NO_COLS + j;
1008 isp = is_promoted[piece];
1009 *p = (isp ? '+' : ' ');
1012 if (piece == no_piece)
1014 else if (color[sq] == white)
1027 for (side = 0; side <= 1; side++)
1029 sprintf(fname, "%d %d %d %d %d %d %d %d\n",
1030 Captured[side][pawn],
1031 Captured[side][lance],
1032 Captured[side][knight],
1033 Captured[side][silver],
1034 Captured[side][gold],
1035 Captured[side][bishop],
1036 Captured[side][rook],
1037 Captured[side][king]);
1042 if (computer == black)
1043 fputs("white to play\n", fd);
1045 fputs("black to play\n", fd);
1056 char fname[256], sflags[4];
1061 strcpy(fname, savefile);
1065 /* Enter file name */
1066 ShowMessage(CP[63]);
1079 if (fname[0] == '\0')
1082 if ((fd = fopen(fname, "a")) != NULL)
1086 for (i = 1; i <= GameCnt; i++)
1088 struct GameRec *g = &GameList[i];
1089 char mvnr[20], mvs[20];
1092 sprintf(mvnr, "%d.", (i + 1)/2);
1097 t = (g->gmove & 0xFF);
1098 algbr(f, t, g->flags);
1101 /* determine move quality string */
1102 if (g->flags & goodmove)
1105 if (g->flags & badmove)
1108 #ifdef EASY_OPENINGS
1109 if (g->flags & difficult)
1115 /* determine move string */
1118 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1122 sprintf(mvs, "%c%c%c%c%c%s%s ",
1123 mvstr[0][0], mvstr[0][1],
1124 (g->flags & capture) ? 'x' : '-',
1125 mvstr[0][2], mvstr[0][3],
1126 (mvstr[0][4] == '+') ? "+" : "",
1130 fprintf(fd, "%s%s%c%s",
1134 : (is_promoted[g->fpiece] ? "+" : "")),
1148 ShowMessage(CP[70]);
1152 /* ShowMessage("Could not open file"); */
1153 ShowMessage(CP[48]);
1165 char fname[256], dbuf[256];
1169 strcpy(fname, listfile);
1174 strncpy(dbuf, ctime(&when), 20);
1181 /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
1184 program played white */
1186 sprintf(fname, "CLp%s.%s%s-%s%s%s%c",
1187 patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1188 dbuf + 17, ColorStr[computer][0]);
1190 /* replace space padding with 0 */
1191 for (i = 0; fname[i] != '\0'; i++)
1193 if (fname[i] == ' ')
1198 fd = fopen(fname, "w");
1202 printf(CP[219], fname);
1206 /* fprintf(fd, "gnushogi game %d\n", u); */
1207 fprintf(fd, CP[161], version, patchlevel);
1208 fprintf(fd, CP[10]);
1209 fprintf(fd, CP[11]);
1211 for (i = 1; i <= GameCnt; i++)
1213 f = GameList[i].gmove >> 8;
1214 t = (GameList[i].gmove & 0xFF);
1215 algbr(f, t, GameList[i].flags);
1217 if (GameList[i].flags & book)
1219 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1222 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1223 pxx[GameList[i].fpiece],
1225 ? &mvstr[0][1] : mvstr[0]),
1232 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1235 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1236 pxx[GameList[i].fpiece],
1237 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1238 GameList[i].score, GameList[i].depth,
1239 GameList[i].nodes, GameList[i].time);
1252 fprintf(fd, "\n\n");
1254 if (GameList[GameCnt].flags & draw)
1256 fprintf(fd, CP[54], DRAW);
1258 if (DRAW == CP[101])
1262 fprintf(fd, "repetition by positions ");
1264 for (j = GameCnt - 1; j >= Game50; j -= 2)
1266 if (GameList[j].hashkey == hashkey &&
1267 GameList[j].hashbd == hashbd)
1268 fprintf(fd, "%d ", j);
1274 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1276 fprintf(fd, "%s\n", ColorStr[player ]);
1278 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1280 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1294 GameList[GameCnt].flags |= badmove;
1298 GameList[GameCnt].flags |= goodmove;
1301 #ifdef EASY_OPENINGS
1303 GameList[GameCnt].flags |= difficult;
1313 * Undo the most recent half-move.
1321 f = GameList[GameCnt].gmove >> 8;
1322 t = GameList[GameCnt].gmove & 0x7F;
1326 /* the move was a drop */
1327 Captured[color[t]][board[t]]++;
1328 board[t] = no_piece;
1334 if (GameList[GameCnt].flags & promote)
1335 board[f] = unpromoted[board[t]];
1337 board[f] = board[t];
1339 color[f] = color[t];
1340 board[t] = GameList[GameCnt].piece;
1341 color[t] = GameList[GameCnt].color;
1343 if (board[t] != no_piece)
1344 Captured[color[f]][unpromoted[board[t]]]--;
1346 if (color[t] != neutral)
1354 if (TCflag && (TCmoves > 1))
1355 ++TimeControl.moves[color[f]];
1357 hashkey = GameList[GameCnt].hashkey;
1358 hashbd = GameList[GameCnt].hashbd;
1360 computer = computer ^ 1;
1361 opponent = opponent ^ 1;
1364 player = player ^ 1;
1366 UpdateDisplay(0, 0, 1, 0);
1368 if (flag.regularstart)
1375 FlagString(unsigned short flags, char *s)
1380 if (flags & promote)
1381 strcat(s, " promote");
1383 if (flags & dropmask)
1384 strcat(s, " drop:");
1386 if ((piece = (flags & pmask)))
1390 if (is_promoted[piece])
1393 s[l++] = pxx[piece];
1397 if (flags & capture)
1398 strcat(s, " capture");
1401 strcat(s, " exact");
1404 strcat(s, " tesuji");
1407 strcat(s, " check");
1413 strcat(s, " stupid");
1415 if (flags & questionable)
1416 strcat(s, " questionable");
1418 if (flags & kingattack)
1419 strcat(s, " kingattack");
1428 TestSpeed(void(*f)(short side, short ply,
1429 short in_check, short blockable),
1437 long cnt, rate, t1, t2;
1439 #ifdef HAVE_GETTIMEOFDAY
1443 #ifdef HAVE_GETTIMEOFDAY
1444 gettimeofday(&tv, NULL);
1445 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1450 for (i = 0; i < j; i++)
1452 f(opponent, 2, -1, true);
1455 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1457 if (!pick(jj, TrPnt[3] - 1))
1463 #ifdef HAVE_GETTIMEOFDAY
1464 gettimeofday(&tv, NULL);
1465 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1470 cnt = j * (TrPnt[3] - TrPnt[2]);
1477 rate = (((et) ? ((cnt * 100) / et) : 0));
1479 #ifdef DYNAMIC_ZNODES
1485 printf(CP[91], cnt, rate);
1493 TestPSpeed(short(*f) (short side), unsigned j)
1496 long cnt, rate, t1, t2;
1497 #ifdef HAVE_GETTIMEOFDAY
1501 #ifdef HAVE_GETTIMEOFDAY
1502 gettimeofday(&tv, NULL);
1503 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1508 for (i = 0; i < j; i++)
1511 #ifdef HAVE_GETTIMEOFDAY
1512 gettimeofday(&tv, NULL);
1513 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1525 rate = (et) ? ((cnt * 100) / et) : 0;
1527 /* printf("Nodes= %ld Nodes/sec= %ld\n", cnt, rate); */
1530 printf(CP[91], cnt, rate);
1544 time = &s[strlen(CP[228])];
1545 t = (int)strtol(time, &time, 10);
1550 sec = (int)strtol(time, &time, 10);
1553 m = (int)strtol(time, &time, 10);
1556 TimeControl.clock[opponent] = t;
1559 TimeControl.moves[opponent] = m;
1561 ElapsedTime(COMPUTE_AND_INIT_MODE);
1565 /* just to inform xshogi about availability of otime command */
1566 printf("otime %d %d\n", t, m);
1573 SetMachineTime(char *s)
1578 time = &s[strlen(CP[197])];
1580 t = (int)strtol(time, &time, 10);
1585 sec = (int)strtol(time, &time, 10);
1588 m = (int)strtol(time, &time, 10);
1591 TimeControl.clock[computer] = t;
1594 TimeControl.moves[computer] = m;
1596 ElapsedTime(COMPUTE_AND_INIT_MODE);
1600 /* just to inform xshogi about availability of time command */
1601 printf("time %d %d\n", t, m);
1609 /* FIXME! This is truly the function from hell! */
1612 * Process the user's command. If easy mode is OFF (the computer is thinking
1613 * on opponents time) and the program is out of book, then make the 'hint'
1614 * move on the board and call SelectMove() to find a response. The user
1615 * terminates the search by entering ^C (quit siqnal) before entering a
1616 * command. If the opponent does not make the hint move, then set Sdepth to
1621 InputCommand(char *command)
1624 short have_shown_prompt = false;
1625 short ok, done, is_move = false;
1629 ok = flag.quit = done = false;
1633 if (TTadd > ttbllimit)
1637 if ((hint > 0) && !flag.easy && !flag.force)
1640 * A hint move for the player is available. Compute a move for the
1641 * opponent in background mode assuming that the hint move will be
1642 * selected by the player.
1645 ft = time0; /* Save reference time for the player. */
1647 algbr((short) hint >> 8, (short) hint & 0xff, false);
1648 strcpy(s, mvstr[0]);
1655 /* do the hint move */
1656 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1660 #ifdef QUIETBACKGROUND
1671 have_shown_prompt = true;
1672 #endif /* QUIETBACKGROUND */
1674 /* Start computing a move until the search is interrupted. */
1676 #ifdef INTERRUPT_TEST
1680 /* would love to put null move in here */
1681 /* after we make the hint move make a 2 ply search
1682 * with both plys our moves */
1683 /* think on opponents time */
1684 SelectMove(computer, BACKGROUND_MODE);
1686 #ifdef INTERRUPT_TEST
1687 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1691 printf("searching not terminated by interrupt!\n");
1695 printf("elapsed time from interrupt to "
1696 "terminating search: %ld\n", it);
1700 /* undo the hint and carry on */
1701 VerifyMove(s, UNMAKE_MODE, &mv);
1705 time0 = ft; /* Restore reference time for the player. */
1708 while(!(ok || flag.quit || done))
1712 #ifdef QUIETBACKGROUND
1713 if (!have_shown_prompt)
1715 #endif /* QUIETBACKGROUND */
1727 #ifdef QUIETBACKGROUND
1730 have_shown_prompt = false;
1731 #endif /* QUIETBACKGROUND */
1733 if (command == NULL)
1737 s[0] = sx[0] = '\0';
1740 (void)fgets(sx, 256, stdin);
1745 eof = (getstr(sx) == ERR);
1750 strcpy(sx, command);
1754 sscanf(sx, "%s", s);
1762 if (strcmp(s, CP[131]) == 0) /* bd -- display board */
1764 /* FIXME: Hack alert! */
1765 short old_xshogi = XSHOGI;
1768 display_type = DISPLAY_RAW;
1771 UpdateDisplay(0, 0, 1, 0);
1774 display_type = DISPLAY_X;
1776 else if (strcmp(s, "post") == 0)
1778 flag.post = !flag.post;
1780 else if (strcmp(s, CP[129]) == 0)
1782 /* noop */ ; /* alg */
1784 else if ((strcmp(s, CP[180]) == 0)
1785 || (strcmp(s, CP[216]) == 0)) /* quit exit */
1790 else if (strcmp(s, CP[178]) == 0) /* post */
1792 flag.post = !flag.post;
1795 else if ((strcmp(s, CP[191]) == 0)
1796 || (strcmp(s, CP[154]) == 0)) /* set edit */
1800 else if (NOT_CURSES && (strcmp(s, CP[190]) == 0)) /* setup */
1804 else if (strcmp(s, CP[156]) == 0) /* first */
1808 else if (strcmp(s, CP[162]) == 0) /* go */
1813 if (computer == black)
1824 else if (strcmp(s, CP[166]) == 0) /* help */
1828 else if (strcmp(s, CP[221]) == 0) /* material */
1830 flag.material = !flag.material;
1832 else if (strcmp(s, CP[157]) == 0) /* force */
1837 flag.bothsides = false;
1841 flag.force = !flag.force;
1842 flag.bothsides = false;
1845 else if (strcmp(s, CP[134]) == 0) /* book */
1847 Book = Book ? 0 : BOOKFAIL;
1849 else if (strcmp(s, CP[172]) == 0) /* new */
1852 UpdateDisplay(0, 0, 1, 0);
1854 else if (strcmp(s, CP[171]) == 0) /* list */
1858 else if ((strcmp(s, CP[169]) == 0)
1859 || (strcmp(s, CP[217]) == 0)) /* level clock */
1863 else if (strcmp(s, CP[165]) == 0) /* hash */
1865 flag.hash = !flag.hash;
1867 else if (strcmp(s, CP[227]) == 0) /* gamein */
1869 flag.gamein = !flag.gamein;
1871 else if (strcmp(s, CP[226]) == 0) /* beep */
1873 flag.beep = !flag.beep;
1875 else if (strcmp(s, CP[197]) == 0) /* time */
1879 else if (strcmp(s, CP[228]) == 0) /* otime */
1883 else if (strcmp(s, CP[33]) == 0) /* Awindow */
1885 ChangeAlphaWindow();
1887 else if (strcmp(s, CP[39]) == 0) /* Bwindow */
1891 else if (strcmp(s, CP[183]) == 0) /* rcptr */
1893 flag.rcptr = !flag.rcptr;
1895 else if (strcmp(s, CP[168]) == 0) /* hint */
1899 else if (strcmp(s, CP[135]) == 0) /* both */
1901 flag.bothsides = !flag.bothsides;
1904 ElapsedTime(COMPUTE_AND_INIT_MODE);
1905 SelectMove(opponent, FOREGROUND_MODE);
1908 else if (strcmp(s, CP[185]) == 0) /* reverse */
1910 flag.reverse = !flag.reverse;
1912 UpdateDisplay(0, 0, 1, 0);
1914 else if (strcmp(s, CP[195]) == 0) /* switch */
1916 computer = computer ^ 1;
1917 opponent = opponent ^ 1;
1918 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1923 else if (strcmp(s, CP[203]) == 0) /* black */
1932 * ok = true; don't automatically start with black command
1935 else if (strcmp(s, CP[133]) == 0) /* white */
1944 * ok = true; don't automatically start with white command
1947 else if (strcmp(s, CP[201]) == 0 && GameCnt > 0) /* undo */
1951 else if (strcmp(s, CP[184]) == 0 && GameCnt > 1) /* remove */
1956 /* CHECKME: are these next three correct? */
1957 else if (!XSHOGI && strcmp(s, CP[207]) == 0) /* xget */
1961 else if (!XSHOGI && strcmp(s, "xsave") == 0) /* xsave */
1965 else if (!XSHOGI && strcmp(s, "bsave") == 0) /* bsave */
1969 #ifdef EASY_OPENINGS
1970 else if ((strcmp(s, "?") == 0)
1971 || (strcmp(s, "!") == 0)
1972 || (strcmp(s, "~") == 0))
1974 else if ((strcmp(s, "?") == 0)
1975 || (strcmp(s, "!") == 0))
1980 else if (strcmp(s, CP[160]) == 0) /* get */
1984 else if (strcmp(s, CP[189]) == 0) /* save */
1988 else if (strcmp(s, CP[151]) == 0) /* depth */
1990 ChangeSearchDepth();
1992 else if (strcmp(s, CP[164]) == 0) /* hashdepth */
1996 else if (strcmp(s, CP[182]) == 0) /* random */
2000 else if (strcmp(s, CP[229]) == 0) /* hard */
2004 else if (strcmp(s, CP[152]) == 0) /* easy */
2006 flag.easy = !flag.easy;
2008 else if (strcmp(s, CP[230]) == 0) /* tsume */
2010 flag.tsume = !flag.tsume;
2012 else if (strcmp(s, CP[143]) == 0) /* contempt */
2016 else if (strcmp(s, CP[209]) == 0) /* xwndw */
2020 else if (strcmp(s, CP[186]) == 0) /* rv */
2023 UpdateDisplay(0, 0, 1, 0);
2025 else if (strcmp(s, CP[145]) == 0) /* coords */
2027 flag.coords = !flag.coords;
2028 UpdateDisplay(0, 0, 1, 0);
2030 else if (strcmp(s, CP[193]) == 0) /* stars */
2032 flag.stars = !flag.stars;
2033 UpdateDisplay(0, 0, 1, 0);
2035 else if (!XSHOGI && strcmp(s, CP[5]) == 0) /* moves */
2042 extern unsigned short PrVar[MAXDEPTH];
2044 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
2051 ShowMessage(CP[108]); /* test movelist */
2052 temp = generate_move_flags;
2053 generate_move_flags = true;
2054 TestSpeed(MoveList, 1);
2055 generate_move_flags = temp;
2056 ShowMessage(CP[107]); /* test capturelist */
2057 TestSpeed(CaptureList, 1);
2058 ShowMessage(CP[85]); /* test score position */
2059 ExaminePosition(opponent);
2060 TestPSpeed(ScorePosition, 1);
2062 else if (!XSHOGI && strcmp(s, CP[196]) == 0) /* test */
2065 ShowMessage(CP[108]); /* test movelist */
2066 TestSpeed(MoveList, 2000);
2067 ShowMessage(CP[107]); /* test capturelist */
2068 TestSpeed(CaptureList, 3000);
2069 ShowMessage(CP[85]); /* test score position */
2070 ExaminePosition(opponent);
2071 TestPSpeed(ScorePosition, 1500);
2073 ShowMessage(CP[108]); /* test movelist */
2074 TestSpeed(MoveList, 20000);
2075 ShowMessage(CP[107]); /* test capturelist */
2076 TestSpeed(CaptureList, 30000);
2077 ShowMessage(CP[85]); /* test score position */
2078 ExaminePosition(opponent);
2079 TestPSpeed(ScorePosition, 15000);
2082 else if (!XSHOGI && strcmp(s, CP[179]) == 0) /* p */
2086 else if (!XSHOGI && strcmp(s, CP[148]) == 0) /* debug */
2096 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2098 /* check for repetition */
2099 short rpt = repetition();
2105 GameList[GameCnt].flags |= draw;
2119 ElapsedTime(COMPUTE_AND_INIT_MODE);
2123 computer = opponent;
2124 opponent = computer ^ 1;
2129 /* add remaining time in milliseconds for xshogi */
2132 printf("%d. %s %ld\n",
2133 ++mycnt2, s, TimeControl.clock[player] * 10);
2136 #ifdef notdef /* optional pass best line to frontend with move */
2137 # if !defined NOPOST
2139 if (flag.post && !flag.mate)
2143 printf(" %6d ", MSCORE);
2145 for (i = 1; MV[i] > 0; i++)
2147 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2148 printf("%5s ", mvstr[0]);
2156 signal(SIGINT, TerminateSearch);
2163 SetTimeControl(void)
2167 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2168 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2169 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2173 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2174 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2177 flag.onemove = (TCmoves == 1);
2179 ElapsedTime(COMPUTE_AND_INIT_MODE);