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 * ----------------------------------------------------------------------
37 #include <sys/param.h>
38 #include <sys/types.h>
43 #include "cursesdsp.h"
45 #define FLUSH_SCANW fflush(stdout), scanw
51 #define VIR_C(s) ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
52 #define VIR_R(s) ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
54 unsigned short MV[MAXDEPTH];
58 /* Forward declarations. */
59 /* FIXME: change this name, puh-leeze! */
61 static void UpdateCatched(void);
64 /****************************************
65 * Trivial output functions.
66 ****************************************/
77 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_Printf(const char *format, ...)
155 static char buffer[60];
157 va_start(ap, format);
158 vsnprintf(buffer, sizeof(buffer), format, ap);
159 printw("%s", buffer);
165 Curses_RequestInputString(char* buffer)
167 FLUSH_SCANW("%s", buffer);
172 ShowNodeCnt(long NodeCnt)
175 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
176 printw("n = %ld n/s = %ld",
177 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
183 Curses_ShowPatternCount(short side, short n)
187 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
200 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
201 printw("%s", (computer == white) ? CP[218] : CP[74]);
202 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
203 printw("%s", (computer == black) ? CP[218] : CP[74]);
211 printw(CP[121]); /* Your move is? */
217 Curses_ShowResponseTime(void)
223 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
224 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
225 printw("%ld, %d, %ld, %ld, %ld, %d",
226 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
233 Curses_ShowResults(short score, unsigned short *bstline, char ch)
235 unsigned char d, ply;
239 Curses_ShowDepth(ch);
243 for (ply = 1; bstline[ply] > 0; ply++)
251 algbr((short) bstline[ply] >> 8,
252 (short) bstline[ply] & 0xFF, false);
253 printw("%5s ", mvstr[0]);
268 ShowScore(short score)
271 printw(CP[104], score);
277 Curses_ShowSidetoMove(void)
280 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
286 Curses_ShowStage(void)
289 printw("Stage= %2d%c B= %2d W= %2d",
290 stage, flag.tsume?'T':' ', balance[black], balance[white]);
295 /****************************************
296 * End of trivial output routines.
297 ****************************************/
301 Curses_Initialize(void)
303 signal(SIGINT, Curses_Die);
304 signal(SIGQUIT, Curses_Die);
311 Curses_ExitShogi(void)
332 signal(SIGINT, SIG_IGN);
333 signal(SIGQUIT, SIG_IGN);
335 Curses_ShowMessage(CP[31]); /* Abort? */
336 FLUSH_SCANW("%s", s);
338 if (strcmp(s, CP[210]) == 0) /* yes */
341 signal(SIGINT, Curses_Die);
342 signal(SIGQUIT, Curses_Die);
348 Curses_TerminateSearch(int sig)
350 signal(SIGINT, SIG_IGN);
351 signal(SIGQUIT, SIG_IGN);
354 flag.musttimeout = true;
356 Curses_ShowMessage("Terminate Search");
357 flag.bothsides = false;
358 signal(SIGINT, Curses_Die);
359 signal(SIGQUIT, Curses_Die);
367 Curses_ClearScreen();
368 /* printw("GNU Shogi ??p? command summary\n"); */
369 printw(CP[40], version, patchlevel);
370 printw("-------------------------------"
371 "---------------------------------\n");
372 /* printw("7g7f move from 7g to 7f quit
375 /* printw("S6h move silver to 6h beep
376 * turn %s\n", (flag.beep) ? "off" : "on"); */
377 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
378 /* printw("2d2c+ move to 2c and promote\n"); */
379 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
380 /* printw("P*5e drop a pawn to 5e easy
381 * turn %s\n", (flag.easy) ? "off" : "on"); */
382 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
384 * turn %s\n", (flag.hash) ? "off" : "on"); */
385 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
386 /* printw("bd redraw board reverse
387 * board display\n"); */
389 /* printw("list game to shogi.lst book
390 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
392 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
393 /* printw("undo undo last ply remove
394 * take back a move\n"); */
396 /* printw("edit edit board force
397 * enter game moves\n"); */
399 /* printw("switch sides with computer both
400 * computer match\n"); */
402 /* printw("black computer plays black white
403 * computer plays white\n"); */
405 /* printw("depth set search depth clock
406 * set time control\n"); */
408 /* printw("hint suggest a move post
409 * turn %s principle variation\n", (flag.post) ? "off" :
411 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
412 /* printw("save game to file get
413 * game from file\n"); */
415 /* printw("random randomize play new
416 * start new game\n"); */
419 printw(CP[47], ColorStr[computer]);
421 printw(CP[97], ColorStr[opponent]);
423 printw(CP[79], MaxResponseTime/100);
425 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
427 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
429 printw(CP[52], MaxSearchDepth);
431 printw(CP[100], (dither) ? CP[93] : CP[92]);
433 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
437 printw(CP[110], (TCflag) ? CP[93] : CP[92],
438 TimeControl.moves[black],
439 TimeControl.clock[black] / 100,
440 OperatorTime, MaxSearchDepth);
445 fflush(stdin); /* what is this supposed to do?? */
449 Curses_ClearScreen();
450 Curses_UpdateDisplay(0, 0, 1, 0);
454 static const short x0[2] = { 54, 2 };
455 static const short y0[2] = { 20, 4 };
459 * Set up a board position. Pieces are entered by typing the piece followed
460 * by the location. For example, N3f will place a knight on square 3f.
461 * P* will put a pawn to the captured pieces.
465 Curses_EditBoard(void)
471 flag.regularstart = true;
473 Curses_ClearScreen();
474 Curses_UpdateDisplay(0, 0, 1, 0);
488 printw(CP[60], ColorStr[a]); /* Editing %s */
491 FLUSH_SCANW("%s", s);
493 if (s[0] == CP[28][0]) /* # */
495 for (sq = 0; sq < NO_SQUARES; sq++)
497 board[sq] = no_piece;
506 if (s[0] == CP[136][0]) /* c */
511 for (i = NO_PIECES; i > no_piece; i--)
513 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
517 Captured[a][unpromoted[i]]++;
527 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
531 for (i = NO_PIECES; i > no_piece; i--)
533 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
543 color[sq] = ((board[sq] == no_piece) ? neutral : a);
547 while (s[0] != CP[29][0]); /* . */
549 for (sq = 0; sq < NO_SQUARES; sq++)
550 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
557 Curses_ClearScreen();
558 Curses_UpdateDisplay(0, 0, 1, 0);
568 for (side = black; side <= white; side++)
570 short x, y, piece, cside, k;
572 cside = flag.reverse ? (side ^ 1) : side;
577 for (piece = pawn; piece <= king; piece++)
581 if ((n = Captured[side][piece]))
584 printw("%i%c", n, pxx[piece]);
616 Curses_SearchStartStuff(short side)
620 signal(SIGINT, Curses_TerminateSearch);
621 signal(SIGQUIT, Curses_TerminateSearch);
623 for (i = 4; i < 14; i++) /* CHECKME */
633 Curses_OutputMove(void)
636 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
645 printw(CP[84], mvstr[0]); /* My move is %s */
654 if (root->flags & draw)
656 else if (root->score == -(SCORE_LIMIT + 999))
658 else if (root->score == SCORE_LIMIT + 998)
661 else if (root->score < -SCORE_LIMIT)
662 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
663 else if (root->score > SCORE_LIMIT)
664 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
665 #endif /* VERYBUGGY */
679 if (Tree[t].f || Tree[t].t)
687 ShowNodeCnt(NodeCnt);
689 printw(CP[81], t); /* Max Tree= */
693 Curses_ShowSidetoMove();
706 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
707 s = (short) ((dt - 6000 * (long) m) / 100);
711 m = (short) ((dt = et) / 6000);
712 s = (short) (et - 6000 * (long) m) / 100;
722 gotoXY(20, (flag.reverse) ? 2 : 23);
724 gotoXY(20, (flag.reverse) ? 23 : 2);
726 /* printw("%d:%02d %ld ", m, s, dt); */
727 printw("%d:%02d ", m, s);
730 ShowNodeCnt(NodeCnt);
743 if (color[sq] == neutral)
747 else if (flag.reverse ^ (color[sq] == black))
759 if (is_promoted[(int)piece])
762 y = pxx[unpromoted[(int)piece]];
770 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
771 printw("%c%c%c%c", l, p, y, r);
778 * Curses_ShowPostnValue(): must have called ExaminePosition() first
782 Curses_ShowPostnValue(short sq)
786 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
787 score = ScorePosition(color[sq]);
789 if (color[sq] != neutral)
790 #if defined SAVE_SVALUE
796 printw("%3d ", svalue[sq]);
808 Curses_ShowPostnValues(void)
812 ExaminePosition(opponent);
814 for (sq = 0; sq < NO_SQUARES; sq++)
815 Curses_ShowPostnValue(sq);
817 score = ScorePosition(opponent);
819 printw(CP[103], score,
820 mtl[computer], pscore[computer], GameType[computer],
821 mtl[opponent], pscore[opponent], GameType[opponent]);
829 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
843 for (j=0; j<NO_COLS; j++)
846 while (i <= 1 + 2*NO_ROWS)
853 z = NO_ROWS + 2 - ((i + 1) / 2);
855 printw(" %c |", ROW_NAME(z+1));
856 for (j=0; j<NO_COLS; j++)
861 if (i < 2 + 2*NO_ROWS)
864 for (j=0; j<NO_COLS; j++)
870 for (j=0; j<NO_COLS; j++)
873 gotoXY(3, 4 + 2*NO_ROWS);
881 for (sq = 0; sq < NO_SQUARES; sq++)
884 else /* not redraw */
892 if ((isspec & capture) || (isspec & dropmask) || redraw)
896 for (side = black; side <= white; side++)
898 short x, y, piece, cside, k;
899 cside = flag.reverse ? (side ^ 1) : side;
904 for (piece = pawn; piece <= king; piece++)
908 if ((n = Captured[side][piece]))
911 printw("%i%c", n, pxx[piece]);
913 if (cside == black) y--; else y++;
943 Curses_ChangeAlphaWindow(void)
945 Curses_ShowMessage(CP[114]);
946 FLUSH_SCANW("%hd", &WAwindow);
947 Curses_ShowMessage(CP[34]);
948 FLUSH_SCANW("%hd", &BAwindow);
954 Curses_ChangeBetaWindow(void)
956 Curses_ShowMessage(CP[115]);
957 FLUSH_SCANW("%hd", &WBwindow);
958 Curses_ShowMessage(CP[35]);
959 FLUSH_SCANW("%hd", &BBwindow);
965 Curses_GiveHint(void)
971 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
972 strcpy(s, CP[198]); /* try */
974 Curses_ShowMessage(s);
978 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);
1013 Curses_ChangeXwindow(void)
1015 Curses_ShowMessage(CP[208]);
1016 FLUSH_SCANW("%hd", &xwndw);
1022 Curses_SelectLevel(char *sx)
1026 Curses_ClearScreen();
1028 printw(CP[41], version, patchlevel);
1058 FLUSH_SCANW("%d", &item);
1105 flag.onemove = true;
1111 flag.onemove = true;
1117 flag.onemove = true;
1121 TCflag = (TCmoves > 0);
1123 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1126 Curses_ClearScreen();
1127 Curses_UpdateDisplay(0, 0, 1, 0);
1133 Curses_DoDebug(void)
1135 short c, p, sq, tp, tc, tsq, score;
1138 ExaminePosition(opponent);
1139 Curses_ShowMessage(CP[65]);
1140 FLUSH_SCANW("%s", s);
1143 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1146 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1149 for (p = king; p > no_piece; p--)
1151 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1155 for (sq = 0; sq < NO_SQUARES; sq++)
1161 tsq = PieceList[c][1];
1162 PieceList[c][1] = sq;
1163 Curses_ShowPostnValue(sq);
1164 PieceList[c][1] = tsq;
1169 score = ScorePosition(opponent);
1171 printw(CP[103], score,
1172 mtl[computer], pscore[computer], GameType[computer],
1173 mtl[opponent], pscore[opponent], GameType[opponent]);
1181 Curses_DoTable(short table[NO_SQUARES])
1184 ExaminePosition(opponent);
1186 for (sq = 0; sq < NO_SQUARES; sq++)
1188 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1189 printw("%3d ", table[sq]);