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
9 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
11 * GNU SHOGI is based on GNU CHESS
13 * Copyright (c) 1988, 1989, 1990 John Stanback
14 * Copyright (c) 1992 Free Software Foundation
16 * This file is part of GNU SHOGI.
18 * GNU Shogi is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 3 of the License,
21 * or (at your option) any later version.
23 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 * You should have received a copy of the GNU General Public License along
29 * with GNU Shogi; see the file COPYING. If not, see
30 * <http://www.gnu.org/licenses/>.
31 * ----------------------------------------------------------------------
34 /* request *snprintf prototypes*/
35 #define _POSIX_C_SOURCE 200112L
42 #include <sys/param.h>
43 #include <sys/types.h>
54 /* Definition of FIONREAD */
55 #include <sys/filio.h>
59 /* Definition of errno(). */
63 #define FLUSH_SCANW fflush(stdout), scanw
69 #define VIR_C(s) ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
70 #define VIR_R(s) ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
72 unsigned short MV[MAXDEPTH];
76 /****************************************
77 * forward declarations
78 ****************************************/
80 /* FIXME: change this name, puh-leeze! */
81 static void UpdateCatched(void);
82 static void DrawPiece(short sq);
83 static void ShowScore(short score);
84 void Curses_UpdateDisplay(short f, short t, short redraw, short isspec);
85 void Curses_Die(int sig);
86 void Curses_ShowSidetoMove(void);
88 /****************************************
89 * Trivial output functions.
90 ****************************************/
101 Curses_ClearScreen(void)
109 gotoXY(short x, short y)
116 Curses_ShowCurrentMove(short pnt, short f, short t)
120 printw("(%2d) %5s ", pnt, mvstr[0]);
125 Curses_ShowDepth(char ch)
128 printw("Depth= %d%c ", Sdepth, ch);
134 Curses_ShowGameType(void)
139 printw("%c vs. %c", GameType[black], GameType[white]);
148 printw("GNU Shogi %s", PACKAGE_VERSION);
153 Curses_ShowLine(unsigned short *bstline)
159 Curses_ShowMessage(char *s)
168 Curses_AlwaysShowMessage(const char *format, ...)
170 static char buffer[60];
172 va_start(ap, format);
173 vsnprintf(buffer, sizeof(buffer), format, ap);
174 Curses_ShowMessage(buffer);
180 Curses_Printf(const char *format, ...)
182 static char buffer[60];
184 va_start(ap, format);
185 vsnprintf(buffer, sizeof(buffer), format, ap);
186 printw("%s", buffer);
192 Curses_doRequestInputString(const char* fmt, char* buffer)
194 FLUSH_SCANW(fmt, buffer);
199 Curses_GetString(char* sx)
202 return (getstr(sx) == ERR);
207 Curses_ShowNodeCnt(long NodeCnt)
210 /* printw("Nodes = %8ld, Nodes/Sec = %5ld", NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
211 printw("n = %ld n/s = %ld",
212 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
218 Curses_ShowPatternCount(short side, short n)
222 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
235 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
236 printw("%s", (computer == white) ? "Computer" : "Human ");
237 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
238 printw("%s", (computer == black) ? "Computer" : "Human ");
243 Curses_ShowPrompt(void)
245 Curses_ShowSidetoMove();
247 printw("Your move is? ");
253 Curses_ShowResponseTime(void)
259 printw("%ld, %d, %ld, %ld, %ld, %d",
260 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
267 Curses_ShowResults(short score, unsigned short *bstline, char ch)
269 unsigned char d, ply;
273 Curses_ShowDepth(ch);
277 for (ply = 1; bstline[ply] > 0; ply++)
285 algbr((short) bstline[ply] >> 8,
286 (short) bstline[ply] & 0xFF, false);
287 printw("%5s ", mvstr[0]);
302 ShowScore(short score)
305 printw("Score= %d", score);
311 Curses_ShowSidetoMove(void)
314 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
320 Curses_ShowStage(void)
323 printw("Stage= %2d%c B= %2d W= %2d",
324 stage, flag.tsume?'T':' ', balance[black], balance[white]);
328 /****************************************
329 * End of trivial output routines.
330 ****************************************/
333 Curses_Initialize(void)
335 signal(SIGINT, Curses_Die);
336 signal(SIGQUIT, Curses_Die);
343 Curses_ExitShogi(void)
363 signal(SIGINT, SIG_IGN);
364 signal(SIGQUIT, SIG_IGN);
366 Curses_ShowMessage("Abort? ");
367 FLUSH_SCANW("%s", s);
369 if (strcmp(s, "yes") == 0)
372 signal(SIGINT, Curses_Die);
373 signal(SIGQUIT, Curses_Die);
378 Curses_TerminateSearch(int sig)
380 signal(SIGINT, SIG_IGN);
381 signal(SIGQUIT, SIG_IGN);
384 flag.musttimeout = true;
386 Curses_ShowMessage("Terminate Search");
387 flag.bothsides = false;
388 signal(SIGINT, Curses_Die);
389 signal(SIGQUIT, Curses_Die);
396 Curses_ClearScreen();
397 printw("GNU Shogi %s command summary\n", PACKAGE_VERSION);
398 printw("-------------------------------"
399 "---------------------------------\n");
400 printw("7g7f move from 7g to 7f quit Exit Shogi\n");
401 printw("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
402 printw("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
403 printw("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
404 printw("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
405 printw("bd redraw board reverse board display\n");
406 printw("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, BOOKSIZE);
407 printw("undo undo last ply remove take back a move\n");
408 printw("edit edit board force toggle manual move mode\n");
409 printw("switch sides with computer both computer match\n");
410 printw("black computer plays black white computer plays white\n");
411 printw("depth set search depth clock set time control\n");
412 printw("post principle variation hint suggest a move\n", (flag.post) ? "OFF" : "ON");
413 printw("save game to file get game from file\n");
414 printw("random randomize play new start new game\n");
416 printw("Computer: %s", ColorStr[computer]);
418 printw("Opponent: %s", ColorStr[opponent]);
420 printw("Level: %ld", MaxResponseTime/100);
422 printw("Easy mode: %s", (flag.easy) ? "ON" : "OFF");
424 printw("Tsume: %s", (flag.tsume) ? "ON" : "OFF");
426 printw("Depth: %d", MaxSearchDepth);
428 printw("Random: %s", (dither) ? "ON" : "OFF");
430 printw("Transposition table: %s", (flag.hash) ? "ON" : "OFF");
432 printw("Hit <RET> to return: ");
434 printw("Time Control %s %d moves %d sec %d add %d depth\n", (TCflag) ? "ON" : "OFF",
435 TimeControl.moves[black],
436 TimeControl.clock[black] / 100,
437 OperatorTime, MaxSearchDepth);
442 fflush(stdin); /* what is this supposed to do?? */
446 Curses_ClearScreen();
447 Curses_UpdateDisplay(0, 0, 1, 0);
451 static const short x0[2] = { 54, 2 };
452 static const short y0[2] = { 20, 4 };
456 * Set up a board position. Pieces are entered by typing the piece followed
457 * by the location. For example, N3f will place a knight on square 3f.
458 * P* will put a pawn to the captured pieces.
462 Curses_EditBoard(void)
468 flag.regularstart = true;
470 Curses_ClearScreen();
471 Curses_UpdateDisplay(0, 0, 1, 0);
473 printw(". Exit to main\n");
475 printw("# Clear board\n");
477 printw("c Change sides\n");
479 printw("Enter piece & location: ");
485 printw("Editing: %s", ColorStr[a]);
488 FLUSH_SCANW("%s", s);
492 for (sq = 0; sq < NO_SQUARES; sq++)
494 board[sq] = no_piece;
508 for (i = NO_PIECES; i > no_piece; i--)
510 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
514 Captured[a][unpromoted[i]]++;
524 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
528 for (i = NO_PIECES; i > no_piece; i--)
530 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
540 color[sq] = ((board[sq] == no_piece) ? neutral : a);
546 for (sq = 0; sq < NO_SQUARES; sq++)
547 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
554 Curses_ClearScreen();
555 Curses_UpdateDisplay(0, 0, 1, 0);
564 for (side = black; side <= white; side++)
566 short x, y, piece, cside, k;
568 cside = flag.reverse ? (side ^ 1) : side;
573 for (piece = pawn; piece <= king; piece++)
577 if ((n = Captured[side][piece]))
580 printw("%i%c", n, pxx[piece]);
611 Curses_SearchStartStuff(short side)
615 signal(SIGINT, Curses_TerminateSearch);
616 signal(SIGQUIT, Curses_TerminateSearch);
618 for (i = 4; i < 14; i++) /* CHECKME */
627 Curses_OutputMove(void)
630 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
635 printw("Illegal position.");
639 printw("My move is: %5s", mvstr[0]);
648 if (root->flags & draw)
649 printw("Drawn game!");
650 else if (root->score == -(SCORE_LIMIT + 999))
651 printw("Opponent mates!");
652 else if (root->score == SCORE_LIMIT + 998)
653 printw("Computer mates!");
655 else if (root->score < -SCORE_LIMIT)
656 printw("Opp: mate in %d!", SCORE_LIMIT + 999 + root->score - 1);
657 else if (root->score > SCORE_LIMIT)
658 printw("Comp: mate in %d!", SCORE_LIMIT + 998 - root->score - 1);
659 #endif /* VERYBUGGY */
673 if (Tree[t].f || Tree[t].t)
681 Curses_ShowNodeCnt(NodeCnt);
683 printw("Max Tree = %5d", t);
687 Curses_ShowSidetoMove();
692 Curses_UpdateClocks(void)
699 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
700 s = (short) ((dt - 6000 * (long) m) / 100);
704 m = (short) ((dt = et) / 6000);
705 s = (short) (et - 6000 * (long) m) / 100;
715 gotoXY(20, (flag.reverse) ? 2 : 23);
717 gotoXY(20, (flag.reverse) ? 23 : 2);
719 /* printw("%d:%02d %ld ", m, s, dt); */
720 printw("%d:%02d ", m, s);
723 Curses_ShowNodeCnt(NodeCnt);
735 if (color[sq] == neutral)
739 else if (flag.reverse ^ (color[sq] == black))
751 if (is_promoted[(int)piece])
754 y = pxx[unpromoted[(int)piece]];
762 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
763 printw("%c%c%c%c", l, p, y, r);
768 * Curses_ShowPostnValue(): must have called ExaminePosition() first
771 Curses_ShowPostnValue(short sq)
773 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
774 (void) ScorePosition(color[sq]);
776 if (color[sq] != neutral)
777 #if defined SAVE_SVALUE
783 printw("%3d ", svalue[sq]);
794 Curses_ShowPostnValues(void)
798 ExaminePosition(opponent);
800 for (sq = 0; sq < NO_SQUARES; sq++)
801 Curses_ShowPostnValue(sq);
803 score = ScorePosition(opponent);
805 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
806 mtl[computer], pscore[computer], GameType[computer],
807 mtl[opponent], pscore[opponent], GameType[opponent]);
814 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
828 for (j=0; j<NO_COLS; j++)
831 while (i <= 1 + 2*NO_ROWS)
838 z = NO_ROWS + 2 - ((i + 1) / 2);
840 printw(" %c |", ROW_NAME(z+1));
841 for (j=0; j<NO_COLS; j++)
846 if (i < 2 + 2*NO_ROWS)
849 for (j=0; j<NO_COLS; j++)
855 for (j=0; j<NO_COLS; j++)
858 gotoXY(3, 4 + 2*NO_ROWS);
863 printw(" 1 2 3 4 5 6 7 8 9");
865 printw(" 9 8 7 6 5 4 3 2 1");
868 printw(" 1 2 3 4 5");
870 printw(" 1 2 3 4 5");
873 for (sq = 0; sq < NO_SQUARES; sq++)
876 else /* not redraw */
884 if ((isspec & capture) || (isspec & dropmask) || redraw)
888 for (side = black; side <= white; side++)
890 short x, y, piece, cside, k;
891 cside = flag.reverse ? (side ^ 1) : side;
896 for (piece = pawn; piece <= king; piece++)
900 if ((n = Captured[side][piece]))
903 printw("%i%c", n, pxx[piece]);
905 if (cside == black) y--; else y++;
932 Curses_ChangeAlphaWindow(void)
934 Curses_ShowMessage("WAwindow = ");
935 FLUSH_SCANW("%hd", &WAwindow);
936 Curses_ShowMessage("BAwindow = ");
937 FLUSH_SCANW("%hd", &BAwindow);
942 Curses_ChangeBetaWindow(void)
944 Curses_ShowMessage("WBwindow = ");
945 FLUSH_SCANW("%hd", &WBwindow);
946 Curses_ShowMessage("BBwindow = ");
947 FLUSH_SCANW("%hd", &BBwindow);
952 Curses_GiveHint(void)
958 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
961 Curses_ShowMessage(s);
965 Curses_ShowMessage("I have no idea.\n");
971 Curses_ChangeSearchDepth(char* sx)
973 Curses_ShowMessage("depth = ");
974 FLUSH_SCANW("%hd", &MaxSearchDepth);
975 TCflag = !(MaxSearchDepth > 0);
980 Curses_ChangeHashDepth(void)
982 Curses_ShowMessage("hashdepth = ");
983 FLUSH_SCANW("%hd", &HashDepth);
984 Curses_ShowMessage("MoveLimit = ");
985 FLUSH_SCANW("%hd", &HashMoveLimit);
990 Curses_SetContempt(void)
992 Curses_ShowMessage("contempt = ");
993 FLUSH_SCANW("%hd", &contempt);
998 Curses_ChangeXwindow(void)
1000 Curses_ShowMessage("xwndw= ");
1001 FLUSH_SCANW("%hd", &xwndw);
1006 Curses_SelectLevel(char *sx)
1010 Curses_ClearScreen();
1012 printw("GNU Shogi %s", PACKAGE_VERSION);
1014 printw(" 1. 40 moves in 5 minutes");
1016 printw(" 2. 40 moves in 15 minutes");
1018 printw(" 3. 40 moves in 30 minutes");
1020 printw(" 4. all moves in 15 minutes");
1022 printw(" 5. all moves in 30 minutes");
1024 printw(" 6. all moves in 15 minutes, 30 seconds fischer clock");
1026 printw(" 7. all moves in 30 minutes, 30 seconds fischer clock");
1028 printw(" 8. 1 move in 1 minute");
1030 printw(" 9. 1 move in 15 minutes");
1032 printw("10. 1 move in 30 minutes");
1040 printw("Enter Level: ");
1042 FLUSH_SCANW("%d", &item);
1089 flag.onemove = true;
1095 flag.onemove = true;
1101 flag.onemove = true;
1105 TCflag = (TCmoves > 0);
1107 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1110 Curses_ClearScreen();
1111 Curses_UpdateDisplay(0, 0, 1, 0);
1116 Curses_DoDebug(void)
1118 short c, p, sq, tp, tc, tsq, score;
1121 ExaminePosition(opponent);
1122 Curses_ShowMessage("Enter piece: ");
1123 FLUSH_SCANW("%s", s);
1126 if ((s[0] == 'b') || (s[0] == 'B'))
1129 if ((s[0] == 'w') || (s[0] == 'W'))
1132 for (p = king; p > no_piece; p--)
1134 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1138 for (sq = 0; sq < NO_SQUARES; sq++)
1144 tsq = PieceList[c][1];
1145 PieceList[c][1] = sq;
1146 Curses_ShowPostnValue(sq);
1147 PieceList[c][1] = tsq;
1152 score = ScorePosition(opponent);
1154 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
1155 mtl[computer], pscore[computer], GameType[computer],
1156 mtl[opponent], pscore[opponent], GameType[opponent]);
1163 Curses_DoTable(short table[NO_SQUARES])
1166 ExaminePosition(opponent);
1168 for (sq = 0; sq < NO_SQUARES; sq++)
1170 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1171 printw("%3d ", table[sq]);
1177 Curses_PollForInput(void)
1182 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
1186 "You probably have a non-ANSI <ioctl.h>; "
1187 "see README. %d %d %x\n",
1188 i, errno, FIONREAD);
1197 flag.bothsides = false;
1203 Curses_SetupBoard(void)
1205 Curses_ShowMessage("'setup' command is not supported in Cursesmode");
1209 struct display curses_display =
1211 .ChangeAlphaWindow = Curses_ChangeAlphaWindow,
1212 .ChangeBetaWindow = Curses_ChangeBetaWindow,
1213 .ChangeHashDepth = Curses_ChangeHashDepth,
1214 .ChangeSearchDepth = Curses_ChangeSearchDepth,
1215 .ChangeXwindow = Curses_ChangeXwindow,
1216 .ClearScreen = Curses_ClearScreen,
1217 .DoDebug = Curses_DoDebug,
1218 .DoTable = Curses_DoTable,
1219 .EditBoard = Curses_EditBoard,
1220 .ExitShogi = Curses_ExitShogi,
1221 .GiveHint = Curses_GiveHint,
1222 .Initialize = Curses_Initialize,
1223 .ShowNodeCnt = Curses_ShowNodeCnt,
1224 .OutputMove = Curses_OutputMove,
1225 .PollForInput = Curses_PollForInput,
1226 .SetContempt = Curses_SetContempt,
1227 .SearchStartStuff = Curses_SearchStartStuff,
1228 .SelectLevel = Curses_SelectLevel,
1229 .ShowCurrentMove = Curses_ShowCurrentMove,
1230 .ShowDepth = Curses_ShowDepth,
1231 .ShowGameType = Curses_ShowGameType,
1232 .ShowLine = Curses_ShowLine,
1233 .ShowMessage = Curses_ShowMessage,
1234 .AlwaysShowMessage = Curses_AlwaysShowMessage,
1235 .Printf = Curses_Printf,
1236 .doRequestInputString = Curses_doRequestInputString,
1237 .GetString = Curses_GetString,
1238 .SetupBoard = Curses_SetupBoard,
1239 .ShowPatternCount = Curses_ShowPatternCount,
1240 .ShowPostnValue = Curses_ShowPostnValue,
1241 .ShowPostnValues = Curses_ShowPostnValues,
1242 .ShowPrompt = Curses_ShowPrompt,
1243 .ShowResponseTime = Curses_ShowResponseTime,
1244 .ShowResults = Curses_ShowResults,
1245 .ShowSidetoMove = Curses_ShowSidetoMove,
1246 .ShowStage = Curses_ShowStage,
1247 .TerminateSearch = Curses_TerminateSearch,
1248 .UpdateClocks = Curses_UpdateClocks,
1249 .UpdateDisplay = Curses_UpdateDisplay,
1250 .help = Curses_help,