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 * ----------------------------------------------------------------------
33 /* request *snprintf prototypes*/
34 #define _POSIX_C_SOURCE 200112L
40 #include <sys/param.h>
41 #include <sys/types.h>
46 #include "cursesdsp.h"
53 /* Definition of FIONREAD */
54 #include <sys/filio.h>
58 /* Definition of errno(). */
62 #define FLUSH_SCANW fflush(stdout), scanw
68 #define VIR_C(s) ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
69 #define VIR_R(s) ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
71 unsigned short MV[MAXDEPTH];
75 /* Forward declarations. */
76 /* FIXME: change this name, puh-leeze! */
78 static void UpdateCatched(void);
79 static void DrawPiece(short sq);
80 static void ShowScore(short score);
82 /****************************************
83 * Trivial output functions.
84 ****************************************/
95 Curses_ClearScreen(void)
103 gotoXY(short x, short y)
118 Curses_ShowCurrentMove(short pnt, short f, short t)
122 printw("(%2d) %5s ", pnt, mvstr[0]);
127 Curses_ShowDepth(char ch)
130 printw("Depth= %d%c ", Sdepth, ch);
136 Curses_ShowGameType(void)
141 printw("%c vs. %c", GameType[black], GameType[white]);
150 printw("GNU Shogi %s", PACKAGE_VERSION);
155 Curses_ShowLine(unsigned short *bstline)
161 Curses_ShowMessage(char *s)
170 Curses_AlwaysShowMessage(const char *format, va_list ap)
172 static char buffer[60];
173 vsnprintf(buffer, sizeof(buffer), format, ap);
174 Curses_ShowMessage(buffer);
179 Curses_Printf(const char *format, va_list ap)
181 static char buffer[60];
182 vsnprintf(buffer, sizeof(buffer), format, ap);
183 printw("%s", buffer);
188 Curses_doRequestInputString(const char* fmt, char* buffer)
190 FLUSH_SCANW(fmt, buffer);
195 Curses_GetString(char* sx)
198 return (getstr(sx) == ERR);
203 Curses_ShowNodeCnt(long NodeCnt)
206 /* printw("Nodes = %8ld, Nodes/Sec = %5ld", NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
207 printw("n = %ld n/s = %ld",
208 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
214 Curses_ShowPatternCount(short side, short n)
218 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
231 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
232 printw("%s", (computer == white) ? "Computer" : "Human ");
233 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
234 printw("%s", (computer == black) ? "Computer" : "Human ");
239 Curses_ShowPrompt(void)
241 Curses_ShowSidetoMove();
243 printw("Your move is? ");
249 Curses_ShowResponseTime(void)
255 printw("%ld, %d, %ld, %ld, %ld, %d",
256 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
263 Curses_ShowResults(short score, unsigned short *bstline, char ch)
265 unsigned char d, ply;
269 Curses_ShowDepth(ch);
273 for (ply = 1; bstline[ply] > 0; ply++)
281 algbr((short) bstline[ply] >> 8,
282 (short) bstline[ply] & 0xFF, false);
283 printw("%5s ", mvstr[0]);
298 ShowScore(short score)
301 printw("Score= %d", score);
307 Curses_ShowSidetoMove(void)
310 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
316 Curses_ShowStage(void)
319 printw("Stage= %2d%c B= %2d W= %2d",
320 stage, flag.tsume?'T':' ', balance[black], balance[white]);
324 /****************************************
325 * End of trivial output routines.
326 ****************************************/
329 Curses_Initialize(void)
331 signal(SIGINT, Curses_Die);
332 signal(SIGQUIT, Curses_Die);
339 Curses_ExitShogi(void)
359 signal(SIGINT, SIG_IGN);
360 signal(SIGQUIT, SIG_IGN);
362 Curses_ShowMessage("Abort? ");
363 FLUSH_SCANW("%s", s);
365 if (strcmp(s, "yes") == 0)
368 signal(SIGINT, Curses_Die);
369 signal(SIGQUIT, Curses_Die);
374 Curses_TerminateSearch(int sig)
376 signal(SIGINT, SIG_IGN);
377 signal(SIGQUIT, SIG_IGN);
380 flag.musttimeout = true;
382 Curses_ShowMessage("Terminate Search");
383 flag.bothsides = false;
384 signal(SIGINT, Curses_Die);
385 signal(SIGQUIT, Curses_Die);
392 Curses_ClearScreen();
393 printw("GNU Shogi %s command summary\n", PACKAGE_VERSION);
394 printw("-------------------------------"
395 "---------------------------------\n");
396 printw("7g7f move from 7g to 7f quit Exit Shogi\n");
397 printw("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
398 printw("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
399 printw("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
400 printw("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
401 printw("bd redraw board reverse board display\n");
402 printw("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, BOOKSIZE);
403 printw("undo undo last ply remove take back a move\n");
404 printw("edit edit board force toggle manual move mode\n");
405 printw("switch sides with computer both computer match\n");
406 printw("black computer plays black white computer plays white\n");
407 printw("depth set search depth clock set time control\n");
408 printw("post principle variation hint suggest a move\n", (flag.post) ? "OFF" : "ON");
409 printw("save game to file get game from file\n");
410 printw("random randomize play new start new game\n");
412 printw("Computer: %s", ColorStr[computer]);
414 printw("Opponent: %s", ColorStr[opponent]);
416 printw("Level: %ld", MaxResponseTime/100);
418 printw("Easy mode: %s", (flag.easy) ? "ON" : "OFF");
420 printw("Tsume: %s", (flag.tsume) ? "ON" : "OFF");
422 printw("Depth: %d", MaxSearchDepth);
424 printw("Random: %s", (dither) ? "ON" : "OFF");
426 printw("Transposition table: %s", (flag.hash) ? "ON" : "OFF");
428 printw("Hit <RET> to return: ");
430 printw("Time Control %s %d moves %d sec %d add %d depth\n", (TCflag) ? "ON" : "OFF",
431 TimeControl.moves[black],
432 TimeControl.clock[black] / 100,
433 OperatorTime, MaxSearchDepth);
438 fflush(stdin); /* what is this supposed to do?? */
442 Curses_ClearScreen();
443 Curses_UpdateDisplay(0, 0, 1, 0);
447 static const short x0[2] = { 54, 2 };
448 static const short y0[2] = { 20, 4 };
452 * Set up a board position. Pieces are entered by typing the piece followed
453 * by the location. For example, N3f will place a knight on square 3f.
454 * P* will put a pawn to the captured pieces.
458 Curses_EditBoard(void)
464 flag.regularstart = true;
466 Curses_ClearScreen();
467 Curses_UpdateDisplay(0, 0, 1, 0);
469 printw(". Exit to main\n");
471 printw("# Clear board\n");
473 printw("c Change sides\n");
475 printw("Enter piece & location: ");
481 printw("Editing: %s", ColorStr[a]);
484 FLUSH_SCANW("%s", s);
488 for (sq = 0; sq < NO_SQUARES; sq++)
490 board[sq] = no_piece;
504 for (i = NO_PIECES; i > no_piece; i--)
506 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
510 Captured[a][unpromoted[i]]++;
520 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
524 for (i = NO_PIECES; i > no_piece; i--)
526 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
536 color[sq] = ((board[sq] == no_piece) ? neutral : a);
542 for (sq = 0; sq < NO_SQUARES; sq++)
543 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
550 Curses_ClearScreen();
551 Curses_UpdateDisplay(0, 0, 1, 0);
560 for (side = black; side <= white; side++)
562 short x, y, piece, cside, k;
564 cside = flag.reverse ? (side ^ 1) : side;
569 for (piece = pawn; piece <= king; piece++)
573 if ((n = Captured[side][piece]))
576 printw("%i%c", n, pxx[piece]);
607 Curses_SearchStartStuff(short side)
611 signal(SIGINT, Curses_TerminateSearch);
612 signal(SIGQUIT, Curses_TerminateSearch);
614 for (i = 4; i < 14; i++) /* CHECKME */
623 Curses_OutputMove(void)
626 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
631 printw("Illegal position.");
635 printw("My move is: %5s", mvstr[0]);
644 if (root->flags & draw)
645 printw("Drawn game!");
646 else if (root->score == -(SCORE_LIMIT + 999))
647 printw("Opponent mates!");
648 else if (root->score == SCORE_LIMIT + 998)
649 printw("Computer mates!");
651 else if (root->score < -SCORE_LIMIT)
652 printw("Opp: mate in %d!", SCORE_LIMIT + 999 + root->score - 1);
653 else if (root->score > SCORE_LIMIT)
654 printw("Comp: mate in %d!", SCORE_LIMIT + 998 - root->score - 1);
655 #endif /* VERYBUGGY */
669 if (Tree[t].f || Tree[t].t)
677 ShowNodeCnt(NodeCnt);
679 printw("Max Tree = %5d", t);
683 Curses_ShowSidetoMove();
688 Curses_UpdateClocks(void)
695 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
696 s = (short) ((dt - 6000 * (long) m) / 100);
700 m = (short) ((dt = et) / 6000);
701 s = (short) (et - 6000 * (long) m) / 100;
711 gotoXY(20, (flag.reverse) ? 2 : 23);
713 gotoXY(20, (flag.reverse) ? 23 : 2);
715 /* printw("%d:%02d %ld ", m, s, dt); */
716 printw("%d:%02d ", m, s);
719 ShowNodeCnt(NodeCnt);
731 if (color[sq] == neutral)
735 else if (flag.reverse ^ (color[sq] == black))
747 if (is_promoted[(int)piece])
750 y = pxx[unpromoted[(int)piece]];
758 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
759 printw("%c%c%c%c", l, p, y, r);
764 * Curses_ShowPostnValue(): must have called ExaminePosition() first
767 Curses_ShowPostnValue(short sq)
771 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
772 score = ScorePosition(color[sq]);
774 if (color[sq] != neutral)
775 #if defined SAVE_SVALUE
781 printw("%3d ", svalue[sq]);
792 Curses_ShowPostnValues(void)
796 ExaminePosition(opponent);
798 for (sq = 0; sq < NO_SQUARES; sq++)
799 Curses_ShowPostnValue(sq);
801 score = ScorePosition(opponent);
803 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
804 mtl[computer], pscore[computer], GameType[computer],
805 mtl[opponent], pscore[opponent], GameType[opponent]);
812 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
826 for (j=0; j<NO_COLS; j++)
829 while (i <= 1 + 2*NO_ROWS)
836 z = NO_ROWS + 2 - ((i + 1) / 2);
838 printw(" %c |", ROW_NAME(z+1));
839 for (j=0; j<NO_COLS; j++)
844 if (i < 2 + 2*NO_ROWS)
847 for (j=0; j<NO_COLS; j++)
853 for (j=0; j<NO_COLS; j++)
856 gotoXY(3, 4 + 2*NO_ROWS);
861 printw(" 1 2 3 4 5 6 7 8 9");
863 printw(" 9 8 7 6 5 4 3 2 1");
866 printw(" 1 2 3 4 5");
868 printw(" 1 2 3 4 5");
871 for (sq = 0; sq < NO_SQUARES; sq++)
874 else /* not redraw */
882 if ((isspec & capture) || (isspec & dropmask) || redraw)
886 for (side = black; side <= white; side++)
888 short x, y, piece, cside, k;
889 cside = flag.reverse ? (side ^ 1) : side;
894 for (piece = pawn; piece <= king; piece++)
898 if ((n = Captured[side][piece]))
901 printw("%i%c", n, pxx[piece]);
903 if (cside == black) y--; else y++;
930 Curses_ChangeAlphaWindow(void)
932 Curses_ShowMessage("WAwindow = ");
933 FLUSH_SCANW("%hd", &WAwindow);
934 Curses_ShowMessage("BAwindow = ");
935 FLUSH_SCANW("%hd", &BAwindow);
940 Curses_ChangeBetaWindow(void)
942 Curses_ShowMessage("WBwindow = ");
943 FLUSH_SCANW("%hd", &WBwindow);
944 Curses_ShowMessage("BBwindow = ");
945 FLUSH_SCANW("%hd", &BBwindow);
950 Curses_GiveHint(void)
956 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
959 Curses_ShowMessage(s);
963 Curses_ShowMessage("I have no idea.\n");
969 Curses_ChangeSearchDepth(void)
971 Curses_ShowMessage("depth = ");
972 FLUSH_SCANW("%hd", &MaxSearchDepth);
973 TCflag = !(MaxSearchDepth > 0);
978 Curses_ChangeHashDepth(void)
980 Curses_ShowMessage("hashdepth = ");
981 FLUSH_SCANW("%hd", &HashDepth);
982 Curses_ShowMessage("MoveLimit = ");
983 FLUSH_SCANW("%hd", &HashMoveLimit);
988 Curses_SetContempt(void)
990 Curses_ShowMessage("contempt = ");
991 FLUSH_SCANW("%hd", &contempt);
996 Curses_ChangeXwindow(void)
998 Curses_ShowMessage("xwndw= ");
999 FLUSH_SCANW("%hd", &xwndw);
1004 Curses_SelectLevel(char *sx)
1008 Curses_ClearScreen();
1010 printw("GNU Shogi %s", PACKAGE_VERSION);
1012 printw(" 1. 40 moves in 5 minutes");
1014 printw(" 2. 40 moves in 15 minutes");
1016 printw(" 3. 40 moves in 30 minutes");
1018 printw(" 4. all moves in 15 minutes");
1020 printw(" 5. all moves in 30 minutes");
1022 printw(" 6. all moves in 15 minutes, 30 seconds fischer clock");
1024 printw(" 7. all moves in 30 minutes, 30 seconds fischer clock");
1026 printw(" 8. 1 move in 1 minute");
1028 printw(" 9. 1 move in 15 minutes");
1030 printw("10. 1 move in 30 minutes");
1038 printw("Enter Level: ");
1040 FLUSH_SCANW("%d", &item);
1087 flag.onemove = true;
1093 flag.onemove = true;
1099 flag.onemove = true;
1103 TCflag = (TCmoves > 0);
1105 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1108 Curses_ClearScreen();
1109 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("Enter piece: ");
1121 FLUSH_SCANW("%s", s);
1124 if ((s[0] == 'b') || (s[0] == 'B'))
1127 if ((s[0] == 'w') || (s[0] == '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("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
1153 mtl[computer], pscore[computer], GameType[computer],
1154 mtl[opponent], pscore[opponent], GameType[opponent]);
1161 Curses_DoTable(short table[NO_SQUARES])
1164 ExaminePosition(opponent);
1166 for (sq = 0; sq < NO_SQUARES; sq++)
1168 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1169 printw("%3d ", table[sq]);
1175 Curses_PollForInput(void)
1180 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
1184 "You probably have a non-ANSI <ioctl.h>; "
1185 "see README. %d %d %x\n",
1186 i, errno, FIONREAD);
1195 flag.bothsides = false;
1201 Curses_SetupBoard(void)
1203 Curses_ShowMessage("'setup' command is not supported in Cursesmode");