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"
51 /* Definition of FIONREAD */
52 #include <sys/filio.h>
56 /* Definition of errno(). */
60 #define FLUSH_SCANW fflush(stdout), scanw
66 #define VIR_C(s) ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
67 #define VIR_R(s) ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
69 unsigned short MV[MAXDEPTH];
73 /* Forward declarations. */
74 /* FIXME: change this name, puh-leeze! */
76 static void UpdateCatched(void);
77 static void DrawPiece(short sq);
78 static void ShowScore(short score);
80 /****************************************
81 * Trivial output functions.
82 ****************************************/
93 Curses_ClearScreen(void)
101 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, va_list ap)
170 static char buffer[60];
171 vsnprintf(buffer, sizeof(buffer), format, ap);
172 Curses_ShowMessage(buffer);
177 Curses_Printf(const char *format, va_list ap)
179 static char buffer[60];
180 vsnprintf(buffer, sizeof(buffer), format, ap);
181 printw("%s", buffer);
186 Curses_doRequestInputString(const char* fmt, char* buffer)
188 FLUSH_SCANW(fmt, buffer);
193 Curses_GetString(char* sx)
196 return (getstr(sx) == ERR);
201 Curses_ShowNodeCnt(long NodeCnt)
204 /* printw("Nodes = %8ld, Nodes/Sec = %5ld", NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
205 printw("n = %ld n/s = %ld",
206 NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
212 Curses_ShowPatternCount(short side, short n)
216 gotoXY(TAB + 10 + 3 * side, 20); /* CHECKME */
229 gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
230 printw("%s", (computer == white) ? "Computer" : "Human ");
231 gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
232 printw("%s", (computer == black) ? "Computer" : "Human ");
237 Curses_ShowPrompt(void)
239 Curses_ShowSidetoMove();
241 printw("Your move is? ");
247 Curses_ShowResponseTime(void)
253 printw("%ld, %d, %ld, %ld, %ld, %d",
254 ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
261 Curses_ShowResults(short score, unsigned short *bstline, char ch)
263 unsigned char d, ply;
267 Curses_ShowDepth(ch);
271 for (ply = 1; bstline[ply] > 0; ply++)
279 algbr((short) bstline[ply] >> 8,
280 (short) bstline[ply] & 0xFF, false);
281 printw("%5s ", mvstr[0]);
296 ShowScore(short score)
299 printw("Score= %d", score);
305 Curses_ShowSidetoMove(void)
308 printw("%2d: %s", 1 + GameCnt / 2, ColorStr[player]);
314 Curses_ShowStage(void)
317 printw("Stage= %2d%c B= %2d W= %2d",
318 stage, flag.tsume?'T':' ', balance[black], balance[white]);
322 /****************************************
323 * End of trivial output routines.
324 ****************************************/
327 Curses_Initialize(void)
329 signal(SIGINT, Curses_Die);
330 signal(SIGQUIT, Curses_Die);
337 Curses_ExitShogi(void)
357 signal(SIGINT, SIG_IGN);
358 signal(SIGQUIT, SIG_IGN);
360 Curses_ShowMessage("Abort? ");
361 FLUSH_SCANW("%s", s);
363 if (strcmp(s, "yes") == 0)
366 signal(SIGINT, Curses_Die);
367 signal(SIGQUIT, Curses_Die);
372 Curses_TerminateSearch(int sig)
374 signal(SIGINT, SIG_IGN);
375 signal(SIGQUIT, SIG_IGN);
378 flag.musttimeout = true;
380 Curses_ShowMessage("Terminate Search");
381 flag.bothsides = false;
382 signal(SIGINT, Curses_Die);
383 signal(SIGQUIT, Curses_Die);
390 Curses_ClearScreen();
391 printw("GNU Shogi %s command summary\n", PACKAGE_VERSION);
392 printw("-------------------------------"
393 "---------------------------------\n");
394 printw("7g7f move from 7g to 7f quit Exit Shogi\n");
395 printw("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
396 printw("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
397 printw("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
398 printw("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
399 printw("bd redraw board reverse board display\n");
400 printw("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, BOOKSIZE);
401 printw("undo undo last ply remove take back a move\n");
402 printw("edit edit board force toggle manual move mode\n");
403 printw("switch sides with computer both computer match\n");
404 printw("black computer plays black white computer plays white\n");
405 printw("depth set search depth clock set time control\n");
406 printw("post principle variation hint suggest a move\n", (flag.post) ? "OFF" : "ON");
407 printw("save game to file get game from file\n");
408 printw("random randomize play new start new game\n");
410 printw("Computer: %s", ColorStr[computer]);
412 printw("Opponent: %s", ColorStr[opponent]);
414 printw("Level: %ld", MaxResponseTime/100);
416 printw("Easy mode: %s", (flag.easy) ? "ON" : "OFF");
418 printw("Tsume: %s", (flag.tsume) ? "ON" : "OFF");
420 printw("Depth: %d", MaxSearchDepth);
422 printw("Random: %s", (dither) ? "ON" : "OFF");
424 printw("Transposition table: %s", (flag.hash) ? "ON" : "OFF");
426 printw("Hit <RET> to return: ");
428 printw("Time Control %s %d moves %d sec %d add %d depth\n", (TCflag) ? "ON" : "OFF",
429 TimeControl.moves[black],
430 TimeControl.clock[black] / 100,
431 OperatorTime, MaxSearchDepth);
436 fflush(stdin); /* what is this supposed to do?? */
440 Curses_ClearScreen();
441 Curses_UpdateDisplay(0, 0, 1, 0);
445 static const short x0[2] = { 54, 2 };
446 static const short y0[2] = { 20, 4 };
450 * Set up a board position. Pieces are entered by typing the piece followed
451 * by the location. For example, N3f will place a knight on square 3f.
452 * P* will put a pawn to the captured pieces.
456 Curses_EditBoard(void)
462 flag.regularstart = true;
464 Curses_ClearScreen();
465 Curses_UpdateDisplay(0, 0, 1, 0);
467 printw(". Exit to main\n");
469 printw("# Clear board\n");
471 printw("c Change sides\n");
473 printw("Enter piece & location: ");
479 printw("Editing: %s", ColorStr[a]);
482 FLUSH_SCANW("%s", s);
486 for (sq = 0; sq < NO_SQUARES; sq++)
488 board[sq] = no_piece;
502 for (i = NO_PIECES; i > no_piece; i--)
504 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
508 Captured[a][unpromoted[i]]++;
518 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
522 for (i = NO_PIECES; i > no_piece; i--)
524 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
534 color[sq] = ((board[sq] == no_piece) ? neutral : a);
540 for (sq = 0; sq < NO_SQUARES; sq++)
541 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
548 Curses_ClearScreen();
549 Curses_UpdateDisplay(0, 0, 1, 0);
558 for (side = black; side <= white; side++)
560 short x, y, piece, cside, k;
562 cside = flag.reverse ? (side ^ 1) : side;
567 for (piece = pawn; piece <= king; piece++)
571 if ((n = Captured[side][piece]))
574 printw("%i%c", n, pxx[piece]);
605 Curses_SearchStartStuff(short side)
609 signal(SIGINT, Curses_TerminateSearch);
610 signal(SIGQUIT, Curses_TerminateSearch);
612 for (i = 4; i < 14; i++) /* CHECKME */
621 Curses_OutputMove(void)
624 Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
629 printw("Illegal position.");
633 printw("My move is: %5s", mvstr[0]);
642 if (root->flags & draw)
643 printw("Drawn game!");
644 else if (root->score == -(SCORE_LIMIT + 999))
645 printw("Opponent mates!");
646 else if (root->score == SCORE_LIMIT + 998)
647 printw("Computer mates!");
649 else if (root->score < -SCORE_LIMIT)
650 printw("Opp: mate in %d!", SCORE_LIMIT + 999 + root->score - 1);
651 else if (root->score > SCORE_LIMIT)
652 printw("Comp: mate in %d!", SCORE_LIMIT + 998 - root->score - 1);
653 #endif /* VERYBUGGY */
667 if (Tree[t].f || Tree[t].t)
675 ShowNodeCnt(NodeCnt);
677 printw("Max Tree = %5d", t);
681 Curses_ShowSidetoMove();
686 Curses_UpdateClocks(void)
693 m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
694 s = (short) ((dt - 6000 * (long) m) / 100);
698 m = (short) ((dt = et) / 6000);
699 s = (short) (et - 6000 * (long) m) / 100;
709 gotoXY(20, (flag.reverse) ? 2 : 23);
711 gotoXY(20, (flag.reverse) ? 23 : 2);
713 /* printw("%d:%02d %ld ", m, s, dt); */
714 printw("%d:%02d ", m, s);
717 ShowNodeCnt(NodeCnt);
729 if (color[sq] == neutral)
733 else if (flag.reverse ^ (color[sq] == black))
745 if (is_promoted[(int)piece])
748 y = pxx[unpromoted[(int)piece]];
756 gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
757 printw("%c%c%c%c", l, p, y, r);
762 * Curses_ShowPostnValue(): must have called ExaminePosition() first
765 Curses_ShowPostnValue(short sq)
769 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
770 score = ScorePosition(color[sq]);
772 if (color[sq] != neutral)
773 #if defined SAVE_SVALUE
779 printw("%3d ", svalue[sq]);
790 Curses_ShowPostnValues(void)
794 ExaminePosition(opponent);
796 for (sq = 0; sq < NO_SQUARES; sq++)
797 Curses_ShowPostnValue(sq);
799 score = ScorePosition(opponent);
801 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
802 mtl[computer], pscore[computer], GameType[computer],
803 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);
859 printw(" 1 2 3 4 5 6 7 8 9");
861 printw(" 9 8 7 6 5 4 3 2 1");
864 printw(" 1 2 3 4 5");
866 printw(" 1 2 3 4 5");
869 for (sq = 0; sq < NO_SQUARES; sq++)
872 else /* not redraw */
880 if ((isspec & capture) || (isspec & dropmask) || redraw)
884 for (side = black; side <= white; side++)
886 short x, y, piece, cside, k;
887 cside = flag.reverse ? (side ^ 1) : side;
892 for (piece = pawn; piece <= king; piece++)
896 if ((n = Captured[side][piece]))
899 printw("%i%c", n, pxx[piece]);
901 if (cside == black) y--; else y++;
928 Curses_ChangeAlphaWindow(void)
930 Curses_ShowMessage("WAwindow = ");
931 FLUSH_SCANW("%hd", &WAwindow);
932 Curses_ShowMessage("BAwindow = ");
933 FLUSH_SCANW("%hd", &BAwindow);
938 Curses_ChangeBetaWindow(void)
940 Curses_ShowMessage("WBwindow = ");
941 FLUSH_SCANW("%hd", &WBwindow);
942 Curses_ShowMessage("BBwindow = ");
943 FLUSH_SCANW("%hd", &BBwindow);
948 Curses_GiveHint(void)
954 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
957 Curses_ShowMessage(s);
961 Curses_ShowMessage("I have no idea.\n");
967 Curses_ChangeSearchDepth(void)
969 Curses_ShowMessage("depth = ");
970 FLUSH_SCANW("%hd", &MaxSearchDepth);
971 TCflag = !(MaxSearchDepth > 0);
976 Curses_ChangeHashDepth(void)
978 Curses_ShowMessage("hashdepth = ");
979 FLUSH_SCANW("%hd", &HashDepth);
980 Curses_ShowMessage("MoveLimit = ");
981 FLUSH_SCANW("%hd", &HashMoveLimit);
986 Curses_SetContempt(void)
988 Curses_ShowMessage("contempt = ");
989 FLUSH_SCANW("%hd", &contempt);
994 Curses_ChangeXwindow(void)
996 Curses_ShowMessage("xwndw= ");
997 FLUSH_SCANW("%hd", &xwndw);
1002 Curses_SelectLevel(char *sx)
1006 Curses_ClearScreen();
1008 printw("GNU Shogi %s", PACKAGE_VERSION);
1010 printw(" 1. 40 moves in 5 minutes");
1012 printw(" 2. 40 moves in 15 minutes");
1014 printw(" 3. 40 moves in 30 minutes");
1016 printw(" 4. all moves in 15 minutes");
1018 printw(" 5. all moves in 30 minutes");
1020 printw(" 6. all moves in 15 minutes, 30 seconds fischer clock");
1022 printw(" 7. all moves in 30 minutes, 30 seconds fischer clock");
1024 printw(" 8. 1 move in 1 minute");
1026 printw(" 9. 1 move in 15 minutes");
1028 printw("10. 1 move in 30 minutes");
1036 printw("Enter Level: ");
1038 FLUSH_SCANW("%d", &item);
1085 flag.onemove = true;
1091 flag.onemove = true;
1097 flag.onemove = true;
1101 TCflag = (TCmoves > 0);
1103 TimeControl.clock[black] = TimeControl.clock[white] = 0;
1106 Curses_ClearScreen();
1107 Curses_UpdateDisplay(0, 0, 1, 0);
1112 Curses_DoDebug(void)
1114 short c, p, sq, tp, tc, tsq, score;
1117 ExaminePosition(opponent);
1118 Curses_ShowMessage("Enter piece: ");
1119 FLUSH_SCANW("%s", s);
1122 if ((s[0] == 'b') || (s[0] == 'B'))
1125 if ((s[0] == 'w') || (s[0] == 'W'))
1128 for (p = king; p > no_piece; p--)
1130 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1134 for (sq = 0; sq < NO_SQUARES; sq++)
1140 tsq = PieceList[c][1];
1141 PieceList[c][1] = sq;
1142 Curses_ShowPostnValue(sq);
1143 PieceList[c][1] = tsq;
1148 score = ScorePosition(opponent);
1150 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
1151 mtl[computer], pscore[computer], GameType[computer],
1152 mtl[opponent], pscore[opponent], GameType[opponent]);
1159 Curses_DoTable(short table[NO_SQUARES])
1162 ExaminePosition(opponent);
1164 for (sq = 0; sq < NO_SQUARES; sq++)
1166 gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1167 printw("%3d ", table[sq]);
1173 Curses_PollForInput(void)
1178 if ((i = ioctl((int) 0, FIONREAD, &nchar)))
1182 "You probably have a non-ANSI <ioctl.h>; "
1183 "see README. %d %d %x\n",
1184 i, errno, FIONREAD);
1193 flag.bothsides = false;
1199 Curses_SetupBoard(void)
1201 Curses_ShowMessage("'setup' command is not supported in Cursesmode");