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_ShowNodeCnt(long NodeCnt)
188 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
189 printw("n = %ld n/s = %ld",
190 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
196 Curses_ShowPatternCount(short side, short n)
200 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
213 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
214 printw("%s", (computer == white) ? CP[218] : CP[74]);
215 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
216 printw("%s", (computer == black) ? CP[218] : CP[74]);
224 printw(CP[121]); /* Your move is? */
230 Curses_ShowResponseTime(void)
236 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
237 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
238 printw("%ld, %d, %ld, %ld, %ld, %d",
239 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
246 Curses_ShowResults(short score, unsigned short *bstline, char ch)
248 unsigned char d, ply;
252 Curses_ShowDepth(ch);
256 for (ply = 1; bstline[ply] > 0; ply++)
264 algbr((short) bstline[ply] >> 8,
265 (short) bstline[ply] & 0xFF, false);
266 printw("%5s ", mvstr[0]);
281 ShowScore(short score)
284 printw(CP[104], score);
290 Curses_ShowSidetoMove(void)
293 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
299 Curses_ShowStage(void)
302 printw("Stage= %2d%c B= %2d W= %2d",
303 stage, flag.tsume?'T':' ', balance[black], balance[white]);
308 /****************************************
309 * End of trivial output routines.
310 ****************************************/
314 Curses_Initialize(void)
316 signal(SIGINT, Curses_Die);
317 signal(SIGQUIT, Curses_Die);
324 Curses_ExitShogi(void)
345 signal(SIGINT, SIG_IGN);
346 signal(SIGQUIT, SIG_IGN);
348 Curses_ShowMessage(CP[31]); /* Abort? */
349 FLUSH_SCANW("%s", s);
351 if (strcmp(s, CP[210]) == 0) /* yes */
354 signal(SIGINT, Curses_Die);
355 signal(SIGQUIT, Curses_Die);
361 Curses_TerminateSearch(int sig)
363 signal(SIGINT, SIG_IGN);
364 signal(SIGQUIT, SIG_IGN);
367 flag.musttimeout = true;
369 Curses_ShowMessage("Terminate Search");
370 flag.bothsides = false;
371 signal(SIGINT, Curses_Die);
372 signal(SIGQUIT, Curses_Die);
380 Curses_ClearScreen();
381 /* printw("GNU Shogi ??p? command summary\n"); */
382 printw(CP[40], version, patchlevel);
383 printw("-------------------------------"
384 "---------------------------------\n");
385 /* printw("7g7f move from 7g to 7f quit
388 /* printw("S6h move silver to 6h beep
389 * turn %s\n", (flag.beep) ? "off" : "on"); */
390 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
391 /* printw("2d2c+ move to 2c and promote\n"); */
392 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
393 /* printw("P*5e drop a pawn to 5e easy
394 * turn %s\n", (flag.easy) ? "off" : "on"); */
395 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
397 * turn %s\n", (flag.hash) ? "off" : "on"); */
398 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
399 /* printw("bd redraw board reverse
400 * board display\n"); */
402 /* printw("list game to shogi.lst book
403 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
405 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
406 /* printw("undo undo last ply remove
407 * take back a move\n"); */
409 /* printw("edit edit board force
410 * enter game moves\n"); */
412 /* printw("switch sides with computer both
413 * computer match\n"); */
415 /* printw("black computer plays black white
416 * computer plays white\n"); */
418 /* printw("depth set search depth clock
419 * set time control\n"); */
421 /* printw("hint suggest a move post
422 * turn %s principle variation\n", (flag.post) ? "off" :
424 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
425 /* printw("save game to file get
426 * game from file\n"); */
428 /* printw("random randomize play new
429 * start new game\n"); */
432 printw(CP[47], ColorStr[computer]);
434 printw(CP[97], ColorStr[opponent]);
436 printw(CP[79], MaxResponseTime/100);
438 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
440 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
442 printw(CP[52], MaxSearchDepth);
444 printw(CP[100], (dither) ? CP[93] : CP[92]);
446 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
450 printw(CP[110], (TCflag) ? CP[93] : CP[92],
451 TimeControl.moves[black],
452 TimeControl.clock[black] / 100,
453 OperatorTime, MaxSearchDepth);
458 fflush(stdin); /* what is this supposed to do?? */
462 Curses_ClearScreen();
463 Curses_UpdateDisplay(0, 0, 1, 0);
467 static const short x0[2] = { 54, 2 };
468 static const short y0[2] = { 20, 4 };
472 * Set up a board position. Pieces are entered by typing the piece followed
473 * by the location. For example, N3f will place a knight on square 3f.
474 * P* will put a pawn to the captured pieces.
478 Curses_EditBoard(void)
484 flag.regularstart = true;
486 Curses_ClearScreen();
487 Curses_UpdateDisplay(0, 0, 1, 0);
501 printw(CP[60], ColorStr[a]); /* Editing %s */
504 FLUSH_SCANW("%s", s);
506 if (s[0] == CP[28][0]) /* # */
508 for (sq = 0; sq < NO_SQUARES; sq++)
510 board[sq] = no_piece;
519 if (s[0] == CP[136][0]) /* c */
524 for (i = NO_PIECES; i > no_piece; i--)
526 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
530 Captured[a][unpromoted[i]]++;
540 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
544 for (i = NO_PIECES; i > no_piece; i--)
546 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
556 color[sq] = ((board[sq] == no_piece) ? neutral : a);
560 while (s[0] != CP[29][0]); /* . */
562 for (sq = 0; sq < NO_SQUARES; sq++)
563 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
570 Curses_ClearScreen();
571 Curses_UpdateDisplay(0, 0, 1, 0);
581 for (side = black; side <= white; side++)
583 short x, y, piece, cside, k;
585 cside = flag.reverse ? (side ^ 1) : side;
590 for (piece = pawn; piece <= king; piece++)
594 if ((n = Captured[side][piece]))
597 printw("%i%c", n, pxx[piece]);
629 Curses_SearchStartStuff(short side)
633 signal(SIGINT, Curses_TerminateSearch);
634 signal(SIGQUIT, Curses_TerminateSearch);
636 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();
719 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
720 s = (short) ((dt - 6000 * (long) m) / 100);
724 m = (short) ((dt = et) / 6000);
725 s = (short) (et - 6000 * (long) m) / 100;
735 gotoXY(20, (flag.reverse) ? 2 : 23);
737 gotoXY(20, (flag.reverse) ? 23 : 2);
739 /* printw("%d:%02d %ld ", m, s, dt); */
740 printw("%d:%02d ", m, s);
743 ShowNodeCnt(NodeCnt);
756 if (color[sq] == neutral)
760 else if (flag.reverse ^ (color[sq] == black))
772 if (is_promoted[(int)piece])
775 y = pxx[unpromoted[(int)piece]];
783 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
784 printw("%c%c%c%c", l, p, y, r);
791 * Curses_ShowPostnValue(): must have called ExaminePosition() first
795 Curses_ShowPostnValue(short sq)
799 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
800 score = ScorePosition(color[sq]);
802 if (color[sq] != neutral)
803 #if defined SAVE_SVALUE
809 printw("%3d ", svalue[sq]);
821 Curses_ShowPostnValues(void)
825 ExaminePosition(opponent);
827 for (sq = 0; sq < NO_SQUARES; sq++)
828 Curses_ShowPostnValue(sq);
830 score = ScorePosition(opponent);
832 printw(CP[103], score,
833 mtl[computer], pscore[computer], GameType[computer],
834 mtl[opponent], pscore[opponent], GameType[opponent]);
842 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
856 for (j=0; j<NO_COLS; j++)
859 while (i <= 1 + 2*NO_ROWS)
866 z = NO_ROWS + 2 - ((i + 1) / 2);
868 printw(" %c |", ROW_NAME(z+1));
869 for (j=0; j<NO_COLS; j++)
874 if (i < 2 + 2*NO_ROWS)
877 for (j=0; j<NO_COLS; j++)
883 for (j=0; j<NO_COLS; j++)
886 gotoXY(3, 4 + 2*NO_ROWS);
894 for (sq = 0; sq < NO_SQUARES; sq++)
897 else /* not redraw */
905 if ((isspec & capture) || (isspec & dropmask) || redraw)
909 for (side = black; side <= white; side++)
911 short x, y, piece, cside, k;
912 cside = flag.reverse ? (side ^ 1) : side;
917 for (piece = pawn; piece <= king; piece++)
921 if ((n = Captured[side][piece]))
924 printw("%i%c", n, pxx[piece]);
926 if (cside == black) y--; else y++;
956 Curses_ChangeAlphaWindow(void)
958 Curses_ShowMessage(CP[114]);
959 FLUSH_SCANW("%hd", &WAwindow);
960 Curses_ShowMessage(CP[34]);
961 FLUSH_SCANW("%hd", &BAwindow);
967 Curses_ChangeBetaWindow(void)
969 Curses_ShowMessage(CP[115]);
970 FLUSH_SCANW("%hd", &WBwindow);
971 Curses_ShowMessage(CP[35]);
972 FLUSH_SCANW("%hd", &BBwindow);
978 Curses_GiveHint(void)
984 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
985 strcpy(s, CP[198]); /* try */
987 Curses_ShowMessage(s);
991 Curses_ShowMessage(CP[223]);
998 Curses_ChangeSearchDepth(void)
1000 Curses_ShowMessage(CP[150]);
1001 FLUSH_SCANW("%hd", &MaxSearchDepth);
1002 TCflag = !(MaxSearchDepth > 0);
1007 Curses_ChangeHashDepth(void)
1009 Curses_ShowMessage(CP[163]);
1010 FLUSH_SCANW("%hd", &HashDepth);
1011 Curses_ShowMessage(CP[82]);
1012 FLUSH_SCANW("%hd", &HashMoveLimit);
1017 Curses_SetContempt(void)
1019 Curses_ShowMessage(CP[142]);
1020 FLUSH_SCANW("%hd", &contempt);
1026 Curses_ChangeXwindow(void)
1028 Curses_ShowMessage(CP[208]);
1029 FLUSH_SCANW("%hd", &xwndw);
1035 Curses_SelectLevel(char *sx)
1039 Curses_ClearScreen();
1041 printw(CP[41], version, patchlevel);
1071 FLUSH_SCANW("%d", &item);
1118 flag.onemove = true;
1124 flag.onemove = true;
1130 flag.onemove = true;
1134 TCflag = (TCmoves > 0);
1136 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1139 Curses_ClearScreen();
1140 Curses_UpdateDisplay(0, 0, 1, 0);
1146 Curses_DoDebug(void)
1148 short c, p, sq, tp, tc, tsq, score;
1151 ExaminePosition(opponent);
1152 Curses_ShowMessage(CP[65]);
1153 FLUSH_SCANW("%s", s);
1156 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1159 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1162 for (p = king; p > no_piece; p--)
1164 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1168 for (sq = 0; sq < NO_SQUARES; sq++)
1174 tsq = PieceList[c][1];
1175 PieceList[c][1] = sq;
1176 Curses_ShowPostnValue(sq);
1177 PieceList[c][1] = tsq;
1182 score = ScorePosition(opponent);
1184 printw(CP[103], score,
1185 mtl[computer], pscore[computer], GameType[computer],
1186 mtl[opponent], pscore[opponent], GameType[opponent]);
1194 Curses_DoTable(short table[NO_SQUARES])
1197 ExaminePosition(opponent);
1199 for (sq = 0; sq < NO_SQUARES; sq++)
1201 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1202 printw("%3d ", table[sq]);