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 * ----------------------------------------------------------------------
34 #if defined HAVE_GETTIMEOFDAY
41 #include <sys/param.h>
42 #include <sys/types.h>
50 int InBackground = false;
56 movealgbr(short m, char *s)
59 short piece = 0, flag = 0;
72 piece = f - NO_SQUARES;
74 if (piece > NO_PIECES)
77 flag = (dropmask | piece);
124 #endif /* BOOKTEST */
130 * Generate move strings in different formats.
134 algbr(short f, short t, short flag)
140 piece = f - NO_SQUARES;
142 if (f > (NO_SQUARES + NO_PIECES))
145 flag = (dropmask | piece);
154 if ((f == t) && ((f != 0) || (t != 0)))
159 printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
161 printw("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
164 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
166 else if ((flag & dropmask) != 0)
168 short piece = flag & pmask;
170 mvstr[0][0] = pxx[piece];
172 mvstr[0][2] = cxx[column(t)];
173 mvstr[0][3] = rxx[row(t)];
175 strcpy(mvstr[1], mvstr[0]);
176 strcpy(mvstr[2], mvstr[0]);
177 strcpy(mvstr[3], mvstr[0]);
179 else if ((f != 0) || (t != 0))
181 /* algebraic notation */
182 mvstr[0][0] = cxx[column(f)];
183 mvstr[0][1] = rxx[row(f)];
184 mvstr[0][2] = cxx[column(t)];
185 mvstr[0][3] = rxx[row(t)];
186 mvstr[0][4] = mvstr[3][0] = '\0';
187 mvstr[1][0] = pxx[board[f]];
189 mvstr[2][0] = mvstr[1][0];
190 mvstr[2][1] = mvstr[0][1];
192 mvstr[2][2] = mvstr[1][1] = mvstr[0][2]; /* to column */
193 mvstr[2][3] = mvstr[1][2] = mvstr[0][3]; /* to row */
194 mvstr[2][4] = mvstr[1][3] = '\0';
195 strcpy(mvstr[3], mvstr[2]);
196 mvstr[3][1] = mvstr[0][0];
200 strcat(mvstr[0], "+");
201 strcat(mvstr[1], "+");
202 strcat(mvstr[2], "+");
203 strcat(mvstr[3], "+");
208 mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
215 * Compare the string 's' to the list of legal moves available for the
216 * opponent. If a match is found, make the move on the board.
220 VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
222 static short pnt, tempb, tempc, tempsf, tempst, cnt;
223 static struct leaf xnode;
225 short i, l, local_flags;
228 /* check and remove quality flags */
229 for (i = local_flags = 0, l = strlen(s); i < l; i++)
234 local_flags |= badmove;
239 local_flags |= goodmove;
245 local_flags |= difficult;
254 if (iop == UNMAKE_MODE)
256 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
262 if (iop == VERIFY_AND_MAKE_MODE)
263 generate_move_flags = true;
265 MoveList(opponent, 2, -1, true);
266 generate_move_flags = false;
269 while (pnt < TrPnt[3])
272 algbr(node->f, node->t, (short) node->flags);
274 if ((strcmp(s, mvstr[0]) == 0)
275 || (strcmp(s, mvstr[1]) == 0)
276 || (strcmp(s, mvstr[2]) == 0)
277 || (strcmp(s, mvstr[3]) == 0))
284 if ((cnt == 1) && (xnode.score > DONTUSE))
288 MakeMove(opponent, &xnode, &tempb, &tempc,
289 &tempsf, &tempst, &INCscore);
291 if (SqAttacked(PieceList[opponent][0], computer, &blocked))
293 UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
297 /* Illegal move in check */
298 printf(CP[77], mvstr[0]);
303 /* Illegal move in check */
304 sprintf(buffer, CP[77], s);
312 if (iop == VERIFY_AND_TRY_MODE)
315 UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
316 GameList[GameCnt].depth = GameList[GameCnt].score = 0;
317 GameList[GameCnt].nodes = 0;
318 ElapsedTime(COMPUTE_AND_INIT_MODE);
319 GameList[GameCnt].time = (short) (et + 50)/100;
320 GameList[GameCnt].flags |= local_flags;
324 TimeControl.clock[opponent] -= et;
325 timeopp[oppptr] = et;
326 --TimeControl.moves[opponent];
329 *mv = (xnode.f << 8) | xnode.t;
330 algbr(xnode.f, xnode.t, false);
332 /* in force mode, check for mate conditions */
335 if (IsCheckmate(opponent ^ 1, -1, -1))
339 sprintf(buf, "%s mates!\n", ColorStr[opponent]);
357 sprintf(buffer, CP[76], s);
361 if (!barebones && (cnt > 1))
363 sprintf(buffer, CP[32], s);
373 parser(char *f, int side, short *fpiece)
381 for (i = 1, *fpiece = no_piece; i < NO_PIECES; i++)
383 if (f[0] == pxx[i] || f[0] == qxx[i])
385 *fpiece = (p ? promoted[i] : unpromoted[i]);
390 if (f[1] == '*' || f[1] == '\'')
395 return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
403 p = (f[5] == '+') ? 0x80 : 0;
405 return (locn(r1, c1) << 8) | locn(r2, c2) | p;
413 while (*InPtr != ' ')
416 while (*InPtr == ' ')
425 while (*InPtr == ' ')
442 strcpy(fname, savefile);
446 /* Enter file name */
461 if (fname[0] == '\0')
462 strcpy(fname, CP[137]);
464 if ((fd = fopen(fname, "r")) != NULL)
467 fgets(fname, 256, fd);
468 computer = opponent = black;
477 /* FIXME: write a skipn() function so that we can get
478 * 3 skips by doing skipn(3) */
482 Game50 = atoi(InPtr);
484 flag.force = (*InPtr == 'f');
485 fgets(fname, 256, fd); /* empty */
486 fgets(fname, 256, fd);
489 TCflag = atoi(InPtr);
493 OperatorTime = atoi(InPtr);
494 fgets(fname, 256, fd);
497 TimeControl.clock[black] = atol(InPtr);
500 TimeControl.moves[black] = atoi(InPtr);
501 fgets(fname, 256, fd);
504 TimeControl.clock[white] = atol(InPtr);
507 TimeControl.moves[white] = atoi(InPtr);
508 fgets(fname, 256, fd); /* empty */
510 for (i = NO_ROWS - 1; i > -1; i--)
512 fgets(fname, 256, fd);
516 for (j = 0; j < NO_COLS; j++)
518 sq = i * NO_COLS + j;
524 board[sq] = no_piece;
529 for (c = 0; c < NO_PIECES; c++)
534 board[sq] = promoted[c];
536 board[sq] = unpromoted[c];
542 for (c = 0; c < NO_PIECES; c++)
547 board[sq] = promoted[c];
549 board[sq] = unpromoted[c];
557 Mvboard[sq] = atoi(InPtr);
562 fgets(fname, 256, fd); /* empty */
563 fgets(fname, 256, fd); /* 9 8 7 ... */
564 fgets(fname, 256, fd); /* empty */
565 fgets(fname, 256, fd); /* p l n ... */
568 for (side = 0; side <= 1; side++)
570 fgets(fname, 256, fd);
574 Captured[side][pawn] = atoi(InPtr);
576 Captured[side][lance] = atoi(InPtr);
578 Captured[side][knight] = atoi(InPtr);
580 Captured[side][silver] = atoi(InPtr);
582 Captured[side][gold] = atoi(InPtr);
584 Captured[side][bishop] = atoi(InPtr);
586 Captured[side][rook] = atoi(InPtr);
588 Captured[side][king] = atoi(InPtr);
592 flag.regularstart = true;
594 fgets(fname, 256, fd); /* empty */
595 fgets(fname, 256, fd); /* move score ... */
597 while (fgets(fname, 256, fd))
606 g = &GameList[GameCnt];
607 g->gmove = parser(InPtr, side, &g->fpiece);
609 g->score = atoi(InPtr);
611 g->depth = atoi(InPtr);
613 g->nodes = atol(InPtr);
615 g->time = atol(InPtr);
617 g->flags = c = atoi(InPtr);
619 g->hashkey = strtol(InPtr, (char **) NULL, 16);
621 g->hashbd = strtol(InPtr, (char **) NULL, 16);
629 for (piece = no_piece, i = 0; i < NO_PIECES; i++)
631 if (pxx[i] == *InPtr)
639 g->color = ((*InPtr == CP[119][0]) ? white : black);
641 g->piece = (*InPtr == '+'
643 : unpromoted[piece]);
652 if (TimeControl.clock[black] > 0)
660 UpdateDisplay(0, 0, 1, 0);
672 short sq, i, c, f, t;
675 char empty[2] = "\n";
679 strcpy(fname, savefile);
683 /* Enter file name */
697 if (fname[0] == '\0') /* shogi.000 */
698 strcpy(fname, CP[137]);
700 if ((fd = fopen(fname, "w")) != NULL)
705 if (computer == white)
708 if (computer == black)
711 fprintf(fd, CP[37], w, b, Game50,
712 flag.force ? "force" : "");
714 fprintf(fd, CP[111], TCflag, OperatorTime);
716 TimeControl.clock[black], TimeControl.moves[black],
717 TimeControl.clock[white], TimeControl.moves[white]);
720 for (i = NO_ROWS - 1; i > -1; i--)
722 fprintf(fd, "%c ", 'i' - i);
724 for (c = 0; c < NO_COLS; c++)
726 sq = i * NO_COLS + c;
728 p = is_promoted[piece] ? '+' : ' ';
729 fprintf(fd, "%c", p);
745 fprintf(fd, "%c", p);
750 for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
751 fprintf(fd, " %d", Mvboard[f]);
757 fprintf(fd, " 9 8 7 6 5 4 3 2 1\n");
759 fprintf(fd, " p l n s g b r k\n");
761 for (side = 0; side <= 1; side++)
763 fprintf(fd, "%c", (side == black) ? 'B' : 'W');
764 fprintf(fd, " %2d", Captured[side][pawn]);
765 fprintf(fd, " %2d", Captured[side][lance]);
766 fprintf(fd, " %2d", Captured[side][knight]);
767 fprintf(fd, " %2d", Captured[side][silver]);
768 fprintf(fd, " %2d", Captured[side][gold]);
769 fprintf(fd, " %2d", Captured[side][bishop]);
770 fprintf(fd, " %2d", Captured[side][rook]);
771 fprintf(fd, " %2d", Captured[side][king]);
776 fprintf(fd, CP[126]);
778 for (i = 1; i <= GameCnt; i++)
780 struct GameRec *g = &GameList[i];
783 t = (g->gmove & 0xFF);
784 algbr(f, t, g->flags);
786 fprintf(fd, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
789 : (is_promoted[g->fpiece] ? '+' : ' ')),
791 ((f > NO_SQUARES) ? &mvstr[0][1] : mvstr[0]),
793 g->nodes, g->time, g->flags,
794 g->hashkey, g->hashbd);
796 if (g->piece != no_piece)
798 fprintf(fd, " %c %s %c\n",
799 pxx[g->piece], ColorStr[g->color],
800 (is_promoted[g->piece] ? '+' : ' '));
815 /* ShowMessage("Could not open file"); */
823 * GetXGame, SaveXGame and BookGame used to only be defined if
824 * xshogi wasn't defined -- wonder why?
836 /* Enter file name */
849 if (fname[0] == '\0') /* XSHOGI.position.read */
850 strcpy(fname, CP[205]);
852 if ((fd = fopen(fname, "r")) != NULL)
855 flag.regularstart = false;
858 /* xshogi position file ... */
859 fgets(fname, 256, fd);
864 if (strcmp(fname, CP[206]))
868 /* -- empty line -- */
869 fgets(fname, 256, fd);
870 /* -- empty line -- */
871 fgets(fname, 256, fd);
873 for (i = NO_ROWS - 1; i > -1; i--)
875 fgets(fname, 256, fd);
878 for (j = 0; j < NO_COLS; j++)
880 sq = i * NO_COLS + j;
886 board[sq] = no_piece;
891 for (c = 0; c < NO_PIECES; c++)
896 board[sq] = promoted[c];
898 board[sq] = unpromoted[c];
904 for (c = 0; c < NO_PIECES; c++)
909 board[sq] = promoted[c];
911 board[sq] = unpromoted[c];
924 for (side = 0; side <= 1; side++)
926 fgets(fname, 256, fd);
928 Captured[side][pawn] = atoi(InPtr);
930 Captured[side][lance] = atoi(InPtr);
932 Captured[side][knight] = atoi(InPtr);
934 Captured[side][silver] = atoi(InPtr);
936 Captured[side][gold] = atoi(InPtr);
938 Captured[side][bishop] = atoi(InPtr);
940 Captured[side][rook] = atoi(InPtr);
942 Captured[side][king] = atoi(InPtr);
945 if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
958 UpdateDisplay(0, 0, 1, 0);
973 /* Enter file name */
986 if (fname[0] == '\0') /* XSHOGI.position.read */
987 strcpy(fname, CP[205]);
989 if ((fd = fopen(fname, "w")) != NULL)
991 /* xshogi position file ... */
992 fputs("# xshogi position file -- \n", fd);
993 /* -- empty line -- */
995 /* -- empty line -- */
998 for (i = NO_ROWS - 1; i > -1; i--)
1002 for (j = 0; j < NO_COLS; j++)
1004 sq = i * NO_COLS + j;
1006 isp = is_promoted[piece];
1007 *p = (isp ? '+' : ' ');
1010 if (piece == no_piece)
1012 else if (color[sq] == white)
1025 for (side = 0; side <= 1; side++)
1027 sprintf(fname, "%d %d %d %d %d %d %d %d\n",
1028 Captured[side][pawn],
1029 Captured[side][lance],
1030 Captured[side][knight],
1031 Captured[side][silver],
1032 Captured[side][gold],
1033 Captured[side][bishop],
1034 Captured[side][rook],
1035 Captured[side][king]);
1040 if (computer == black)
1041 fputs("white to play\n", fd);
1043 fputs("black to play\n", fd);
1054 char fname[256], sflags[4];
1059 strcpy(fname, savefile);
1063 /* Enter file name */
1064 ShowMessage(CP[63]);
1077 if (fname[0] == '\0')
1080 if ((fd = fopen(fname, "a")) != NULL)
1084 for (i = 1; i <= GameCnt; i++)
1086 struct GameRec *g = &GameList[i];
1087 char mvnr[20], mvs[20];
1090 sprintf(mvnr, "%d.", (i + 1)/2);
1095 t = (g->gmove & 0xFF);
1096 algbr(f, t, g->flags);
1099 /* determine move quality string */
1100 if (g->flags & goodmove)
1103 if (g->flags & badmove)
1106 #ifdef EASY_OPENINGS
1107 if (g->flags & difficult)
1113 /* determine move string */
1116 sprintf(mvs, "%s%s ", &mvstr[0][1], sflags);
1120 sprintf(mvs, "%c%c%c%c%c%s%s ",
1121 mvstr[0][0], mvstr[0][1],
1122 (g->flags & capture) ? 'x' : '-',
1123 mvstr[0][2], mvstr[0][3],
1124 (mvstr[0][4] == '+') ? "+" : "",
1128 fprintf(fd, "%s%s%c%s",
1132 : (is_promoted[g->fpiece] ? "+" : "")),
1146 ShowMessage(CP[70]);
1150 /* ShowMessage("Could not open file"); */
1151 ShowMessage(CP[48]);
1163 char fname[256], dbuf[256];
1167 strcpy(fname, listfile);
1172 strncpy(dbuf, ctime(&when), 20);
1179 /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
1182 program played white */
1184 sprintf(fname, "CLp%s.%s%s-%s%s%s%c",
1185 patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
1186 dbuf + 17, ColorStr[computer][0]);
1188 /* replace space padding with 0 */
1189 for (i = 0; fname[i] != '\0'; i++)
1191 if (fname[i] == ' ')
1196 fd = fopen(fname, "w");
1200 printf(CP[219], fname);
1204 /* fprintf(fd, "gnushogi game %d\n", u); */
1205 fprintf(fd, CP[161], version, patchlevel);
1206 fprintf(fd, CP[10]);
1207 fprintf(fd, CP[11]);
1209 for (i = 1; i <= GameCnt; i++)
1211 f = GameList[i].gmove >> 8;
1212 t = (GameList[i].gmove & 0xFF);
1213 algbr(f, t, GameList[i].flags);
1215 if (GameList[i].flags & book)
1217 fprintf(fd, "%c%c%-5s %5d Book%7ld %5ld",
1220 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1221 pxx[GameList[i].fpiece],
1223 ? &mvstr[0][1] : mvstr[0]),
1230 fprintf(fd, "%c%c%-5s %5d %2d %7ld %5ld",
1233 : (is_promoted[GameList[i].fpiece] ? '+' : ' ')),
1234 pxx[GameList[i].fpiece],
1235 (f > NO_SQUARES ? &mvstr[0][1] : mvstr[0]),
1236 GameList[i].score, GameList[i].depth,
1237 GameList[i].nodes, GameList[i].time);
1250 fprintf(fd, "\n\n");
1252 if (GameList[GameCnt].flags & draw)
1254 fprintf(fd, CP[54], DRAW);
1256 if (DRAW == CP[101])
1260 fprintf(fd, "repetition by positions ");
1262 for (j = GameCnt - 1; j >= Game50; j -= 2)
1264 if (GameList[j].hashkey == hashkey &&
1265 GameList[j].hashbd == hashbd)
1266 fprintf(fd, "%d ", j);
1272 else if (GameList[GameCnt].score == -(SCORE_LIMIT + 999))
1274 fprintf(fd, "%s\n", ColorStr[player ]);
1276 else if (GameList[GameCnt].score == (SCORE_LIMIT + 998))
1278 fprintf(fd, "%s\n", ColorStr[player ^ 1]);
1292 GameList[GameCnt].flags |= badmove;
1296 GameList[GameCnt].flags |= goodmove;
1299 #ifdef EASY_OPENINGS
1301 GameList[GameCnt].flags |= difficult;
1311 * Undo the most recent half-move.
1319 f = GameList[GameCnt].gmove >> 8;
1320 t = GameList[GameCnt].gmove & 0x7F;
1324 /* the move was a drop */
1325 Captured[color[t]][board[t]]++;
1326 board[t] = no_piece;
1332 if (GameList[GameCnt].flags & promote)
1333 board[f] = unpromoted[board[t]];
1335 board[f] = board[t];
1337 color[f] = color[t];
1338 board[t] = GameList[GameCnt].piece;
1339 color[t] = GameList[GameCnt].color;
1341 if (board[t] != no_piece)
1342 Captured[color[f]][unpromoted[board[t]]]--;
1344 if (color[t] != neutral)
1352 if (TCflag && (TCmoves > 1))
1353 ++TimeControl.moves[color[f]];
1355 hashkey = GameList[GameCnt].hashkey;
1356 hashbd = GameList[GameCnt].hashbd;
1358 computer = computer ^ 1;
1359 opponent = opponent ^ 1;
1362 player = player ^ 1;
1364 UpdateDisplay(0, 0, 1, 0);
1366 if (flag.regularstart)
1373 FlagString(unsigned short flags, char *s)
1378 if (flags & promote)
1379 strcat(s, " promote");
1381 if (flags & dropmask)
1382 strcat(s, " drop:");
1384 if ((piece = (flags & pmask)))
1388 if (is_promoted[piece])
1391 s[l++] = pxx[piece];
1395 if (flags & capture)
1396 strcat(s, " capture");
1399 strcat(s, " exact");
1402 strcat(s, " tesuji");
1405 strcat(s, " check");
1411 strcat(s, " stupid");
1413 if (flags & questionable)
1414 strcat(s, " questionable");
1416 if (flags & kingattack)
1417 strcat(s, " kingattack");
1426 TestSpeed(void(*f)(short side, short ply,
1427 short in_check, short blockable),
1435 long cnt, rate, t1, t2;
1437 #ifdef HAVE_GETTIMEOFDAY
1441 #ifdef HAVE_GETTIMEOFDAY
1442 gettimeofday(&tv, NULL);
1443 t1 = (tv.tv_sec*100 + (tv.tv_usec/10000));
1448 for (i = 0; i < j; i++)
1450 f(opponent, 2, -1, true);
1453 for (jj = TrPnt[2]; i < TrPnt[3]; jj++)
1455 if (!pick(jj, TrPnt[3] - 1))
1461 #ifdef HAVE_GETTIMEOFDAY
1462 gettimeofday(&tv, NULL);
1463 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1468 cnt = j * (TrPnt[3] - TrPnt[2]);
1475 rate = (((et) ? ((cnt * 100) / et) : 0));
1477 #ifdef DYNAMIC_ZNODES
1483 printf(CP[91], cnt, rate);
1491 TestPSpeed(short(*f) (short side), unsigned j)
1494 long cnt, rate, t1, t2;
1495 #ifdef HAVE_GETTIMEOFDAY
1499 #ifdef HAVE_GETTIMEOFDAY
1500 gettimeofday(&tv, NULL);
1501 t1 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1506 for (i = 0; i < j; i++)
1509 #ifdef HAVE_GETTIMEOFDAY
1510 gettimeofday(&tv, NULL);
1511 t2 = (tv.tv_sec * 100 + (tv.tv_usec / 10000));
1523 rate = (et) ? ((cnt * 100) / et) : 0;
1525 /* printf("Nodes= %ld Nodes/sec= %ld\n", cnt, rate); */
1528 printf(CP[91], cnt, rate);
1542 time = &s[strlen(CP[228])];
1543 t = (int)strtol(time, &time, 10);
1548 sec = (int)strtol(time, &time, 10);
1551 m = (int)strtol(time, &time, 10);
1554 TimeControl.clock[opponent] = t;
1557 TimeControl.moves[opponent] = m;
1559 ElapsedTime(COMPUTE_AND_INIT_MODE);
1563 /* just to inform xshogi about availability of otime command */
1564 printf("otime %d %d\n", t, m);
1571 SetMachineTime(char *s)
1576 time = &s[strlen(CP[197])];
1578 t = (int)strtol(time, &time, 10);
1583 sec = (int)strtol(time, &time, 10);
1586 m = (int)strtol(time, &time, 10);
1589 TimeControl.clock[computer] = t;
1592 TimeControl.moves[computer] = m;
1594 ElapsedTime(COMPUTE_AND_INIT_MODE);
1598 /* just to inform xshogi about availability of time command */
1599 printf("time %d %d\n", t, m);
1607 /* FIXME! This is truly the function from hell! */
1610 * Process the user's command. If easy mode is OFF (the computer is thinking
1611 * on opponents time) and the program is out of book, then make the 'hint'
1612 * move on the board and call SelectMove() to find a response. The user
1613 * terminates the search by entering ^C (quit siqnal) before entering a
1614 * command. If the opponent does not make the hint move, then set Sdepth to
1619 InputCommand(char *command)
1622 short have_shown_prompt = false;
1623 short ok, done, is_move = false;
1627 ok = flag.quit = done = false;
1631 if (TTadd > ttbllimit)
1635 if ((hint > 0) && !flag.easy && !flag.force)
1638 * A hint move for the player is available. Compute a move for the
1639 * opponent in background mode assuming that the hint move will be
1640 * selected by the player.
1643 ft = time0; /* Save reference time for the player. */
1645 algbr((short) hint >> 8, (short) hint & 0xff, false);
1646 strcpy(s, mvstr[0]);
1653 /* do the hint move */
1654 if (VerifyMove(s, VERIFY_AND_TRY_MODE, &mv))
1658 #ifdef QUIETBACKGROUND
1669 have_shown_prompt = true;
1670 #endif /* QUIETBACKGROUND */
1672 /* Start computing a move until the search is interrupted. */
1674 #ifdef INTERRUPT_TEST
1678 /* would love to put null move in here */
1679 /* after we make the hint move make a 2 ply search
1680 * with both plys our moves */
1681 /* think on opponents time */
1682 SelectMove(computer, BACKGROUND_MODE);
1684 #ifdef INTERRUPT_TEST
1685 ElapsedTime(COMPUTE_INTERRUPT_MODE);
1689 printf("searching not terminated by interrupt!\n");
1693 printf("elapsed time from interrupt to "
1694 "terminating search: %ld\n", it);
1698 /* undo the hint and carry on */
1699 VerifyMove(s, UNMAKE_MODE, &mv);
1703 time0 = ft; /* Restore reference time for the player. */
1706 while(!(ok || flag.quit || done))
1710 #ifdef QUIETBACKGROUND
1711 if (!have_shown_prompt)
1713 #endif /* QUIETBACKGROUND */
1725 #ifdef QUIETBACKGROUND
1728 have_shown_prompt = false;
1729 #endif /* QUIETBACKGROUND */
1731 if (command == NULL)
1735 s[0] = sx[0] = '\0';
1738 (void)fgets(sx, 256, stdin);
1743 eof = (getstr(sx) == ERR);
1748 strcpy(sx, command);
1752 sscanf(sx, "%s", s);
1760 if (strcmp(s, CP[131]) == 0) /* bd -- display board */
1762 /* FIXME: Hack alert! */
1763 short old_xshogi = XSHOGI;
1766 display_type = DISPLAY_RAW;
1769 UpdateDisplay(0, 0, 1, 0);
1772 display_type = DISPLAY_X;
1774 else if (strcmp(s, "post") == 0)
1776 flag.post = !flag.post;
1778 else if (strcmp(s, CP[129]) == 0)
1780 /* noop */ ; /* alg */
1782 else if ((strcmp(s, CP[180]) == 0)
1783 || (strcmp(s, CP[216]) == 0)) /* quit exit */
1788 else if (strcmp(s, CP[178]) == 0) /* post */
1790 flag.post = !flag.post;
1793 else if ((strcmp(s, CP[191]) == 0)
1794 || (strcmp(s, CP[154]) == 0)) /* set edit */
1798 else if (NOT_CURSES && (strcmp(s, CP[190]) == 0)) /* setup */
1802 else if (strcmp(s, CP[156]) == 0) /* first */
1806 else if (strcmp(s, CP[162]) == 0) /* go */
1811 if (computer == black)
1822 else if (strcmp(s, CP[166]) == 0) /* help */
1826 else if (strcmp(s, CP[221]) == 0) /* material */
1828 flag.material = !flag.material;
1830 else if (strcmp(s, CP[157]) == 0) /* force */
1835 flag.bothsides = false;
1839 flag.force = !flag.force;
1840 flag.bothsides = false;
1843 else if (strcmp(s, CP[134]) == 0) /* book */
1845 Book = Book ? 0 : BOOKFAIL;
1847 else if (strcmp(s, CP[172]) == 0) /* new */
1850 UpdateDisplay(0, 0, 1, 0);
1852 else if (strcmp(s, CP[171]) == 0) /* list */
1856 else if ((strcmp(s, CP[169]) == 0)
1857 || (strcmp(s, CP[217]) == 0)) /* level clock */
1861 else if (strcmp(s, CP[165]) == 0) /* hash */
1863 flag.hash = !flag.hash;
1865 else if (strcmp(s, CP[227]) == 0) /* gamein */
1867 flag.gamein = !flag.gamein;
1869 else if (strcmp(s, CP[226]) == 0) /* beep */
1871 flag.beep = !flag.beep;
1873 else if (strcmp(s, CP[197]) == 0) /* time */
1877 else if (strcmp(s, CP[228]) == 0) /* otime */
1881 else if (strcmp(s, CP[33]) == 0) /* Awindow */
1883 ChangeAlphaWindow();
1885 else if (strcmp(s, CP[39]) == 0) /* Bwindow */
1889 else if (strcmp(s, CP[183]) == 0) /* rcptr */
1891 flag.rcptr = !flag.rcptr;
1893 else if (strcmp(s, CP[168]) == 0) /* hint */
1897 else if (strcmp(s, CP[135]) == 0) /* both */
1899 flag.bothsides = !flag.bothsides;
1902 ElapsedTime(COMPUTE_AND_INIT_MODE);
1903 SelectMove(opponent, FOREGROUND_MODE);
1906 else if (strcmp(s, CP[185]) == 0) /* reverse */
1908 flag.reverse = !flag.reverse;
1910 UpdateDisplay(0, 0, 1, 0);
1912 else if (strcmp(s, CP[195]) == 0) /* switch */
1914 computer = computer ^ 1;
1915 opponent = opponent ^ 1;
1916 xwndw = (computer == black) ? WXWNDW : BXWNDW;
1921 else if (strcmp(s, CP[203]) == 0) /* black */
1930 * ok = true; don't automatically start with black command
1933 else if (strcmp(s, CP[133]) == 0) /* white */
1942 * ok = true; don't automatically start with white command
1945 else if (strcmp(s, CP[201]) == 0 && GameCnt > 0) /* undo */
1949 else if (strcmp(s, CP[184]) == 0 && GameCnt > 1) /* remove */
1954 /* CHECKME: are these next three correct? */
1955 else if (!XSHOGI && strcmp(s, CP[207]) == 0) /* xget */
1959 else if (!XSHOGI && strcmp(s, "xsave") == 0) /* xsave */
1963 else if (!XSHOGI && strcmp(s, "bsave") == 0) /* bsave */
1967 #ifdef EASY_OPENINGS
1968 else if ((strcmp(s, "?") == 0)
1969 || (strcmp(s, "!") == 0)
1970 || (strcmp(s, "~") == 0))
1972 else if ((strcmp(s, "?") == 0)
1973 || (strcmp(s, "!") == 0))
1978 else if (strcmp(s, CP[160]) == 0) /* get */
1982 else if (strcmp(s, CP[189]) == 0) /* save */
1986 else if (strcmp(s, CP[151]) == 0) /* depth */
1988 ChangeSearchDepth();
1990 else if (strcmp(s, CP[164]) == 0) /* hashdepth */
1994 else if (strcmp(s, CP[182]) == 0) /* random */
1998 else if (strcmp(s, CP[229]) == 0) /* hard */
2002 else if (strcmp(s, CP[152]) == 0) /* easy */
2004 flag.easy = !flag.easy;
2006 else if (strcmp(s, CP[230]) == 0) /* tsume */
2008 flag.tsume = !flag.tsume;
2010 else if (strcmp(s, CP[143]) == 0) /* contempt */
2014 else if (strcmp(s, CP[209]) == 0) /* xwndw */
2018 else if (strcmp(s, CP[186]) == 0) /* rv */
2021 UpdateDisplay(0, 0, 1, 0);
2023 else if (strcmp(s, CP[145]) == 0) /* coords */
2025 flag.coords = !flag.coords;
2026 UpdateDisplay(0, 0, 1, 0);
2028 else if (strcmp(s, CP[193]) == 0) /* stars */
2030 flag.stars = !flag.stars;
2031 UpdateDisplay(0, 0, 1, 0);
2033 else if (!XSHOGI && strcmp(s, CP[5]) == 0) /* moves */
2040 extern unsigned short PrVar[MAXDEPTH];
2042 SwagHt = (GameList[GameCnt].gmove == PrVar[1])
2049 ShowMessage(CP[108]); /* test movelist */
2050 temp = generate_move_flags;
2051 generate_move_flags = true;
2052 TestSpeed(MoveList, 1);
2053 generate_move_flags = temp;
2054 ShowMessage(CP[107]); /* test capturelist */
2055 TestSpeed(CaptureList, 1);
2056 ShowMessage(CP[85]); /* test score position */
2057 ExaminePosition(opponent);
2058 TestPSpeed(ScorePosition, 1);
2060 else if (!XSHOGI && strcmp(s, CP[196]) == 0) /* test */
2063 ShowMessage(CP[108]); /* test movelist */
2064 TestSpeed(MoveList, 2000);
2065 ShowMessage(CP[107]); /* test capturelist */
2066 TestSpeed(CaptureList, 3000);
2067 ShowMessage(CP[85]); /* test score position */
2068 ExaminePosition(opponent);
2069 TestPSpeed(ScorePosition, 1500);
2071 ShowMessage(CP[108]); /* test movelist */
2072 TestSpeed(MoveList, 20000);
2073 ShowMessage(CP[107]); /* test capturelist */
2074 TestSpeed(CaptureList, 30000);
2075 ShowMessage(CP[85]); /* test score position */
2076 ExaminePosition(opponent);
2077 TestPSpeed(ScorePosition, 15000);
2080 else if (!XSHOGI && strcmp(s, CP[179]) == 0) /* p */
2084 else if (!XSHOGI && strcmp(s, CP[148]) == 0) /* debug */
2094 else if ((ok = VerifyMove(s, VERIFY_AND_MAKE_MODE, &mv)))
2096 /* check for repetition */
2097 short rpt = repetition();
2103 GameList[GameCnt].flags |= draw;
2117 ElapsedTime(COMPUTE_AND_INIT_MODE);
2121 computer = opponent;
2122 opponent = computer ^ 1;
2127 /* add remaining time in milliseconds for xshogi */
2130 printf("%d. %s %ld\n",
2131 ++mycnt2, s, TimeControl.clock[player] * 10);
2134 #ifdef notdef /* optional pass best line to frontend with move */
2135 # if !defined NOPOST
2137 if (flag.post && !flag.mate)
2141 printf(" %6d ", MSCORE);
2143 for (i = 1; MV[i] > 0; i++)
2145 algbr((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
2146 printf("%5s ", mvstr[0]);
2154 signal(SIGINT, TerminateSearch);
2161 SetTimeControl(void)
2165 TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
2166 TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
2167 TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
2171 TimeControl.moves[black] = TimeControl.moves[white] = 0;
2172 TimeControl.clock[black] = TimeControl.clock[white] = 0;
2175 flag.onemove = (TCmoves == 1);
2177 ElapsedTime(COMPUTE_AND_INIT_MODE);