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"
46 #define FLUSH_SCANW fflush(stdout), scanw
52 #define VIR_C(s) ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
53 #define VIR_R(s) ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
55 unsigned short MV[MAXDEPTH];
59 /* Forward declarations. */
60 /* FIXME: change this name, puh-leeze! */
62 static void UpdateCatched(void);
65 /****************************************
66 * Trivial output functions.
67 ****************************************/
78 Curses_ClearScreen(void)
95 gotoXY(short x, short y)
102 Curses_ShowCurrentMove(short pnt, short f, short t)
106 printw("(%2d) %5s ", pnt, mvstr[0]);
111 Curses_ShowDepth(char ch)
114 printw(CP[53], Sdepth, ch); /* Depth = %d%c */
120 Curses_ShowGameType(void)
125 printw("%c vs. %c", GameType[black], GameType[white]);
134 printw(CP[69], version, patchlevel);
139 Curses_ShowLine(unsigned short *bstline)
145 Curses_ShowMessage(char *s)
154 Curses_AlwaysShowMessage(const char *format, ...)
156 static char buffer[60];
158 va_start(ap, format);
159 vsnprintf(buffer, sizeof(buffer), format, ap);
160 Curses_ShowMessage(buffer);
166 Curses_Printf(const char *format, ...)
168 static char buffer[60];
170 va_start(ap, format);
171 vsnprintf(buffer, sizeof(buffer), format, ap);
172 printw("%s", buffer);
178 Curses_doRequestInputString(const char* fmt, char* buffer)
180 FLUSH_SCANW(fmt, buffer);
185 Curses_GetString(char* sx)
188 return (getstr(sx) == ERR);
193 Curses_ShowNodeCnt(long NodeCnt)
196 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
197 printw("n = %ld n/s = %ld",
198 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
204 Curses_ShowPatternCount(short side, short n)
208 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
221 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
222 printw("%s", (computer == white) ? CP[218] : CP[74]);
223 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
224 printw("%s", (computer == black) ? CP[218] : CP[74]);
232 printw(CP[121]); /* Your move is? */
238 Curses_ShowResponseTime(void)
244 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
245 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
246 printw("%ld, %d, %ld, %ld, %ld, %d",
247 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
254 Curses_ShowResults(short score, unsigned short *bstline, char ch)
256 unsigned char d, ply;
260 Curses_ShowDepth(ch);
264 for (ply = 1; bstline[ply] > 0; ply++)
272 algbr((short) bstline[ply] >> 8,
273 (short) bstline[ply] & 0xFF, false);
274 printw("%5s ", mvstr[0]);
289 ShowScore(short score)
292 printw(CP[104], score);
298 Curses_ShowSidetoMove(void)
301 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
307 Curses_ShowStage(void)
310 printw("Stage= %2d%c B= %2d W= %2d",
311 stage, flag.tsume?'T':' ', balance[black], balance[white]);
316 /****************************************
317 * End of trivial output routines.
318 ****************************************/
322 Curses_Initialize(void)
324 signal(SIGINT, Curses_Die);
325 signal(SIGQUIT, Curses_Die);
332 Curses_ExitShogi(void)
353 signal(SIGINT, SIG_IGN);
354 signal(SIGQUIT, SIG_IGN);
356 Curses_ShowMessage(CP[31]); /* Abort? */
357 FLUSH_SCANW("%s", s);
359 if (strcmp(s, CP[210]) == 0) /* yes */
362 signal(SIGINT, Curses_Die);
363 signal(SIGQUIT, Curses_Die);
369 Curses_TerminateSearch(int sig)
371 signal(SIGINT, SIG_IGN);
372 signal(SIGQUIT, SIG_IGN);
375 flag.musttimeout = true;
377 Curses_ShowMessage("Terminate Search");
378 flag.bothsides = false;
379 signal(SIGINT, Curses_Die);
380 signal(SIGQUIT, Curses_Die);
388 Curses_ClearScreen();
389 /* printw("GNU Shogi ??p? command summary\n"); */
390 printw(CP[40], version, patchlevel);
391 printw("-------------------------------"
392 "---------------------------------\n");
393 /* printw("7g7f move from 7g to 7f quit
396 /* printw("S6h move silver to 6h beep
397 * turn %s\n", (flag.beep) ? "off" : "on"); */
398 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
399 /* printw("2d2c+ move to 2c and promote\n"); */
400 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
401 /* printw("P*5e drop a pawn to 5e easy
402 * turn %s\n", (flag.easy) ? "off" : "on"); */
403 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
405 * turn %s\n", (flag.hash) ? "off" : "on"); */
406 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
407 /* printw("bd redraw board reverse
408 * board display\n"); */
410 /* printw("list game to shogi.lst book
411 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
413 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
414 /* printw("undo undo last ply remove
415 * take back a move\n"); */
417 /* printw("edit edit board force
418 * enter game moves\n"); */
420 /* printw("switch sides with computer both
421 * computer match\n"); */
423 /* printw("black computer plays black white
424 * computer plays white\n"); */
426 /* printw("depth set search depth clock
427 * set time control\n"); */
429 /* printw("hint suggest a move post
430 * turn %s principle variation\n", (flag.post) ? "off" :
432 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
433 /* printw("save game to file get
434 * game from file\n"); */
436 /* printw("random randomize play new
437 * start new game\n"); */
440 printw(CP[47], ColorStr[computer]);
442 printw(CP[97], ColorStr[opponent]);
444 printw(CP[79], MaxResponseTime/100);
446 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
448 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
450 printw(CP[52], MaxSearchDepth);
452 printw(CP[100], (dither) ? CP[93] : CP[92]);
454 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
458 printw(CP[110], (TCflag) ? CP[93] : CP[92],
459 TimeControl.moves[black],
460 TimeControl.clock[black] / 100,
461 OperatorTime, MaxSearchDepth);
466 fflush(stdin); /* what is this supposed to do?? */
470 Curses_ClearScreen();
471 Curses_UpdateDisplay(0, 0, 1, 0);
475 static const short x0[2] = { 54, 2 };
476 static const short y0[2] = { 20, 4 };
480 * Set up a board position. Pieces are entered by typing the piece followed
481 * by the location. For example, N3f will place a knight on square 3f.
482 * P* will put a pawn to the captured pieces.
486 Curses_EditBoard(void)
492 flag.regularstart = true;
494 Curses_ClearScreen();
495 Curses_UpdateDisplay(0, 0, 1, 0);
509 printw(CP[60], ColorStr[a]); /* Editing %s */
512 FLUSH_SCANW("%s", s);
514 if (s[0] == CP[28][0]) /* # */
516 for (sq = 0; sq < NO_SQUARES; sq++)
518 board[sq] = no_piece;
527 if (s[0] == CP[136][0]) /* c */
532 for (i = NO_PIECES; i > no_piece; i--)
534 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
538 Captured[a][unpromoted[i]]++;
548 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
552 for (i = NO_PIECES; i > no_piece; i--)
554 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
564 color[sq] = ((board[sq] == no_piece) ? neutral : a);
568 while (s[0] != CP[29][0]); /* . */
570 for (sq = 0; sq < NO_SQUARES; sq++)
571 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
578 Curses_ClearScreen();
579 Curses_UpdateDisplay(0, 0, 1, 0);
589 for (side = black; side <= white; side++)
591 short x, y, piece, cside, k;
593 cside = flag.reverse ? (side ^ 1) : side;
598 for (piece = pawn; piece <= king; piece++)
602 if ((n = Captured[side][piece]))
605 printw("%i%c", n, pxx[piece]);
637 Curses_SearchStartStuff(short side)
641 signal(SIGINT, Curses_TerminateSearch);
642 signal(SIGQUIT, Curses_TerminateSearch);
644 for (i = 4; i < 14; i++) /* CHECKME */
654 Curses_OutputMove(void)
657 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
666 printw(CP[84], mvstr[0]); /* My move is %s */
675 if (root->flags & draw)
677 else if (root->score == -(SCORE_LIMIT + 999))
679 else if (root->score == SCORE_LIMIT + 998)
682 else if (root->score < -SCORE_LIMIT)
683 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
684 else if (root->score > SCORE_LIMIT)
685 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
686 #endif /* VERYBUGGY */
700 if (Tree[t].f || Tree[t].t)
708 ShowNodeCnt(NodeCnt);
710 printw(CP[81], t); /* Max Tree= */
714 Curses_ShowSidetoMove();
727 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
728 s = (short) ((dt - 6000 * (long) m) / 100);
732 m = (short) ((dt = et) / 6000);
733 s = (short) (et - 6000 * (long) m) / 100;
743 gotoXY(20, (flag.reverse) ? 2 : 23);
745 gotoXY(20, (flag.reverse) ? 23 : 2);
747 /* printw("%d:%02d %ld ", m, s, dt); */
748 printw("%d:%02d ", m, s);
751 ShowNodeCnt(NodeCnt);
764 if (color[sq] == neutral)
768 else if (flag.reverse ^ (color[sq] == black))
780 if (is_promoted[(int)piece])
783 y = pxx[unpromoted[(int)piece]];
791 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
792 printw("%c%c%c%c", l, p, y, r);
799 * Curses_ShowPostnValue(): must have called ExaminePosition() first
803 Curses_ShowPostnValue(short sq)
807 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
808 score = ScorePosition(color[sq]);
810 if (color[sq] != neutral)
811 #if defined SAVE_SVALUE
817 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]);
850 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
864 for (j=0; j<NO_COLS; j++)
867 while (i <= 1 + 2*NO_ROWS)
874 z = NO_ROWS + 2 - ((i + 1) / 2);
876 printw(" %c |", ROW_NAME(z+1));
877 for (j=0; j<NO_COLS; j++)
882 if (i < 2 + 2*NO_ROWS)
885 for (j=0; j<NO_COLS; j++)
891 for (j=0; j<NO_COLS; j++)
894 gotoXY(3, 4 + 2*NO_ROWS);
902 for (sq = 0; sq < NO_SQUARES; sq++)
905 else /* not redraw */
913 if ((isspec & capture) || (isspec & dropmask) || redraw)
917 for (side = black; side <= white; side++)
919 short x, y, piece, cside, k;
920 cside = flag.reverse ? (side ^ 1) : side;
925 for (piece = pawn; piece <= king; piece++)
929 if ((n = Captured[side][piece]))
932 printw("%i%c", n, pxx[piece]);
934 if (cside == black) y--; else y++;
964 Curses_ChangeAlphaWindow(void)
966 Curses_ShowMessage(CP[114]);
967 FLUSH_SCANW("%hd", &WAwindow);
968 Curses_ShowMessage(CP[34]);
969 FLUSH_SCANW("%hd", &BAwindow);
975 Curses_ChangeBetaWindow(void)
977 Curses_ShowMessage(CP[115]);
978 FLUSH_SCANW("%hd", &WBwindow);
979 Curses_ShowMessage(CP[35]);
980 FLUSH_SCANW("%hd", &BBwindow);
986 Curses_GiveHint(void)
992 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
993 strcpy(s, CP[198]); /* try */
995 Curses_ShowMessage(s);
999 Curses_ShowMessage(CP[223]);
1006 Curses_ChangeSearchDepth(void)
1008 Curses_ShowMessage(CP[150]);
1009 FLUSH_SCANW("%hd", &MaxSearchDepth);
1010 TCflag = !(MaxSearchDepth > 0);
1015 Curses_ChangeHashDepth(void)
1017 Curses_ShowMessage(CP[163]);
1018 FLUSH_SCANW("%hd", &HashDepth);
1019 Curses_ShowMessage(CP[82]);
1020 FLUSH_SCANW("%hd", &HashMoveLimit);
1025 Curses_SetContempt(void)
1027 Curses_ShowMessage(CP[142]);
1028 FLUSH_SCANW("%hd", &contempt);
1034 Curses_ChangeXwindow(void)
1036 Curses_ShowMessage(CP[208]);
1037 FLUSH_SCANW("%hd", &xwndw);
1043 Curses_SelectLevel(char *sx)
1047 Curses_ClearScreen();
1049 printw(CP[41], version, patchlevel);
1079 FLUSH_SCANW("%d", &item);
1126 flag.onemove = true;
1132 flag.onemove = true;
1138 flag.onemove = true;
1142 TCflag = (TCmoves > 0);
1144 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1147 Curses_ClearScreen();
1148 Curses_UpdateDisplay(0, 0, 1, 0);
1154 Curses_DoDebug(void)
1156 short c, p, sq, tp, tc, tsq, score;
1159 ExaminePosition(opponent);
1160 Curses_ShowMessage(CP[65]);
1161 FLUSH_SCANW("%s", s);
1164 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1167 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1170 for (p = king; p > no_piece; p--)
1172 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1176 for (sq = 0; sq < NO_SQUARES; sq++)
1182 tsq = PieceList[c][1];
1183 PieceList[c][1] = sq;
1184 Curses_ShowPostnValue(sq);
1185 PieceList[c][1] = tsq;
1190 score = ScorePosition(opponent);
1192 printw(CP[103], score,
1193 mtl[computer], pscore[computer], GameType[computer],
1194 mtl[opponent], pscore[opponent], GameType[opponent]);
1202 Curses_DoTable(short table[NO_SQUARES])
1205 ExaminePosition(opponent);
1207 for (sq = 0; sq < NO_SQUARES; sq++)
1209 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1210 printw("%3d ", table[sq]);