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)
94 gotoXY(short x, short y)
101 Curses_ShowCurrentMove(short pnt, short f, short t)
105 printw("(%2d) %5s ", pnt, mvstr[0]);
110 Curses_ShowDepth(char ch)
113 printw(CP[53], Sdepth, ch); /* Depth = %d%c */
119 Curses_ShowGameType(void)
124 printw("%c vs. %c", GameType[black], GameType[white]);
133 printw(CP[69], version, patchlevel);
138 Curses_ShowLine(unsigned short *bstline)
144 Curses_ShowMessage(char *s)
153 Curses_AlwaysShowMessage(const char *format, ...)
155 static char buffer[60];
157 va_start(ap, format);
158 vsnprintf(buffer, sizeof(buffer), format, ap);
159 Curses_ShowMessage(buffer);
165 Curses_Printf(const char *format, ...)
167 static char buffer[60];
169 va_start(ap, format);
170 vsnprintf(buffer, sizeof(buffer), format, ap);
171 printw("%s", buffer);
177 Curses_doRequestInputString(const char* fmt, char* buffer)
179 FLUSH_SCANW(fmt, buffer);
184 Curses_GetString(char* sx)
187 return (getstr(sx) == ERR);
192 Curses_ShowNodeCnt(long NodeCnt)
195 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
196 printw("n = %ld n/s = %ld",
197 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
203 Curses_ShowPatternCount(short side, short n)
207 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
220 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
221 printw("%s", (computer == white) ? CP[218] : CP[74]);
222 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
223 printw("%s", (computer == black) ? CP[218] : CP[74]);
228 Curses_ShowPrompt(void)
230 Curses_ShowSidetoMove();
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]);
315 /****************************************
316 * End of trivial output routines.
317 ****************************************/
320 Curses_Initialize(void)
322 signal(SIGINT, Curses_Die);
323 signal(SIGQUIT, Curses_Die);
330 Curses_ExitShogi(void)
350 signal(SIGINT, SIG_IGN);
351 signal(SIGQUIT, SIG_IGN);
353 Curses_ShowMessage(CP[31]); /* Abort? */
354 FLUSH_SCANW("%s", s);
356 if (strcmp(s, CP[210]) == 0) /* yes */
359 signal(SIGINT, Curses_Die);
360 signal(SIGQUIT, Curses_Die);
365 Curses_TerminateSearch(int sig)
367 signal(SIGINT, SIG_IGN);
368 signal(SIGQUIT, SIG_IGN);
371 flag.musttimeout = true;
373 Curses_ShowMessage("Terminate Search");
374 flag.bothsides = false;
375 signal(SIGINT, Curses_Die);
376 signal(SIGQUIT, Curses_Die);
383 Curses_ClearScreen();
384 /* printw("GNU Shogi ??p? command summary\n"); */
385 printw(CP[40], version, patchlevel);
386 printw("-------------------------------"
387 "---------------------------------\n");
388 /* printw("7g7f move from 7g to 7f quit
391 /* printw("S6h move silver to 6h beep
392 * turn %s\n", (flag.beep) ? "off" : "on"); */
393 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
394 /* printw("2d2c+ move to 2c and promote\n"); */
395 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
396 /* printw("P*5e drop a pawn to 5e easy
397 * turn %s\n", (flag.easy) ? "off" : "on"); */
398 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
400 * turn %s\n", (flag.hash) ? "off" : "on"); */
401 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
402 /* printw("bd redraw board reverse
403 * board display\n"); */
405 /* printw("list game to shogi.lst book
406 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
408 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
409 /* printw("undo undo last ply remove
410 * take back a move\n"); */
412 /* printw("edit edit board force
413 * enter game moves\n"); */
415 /* printw("switch sides with computer both
416 * computer match\n"); */
418 /* printw("black computer plays black white
419 * computer plays white\n"); */
421 /* printw("depth set search depth clock
422 * set time control\n"); */
424 /* printw("hint suggest a move post
425 * turn %s principle variation\n", (flag.post) ? "off" :
427 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
428 /* printw("save game to file get
429 * game from file\n"); */
431 /* printw("random randomize play new
432 * start new game\n"); */
435 printw(CP[47], ColorStr[computer]);
437 printw(CP[97], ColorStr[opponent]);
439 printw(CP[79], MaxResponseTime/100);
441 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
443 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
445 printw(CP[52], MaxSearchDepth);
447 printw(CP[100], (dither) ? CP[93] : CP[92]);
449 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
453 printw(CP[110], (TCflag) ? CP[93] : CP[92],
454 TimeControl.moves[black],
455 TimeControl.clock[black] / 100,
456 OperatorTime, MaxSearchDepth);
461 fflush(stdin); /* what is this supposed to do?? */
465 Curses_ClearScreen();
466 Curses_UpdateDisplay(0, 0, 1, 0);
470 static const short x0[2] = { 54, 2 };
471 static const short y0[2] = { 20, 4 };
475 * Set up a board position. Pieces are entered by typing the piece followed
476 * by the location. For example, N3f will place a knight on square 3f.
477 * P* will put a pawn to the captured pieces.
481 Curses_EditBoard(void)
487 flag.regularstart = true;
489 Curses_ClearScreen();
490 Curses_UpdateDisplay(0, 0, 1, 0);
504 printw(CP[60], ColorStr[a]); /* Editing %s */
507 FLUSH_SCANW("%s", s);
509 if (s[0] == CP[28][0]) /* # */
511 for (sq = 0; sq < NO_SQUARES; sq++)
513 board[sq] = no_piece;
522 if (s[0] == CP[136][0]) /* c */
527 for (i = NO_PIECES; i > no_piece; i--)
529 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
533 Captured[a][unpromoted[i]]++;
543 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
547 for (i = NO_PIECES; i > no_piece; i--)
549 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
559 color[sq] = ((board[sq] == no_piece) ? neutral : a);
563 while (s[0] != CP[29][0]); /* . */
565 for (sq = 0; sq < NO_SQUARES; sq++)
566 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
573 Curses_ClearScreen();
574 Curses_UpdateDisplay(0, 0, 1, 0);
583 for (side = black; side <= white; side++)
585 short x, y, piece, cside, k;
587 cside = flag.reverse ? (side ^ 1) : side;
592 for (piece = pawn; piece <= king; piece++)
596 if ((n = Captured[side][piece]))
599 printw("%i%c", n, pxx[piece]);
630 Curses_SearchStartStuff(short side)
634 signal(SIGINT, Curses_TerminateSearch);
635 signal(SIGQUIT, Curses_TerminateSearch);
637 for (i = 4; i < 14; i++) /* CHECKME */
646 Curses_OutputMove(void)
649 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
658 printw(CP[84], mvstr[0]); /* My move is %s */
667 if (root->flags & draw)
669 else if (root->score == -(SCORE_LIMIT + 999))
671 else if (root->score == SCORE_LIMIT + 998)
674 else if (root->score < -SCORE_LIMIT)
675 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
676 else if (root->score > SCORE_LIMIT)
677 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
678 #endif /* VERYBUGGY */
692 if (Tree[t].f || Tree[t].t)
700 ShowNodeCnt(NodeCnt);
702 printw(CP[81], t); /* Max Tree= */
706 Curses_ShowSidetoMove();
718 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
719 s = (short) ((dt - 6000 * (long) m) / 100);
723 m = (short) ((dt = et) / 6000);
724 s = (short) (et - 6000 * (long) m) / 100;
734 gotoXY(20, (flag.reverse) ? 2 : 23);
736 gotoXY(20, (flag.reverse) ? 23 : 2);
738 /* printw("%d:%02d %ld ", m, s, dt); */
739 printw("%d:%02d ", m, s);
742 ShowNodeCnt(NodeCnt);
754 if (color[sq] == neutral)
758 else if (flag.reverse ^ (color[sq] == black))
770 if (is_promoted[(int)piece])
773 y = pxx[unpromoted[(int)piece]];
781 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
782 printw("%c%c%c%c", l, p, y, r);
787 * Curses_ShowPostnValue(): must have called ExaminePosition() first
790 Curses_ShowPostnValue(short sq)
794 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
795 score = ScorePosition(color[sq]);
797 if (color[sq] != neutral)
798 #if defined SAVE_SVALUE
804 printw("%3d ", svalue[sq]);
815 Curses_ShowPostnValues(void)
819 ExaminePosition(opponent);
821 for (sq = 0; sq < NO_SQUARES; sq++)
822 Curses_ShowPostnValue(sq);
824 score = ScorePosition(opponent);
826 printw(CP[103], score,
827 mtl[computer], pscore[computer], GameType[computer],
828 mtl[opponent], pscore[opponent], GameType[opponent]);
835 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
849 for (j=0; j<NO_COLS; j++)
852 while (i <= 1 + 2*NO_ROWS)
859 z = NO_ROWS + 2 - ((i + 1) / 2);
861 printw(" %c |", ROW_NAME(z+1));
862 for (j=0; j<NO_COLS; j++)
867 if (i < 2 + 2*NO_ROWS)
870 for (j=0; j<NO_COLS; j++)
876 for (j=0; j<NO_COLS; j++)
879 gotoXY(3, 4 + 2*NO_ROWS);
887 for (sq = 0; sq < NO_SQUARES; sq++)
890 else /* not redraw */
898 if ((isspec & capture) || (isspec & dropmask) || redraw)
902 for (side = black; side <= white; side++)
904 short x, y, piece, cside, k;
905 cside = flag.reverse ? (side ^ 1) : side;
910 for (piece = pawn; piece <= king; piece++)
914 if ((n = Captured[side][piece]))
917 printw("%i%c", n, pxx[piece]);
919 if (cside == black) y--; else y++;
946 Curses_ChangeAlphaWindow(void)
948 Curses_ShowMessage(CP[114]);
949 FLUSH_SCANW("%hd", &WAwindow);
950 Curses_ShowMessage(CP[34]);
951 FLUSH_SCANW("%hd", &BAwindow);
956 Curses_ChangeBetaWindow(void)
958 Curses_ShowMessage(CP[115]);
959 FLUSH_SCANW("%hd", &WBwindow);
960 Curses_ShowMessage(CP[35]);
961 FLUSH_SCANW("%hd", &BBwindow);
966 Curses_GiveHint(void)
972 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
973 strcpy(s, CP[198]); /* try */
975 Curses_ShowMessage(s);
979 Curses_ShowMessage(CP[223]);
985 Curses_ChangeSearchDepth(void)
987 Curses_ShowMessage(CP[150]);
988 FLUSH_SCANW("%hd", &MaxSearchDepth);
989 TCflag = !(MaxSearchDepth > 0);
994 Curses_ChangeHashDepth(void)
996 Curses_ShowMessage(CP[163]);
997 FLUSH_SCANW("%hd", &HashDepth);
998 Curses_ShowMessage(CP[82]);
999 FLUSH_SCANW("%hd", &HashMoveLimit);
1004 Curses_SetContempt(void)
1006 Curses_ShowMessage(CP[142]);
1007 FLUSH_SCANW("%hd", &contempt);
1012 Curses_ChangeXwindow(void)
1014 Curses_ShowMessage(CP[208]);
1015 FLUSH_SCANW("%hd", &xwndw);
1020 Curses_SelectLevel(char *sx)
1024 Curses_ClearScreen();
1026 printw(CP[41], version, patchlevel);
1056 FLUSH_SCANW("%d", &item);
1103 flag.onemove = true;
1109 flag.onemove = true;
1115 flag.onemove = true;
1119 TCflag = (TCmoves > 0);
1121 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1124 Curses_ClearScreen();
1125 Curses_UpdateDisplay(0, 0, 1, 0);
1130 Curses_DoDebug(void)
1132 short c, p, sq, tp, tc, tsq, score;
1135 ExaminePosition(opponent);
1136 Curses_ShowMessage(CP[65]);
1137 FLUSH_SCANW("%s", s);
1140 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1143 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1146 for (p = king; p > no_piece; p--)
1148 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1152 for (sq = 0; sq < NO_SQUARES; sq++)
1158 tsq = PieceList[c][1];
1159 PieceList[c][1] = sq;
1160 Curses_ShowPostnValue(sq);
1161 PieceList[c][1] = tsq;
1166 score = ScorePosition(opponent);
1168 printw(CP[103], score,
1169 mtl[computer], pscore[computer], GameType[computer],
1170 mtl[opponent], pscore[opponent], GameType[opponent]);
1177 Curses_DoTable(short table[NO_SQUARES])
1180 ExaminePosition(opponent);
1182 for (sq = 0; sq < NO_SQUARES; sq++)
1184 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1185 printw("%3d ", table[sq]);