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
41 #include <sys/param.h>
42 #include <sys/types.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 /****************************************
76 * forward declarations
77 ****************************************/
79 /* FIXME: change this name, puh-leeze! */
80 static void UpdateCatched(void);
81 static void DrawPiece(short sq);
82 static void ShowScore(short score);
83 void Curses_UpdateDisplay(short f, short t, short redraw, short isspec);
84 void Curses_Die(int sig);
85 void Curses_ShowSidetoMove(void);
87 /****************************************
88 * Trivial output functions.
89 ****************************************/
100 Curses_ClearScreen(void)
108 gotoXY(short x, short y)
115 Curses_ShowCurrentMove(short pnt, short f, short t)
119 printw("(%2d) %5s ", pnt, mvstr[0]);
124 Curses_ShowDepth(char ch)
127 printw("Depth= %d%c ", Sdepth, ch);
133 Curses_ShowGameType(void)
138 printw("%c vs. %c", GameType[black], GameType[white]);
147 printw("GNU Shogi %s", PACKAGE_VERSION);
152 Curses_ShowLine(unsigned short *bstline)
158 Curses_ShowMessage(char *s)
167 Curses_AlwaysShowMessage(const char *format, ...)
169 static char buffer[60];
171 va_start(ap, format);
172 vsnprintf(buffer, sizeof(buffer), format, ap);
173 Curses_ShowMessage(buffer);
179 Curses_Printf(const char *format, ...)
181 static char buffer[60];
183 va_start(ap, format);
184 vsnprintf(buffer, sizeof(buffer), format, ap);
185 printw("%s", buffer);
191 Curses_doRequestInputString(const char* fmt, char* buffer)
193 FLUSH_SCANW(fmt, buffer);
198 Curses_GetString(char* sx)
201 return (getstr(sx) == ERR);
206 Curses_ShowNodeCnt(long NodeCnt)
209 /* printw("Nodes = %8ld, Nodes/Sec = %5ld", NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
210 printw("n = %ld n/s = %ld",
211 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
217 Curses_ShowPatternCount(short side, short n)
221 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
234 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
235 printw("%s", (computer == white) ? "Computer" : "Human ");
236 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
237 printw("%s", (computer == black) ? "Computer" : "Human ");
242 Curses_ShowPrompt(void)
244 Curses_ShowSidetoMove();
246 printw("Your move is? ");
252 Curses_ShowResponseTime(void)
258 printw("%ld, %d, %ld, %ld, %ld, %d",
259 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
266 Curses_ShowResults(short score, unsigned short *bstline, char ch)
268 unsigned char d, ply;
272 Curses_ShowDepth(ch);
276 for (ply = 1; bstline[ply] > 0; ply++)
284 algbr((short) bstline[ply] >> 8,
285 (short) bstline[ply] & 0xFF, false);
286 printw("%5s ", mvstr[0]);
301 ShowScore(short score)
304 printw("Score= %d", score);
310 Curses_ShowSidetoMove(void)
313 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
319 Curses_ShowStage(void)
322 printw("Stage= %2d%c B= %2d W= %2d",
323 stage, flag.tsume?'T':' ', balance[black], balance[white]);
327 /****************************************
328 * End of trivial output routines.
329 ****************************************/
332 Curses_Initialize(void)
334 signal(SIGINT, Curses_Die);
335 signal(SIGQUIT, Curses_Die);
342 Curses_ExitShogi(void)
362 signal(SIGINT, SIG_IGN);
363 signal(SIGQUIT, SIG_IGN);
365 Curses_ShowMessage("Abort? ");
366 FLUSH_SCANW("%s", s);
368 if (strcmp(s, "yes") == 0)
371 signal(SIGINT, Curses_Die);
372 signal(SIGQUIT, Curses_Die);
377 Curses_TerminateSearch(int sig)
379 signal(SIGINT, SIG_IGN);
380 signal(SIGQUIT, SIG_IGN);
383 flag.musttimeout = true;
385 Curses_ShowMessage("Terminate Search");
386 flag.bothsides = false;
387 signal(SIGINT, Curses_Die);
388 signal(SIGQUIT, Curses_Die);
395 Curses_ClearScreen();
396 printw("GNU Shogi %s command summary\n", PACKAGE_VERSION);
397 printw("-------------------------------"
398 "---------------------------------\n");
399 printw("7g7f move from 7g to 7f quit Exit Shogi\n");
400 printw("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
401 printw("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
402 printw("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
403 printw("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
404 printw("bd redraw board reverse board display\n");
405 printw("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, BOOKSIZE);
406 printw("undo undo last ply remove take back a move\n");
407 printw("edit edit board force toggle manual move mode\n");
408 printw("switch sides with computer both computer match\n");
409 printw("black computer plays black white computer plays white\n");
410 printw("depth set search depth clock set time control\n");
411 printw("post principle variation hint suggest a move\n", (flag.post) ? "OFF" : "ON");
412 printw("save game to file get game from file\n");
413 printw("random randomize play new start new game\n");
415 printw("Computer: %s", ColorStr[computer]);
417 printw("Opponent: %s", ColorStr[opponent]);
419 printw("Level: %ld", MaxResponseTime/100);
421 printw("Easy mode: %s", (flag.easy) ? "ON" : "OFF");
423 printw("Tsume: %s", (flag.tsume) ? "ON" : "OFF");
425 printw("Depth: %d", MaxSearchDepth);
427 printw("Random: %s", (dither) ? "ON" : "OFF");
429 printw("Transposition table: %s", (flag.hash) ? "ON" : "OFF");
431 printw("Hit <RET> to return: ");
433 printw("Time Control %s %d moves %d sec %d add %d depth\n", (TCflag) ? "ON" : "OFF",
434 TimeControl.moves[black],
435 TimeControl.clock[black] / 100,
436 OperatorTime, MaxSearchDepth);
441 fflush(stdin); /* what is this supposed to do?? */
445 Curses_ClearScreen();
446 Curses_UpdateDisplay(0, 0, 1, 0);
450 static const short x0[2] = { 54, 2 };
451 static const short y0[2] = { 20, 4 };
455 * Set up a board position. Pieces are entered by typing the piece followed
456 * by the location. For example, N3f will place a knight on square 3f.
457 * P* will put a pawn to the captured pieces.
461 Curses_EditBoard(void)
467 flag.regularstart = true;
469 Curses_ClearScreen();
470 Curses_UpdateDisplay(0, 0, 1, 0);
472 printw(". Exit to main\n");
474 printw("# Clear board\n");
476 printw("c Change sides\n");
478 printw("Enter piece & location: ");
484 printw("Editing: %s", ColorStr[a]);
487 FLUSH_SCANW("%s", s);
491 for (sq = 0; sq < NO_SQUARES; sq++)
493 board[sq] = no_piece;
507 for (i = NO_PIECES; i > no_piece; i--)
509 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
513 Captured[a][unpromoted[i]]++;
523 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
527 for (i = NO_PIECES; i > no_piece; i--)
529 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
539 color[sq] = ((board[sq] == no_piece) ? neutral : a);
545 for (sq = 0; sq < NO_SQUARES; sq++)
546 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
553 Curses_ClearScreen();
554 Curses_UpdateDisplay(0, 0, 1, 0);
563 for (side = black; side <= white; side++)
565 short x, y, piece, cside, k;
567 cside = flag.reverse ? (side ^ 1) : side;
572 for (piece = pawn; piece <= king; piece++)
576 if ((n = Captured[side][piece]))
579 printw("%i%c", n, pxx[piece]);
610 Curses_SearchStartStuff(short side)
614 signal(SIGINT, Curses_TerminateSearch);
615 signal(SIGQUIT, Curses_TerminateSearch);
617 for (i = 4; i < 14; i++) /* CHECKME */
626 Curses_OutputMove(void)
629 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
634 printw("Illegal position.");
638 printw("My move is: %5s", mvstr[0]);
647 if (root->flags & draw)
648 printw("Drawn game!");
649 else if (root->score == -(SCORE_LIMIT + 999))
650 printw("Opponent mates!");
651 else if (root->score == SCORE_LIMIT + 998)
652 printw("Computer mates!");
654 else if (root->score < -SCORE_LIMIT)
655 printw("Opp: mate in %d!", SCORE_LIMIT + 999 + root->score - 1);
656 else if (root->score > SCORE_LIMIT)
657 printw("Comp: mate in %d!", SCORE_LIMIT + 998 - root->score - 1);
658 #endif /* VERYBUGGY */
672 if (Tree[t].f || Tree[t].t)
680 Curses_ShowNodeCnt(NodeCnt);
682 printw("Max Tree = %5d", t);
686 Curses_ShowSidetoMove();
691 Curses_UpdateClocks(void)
698 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
699 s = (short) ((dt - 6000 * (long) m) / 100);
703 m = (short) ((dt = et) / 6000);
704 s = (short) (et - 6000 * (long) m) / 100;
714 gotoXY(20, (flag.reverse) ? 2 : 23);
716 gotoXY(20, (flag.reverse) ? 23 : 2);
718 /* printw("%d:%02d %ld ", m, s, dt); */
719 printw("%d:%02d ", m, s);
722 Curses_ShowNodeCnt(NodeCnt);
734 if (color[sq] == neutral)
738 else if (flag.reverse ^ (color[sq] == black))
750 if (is_promoted[(int)piece])
753 y = pxx[unpromoted[(int)piece]];
761 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
762 printw("%c%c%c%c", l, p, y, r);
767 * Curses_ShowPostnValue(): must have called ExaminePosition() first
770 Curses_ShowPostnValue(short sq)
772 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
773 (void) ScorePosition(color[sq]);
775 if (color[sq] != neutral)
776 #if defined SAVE_SVALUE
782 printw("%3d ", svalue[sq]);
793 Curses_ShowPostnValues(void)
797 ExaminePosition(opponent);
799 for (sq = 0; sq < NO_SQUARES; sq++)
800 Curses_ShowPostnValue(sq);
802 score = ScorePosition(opponent);
804 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
805 mtl[computer], pscore[computer], GameType[computer],
806 mtl[opponent], pscore[opponent], GameType[opponent]);
813 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
827 for (j=0; j<NO_COLS; j++)
830 while (i <= 1 + 2*NO_ROWS)
837 z = NO_ROWS + 2 - ((i + 1) / 2);
839 printw(" %c |", ROW_NAME(z+1));
840 for (j=0; j<NO_COLS; j++)
845 if (i < 2 + 2*NO_ROWS)
848 for (j=0; j<NO_COLS; j++)
854 for (j=0; j<NO_COLS; j++)
857 gotoXY(3, 4 + 2*NO_ROWS);
862 printw(" 1 2 3 4 5 6 7 8 9");
864 printw(" 9 8 7 6 5 4 3 2 1");
867 printw(" 1 2 3 4 5");
869 printw(" 1 2 3 4 5");
872 for (sq = 0; sq < NO_SQUARES; sq++)
875 else /* not redraw */
883 if ((isspec & capture) || (isspec & dropmask) || redraw)
887 for (side = black; side <= white; side++)
889 short x, y, piece, cside, k;
890 cside = flag.reverse ? (side ^ 1) : side;
895 for (piece = pawn; piece <= king; piece++)
899 if ((n = Captured[side][piece]))
902 printw("%i%c", n, pxx[piece]);
904 if (cside == black) y--; else y++;
931 Curses_ChangeAlphaWindow(void)
933 Curses_ShowMessage("WAwindow = ");
934 FLUSH_SCANW("%hd", &WAwindow);
935 Curses_ShowMessage("BAwindow = ");
936 FLUSH_SCANW("%hd", &BAwindow);
941 Curses_ChangeBetaWindow(void)
943 Curses_ShowMessage("WBwindow = ");
944 FLUSH_SCANW("%hd", &WBwindow);
945 Curses_ShowMessage("BBwindow = ");
946 FLUSH_SCANW("%hd", &BBwindow);
951 Curses_GiveHint(void)
957 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
960 Curses_ShowMessage(s);
964 Curses_ShowMessage("I have no idea.\n");
970 Curses_ChangeSearchDepth(char* sx)
972 Curses_ShowMessage("depth = ");
973 FLUSH_SCANW("%hd", &MaxSearchDepth);
974 TCflag = !(MaxSearchDepth > 0);
979 Curses_ChangeHashDepth(void)
981 Curses_ShowMessage("hashdepth = ");
982 FLUSH_SCANW("%hd", &HashDepth);
983 Curses_ShowMessage("MoveLimit = ");
984 FLUSH_SCANW("%hd", &HashMoveLimit);
989 Curses_SetContempt(void)
991 Curses_ShowMessage("contempt = ");
992 FLUSH_SCANW("%hd", &contempt);
997 Curses_ChangeXwindow(void)
999 Curses_ShowMessage("xwndw= ");
1000 FLUSH_SCANW("%hd", &xwndw);
1005 Curses_SelectLevel(char *sx)
1009 Curses_ClearScreen();
1011 printw("GNU Shogi %s", PACKAGE_VERSION);
1013 printw(" 1. 40 moves in 5 minutes");
1015 printw(" 2. 40 moves in 15 minutes");
1017 printw(" 3. 40 moves in 30 minutes");
1019 printw(" 4. all moves in 15 minutes");
1021 printw(" 5. all moves in 30 minutes");
1023 printw(" 6. all moves in 15 minutes, 30 seconds fischer clock");
1025 printw(" 7. all moves in 30 minutes, 30 seconds fischer clock");
1027 printw(" 8. 1 move in 1 minute");
1029 printw(" 9. 1 move in 15 minutes");
1031 printw("10. 1 move in 30 minutes");
1039 printw("Enter Level: ");
1041 FLUSH_SCANW("%d", &item);
1088 flag.onemove = true;
1094 flag.onemove = true;
1100 flag.onemove = true;
1104 TCflag = (TCmoves > 0);
1106 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1109 Curses_ClearScreen();
1110 Curses_UpdateDisplay(0, 0, 1, 0);
1115 Curses_DoDebug(void)
1117 short c, p, sq, tp, tc, tsq, score;
1120 ExaminePosition(opponent);
1121 Curses_ShowMessage("Enter piece: ");
1122 FLUSH_SCANW("%s", s);
1125 if ((s[0] == 'b') || (s[0] == 'B'))
1128 if ((s[0] == 'w') || (s[0] == 'W'))
1131 for (p = king; p > no_piece; p--)
1133 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1137 for (sq = 0; sq < NO_SQUARES; sq++)
1143 tsq = PieceList[c][1];
1144 PieceList[c][1] = sq;
1145 Curses_ShowPostnValue(sq);
1146 PieceList[c][1] = tsq;
1151 score = ScorePosition(opponent);
1153 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
1154 mtl[computer], pscore[computer], GameType[computer],
1155 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]);
1176 Curses_PollForInput(void)
1181 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
1185 "You probably have a non-ANSI <ioctl.h>; "
1186 "see README. %d %d %x\n",
1187 i, errno, FIONREAD);
1196 flag.bothsides = false;
1202 Curses_SetupBoard(void)
1204 Curses_ShowMessage("'setup' command is not supported in Cursesmode");
1208 struct display curses_display =
1210 .ChangeAlphaWindow = Curses_ChangeAlphaWindow,
1211 .ChangeBetaWindow = Curses_ChangeBetaWindow,
1212 .ChangeHashDepth = Curses_ChangeHashDepth,
1213 .ChangeSearchDepth = Curses_ChangeSearchDepth,
1214 .ChangeXwindow = Curses_ChangeXwindow,
1215 .ClearScreen = Curses_ClearScreen,
1216 .DoDebug = Curses_DoDebug,
1217 .DoTable = Curses_DoTable,
1218 .EditBoard = Curses_EditBoard,
1219 .ExitShogi = Curses_ExitShogi,
1220 .GiveHint = Curses_GiveHint,
1221 .Initialize = Curses_Initialize,
1222 .ShowNodeCnt = Curses_ShowNodeCnt,
1223 .OutputMove = Curses_OutputMove,
1224 .PollForInput = Curses_PollForInput,
1225 .SetContempt = Curses_SetContempt,
1226 .SearchStartStuff = Curses_SearchStartStuff,
1227 .SelectLevel = Curses_SelectLevel,
1228 .ShowCurrentMove = Curses_ShowCurrentMove,
1229 .ShowDepth = Curses_ShowDepth,
1230 .ShowGameType = Curses_ShowGameType,
1231 .ShowLine = Curses_ShowLine,
1232 .ShowMessage = Curses_ShowMessage,
1233 .AlwaysShowMessage = Curses_AlwaysShowMessage,
1234 .Printf = Curses_Printf,
1235 .doRequestInputString = Curses_doRequestInputString,
1236 .GetString = Curses_GetString,
1237 .SetupBoard = Curses_SetupBoard,
1238 .ShowPatternCount = Curses_ShowPatternCount,
1239 .ShowPostnValue = Curses_ShowPostnValue,
1240 .ShowPostnValues = Curses_ShowPostnValues,
1241 .ShowPrompt = Curses_ShowPrompt,
1242 .ShowResponseTime = Curses_ShowResponseTime,
1243 .ShowResults = Curses_ShowResults,
1244 .ShowSidetoMove = Curses_ShowSidetoMove,
1245 .ShowStage = Curses_ShowStage,
1246 .TerminateSearch = Curses_TerminateSearch,
1247 .UpdateClocks = Curses_UpdateClocks,
1248 .UpdateDisplay = Curses_UpdateDisplay,
1249 .help = Curses_help,