4 * Curses interface 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 3 of the License,
20 * or (at your option) any later version.
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, see
29 * <http://www.gnu.org/licenses/>.
30 * ----------------------------------------------------------------------
38 #include <sys/param.h>
39 #include <sys/types.h>
44 #include "cursesdsp.h"
51 /* Definition of FIONREAD */
52 #include <sys/filio.h>
56 /* Definition of errno(). */
60 #define FLUSH_SCANW fflush(stdout), scanw
66 #define VIR_C(s) ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
67 #define VIR_R(s) ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
69 unsigned short MV[MAXDEPTH];
73 /* Forward declarations. */
74 /* FIXME: change this name, puh-leeze! */
76 static void UpdateCatched(void);
79 /****************************************
80 * Trivial output functions.
81 ****************************************/
92 Curses_ClearScreen(void)
108 gotoXY(short x, short y)
115 Curses_ShowCurrentMove(short pnt, short f, short t)
119 printw("(%2d) %5s ", pnt, mvstr[0]);
124 Curses_ShowDepth(char ch)
127 printw(CP[53], Sdepth, ch); /* Depth = %d%c */
133 Curses_ShowGameType(void)
138 printw("%c vs. %c", GameType[black], GameType[white]);
147 printw(CP[69], version, patchlevel);
152 Curses_ShowLine(unsigned short *bstline)
158 Curses_ShowMessage(char *s)
167 Curses_AlwaysShowMessage(const char *format, ...)
169 static char buffer[60];
171 va_start(ap, format);
172 vsnprintf(buffer, sizeof(buffer), format, ap);
173 Curses_ShowMessage(buffer);
179 Curses_Printf(const char *format, ...)
181 static char buffer[60];
183 va_start(ap, format);
184 vsnprintf(buffer, sizeof(buffer), format, ap);
185 printw("%s", buffer);
191 Curses_doRequestInputString(const char* fmt, char* buffer)
193 FLUSH_SCANW(fmt, buffer);
198 Curses_GetString(char* sx)
201 return (getstr(sx) == ERR);
206 Curses_ShowNodeCnt(long NodeCnt)
209 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
210 printw("n = %ld n/s = %ld",
211 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
217 Curses_ShowPatternCount(short side, short n)
221 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
234 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
235 printw("%s", (computer == white) ? CP[218] : CP[74]);
236 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
237 printw("%s", (computer == black) ? CP[218] : CP[74]);
242 Curses_ShowPrompt(void)
244 Curses_ShowSidetoMove();
246 printw(CP[121]); /* Your move is? */
252 Curses_ShowResponseTime(void)
258 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
259 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
260 printw("%ld, %d, %ld, %ld, %ld, %d",
261 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
268 Curses_ShowResults(short score, unsigned short *bstline, char ch)
270 unsigned char d, ply;
274 Curses_ShowDepth(ch);
278 for (ply = 1; bstline[ply] > 0; ply++)
286 algbr((short) bstline[ply] >> 8,
287 (short) bstline[ply] & 0xFF, false);
288 printw("%5s ", mvstr[0]);
303 ShowScore(short score)
306 printw(CP[104], score);
312 Curses_ShowSidetoMove(void)
315 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
321 Curses_ShowStage(void)
324 printw("Stage= %2d%c B= %2d W= %2d",
325 stage, flag.tsume?'T':' ', balance[black], balance[white]);
329 /****************************************
330 * End of trivial output routines.
331 ****************************************/
334 Curses_Initialize(void)
336 signal(SIGINT, Curses_Die);
337 signal(SIGQUIT, Curses_Die);
344 Curses_ExitShogi(void)
364 signal(SIGINT, SIG_IGN);
365 signal(SIGQUIT, SIG_IGN);
367 Curses_ShowMessage(CP[31]); /* Abort? */
368 FLUSH_SCANW("%s", s);
370 if (strcmp(s, CP[210]) == 0) /* yes */
373 signal(SIGINT, Curses_Die);
374 signal(SIGQUIT, Curses_Die);
379 Curses_TerminateSearch(int sig)
381 signal(SIGINT, SIG_IGN);
382 signal(SIGQUIT, SIG_IGN);
385 flag.musttimeout = true;
387 Curses_ShowMessage("Terminate Search");
388 flag.bothsides = false;
389 signal(SIGINT, Curses_Die);
390 signal(SIGQUIT, Curses_Die);
397 Curses_ClearScreen();
398 /* printw("GNU Shogi ??p? command summary\n"); */
399 printw(CP[40], version, patchlevel);
400 printw("-------------------------------"
401 "---------------------------------\n");
402 /* printw("7g7f move from 7g to 7f quit
405 /* printw("S6h move silver to 6h beep
406 * turn %s\n", (flag.beep) ? "off" : "on"); */
407 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
408 /* printw("2d2c+ move to 2c and promote\n"); */
409 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
410 /* printw("P*5e drop a pawn to 5e easy
411 * turn %s\n", (flag.easy) ? "off" : "on"); */
412 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
414 * turn %s\n", (flag.hash) ? "off" : "on"); */
415 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
416 /* printw("bd redraw board reverse
417 * board display\n"); */
419 /* printw("list game to shogi.lst book
420 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
422 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
423 /* printw("undo undo last ply remove
424 * take back a move\n"); */
426 /* printw("edit edit board force
427 * enter game moves\n"); */
429 /* printw("switch sides with computer both
430 * computer match\n"); */
432 /* printw("black computer plays black white
433 * computer plays white\n"); */
435 /* printw("depth set search depth clock
436 * set time control\n"); */
438 /* printw("hint suggest a move post
439 * turn %s principle variation\n", (flag.post) ? "off" :
441 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
442 /* printw("save game to file get
443 * game from file\n"); */
445 /* printw("random randomize play new
446 * start new game\n"); */
449 printw(CP[47], ColorStr[computer]);
451 printw(CP[97], ColorStr[opponent]);
453 printw(CP[79], MaxResponseTime/100);
455 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
457 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
459 printw(CP[52], MaxSearchDepth);
461 printw(CP[100], (dither) ? CP[93] : CP[92]);
463 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
467 printw(CP[110], (TCflag) ? CP[93] : CP[92],
468 TimeControl.moves[black],
469 TimeControl.clock[black] / 100,
470 OperatorTime, MaxSearchDepth);
475 fflush(stdin); /* what is this supposed to do?? */
479 Curses_ClearScreen();
480 Curses_UpdateDisplay(0, 0, 1, 0);
484 static const short x0[2] = { 54, 2 };
485 static const short y0[2] = { 20, 4 };
489 * Set up a board position. Pieces are entered by typing the piece followed
490 * by the location. For example, N3f will place a knight on square 3f.
491 * P* will put a pawn to the captured pieces.
495 Curses_EditBoard(void)
501 flag.regularstart = true;
503 Curses_ClearScreen();
504 Curses_UpdateDisplay(0, 0, 1, 0);
518 printw(CP[60], ColorStr[a]); /* Editing %s */
521 FLUSH_SCANW("%s", s);
523 if (s[0] == CP[28][0]) /* # */
525 for (sq = 0; sq < NO_SQUARES; sq++)
527 board[sq] = no_piece;
536 if (s[0] == CP[136][0]) /* c */
541 for (i = NO_PIECES; i > no_piece; i--)
543 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
547 Captured[a][unpromoted[i]]++;
557 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
561 for (i = NO_PIECES; i > no_piece; i--)
563 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
573 color[sq] = ((board[sq] == no_piece) ? neutral : a);
577 while (s[0] != CP[29][0]); /* . */
579 for (sq = 0; sq < NO_SQUARES; sq++)
580 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
587 Curses_ClearScreen();
588 Curses_UpdateDisplay(0, 0, 1, 0);
597 for (side = black; side <= white; side++)
599 short x, y, piece, cside, k;
601 cside = flag.reverse ? (side ^ 1) : side;
606 for (piece = pawn; piece <= king; piece++)
610 if ((n = Captured[side][piece]))
613 printw("%i%c", n, pxx[piece]);
644 Curses_SearchStartStuff(short side)
648 signal(SIGINT, Curses_TerminateSearch);
649 signal(SIGQUIT, Curses_TerminateSearch);
651 for (i = 4; i < 14; i++) /* CHECKME */
660 Curses_OutputMove(void)
663 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
672 printw(CP[84], mvstr[0]); /* My move is %s */
681 if (root->flags & draw)
683 else if (root->score == -(SCORE_LIMIT + 999))
685 else if (root->score == SCORE_LIMIT + 998)
688 else if (root->score < -SCORE_LIMIT)
689 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
690 else if (root->score > SCORE_LIMIT)
691 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
692 #endif /* VERYBUGGY */
706 if (Tree[t].f || Tree[t].t)
714 ShowNodeCnt(NodeCnt);
716 printw(CP[81], t); /* Max Tree= */
720 Curses_ShowSidetoMove();
732 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
733 s = (short) ((dt - 6000 * (long) m) / 100);
737 m = (short) ((dt = et) / 6000);
738 s = (short) (et - 6000 * (long) m) / 100;
748 gotoXY(20, (flag.reverse) ? 2 : 23);
750 gotoXY(20, (flag.reverse) ? 23 : 2);
752 /* printw("%d:%02d %ld ", m, s, dt); */
753 printw("%d:%02d ", m, s);
756 ShowNodeCnt(NodeCnt);
768 if (color[sq] == neutral)
772 else if (flag.reverse ^ (color[sq] == black))
784 if (is_promoted[(int)piece])
787 y = pxx[unpromoted[(int)piece]];
795 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
796 printw("%c%c%c%c", l, p, y, r);
801 * Curses_ShowPostnValue(): must have called ExaminePosition() first
804 Curses_ShowPostnValue(short sq)
808 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
809 score = ScorePosition(color[sq]);
811 if (color[sq] != neutral)
812 #if defined SAVE_SVALUE
818 printw("%3d ", svalue[sq]);
829 Curses_ShowPostnValues(void)
833 ExaminePosition(opponent);
835 for (sq = 0; sq < NO_SQUARES; sq++)
836 Curses_ShowPostnValue(sq);
838 score = ScorePosition(opponent);
840 printw(CP[103], score,
841 mtl[computer], pscore[computer], GameType[computer],
842 mtl[opponent], pscore[opponent], GameType[opponent]);
849 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
863 for (j=0; j<NO_COLS; j++)
866 while (i <= 1 + 2*NO_ROWS)
873 z = NO_ROWS + 2 - ((i + 1) / 2);
875 printw(" %c |", ROW_NAME(z+1));
876 for (j=0; j<NO_COLS; j++)
881 if (i < 2 + 2*NO_ROWS)
884 for (j=0; j<NO_COLS; j++)
890 for (j=0; j<NO_COLS; j++)
893 gotoXY(3, 4 + 2*NO_ROWS);
901 for (sq = 0; sq < NO_SQUARES; sq++)
904 else /* not redraw */
912 if ((isspec & capture) || (isspec & dropmask) || redraw)
916 for (side = black; side <= white; side++)
918 short x, y, piece, cside, k;
919 cside = flag.reverse ? (side ^ 1) : side;
924 for (piece = pawn; piece <= king; piece++)
928 if ((n = Captured[side][piece]))
931 printw("%i%c", n, pxx[piece]);
933 if (cside == black) y--; else y++;
960 Curses_ChangeAlphaWindow(void)
962 Curses_ShowMessage(CP[114]);
963 FLUSH_SCANW("%hd", &WAwindow);
964 Curses_ShowMessage(CP[34]);
965 FLUSH_SCANW("%hd", &BAwindow);
970 Curses_ChangeBetaWindow(void)
972 Curses_ShowMessage(CP[115]);
973 FLUSH_SCANW("%hd", &WBwindow);
974 Curses_ShowMessage(CP[35]);
975 FLUSH_SCANW("%hd", &BBwindow);
980 Curses_GiveHint(void)
986 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
987 strcpy(s, CP[198]); /* try */
989 Curses_ShowMessage(s);
993 Curses_ShowMessage(CP[223]);
999 Curses_ChangeSearchDepth(void)
1001 Curses_ShowMessage(CP[150]);
1002 FLUSH_SCANW("%hd", &MaxSearchDepth);
1003 TCflag = !(MaxSearchDepth > 0);
1008 Curses_ChangeHashDepth(void)
1010 Curses_ShowMessage(CP[163]);
1011 FLUSH_SCANW("%hd", &HashDepth);
1012 Curses_ShowMessage(CP[82]);
1013 FLUSH_SCANW("%hd", &HashMoveLimit);
1018 Curses_SetContempt(void)
1020 Curses_ShowMessage(CP[142]);
1021 FLUSH_SCANW("%hd", &contempt);
1026 Curses_ChangeXwindow(void)
1028 Curses_ShowMessage(CP[208]);
1029 FLUSH_SCANW("%hd", &xwndw);
1034 Curses_SelectLevel(char *sx)
1038 Curses_ClearScreen();
1040 printw(CP[41], version, patchlevel);
1070 FLUSH_SCANW("%d", &item);
1117 flag.onemove = true;
1123 flag.onemove = true;
1129 flag.onemove = true;
1133 TCflag = (TCmoves > 0);
1135 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1138 Curses_ClearScreen();
1139 Curses_UpdateDisplay(0, 0, 1, 0);
1144 Curses_DoDebug(void)
1146 short c, p, sq, tp, tc, tsq, score;
1149 ExaminePosition(opponent);
1150 Curses_ShowMessage(CP[65]);
1151 FLUSH_SCANW("%s", s);
1154 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1157 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1160 for (p = king; p > no_piece; p--)
1162 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1166 for (sq = 0; sq < NO_SQUARES; sq++)
1172 tsq = PieceList[c][1];
1173 PieceList[c][1] = sq;
1174 Curses_ShowPostnValue(sq);
1175 PieceList[c][1] = tsq;
1180 score = ScorePosition(opponent);
1182 printw(CP[103], score,
1183 mtl[computer], pscore[computer], GameType[computer],
1184 mtl[opponent], pscore[opponent], GameType[opponent]);
1191 Curses_DoTable(short table[NO_SQUARES])
1194 ExaminePosition(opponent);
1196 for (sq = 0; sq < NO_SQUARES; sq++)
1198 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1199 printw("%3d ", table[sq]);
1205 * Determine the time that has passed since the search was started. If the
1206 * elapsed time exceeds the target(ResponseTime + ExtraTime) then set timeout
1207 * to true which will terminate the search.
1208 * iop = COMPUTE_MODE calculate et, bump ETnodes
1209 * iop = COMPUTE_AND_INIT_MODE calculate et, set timeout if time exceeded,
1210 * set reference time
1213 Curses_ElapsedTime(ElapsedTime_mode iop)
1219 #ifdef HAVE_GETTIMEOFDAY
1223 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
1227 "You probably have a non-ANSI <ioctl.h>; "
1228 "see README. %d %d %x\n",
1229 i, errno, FIONREAD);
1238 flag.bothsides = false;
1241 #ifdef HAVE_GETTIMEOFDAY
1242 gettimeofday(&tv, NULL);
1243 current_time = tv.tv_sec*100 + (tv.tv_usec/10000);
1245 et = ((current_time = time((long *) 0)) - time0) * 100;
1248 #ifdef INTERRUPT_TEST
1249 if (iop == INIT_INTERRUPT_MODE)
1251 itime0 = current_time;
1253 else if (iop == COMPUTE_INTERRUPT_MODE)
1255 it = current_time - itime0;
1260 #ifdef HAVE_GETTIMEOFDAY
1261 et = current_time - time0;
1263 ETnodes = NodeCnt + znodes;
1267 #ifdef INTERRUPT_TEST
1268 printf("elapsed time %ld not positive\n", et);
1273 if (iop == COMPUTE_AND_INIT_MODE)
1275 if ((et > (ResponseTime + ExtraTime)) && (Sdepth > MINDEPTH))
1276 flag.timeout = true;
1278 time0 = current_time;
1283 #ifdef QUIETBACKGROUND