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 ShowNodeCnt(long NodeCnt)
156 /* printw(CP[90], NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
157 printw("n = %ld n/s = %ld",
158 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
164 Curses_ShowPatternCount(short side, short n)
168 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
181 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
182 printw("%s", (computer == white) ? CP[218] : CP[74]);
183 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
184 printw("%s", (computer == black) ? CP[218] : CP[74]);
192 printw(CP[121]); /* Your move is? */
198 Curses_ShowResponseTime(void)
204 /* printw("RT = %ld TCC = %d TCL = %ld EX = %ld ET = %ld TO = %d",
205 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout); */
206 printw("%ld, %d, %ld, %ld, %ld, %d",
207 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
214 Curses_ShowResults(short score, unsigned short *bstline, char ch)
216 unsigned char d, ply;
220 Curses_ShowDepth(ch);
224 for (ply = 1; bstline[ply] > 0; ply++)
232 algbr((short) bstline[ply] >> 8,
233 (short) bstline[ply] & 0xFF, false);
234 printw("%5s ", mvstr[0]);
249 ShowScore(short score)
252 printw(CP[104], score);
258 Curses_ShowSidetoMove(void)
261 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
267 Curses_ShowStage(void)
270 printw("Stage= %2d%c B= %2d W= %2d",
271 stage, flag.tsume?'T':' ', balance[black], balance[white]);
276 /****************************************
277 * End of trivial output routines.
278 ****************************************/
282 Curses_Initialize(void)
284 signal(SIGINT, Curses_Die);
285 signal(SIGQUIT, Curses_Die);
292 Curses_ExitShogi(void)
313 signal(SIGINT, SIG_IGN);
314 signal(SIGQUIT, SIG_IGN);
316 Curses_ShowMessage(CP[31]); /* Abort? */
317 FLUSH_SCANW("%s", s);
319 if (strcmp(s, CP[210]) == 0) /* yes */
322 signal(SIGINT, Curses_Die);
323 signal(SIGQUIT, Curses_Die);
329 Curses_TerminateSearch(int sig)
331 signal(SIGINT, SIG_IGN);
332 signal(SIGQUIT, SIG_IGN);
335 flag.musttimeout = true;
337 Curses_ShowMessage("Terminate Search");
338 flag.bothsides = false;
339 signal(SIGINT, Curses_Die);
340 signal(SIGQUIT, Curses_Die);
348 Curses_ClearScreen();
349 /* printw("GNU Shogi ??p? command summary\n"); */
350 printw(CP[40], version, patchlevel);
351 printw("-------------------------------"
352 "---------------------------------\n");
353 /* printw("7g7f move from 7g to 7f quit
356 /* printw("S6h move silver to 6h beep
357 * turn %s\n", (flag.beep) ? "off" : "on"); */
358 printw(CP[86], (flag.beep) ? CP[92] : CP[93]);
359 /* printw("2d2c+ move to 2c and promote\n"); */
360 printw(CP[128], (flag.material) ? CP[92] : CP[93]);
361 /* printw("P*5e drop a pawn to 5e easy
362 * turn %s\n", (flag.easy) ? "off" : "on"); */
363 printw(CP[173], (flag.easy) ? CP[92] : CP[93]);
365 * turn %s\n", (flag.hash) ? "off" : "on"); */
366 printw(CP[174], (flag.hash) ? CP[92] : CP[93]);
367 /* printw("bd redraw board reverse
368 * board display\n"); */
370 /* printw("list game to shogi.lst book
371 * turn %s used %d of %d\n", (Book) ? "off" : "on", book
373 printw(CP[170], (Book) ? CP[92] : CP[93], bookcount, BOOKSIZE);
374 /* printw("undo undo last ply remove
375 * take back a move\n"); */
377 /* printw("edit edit board force
378 * enter game moves\n"); */
380 /* printw("switch sides with computer both
381 * computer match\n"); */
383 /* printw("black computer plays black white
384 * computer plays white\n"); */
386 /* printw("depth set search depth clock
387 * set time control\n"); */
389 /* printw("hint suggest a move post
390 * turn %s principle variation\n", (flag.post) ? "off" :
392 printw(CP[177], (flag.post) ? CP[92] : CP[93]);
393 /* printw("save game to file get
394 * game from file\n"); */
396 /* printw("random randomize play new
397 * start new game\n"); */
400 printw(CP[47], ColorStr[computer]);
402 printw(CP[97], ColorStr[opponent]);
404 printw(CP[79], MaxResponseTime/100);
406 printw(CP[59], (flag.easy) ? CP[93] : CP[92]);
408 printw(CP[231], (flag.tsume) ? CP[93] : CP[92]);
410 printw(CP[52], MaxSearchDepth);
412 printw(CP[100], (dither) ? CP[93] : CP[92]);
414 printw(CP[112], (flag.hash) ? CP[93] : CP[92]);
418 printw(CP[110], (TCflag) ? CP[93] : CP[92],
419 TimeControl.moves[black],
420 TimeControl.clock[black] / 100,
421 OperatorTime, MaxSearchDepth);
426 fflush(stdin); /* what is this supposed to do?? */
430 Curses_ClearScreen();
431 Curses_UpdateDisplay(0, 0, 1, 0);
435 static const short x0[2] = { 54, 2 };
436 static const short y0[2] = { 20, 4 };
440 * Set up a board position. Pieces are entered by typing the piece followed
441 * by the location. For example, N3f will place a knight on square 3f.
442 * P* will put a pawn to the captured pieces.
446 Curses_EditBoard(void)
452 flag.regularstart = true;
454 Curses_ClearScreen();
455 Curses_UpdateDisplay(0, 0, 1, 0);
469 printw(CP[60], ColorStr[a]); /* Editing %s */
472 FLUSH_SCANW("%s", s);
474 if (s[0] == CP[28][0]) /* # */
476 for (sq = 0; sq < NO_SQUARES; sq++)
478 board[sq] = no_piece;
487 if (s[0] == CP[136][0]) /* c */
492 for (i = NO_PIECES; i > no_piece; i--)
494 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
498 Captured[a][unpromoted[i]]++;
508 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
512 for (i = NO_PIECES; i > no_piece; i--)
514 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
524 color[sq] = ((board[sq] == no_piece) ? neutral : a);
528 while (s[0] != CP[29][0]); /* . */
530 for (sq = 0; sq < NO_SQUARES; sq++)
531 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
538 Curses_ClearScreen();
539 Curses_UpdateDisplay(0, 0, 1, 0);
549 for (side = black; side <= white; side++)
551 short x, y, piece, cside, k;
553 cside = flag.reverse ? (side ^ 1) : side;
558 for (piece = pawn; piece <= king; piece++)
562 if ((n = Captured[side][piece]))
565 printw("%i%c", n, pxx[piece]);
597 Curses_SearchStartStuff(short side)
601 signal(SIGINT, Curses_TerminateSearch);
602 signal(SIGQUIT, Curses_TerminateSearch);
604 for (i = 4; i < 14; i++) /* CHECKME */
614 Curses_OutputMove(void)
617 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
626 printw(CP[84], mvstr[0]); /* My move is %s */
635 if (root->flags & draw)
637 else if (root->score == -(SCORE_LIMIT + 999))
639 else if (root->score == SCORE_LIMIT + 998)
642 else if (root->score < -SCORE_LIMIT)
643 printw(CP[96], SCORE_LIMIT + 999 + root->score - 1);
644 else if (root->score > SCORE_LIMIT)
645 printw(CP[45], SCORE_LIMIT + 998 - root->score - 1);
646 #endif /* VERYBUGGY */
660 if (Tree[t].f || Tree[t].t)
668 ShowNodeCnt(NodeCnt);
670 printw(CP[81], t); /* Max Tree= */
674 Curses_ShowSidetoMove();
687 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
688 s = (short) ((dt - 6000 * (long) m) / 100);
692 m = (short) ((dt = et) / 6000);
693 s = (short) (et - 6000 * (long) m) / 100;
703 gotoXY(20, (flag.reverse) ? 2 : 23);
705 gotoXY(20, (flag.reverse) ? 23 : 2);
707 /* printw("%d:%02d %ld ", m, s, dt); */
708 printw("%d:%02d ", m, s);
711 ShowNodeCnt(NodeCnt);
724 if (color[sq] == neutral)
728 else if (flag.reverse ^ (color[sq] == black))
740 if (is_promoted[(int)piece])
743 y = pxx[unpromoted[(int)piece]];
751 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
752 printw("%c%c%c%c", l, p, y, r);
759 * Curses_ShowPostnValue(): must have called ExaminePosition() first
763 Curses_ShowPostnValue(short sq)
767 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
768 score = ScorePosition(color[sq]);
770 if (color[sq] != neutral)
771 #if defined SAVE_SVALUE
777 printw("%3d ", svalue[sq]);
789 Curses_ShowPostnValues(void)
793 ExaminePosition(opponent);
795 for (sq = 0; sq < NO_SQUARES; sq++)
796 Curses_ShowPostnValue(sq);
798 score = ScorePosition(opponent);
800 printw(CP[103], score,
801 mtl[computer], pscore[computer], GameType[computer],
802 mtl[opponent], pscore[opponent], GameType[opponent]);
810 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
824 for (j=0; j<NO_COLS; j++)
827 while (i <= 1 + 2*NO_ROWS)
834 z = NO_ROWS + 2 - ((i + 1) / 2);
836 printw(" %c |", ROW_NAME(z+1));
837 for (j=0; j<NO_COLS; j++)
842 if (i < 2 + 2*NO_ROWS)
845 for (j=0; j<NO_COLS; j++)
851 for (j=0; j<NO_COLS; j++)
854 gotoXY(3, 4 + 2*NO_ROWS);
862 for (sq = 0; sq < NO_SQUARES; sq++)
865 else /* not redraw */
873 if ((isspec & capture) || (isspec & dropmask) || redraw)
877 for (side = black; side <= white; side++)
879 short x, y, piece, cside, k;
880 cside = flag.reverse ? (side ^ 1) : side;
885 for (piece = pawn; piece <= king; piece++)
889 if ((n = Captured[side][piece]))
892 printw("%i%c", n, pxx[piece]);
894 if (cside == black) y--; else y++;
924 Curses_ChangeAlphaWindow(void)
926 Curses_ShowMessage(CP[114]);
927 FLUSH_SCANW("%hd", &WAwindow);
928 Curses_ShowMessage(CP[34]);
929 FLUSH_SCANW("%hd", &BAwindow);
935 Curses_ChangeBetaWindow(void)
937 Curses_ShowMessage(CP[115]);
938 FLUSH_SCANW("%hd", &WBwindow);
939 Curses_ShowMessage(CP[35]);
940 FLUSH_SCANW("%hd", &BBwindow);
946 Curses_GiveHint(void)
952 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
953 strcpy(s, CP[198]); /* try */
955 Curses_ShowMessage(s);
959 Curses_ShowMessage(CP[223]);
966 Curses_ChangeSearchDepth(void)
968 Curses_ShowMessage(CP[150]);
969 FLUSH_SCANW("%hd", &MaxSearchDepth);
970 TCflag = !(MaxSearchDepth > 0);
975 Curses_ChangeHashDepth(void)
977 Curses_ShowMessage(CP[163]);
978 FLUSH_SCANW("%hd", &HashDepth);
979 Curses_ShowMessage(CP[82]);
980 FLUSH_SCANW("%hd", &HashMoveLimit);
985 Curses_SetContempt(void)
987 Curses_ShowMessage(CP[142]);
988 FLUSH_SCANW("%hd", &contempt);
994 Curses_ChangeXwindow(void)
996 Curses_ShowMessage(CP[208]);
997 FLUSH_SCANW("%hd", &xwndw);
1003 Curses_SelectLevel(char *sx)
1007 Curses_ClearScreen();
1009 printw(CP[41], version, patchlevel);
1039 FLUSH_SCANW("%d", &item);
1086 flag.onemove = true;
1092 flag.onemove = true;
1098 flag.onemove = true;
1102 TCflag = (TCmoves > 0);
1104 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1107 Curses_ClearScreen();
1108 Curses_UpdateDisplay(0, 0, 1, 0);
1114 Curses_DoDebug(void)
1116 short c, p, sq, tp, tc, tsq, score;
1119 ExaminePosition(opponent);
1120 Curses_ShowMessage(CP[65]);
1121 FLUSH_SCANW("%s", s);
1124 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* b B */
1127 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* w W */
1130 for (p = king; p > no_piece; p--)
1132 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1136 for (sq = 0; sq < NO_SQUARES; sq++)
1142 tsq = PieceList[c][1];
1143 PieceList[c][1] = sq;
1144 Curses_ShowPostnValue(sq);
1145 PieceList[c][1] = tsq;
1150 score = ScorePosition(opponent);
1152 printw(CP[103], score,
1153 mtl[computer], pscore[computer], GameType[computer],
1154 mtl[opponent], pscore[opponent], GameType[opponent]);
1162 Curses_DoTable(short table[NO_SQUARES])
1165 ExaminePosition(opponent);
1167 for (sq = 0; sq < NO_SQUARES; sq++)
1169 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1170 printw("%3d ", table[sq]);