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]);
229 Curses_ShowPrompt(void)
231 Curses_ShowSidetoMove();
233 printw(CP[121]); /* Your move is? */
239 Curses_ShowResponseTime(void)
245 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
246 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
247 printw("%ld, %d, %ld, %ld, %ld, %d",
248 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
255 Curses_ShowResults(short score, unsigned short *bstline, char ch)
257 unsigned char d, ply;
261 Curses_ShowDepth(ch);
265 for (ply = 1; bstline[ply] > 0; ply++)
273 algbr((short) bstline[ply] >> 8,
274 (short) bstline[ply] & 0xFF, false);
275 printw("%5s ", mvstr[0]);
290 ShowScore(short score)
293 printw(CP[104], score);
299 Curses_ShowSidetoMove(void)
302 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
308 Curses_ShowStage(void)
311 printw("Stage= %2d%c B= %2d W= %2d",
312 stage, flag.tsume?'T':' ', balance[black], balance[white]);
317 /****************************************
318 * End of trivial output routines.
319 ****************************************/
323 Curses_Initialize(void)
325 signal(SIGINT, Curses_Die);
326 signal(SIGQUIT, Curses_Die);
333 Curses_ExitShogi(void)
354 signal(SIGINT, SIG_IGN);
355 signal(SIGQUIT, SIG_IGN);
357 Curses_ShowMessage(CP[31]); /* Abort? */
358 FLUSH_SCANW("%s", s);
360 if (strcmp(s, CP[210]) == 0) /* yes */
363 signal(SIGINT, Curses_Die);
364 signal(SIGQUIT, Curses_Die);
370 Curses_TerminateSearch(int sig)
372 signal(SIGINT, SIG_IGN);
373 signal(SIGQUIT, SIG_IGN);
376 flag.musttimeout = true;
378 Curses_ShowMessage("Terminate Search");
379 flag.bothsides = false;
380 signal(SIGINT, Curses_Die);
381 signal(SIGQUIT, Curses_Die);
389 Curses_ClearScreen();
390 /* printw("GNU Shogi ??p? command summary\n"); */
391 printw(CP[40], version, patchlevel);
392 printw("-------------------------------"
393 "---------------------------------\n");
394 /* printw("7g7f move from 7g to 7f quit
397 /* printw("S6h move silver to 6h beep
398 * turn %s\n", (flag.beep) ? "off" : "on"); */
399 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
400 /* printw("2d2c+ move to 2c and promote\n"); */
401 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
402 /* printw("P*5e drop a pawn to 5e easy
403 * turn %s\n", (flag.easy) ? "off" : "on"); */
404 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
406 * turn %s\n", (flag.hash) ? "off" : "on"); */
407 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
408 /* printw("bd redraw board reverse
409 * board display\n"); */
411 /* printw("list game to shogi.lst book
412 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
414 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
415 /* printw("undo undo last ply remove
416 * take back a move\n"); */
418 /* printw("edit edit board force
419 * enter game moves\n"); */
421 /* printw("switch sides with computer both
422 * computer match\n"); */
424 /* printw("black computer plays black white
425 * computer plays white\n"); */
427 /* printw("depth set search depth clock
428 * set time control\n"); */
430 /* printw("hint suggest a move post
431 * turn %s principle variation\n", (flag.post) ? "off" :
433 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
434 /* printw("save game to file get
435 * game from file\n"); */
437 /* printw("random randomize play new
438 * start new game\n"); */
441 printw(CP[47], ColorStr[computer]);
443 printw(CP[97], ColorStr[opponent]);
445 printw(CP[79], MaxResponseTime/100);
447 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
449 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
451 printw(CP[52], MaxSearchDepth);
453 printw(CP[100], (dither) ? CP[93] : CP[92]);
455 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
459 printw(CP[110], (TCflag) ? CP[93] : CP[92],
460 TimeControl.moves[black],
461 TimeControl.clock[black] / 100,
462 OperatorTime, MaxSearchDepth);
467 fflush(stdin); /* what is this supposed to do?? */
471 Curses_ClearScreen();
472 Curses_UpdateDisplay(0, 0, 1, 0);
476 static const short x0[2] = { 54, 2 };
477 static const short y0[2] = { 20, 4 };
481 * Set up a board position. Pieces are entered by typing the piece followed
482 * by the location. For example, N3f will place a knight on square 3f.
483 * P* will put a pawn to the captured pieces.
487 Curses_EditBoard(void)
493 flag.regularstart = true;
495 Curses_ClearScreen();
496 Curses_UpdateDisplay(0, 0, 1, 0);
510 printw(CP[60], ColorStr[a]); /* Editing %s */
513 FLUSH_SCANW("%s", s);
515 if (s[0] == CP[28][0]) /* # */
517 for (sq = 0; sq < NO_SQUARES; sq++)
519 board[sq] = no_piece;
528 if (s[0] == CP[136][0]) /* c */
533 for (i = NO_PIECES; i > no_piece; i--)
535 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
539 Captured[a][unpromoted[i]]++;
549 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
553 for (i = NO_PIECES; i > no_piece; i--)
555 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
565 color[sq] = ((board[sq] == no_piece) ? neutral : a);
569 while (s[0] != CP[29][0]); /* . */
571 for (sq = 0; sq < NO_SQUARES; sq++)
572 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
579 Curses_ClearScreen();
580 Curses_UpdateDisplay(0, 0, 1, 0);
590 for (side = black; side <= white; side++)
592 short x, y, piece, cside, k;
594 cside = flag.reverse ? (side ^ 1) : side;
599 for (piece = pawn; piece <= king; piece++)
603 if ((n = Captured[side][piece]))
606 printw("%i%c", n, pxx[piece]);
638 Curses_SearchStartStuff(short side)
642 signal(SIGINT, Curses_TerminateSearch);
643 signal(SIGQUIT, Curses_TerminateSearch);
645 for (i = 4; i < 14; i++) /* CHECKME */
655 Curses_OutputMove(void)
658 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
667 printw(CP[84], mvstr[0]); /* My move is %s */
676 if (root->flags & draw)
678 else if (root->score == -(SCORE_LIMIT + 999))
680 else if (root->score == SCORE_LIMIT + 998)
683 else if (root->score < -SCORE_LIMIT)
684 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
685 else if (root->score > SCORE_LIMIT)
686 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
687 #endif /* VERYBUGGY */
701 if (Tree[t].f || Tree[t].t)
709 ShowNodeCnt(NodeCnt);
711 printw(CP[81], t); /* Max Tree= */
715 Curses_ShowSidetoMove();
728 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
729 s = (short) ((dt - 6000 * (long) m) / 100);
733 m = (short) ((dt = et) / 6000);
734 s = (short) (et - 6000 * (long) m) / 100;
744 gotoXY(20, (flag.reverse) ? 2 : 23);
746 gotoXY(20, (flag.reverse) ? 23 : 2);
748 /* printw("%d:%02d %ld ", m, s, dt); */
749 printw("%d:%02d ", m, s);
752 ShowNodeCnt(NodeCnt);
765 if (color[sq] == neutral)
769 else if (flag.reverse ^ (color[sq] == black))
781 if (is_promoted[(int)piece])
784 y = pxx[unpromoted[(int)piece]];
792 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
793 printw("%c%c%c%c", l, p, y, r);
800 * 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]);
830 Curses_ShowPostnValues(void)
834 ExaminePosition(opponent);
836 for (sq = 0; sq < NO_SQUARES; sq++)
837 Curses_ShowPostnValue(sq);
839 score = ScorePosition(opponent);
841 printw(CP[103], score,
842 mtl[computer], pscore[computer], GameType[computer],
843 mtl[opponent], pscore[opponent], GameType[opponent]);
851 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
865 for (j=0; j<NO_COLS; j++)
868 while (i <= 1 + 2*NO_ROWS)
875 z = NO_ROWS + 2 - ((i + 1) / 2);
877 printw(" %c |", ROW_NAME(z+1));
878 for (j=0; j<NO_COLS; j++)
883 if (i < 2 + 2*NO_ROWS)
886 for (j=0; j<NO_COLS; j++)
892 for (j=0; j<NO_COLS; j++)
895 gotoXY(3, 4 + 2*NO_ROWS);
903 for (sq = 0; sq < NO_SQUARES; sq++)
906 else /* not redraw */
914 if ((isspec & capture) || (isspec & dropmask) || redraw)
918 for (side = black; side <= white; side++)
920 short x, y, piece, cside, k;
921 cside = flag.reverse ? (side ^ 1) : side;
926 for (piece = pawn; piece <= king; piece++)
930 if ((n = Captured[side][piece]))
933 printw("%i%c", n, pxx[piece]);
935 if (cside == black) y--; else y++;
965 Curses_ChangeAlphaWindow(void)
967 Curses_ShowMessage(CP[114]);
968 FLUSH_SCANW("%hd", &WAwindow);
969 Curses_ShowMessage(CP[34]);
970 FLUSH_SCANW("%hd", &BAwindow);
976 Curses_ChangeBetaWindow(void)
978 Curses_ShowMessage(CP[115]);
979 FLUSH_SCANW("%hd", &WBwindow);
980 Curses_ShowMessage(CP[35]);
981 FLUSH_SCANW("%hd", &BBwindow);
987 Curses_GiveHint(void)
993 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
994 strcpy(s, CP[198]); /* try */
996 Curses_ShowMessage(s);
1000 Curses_ShowMessage(CP[223]);
1007 Curses_ChangeSearchDepth(void)
1009 Curses_ShowMessage(CP[150]);
1010 FLUSH_SCANW("%hd", &MaxSearchDepth);
1011 TCflag = !(MaxSearchDepth > 0);
1016 Curses_ChangeHashDepth(void)
1018 Curses_ShowMessage(CP[163]);
1019 FLUSH_SCANW("%hd", &HashDepth);
1020 Curses_ShowMessage(CP[82]);
1021 FLUSH_SCANW("%hd", &HashMoveLimit);
1026 Curses_SetContempt(void)
1028 Curses_ShowMessage(CP[142]);
1029 FLUSH_SCANW("%hd", &contempt);
1035 Curses_ChangeXwindow(void)
1037 Curses_ShowMessage(CP[208]);
1038 FLUSH_SCANW("%hd", &xwndw);
1044 Curses_SelectLevel(char *sx)
1048 Curses_ClearScreen();
1050 printw(CP[41], version, patchlevel);
1080 FLUSH_SCANW("%d", &item);
1127 flag.onemove = true;
1133 flag.onemove = true;
1139 flag.onemove = true;
1143 TCflag = (TCmoves > 0);
1145 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1148 Curses_ClearScreen();
1149 Curses_UpdateDisplay(0, 0, 1, 0);
1155 Curses_DoDebug(void)
1157 short c, p, sq, tp, tc, tsq, score;
1160 ExaminePosition(opponent);
1161 Curses_ShowMessage(CP[65]);
1162 FLUSH_SCANW("%s", s);
1165 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1168 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1171 for (p = king; p > no_piece; p--)
1173 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1177 for (sq = 0; sq < NO_SQUARES; sq++)
1183 tsq = PieceList[c][1];
1184 PieceList[c][1] = sq;
1185 Curses_ShowPostnValue(sq);
1186 PieceList[c][1] = tsq;
1191 score = ScorePosition(opponent);
1193 printw(CP[103], score,
1194 mtl[computer], pscore[computer], GameType[computer],
1195 mtl[opponent], pscore[opponent], GameType[opponent]);
1203 Curses_DoTable(short table[NO_SQUARES])
1206 ExaminePosition(opponent);
1208 for (sq = 0; sq < NO_SQUARES; sq++)
1210 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1211 printw("%3d ", table[sq]);