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
44 #include <sys/param.h>
45 #include <sys/types.h>
54 /* Definition of FIONREAD */
55 #include <sys/filio.h>
59 /* Definition of errno(). */
63 #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 /****************************************
72 * forward declarations
73 ****************************************/
75 /* FIXME: change this name, puh-leeze! */
76 static void UpdateCatched(void);
77 static void DrawPiece(short sq
);
78 static void ShowScore(short score
);
79 static void Curses_UpdateDisplay(short f
, short t
, short redraw
, short isspec
);
80 static void Curses_Die(int sig
);
81 static void Curses_ShowSidetoMove(void);
83 /****************************************
84 * Trivial output functions.
85 ****************************************/
96 Curses_ClearScreen(void)
104 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 _vprintw(const char *format
, va_list ap
)
163 static char buffer
[60];
164 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
165 printw("%s", buffer
);
169 Curses_ShowMessage(char *format
, ...)
172 va_start(ap
, format
);
174 _vprintw(format
, ap
);
180 Curses_AlwaysShowMessage(const char *format
, ...)
183 va_start(ap
, format
);
185 _vprintw(format
, ap
);
192 Curses_Printf(const char *format
, ...)
195 va_start(ap
, format
);
196 _vprintw(format
, ap
);
202 Curses_doRequestInputString(const char* fmt
, char* buffer
)
204 FLUSH_SCANW(fmt
, buffer
);
209 Curses_GetString(char* sx
)
212 return (getstr(sx
) == ERR
);
217 Curses_ShowNodeCnt(long NodeCnt
)
220 /* printw("Nodes = %8ld, Nodes/Sec = %5ld", NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
221 printw("n = %ld n/s = %ld",
222 NodeCnt
, (et
> 100) ? NodeCnt
/ (et
/ 100) : 0);
228 Curses_ShowPatternCount(short side
, short n
)
232 gotoXY(TAB
+ 10 + 3 * side
, 20); /* CHECKME */
245 gotoXY(5, ((flag
.reverse
) ? (5 + 2*NO_ROWS
) : 2));
246 printw("%s", (computer
== white
) ? "Computer" : "Human ");
247 gotoXY(5, ((flag
.reverse
) ? 2 : (5 + 2*NO_ROWS
)));
248 printw("%s", (computer
== black
) ? "Computer" : "Human ");
253 Curses_ShowPrompt(void)
255 Curses_ShowSidetoMove();
257 printw("Your move is? ");
263 Curses_ShowResponseTime(void)
269 printw("%ld, %d, %ld, %ld, %ld, %d",
270 ResponseTime
, TCC
, TCleft
, ExtraTime
, et
, flag
.timeout
);
277 Curses_ShowResults(short score
, unsigned short *bstline
, char ch
)
279 unsigned char d
, ply
;
283 Curses_ShowDepth(ch
);
287 for (ply
= 1; bstline
[ply
] > 0; ply
++)
295 algbr((short) bstline
[ply
] >> 8,
296 (short) bstline
[ply
] & 0xFF, 0);
297 printw("%5s ", mvstr
[0]);
312 ShowScore(short score
)
315 printw("Score= %d", score
);
321 Curses_ShowSidetoMove(void)
324 printw("%2d: %s", 1 + GameCnt
/ 2, ColorStr
[player
]);
330 Curses_ShowStage(void)
333 printw("Stage= %2d%c B= %2d W= %2d",
334 stage
, flag
.tsume
?'T':' ', balance
[black
], balance
[white
]);
338 /****************************************
339 * End of trivial output routines.
340 ****************************************/
343 Curses_Initialize(void)
345 signal(SIGINT
, Curses_Die
);
346 signal(SIGQUIT
, Curses_Die
);
353 Curses_ExitShogi(void)
373 signal(SIGINT
, SIG_IGN
);
374 signal(SIGQUIT
, SIG_IGN
);
376 Curses_ShowMessage("Abort? ");
377 FLUSH_SCANW("%s", s
);
379 if (strcmp(s
, "yes") == 0)
382 signal(SIGINT
, Curses_Die
);
383 signal(SIGQUIT
, Curses_Die
);
388 Curses_TerminateSearch(int sig
)
390 signal(SIGINT
, SIG_IGN
);
391 signal(SIGQUIT
, SIG_IGN
);
394 flag
.musttimeout
= true;
396 Curses_ShowMessage("Terminate Search");
397 flag
.bothsides
= false;
398 signal(SIGINT
, Curses_Die
);
399 signal(SIGQUIT
, Curses_Die
);
406 Curses_ClearScreen();
407 printw("GNU Shogi %s command summary\n", PACKAGE_VERSION
);
408 printw("-------------------------------"
409 "---------------------------------\n");
410 printw("7g7f move from 7g to 7f quit Exit Shogi\n");
411 printw("S6h move silver to 6h beep turn %s\n", (flag
.beep
) ? "OFF" : "ON");
412 printw("2d2c+ move to 2c and promote\n");
413 printw("P*5e drop pawn to 5e easy turn %s\n", (flag
.easy
) ? "OFF" : "ON");
414 printw("tsume toggle tsume mode hash turn %s\n", (flag
.hash
) ? "OFF" : "ON");
415 printw("bd redraw board reverse board display\n");
416 printw("list game to shogi.lst book turn %s used %d of %d\n", (Book
) ? "OFF" : "ON", bookcount
, BOOKSIZE
);
417 printw("undo undo last ply remove take back a move\n");
418 printw("edit edit board force toggle manual move mode\n");
419 printw("switch sides with computer both computer match\n");
420 printw("black computer plays black white computer plays white\n");
421 printw("depth set search depth clock set time control\n");
422 printw("post principle variation hint suggest a move\n", (flag
.post
) ? "OFF" : "ON");
423 printw("save game to file get game from file\n");
424 printw("random randomize play new start new game\n");
426 printw("Computer: %s", ColorStr
[computer
]);
428 printw("Opponent: %s", ColorStr
[opponent
]);
430 printw("Level: %ld", MaxResponseTime
/100);
432 printw("Easy mode: %s", (flag
.easy
) ? "ON" : "OFF");
434 printw("Tsume: %s", (flag
.tsume
) ? "ON" : "OFF");
436 printw("Depth: %d", MaxSearchDepth
);
438 printw("Random: %s", (dither
) ? "ON" : "OFF");
440 printw("Transposition table: %s", (flag
.hash
) ? "ON" : "OFF");
442 printw("Hit <RET> to return: ");
444 printw("Time Control %s %d moves %d sec %d add %d depth\n", (TCflag
) ? "ON" : "OFF",
445 TimeControl
.moves
[black
],
446 TimeControl
.clock
[black
] / 100,
447 OperatorTime
, MaxSearchDepth
);
452 fflush(stdin
); /* what is this supposed to do?? */
456 Curses_ClearScreen();
457 Curses_UpdateDisplay(0, 0, 1, 0);
461 static const short x0
[2] = { MARGIN
+ 5*NO_COLS
+ 4, 2 };
462 static const short y0
[2] = { 4 + 2 * (NO_ROWS
- 1), 4 };
466 * Set up a board position. Pieces are entered by typing the piece followed
467 * by the location. For example, N3f will place a knight on square 3f.
468 * P* will put a pawn to the captured pieces.
472 Curses_EditBoard(void)
474 short a
, c
, sq
, i
, found
;
478 flag
.regularstart
= true;
480 Curses_ClearScreen();
481 Curses_UpdateDisplay(0, 0, 1, 0);
483 printw(". Exit to main\n");
485 printw("# Clear board\n");
487 printw("c Change sides\n");
489 printw("Enter piece & location: ");
495 printw("Editing: %s", ColorStr
[a
]);
498 FLUSH_SCANW("%s", s
);
507 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
509 board
[sq
] = no_piece
;
526 for (i
= NO_PIECES
; i
> no_piece
; i
--)
528 if ((s
[0] == pxx
[i
]) || (s
[0] == qxx
[i
]))
530 Captured
[a
][unpromoted
[i
]]++;
537 dsp
->AlwaysShowMessage("Invalid piece type '%c'", s
[0]);
544 if ((c
< 0) || (c
>= NO_COLS
) || (r
< 0) || (r
>= NO_ROWS
)) {
545 dsp
->AlwaysShowMessage("Out-of-board '%c%c'", s
[1], s
[2]);
551 for (i
= NO_PIECES
; i
> no_piece
; i
--)
553 if ((s
[0] == pxx
[i
]) || (s
[0] == qxx
[i
]))
557 board
[sq
] = promoted
[i
];
559 board
[sq
] = unpromoted
[i
];
567 dsp
->AlwaysShowMessage("Invalid piece type '%c'", s
[0]);
572 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
573 Mvboard
[sq
] = ((board
[sq
] != Stboard
[sq
]) ? 10 : 0);
580 Curses_ClearScreen();
581 Curses_UpdateDisplay(0, 0, 1, 0);
590 for (side
= black
; side
<= white
; side
++)
592 short x
, y
, piece
, cside
, k
;
594 cside
= flag
.reverse
? (side
^ 1) : side
;
599 for (piece
= pawn
; piece
<= king
; piece
++)
603 if ((n
= Captured
[side
][piece
]))
606 printw("%i%c", n
, pxx
[piece
]);
637 Curses_SearchStartStuff(short side
)
641 signal(SIGINT
, Curses_TerminateSearch
);
642 signal(SIGQUIT
, Curses_TerminateSearch
);
644 for (i
= 4; i
< 14; i
++) /* CHECKME */
653 Curses_OutputMove(void)
656 Curses_UpdateDisplay(root
->f
, root
->t
, 0, (short) root
->flags
);
661 printw("Illegal position.");
665 printw("My move is: %5s", mvstr
[0]);
674 if (root
->flags
& draw
)
675 printw("Drawn game!");
676 else if (root
->score
== -(SCORE_LIMIT
+ 999))
677 printw("Opponent mates!");
678 else if (root
->score
== SCORE_LIMIT
+ 998)
679 printw("Computer mates!");
681 else if (root
->score
< -SCORE_LIMIT
)
682 printw("Opp: mate in %d!", SCORE_LIMIT
+ 999 + root
->score
- 1);
683 else if (root
->score
> SCORE_LIMIT
)
684 printw("Comp: mate in %d!", SCORE_LIMIT
+ 998 - root
->score
- 1);
685 #endif /* VERYBUGGY */
699 if (Tree
[t
].f
|| Tree
[t
].t
)
707 Curses_ShowNodeCnt(NodeCnt
);
709 printw("Max Tree = %5d", t
);
713 Curses_ShowSidetoMove();
718 Curses_UpdateClocks(void)
725 m
= (short) ((dt
= (TimeControl
.clock
[player
] - et
)) / 6000);
726 s
= (short) ((dt
- 6000 * (long) m
) / 100);
730 m
= (short) ((dt
= et
) / 6000);
731 s
= (short) (et
- 6000 * (long) m
) / 100;
741 gotoXY(20, (flag
.reverse
) ? 2 : (5 + 2*NO_ROWS
));
743 gotoXY(20, (flag
.reverse
) ? (5 + 2*NO_ROWS
) : 2);
745 /* printw("%d:%02d %ld ", m, s, dt); */
746 printw("%d:%02d ", m
, s
);
749 Curses_ShowNodeCnt(NodeCnt
);
761 if (color
[sq
] == neutral
)
765 else if (flag
.reverse
^ (color
[sq
] == black
))
777 if (is_promoted
[(int)piece
])
780 y
= pxx
[unpromoted
[(int)piece
]];
788 gotoXY(MARGIN
+ 3 + 5 * VIR_C(sq
), 4 + 2 * ((NO_ROWS
- 1) - VIR_R(sq
)));
789 printw("%c%c%c%c", l
, p
, y
, r
);
794 * Curses_ShowPostnValue(): must have called ExaminePosition() first
797 Curses_ShowPostnValue(short sq
)
799 gotoXY(4 + 5 * VIR_C(sq
), 5 + 2 * (7 - VIR_R(sq
))); /* CHECKME */
800 (void) ScorePosition(color
[sq
]);
802 if (color
[sq
] != neutral
)
803 #if defined SAVE_SVALUE
809 printw("%3d ", svalue
[sq
]);
820 Curses_ShowPostnValues(void)
824 ExaminePosition(opponent
);
826 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
827 Curses_ShowPostnValue(sq
);
829 score
= ScorePosition(opponent
);
831 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score
,
832 mtl
[computer
], pscore
[computer
], GameType
[computer
],
833 mtl
[opponent
], pscore
[opponent
], GameType
[opponent
]);
840 Curses_UpdateDisplay(short f
, short t
, short redraw
, short isspec
)
854 for (j
=0; j
<NO_COLS
; j
++)
857 while (i
<= 1 + 2*NO_ROWS
)
864 z
= NO_ROWS
+ 2 - ((i
+ 1) / 2);
866 printw("%c |", ROW_NAME(z
+1));
867 for (j
=0; j
<NO_COLS
; j
++)
872 if (i
< 2 + 2*NO_ROWS
)
875 for (j
=0; j
<NO_COLS
; j
++)
881 for (j
=0; j
<NO_COLS
; j
++)
884 gotoXY(MARGIN
, 4 + 2*NO_ROWS
);
888 printw(" 1 2 3 4 5 6 7 8 9");
890 printw(" 9 8 7 6 5 4 3 2 1");
893 printw(" 1 2 3 4 5");
895 printw(" 5 4 3 2 1");
898 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
901 else /* not redraw */
909 if ((isspec
& capture
) || (isspec
& dropmask
) || redraw
)
913 for (side
= black
; side
<= white
; side
++)
915 short x
, y
, piece
, cside
, k
;
916 cside
= flag
.reverse
? (side
^ 1) : side
;
921 for (piece
= pawn
; piece
<= king
; piece
++)
925 if ((n
= Captured
[side
][piece
]))
928 printw("%i%c", n
, pxx
[piece
]);
930 if (cside
== black
) y
--; else y
++;
957 Curses_ChangeAlphaWindow(void)
959 Curses_ShowMessage("WAwindow = ");
960 FLUSH_SCANW("%hd", &WAwindow
);
961 Curses_ShowMessage("BAwindow = ");
962 FLUSH_SCANW("%hd", &BAwindow
);
967 Curses_ChangeBetaWindow(void)
969 Curses_ShowMessage("WBwindow = ");
970 FLUSH_SCANW("%hd", &WBwindow
);
971 Curses_ShowMessage("BBwindow = ");
972 FLUSH_SCANW("%hd", &BBwindow
);
977 Curses_GiveHint(void)
983 algbr((short) (hint
>> 8), (short) (hint
& 0xFF), 0);
986 Curses_ShowMessage(s
);
990 Curses_ShowMessage("I have no idea.\n");
996 Curses_ChangeSearchDepth(char* sx
)
998 Curses_ShowMessage("depth = ");
999 FLUSH_SCANW("%hd", &MaxSearchDepth
);
1000 TCflag
= !(MaxSearchDepth
> 0);
1005 Curses_ChangeHashDepth(void)
1007 Curses_ShowMessage("hashdepth = ");
1008 FLUSH_SCANW("%hd", &HashDepth
);
1009 Curses_ShowMessage("MoveLimit = ");
1010 FLUSH_SCANW("%hd", &HashMoveLimit
);
1015 Curses_SetContempt(void)
1017 Curses_ShowMessage("contempt = ");
1018 FLUSH_SCANW("%hd", &contempt
);
1023 Curses_ChangeXwindow(void)
1025 Curses_ShowMessage("xwndw= ");
1026 FLUSH_SCANW("%hd", &xwndw
);
1031 Curses_SelectLevel(char *sx
)
1035 Curses_ClearScreen();
1037 printw("GNU Shogi %s", PACKAGE_VERSION
);
1039 printw(" 1. 40 moves in 5 minutes");
1041 printw(" 2. 40 moves in 15 minutes");
1043 printw(" 3. 40 moves in 30 minutes");
1045 printw(" 4. all moves in 15 minutes");
1047 printw(" 5. all moves in 30 minutes");
1049 printw(" 6. all moves in 15 minutes, 30 seconds fischer clock");
1051 printw(" 7. all moves in 30 minutes, 30 seconds fischer clock");
1053 printw(" 8. 1 move in 1 minute");
1055 printw(" 9. 1 move in 15 minutes");
1057 printw("10. 1 move in 30 minutes");
1065 printw("Enter Level: ");
1067 FLUSH_SCANW("%d", &item
);
1114 flag
.onemove
= true;
1120 flag
.onemove
= true;
1126 flag
.onemove
= true;
1130 TCflag
= (TCmoves
> 0);
1132 TimeControl
.clock
[black
] = TimeControl
.clock
[white
] = 0;
1135 Curses_ClearScreen();
1136 Curses_UpdateDisplay(0, 0, 1, 0);
1141 Curses_DoDebug(void)
1143 short c
, p
, sq
, tp
, tc
, tsq
, score
;
1146 ExaminePosition(opponent
);
1147 Curses_ShowMessage("Enter piece: ");
1148 FLUSH_SCANW("%s", s
);
1151 if ((s
[0] == 'b') || (s
[0] == 'B'))
1154 if ((s
[0] == 'w') || (s
[0] == 'W'))
1157 for (p
= king
; p
> no_piece
; p
--)
1159 if ((s
[1] == pxx
[p
]) || (s
[1] == qxx
[p
]))
1163 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
1169 tsq
= PieceList
[c
][1];
1170 PieceList
[c
][1] = sq
;
1171 Curses_ShowPostnValue(sq
);
1172 PieceList
[c
][1] = tsq
;
1177 score
= ScorePosition(opponent
);
1179 printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score
,
1180 mtl
[computer
], pscore
[computer
], GameType
[computer
],
1181 mtl
[opponent
], pscore
[opponent
], GameType
[opponent
]);
1188 Curses_DoTable(short table
[NO_SQUARES
])
1191 ExaminePosition(opponent
);
1193 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
1195 gotoXY(4 + 5 * VIR_C(sq
), 5 + 2 * (7 - VIR_R(sq
)));
1196 printw("%3d ", table
[sq
]);
1202 Curses_PollForInput(void)
1207 if ((i
= ioctl((int) 0, FIONREAD
, &nchar
)))
1211 "You probably have a non-ANSI <ioctl.h>; "
1212 "see README. %d %d %x\n",
1213 i
, errno
, FIONREAD
);
1222 flag
.bothsides
= false;
1228 Curses_SetupBoard(void)
1230 Curses_ShowMessage("'setup' command is not supported in Cursesmode");
1234 struct display curses_display
=
1236 .ChangeAlphaWindow
= Curses_ChangeAlphaWindow
,
1237 .ChangeBetaWindow
= Curses_ChangeBetaWindow
,
1238 .ChangeHashDepth
= Curses_ChangeHashDepth
,
1239 .ChangeSearchDepth
= Curses_ChangeSearchDepth
,
1240 .ChangeXwindow
= Curses_ChangeXwindow
,
1241 .ClearScreen
= Curses_ClearScreen
,
1242 .DoDebug
= Curses_DoDebug
,
1243 .DoTable
= Curses_DoTable
,
1244 .EditBoard
= Curses_EditBoard
,
1245 .ExitShogi
= Curses_ExitShogi
,
1246 .GiveHint
= Curses_GiveHint
,
1247 .Initialize
= Curses_Initialize
,
1248 .ShowNodeCnt
= Curses_ShowNodeCnt
,
1249 .OutputMove
= Curses_OutputMove
,
1250 .PollForInput
= Curses_PollForInput
,
1251 .SetContempt
= Curses_SetContempt
,
1252 .SearchStartStuff
= Curses_SearchStartStuff
,
1253 .SelectLevel
= Curses_SelectLevel
,
1254 .ShowCurrentMove
= Curses_ShowCurrentMove
,
1255 .ShowDepth
= Curses_ShowDepth
,
1256 .ShowGameType
= Curses_ShowGameType
,
1257 .ShowLine
= Curses_ShowLine
,
1258 .ShowMessage
= Curses_ShowMessage
,
1259 .AlwaysShowMessage
= Curses_AlwaysShowMessage
,
1260 .Printf
= Curses_Printf
,
1261 .doRequestInputString
= Curses_doRequestInputString
,
1262 .GetString
= Curses_GetString
,
1263 .SetupBoard
= Curses_SetupBoard
,
1264 .ShowPatternCount
= Curses_ShowPatternCount
,
1265 .ShowPostnValue
= Curses_ShowPostnValue
,
1266 .ShowPostnValues
= Curses_ShowPostnValues
,
1267 .ShowPrompt
= Curses_ShowPrompt
,
1268 .ShowResponseTime
= Curses_ShowResponseTime
,
1269 .ShowResults
= Curses_ShowResults
,
1270 .ShowSidetoMove
= Curses_ShowSidetoMove
,
1271 .ShowStage
= Curses_ShowStage
,
1272 .TerminateSearch
= Curses_TerminateSearch
,
1273 .UpdateClocks
= Curses_UpdateClocks
,
1274 .UpdateDisplay
= Curses_UpdateDisplay
,
1275 .help
= Curses_help
,