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_Printf(const char *format, ...)
156 static char buffer[60];
158 va_start(ap, format);
159 vsnprintf(buffer, sizeof(buffer), format, ap);
160 printw("%s", buffer);
166 Curses_doRequestInputString(const char* fmt, char* buffer)
168 FLUSH_SCANW(fmt, buffer);
173 Curses_ShowNodeCnt(long NodeCnt)
176 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
177 printw("n = %ld n/s = %ld",
178 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
184 Curses_ShowPatternCount(short side, short n)
188 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
201 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
202 printw("%s", (computer == white) ? CP[218] : CP[74]);
203 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
204 printw("%s", (computer == black) ? CP[218] : CP[74]);
212 printw(CP[121]); /* Your move is? */
218 Curses_ShowResponseTime(void)
224 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
225 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
226 printw("%ld, %d, %ld, %ld, %ld, %d",
227 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
234 Curses_ShowResults(short score, unsigned short *bstline, char ch)
236 unsigned char d, ply;
240 Curses_ShowDepth(ch);
244 for (ply = 1; bstline[ply] > 0; ply++)
252 algbr((short) bstline[ply] >> 8,
253 (short) bstline[ply] & 0xFF, false);
254 printw("%5s ", mvstr[0]);
269 ShowScore(short score)
272 printw(CP[104], score);
278 Curses_ShowSidetoMove(void)
281 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
287 Curses_ShowStage(void)
290 printw("Stage= %2d%c B= %2d W= %2d",
291 stage, flag.tsume?'T':' ', balance[black], balance[white]);
296 /****************************************
297 * End of trivial output routines.
298 ****************************************/
302 Curses_Initialize(void)
304 signal(SIGINT, Curses_Die);
305 signal(SIGQUIT, Curses_Die);
312 Curses_ExitShogi(void)
333 signal(SIGINT, SIG_IGN);
334 signal(SIGQUIT, SIG_IGN);
336 Curses_ShowMessage(CP[31]); /* Abort? */
337 FLUSH_SCANW("%s", s);
339 if (strcmp(s, CP[210]) == 0) /* yes */
342 signal(SIGINT, Curses_Die);
343 signal(SIGQUIT, Curses_Die);
349 Curses_TerminateSearch(int sig)
351 signal(SIGINT, SIG_IGN);
352 signal(SIGQUIT, SIG_IGN);
355 flag.musttimeout = true;
357 Curses_ShowMessage("Terminate Search");
358 flag.bothsides = false;
359 signal(SIGINT, Curses_Die);
360 signal(SIGQUIT, Curses_Die);
368 Curses_ClearScreen();
369 /* printw("GNU Shogi ??p? command summary\n"); */
370 printw(CP[40], version, patchlevel);
371 printw("-------------------------------"
372 "---------------------------------\n");
373 /* printw("7g7f move from 7g to 7f quit
376 /* printw("S6h move silver to 6h beep
377 * turn %s\n", (flag.beep) ? "off" : "on"); */
378 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
379 /* printw("2d2c+ move to 2c and promote\n"); */
380 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
381 /* printw("P*5e drop a pawn to 5e easy
382 * turn %s\n", (flag.easy) ? "off" : "on"); */
383 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
385 * turn %s\n", (flag.hash) ? "off" : "on"); */
386 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
387 /* printw("bd redraw board reverse
388 * board display\n"); */
390 /* printw("list game to shogi.lst book
391 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
393 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
394 /* printw("undo undo last ply remove
395 * take back a move\n"); */
397 /* printw("edit edit board force
398 * enter game moves\n"); */
400 /* printw("switch sides with computer both
401 * computer match\n"); */
403 /* printw("black computer plays black white
404 * computer plays white\n"); */
406 /* printw("depth set search depth clock
407 * set time control\n"); */
409 /* printw("hint suggest a move post
410 * turn %s principle variation\n", (flag.post) ? "off" :
412 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
413 /* printw("save game to file get
414 * game from file\n"); */
416 /* printw("random randomize play new
417 * start new game\n"); */
420 printw(CP[47], ColorStr[computer]);
422 printw(CP[97], ColorStr[opponent]);
424 printw(CP[79], MaxResponseTime/100);
426 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
428 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
430 printw(CP[52], MaxSearchDepth);
432 printw(CP[100], (dither) ? CP[93] : CP[92]);
434 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
438 printw(CP[110], (TCflag) ? CP[93] : CP[92],
439 TimeControl.moves[black],
440 TimeControl.clock[black] / 100,
441 OperatorTime, MaxSearchDepth);
446 fflush(stdin); /* what is this supposed to do?? */
450 Curses_ClearScreen();
451 Curses_UpdateDisplay(0, 0, 1, 0);
455 static const short x0[2] = { 54, 2 };
456 static const short y0[2] = { 20, 4 };
460 * Set up a board position. Pieces are entered by typing the piece followed
461 * by the location. For example, N3f will place a knight on square 3f.
462 * P* will put a pawn to the captured pieces.
466 Curses_EditBoard(void)
472 flag.regularstart = true;
474 Curses_ClearScreen();
475 Curses_UpdateDisplay(0, 0, 1, 0);
489 printw(CP[60], ColorStr[a]); /* Editing %s */
492 FLUSH_SCANW("%s", s);
494 if (s[0] == CP[28][0]) /* # */
496 for (sq = 0; sq < NO_SQUARES; sq++)
498 board[sq] = no_piece;
507 if (s[0] == CP[136][0]) /* c */
512 for (i = NO_PIECES; i > no_piece; i--)
514 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
518 Captured[a][unpromoted[i]]++;
528 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
532 for (i = NO_PIECES; i > no_piece; i--)
534 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
544 color[sq] = ((board[sq] == no_piece) ? neutral : a);
548 while (s[0] != CP[29][0]); /* . */
550 for (sq = 0; sq < NO_SQUARES; sq++)
551 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
558 Curses_ClearScreen();
559 Curses_UpdateDisplay(0, 0, 1, 0);
569 for (side = black; side <= white; side++)
571 short x, y, piece, cside, k;
573 cside = flag.reverse ? (side ^ 1) : side;
578 for (piece = pawn; piece <= king; piece++)
582 if ((n = Captured[side][piece]))
585 printw("%i%c", n, pxx[piece]);
617 Curses_SearchStartStuff(short side)
621 signal(SIGINT, Curses_TerminateSearch);
622 signal(SIGQUIT, Curses_TerminateSearch);
624 for (i = 4; i < 14; i++) /* CHECKME */
634 Curses_OutputMove(void)
637 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
646 printw(CP[84], mvstr[0]); /* My move is %s */
655 if (root->flags & draw)
657 else if (root->score == -(SCORE_LIMIT + 999))
659 else if (root->score == SCORE_LIMIT + 998)
662 else if (root->score < -SCORE_LIMIT)
663 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
664 else if (root->score > SCORE_LIMIT)
665 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
666 #endif /* VERYBUGGY */
680 if (Tree[t].f || Tree[t].t)
688 ShowNodeCnt(NodeCnt);
690 printw(CP[81], t); /* Max Tree= */
694 Curses_ShowSidetoMove();
707 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
708 s = (short) ((dt - 6000 * (long) m) / 100);
712 m = (short) ((dt = et) / 6000);
713 s = (short) (et - 6000 * (long) m) / 100;
723 gotoXY(20, (flag.reverse) ? 2 : 23);
725 gotoXY(20, (flag.reverse) ? 23 : 2);
727 /* printw("%d:%02d %ld ", m, s, dt); */
728 printw("%d:%02d ", m, s);
731 ShowNodeCnt(NodeCnt);
744 if (color[sq] == neutral)
748 else if (flag.reverse ^ (color[sq] == black))
760 if (is_promoted[(int)piece])
763 y = pxx[unpromoted[(int)piece]];
771 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
772 printw("%c%c%c%c", l, p, y, r);
779 * Curses_ShowPostnValue(): must have called ExaminePosition() first
783 Curses_ShowPostnValue(short sq)
787 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
788 score = ScorePosition(color[sq]);
790 if (color[sq] != neutral)
791 #if defined SAVE_SVALUE
797 printw("%3d ", svalue[sq]);
809 Curses_ShowPostnValues(void)
813 ExaminePosition(opponent);
815 for (sq = 0; sq < NO_SQUARES; sq++)
816 Curses_ShowPostnValue(sq);
818 score = ScorePosition(opponent);
820 printw(CP[103], score,
821 mtl[computer], pscore[computer], GameType[computer],
822 mtl[opponent], pscore[opponent], GameType[opponent]);
830 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
844 for (j=0; j<NO_COLS; j++)
847 while (i <= 1 + 2*NO_ROWS)
854 z = NO_ROWS + 2 - ((i + 1) / 2);
856 printw(" %c |", ROW_NAME(z+1));
857 for (j=0; j<NO_COLS; j++)
862 if (i < 2 + 2*NO_ROWS)
865 for (j=0; j<NO_COLS; j++)
871 for (j=0; j<NO_COLS; j++)
874 gotoXY(3, 4 + 2*NO_ROWS);
882 for (sq = 0; sq < NO_SQUARES; sq++)
885 else /* not redraw */
893 if ((isspec & capture) || (isspec & dropmask) || redraw)
897 for (side = black; side <= white; side++)
899 short x, y, piece, cside, k;
900 cside = flag.reverse ? (side ^ 1) : side;
905 for (piece = pawn; piece <= king; piece++)
909 if ((n = Captured[side][piece]))
912 printw("%i%c", n, pxx[piece]);
914 if (cside == black) y--; else y++;
944 Curses_ChangeAlphaWindow(void)
946 Curses_ShowMessage(CP[114]);
947 FLUSH_SCANW("%hd", &WAwindow);
948 Curses_ShowMessage(CP[34]);
949 FLUSH_SCANW("%hd", &BAwindow);
955 Curses_ChangeBetaWindow(void)
957 Curses_ShowMessage(CP[115]);
958 FLUSH_SCANW("%hd", &WBwindow);
959 Curses_ShowMessage(CP[35]);
960 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]);
986 Curses_ChangeSearchDepth(void)
988 Curses_ShowMessage(CP[150]);
989 FLUSH_SCANW("%hd", &MaxSearchDepth);
990 TCflag = !(MaxSearchDepth > 0);
995 Curses_ChangeHashDepth(void)
997 Curses_ShowMessage(CP[163]);
998 FLUSH_SCANW("%hd", &HashDepth);
999 Curses_ShowMessage(CP[82]);
1000 FLUSH_SCANW("%hd", &HashMoveLimit);
1005 Curses_SetContempt(void)
1007 Curses_ShowMessage(CP[142]);
1008 FLUSH_SCANW("%hd", &contempt);
1014 Curses_ChangeXwindow(void)
1016 Curses_ShowMessage(CP[208]);
1017 FLUSH_SCANW("%hd", &xwndw);
1023 Curses_SelectLevel(char *sx)
1027 Curses_ClearScreen();
1029 printw(CP[41], version, patchlevel);
1059 FLUSH_SCANW("%d", &item);
1106 flag.onemove = true;
1112 flag.onemove = true;
1118 flag.onemove = true;
1122 TCflag = (TCmoves > 0);
1124 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1127 Curses_ClearScreen();
1128 Curses_UpdateDisplay(0, 0, 1, 0);
1134 Curses_DoDebug(void)
1136 short c, p, sq, tp, tc, tsq, score;
1139 ExaminePosition(opponent);
1140 Curses_ShowMessage(CP[65]);
1141 FLUSH_SCANW("%s", s);
1144 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1147 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1150 for (p = king; p > no_piece; p--)
1152 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1156 for (sq = 0; sq < NO_SQUARES; sq++)
1162 tsq = PieceList[c][1];
1163 PieceList[c][1] = sq;
1164 Curses_ShowPostnValue(sq);
1165 PieceList[c][1] = tsq;
1170 score = ScorePosition(opponent);
1172 printw(CP[103], score,
1173 mtl[computer], pscore[computer], GameType[computer],
1174 mtl[opponent], pscore[opponent], GameType[opponent]);
1182 Curses_DoTable(short table[NO_SQUARES])
1185 ExaminePosition(opponent);
1187 for (sq = 0; sq < NO_SQUARES; sq++)
1189 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1190 printw("%3d ", table[sq]);