4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
16 * GNU Shogi is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 3 of the License,
19 * or (at your option) any later version.
21 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with GNU Shogi; see the file COPYING. If not, see
28 * <http://www.gnu.org/licenses/>.
29 * ----------------------------------------------------------------------
37 #include <sys/param.h>
38 #include <sys/types.h>
47 /****************************************
48 * forward declarations
49 ****************************************/
51 static void Raw_UpdateDisplay(short f, short t, short redraw, short isspec);
53 /****************************************
54 * Trivial output functions.
55 ****************************************/
69 fputs("\nYour move is? ", stdout);
74 Raw_ShowCurrentMove(short pnt, short f, short t)
80 Raw_ShowDepth(char ch)
83 printf("Depth= %d%c \n", Sdepth, ch);
88 Raw_ShowGameType(void)
91 printf("%c vs. %c\n", GameType[black], GameType[white]);
96 Raw_ShowLine(unsigned short *bstline)
100 for (i = 1; bstline[i] > 0; i++)
102 if ((i > 1) && (i % 8 == 1))
105 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
106 printf("%5s ", mvstr[0]);
114 Raw_ShowMessage(char *s)
122 Raw_AlwaysShowMessage(const char *format, ...)
125 va_start(ap, format);
133 Raw_Printf(const char *format, ...)
136 va_start(ap, format);
143 Raw_doRequestInputString(const char* fmt, char* buffer)
150 Raw_GetString(char* sx)
156 while(!eof && !sx[0])
157 eof = (fgets(sx, 80, stdin) == NULL);
159 /* remove any trailing newline */
160 nl = strchr(sx, '\n');
169 Raw_ShowNodeCnt(long NodeCnt)
171 printf("Nodes = %ld Nodes/sec = %ld\n",
172 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
177 Raw_ShowPatternCount(short side, short n)
180 printf("%s%s matches %d pattern(s)\n", xboard ? "# " : "" , ColorStr[side], n);
185 Raw_ShowResponseTime(void)
191 Raw_ShowResults(short score, unsigned short *bstline, char ch)
193 if (flag.post && (xboard || !XSHOGI))
196 printf("%2d%c %6d %4ld %8ld ",
197 Sdepth, ch, score, et / 100, NodeCnt);
198 Raw_ShowLine(bstline);
204 Raw_ShowSidetoMove(void)
212 printf("stage = %d\n", stage);
213 printf("balance[black] = %d balance[white] = %d\n",
214 balance[black], balance[white]);
217 /****************************************
218 * End of trivial output routines.
219 ****************************************/
229 /* needed because of inconsistency between MSVC run-time system and gcc includes */
230 setbuf(stdout, NULL);
233 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
235 # ifdef HAVE_SETLINEBUF
238 # error "Need setvbuf() or setlinebuf() to compile gnushogi!"
242 printf("GNU Shogi %s\n", PACKAGE_VERSION);
247 if (!TCflag && (MaxResponseTime == 0))
248 MaxResponseTime = 15L * 100L;
256 /* CHECKME: what purpose does this next statement serve? */
257 signal(SIGTERM, SIG_IGN);
267 Raw_TerminateSearch(int sig)
269 #ifdef INTERRUPT_TEST
270 ElapsedTime(INIT_INTERRUPT_MODE);
274 flag.back = true; /* previous: flag.timeout = true; */
276 flag.bothsides = false;
284 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
285 printf("----------------------------------------------------------------\n");
286 printf("7g7f move from 7g to 7f quit Exit Shogi\n");
287 printf("S6h move silver to 6h beep turn %s\n",
288 (flag.beep) ? "OFF" : "ON");
289 printf("2d2c+ move to 2c and promote material turn %s\n",
290 (flag.material) ? "OFF" : "ON");
291 printf("P*5e drop pawn to 5e easy turn %s\n",
292 (flag.easy) ? "OFF" : "ON");
293 printf("tsume toggle tsume mode hash turn %s\n",
294 (flag.hash) ? "OFF" : "ON");
295 printf("bd redraw board reverse board display\n");
296 printf("list game to shogi.lst book turn %s used %d of %d\n",
297 (Book) ? "OFF" : "ON", bookcount, booksize);
298 printf("undo undo last ply remove take back a move\n");
299 printf("edit edit board force toggle manual move mode\n");
300 printf("switch sides with computer both computer match\n");
301 printf("black computer plays black white computer plays white\n");
302 printf("sd set search depth clock set time control\n");
303 printf("post principle variation hint suggest a move\n");
304 printf("save game to file get game from file\n");
305 printf("xsave pos. to xshogi file xget pos. from xshogi file\n");
306 printf("random randomize play new start new game\n");
307 printf("setup first \n");
308 printf("go computer plays now material turn %s\n",
309 flag.material ? "OFF" : "ON");
310 printf("level time control gamein \n");
311 printf("time set engine clock otime set opponent clock\n");
312 printf("Awindow Bwindow \n");
313 printf("rcptr turn %3s bsave book save\n",
314 flag.rcptr ? "OFF" : "ON ");
315 printf("hashdepth hard turn easy OFF\n");
316 printf("contempt xwndw \n");
317 printf("rv turn %3s coords turn %s\n",
318 flag.rv ? "OFF" : "ON ", flag.coords ? "OFF" : "ON");
319 printf("stars turn %3s moves \n",
320 flag.stars ? "OFF" : "ON ");
322 printf("debug depth alias for 'sd'\n");
323 printf("----------------------------------------------------------------\n");
324 printf("Computer: %-12s Opponent: %s\n",
325 ColorStr[computer], ColorStr[opponent]);
326 printf("Depth: %-12d Response time: %ld sec\n",
327 MaxSearchDepth, MaxResponseTime/100);
328 printf("Random: %-12s Easy mode: %s\n",
329 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
330 printf("Beep: %-12s Transposition file: %s\n",
331 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
332 printf("Tsume: %-12s Force: %s\n",
333 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
334 printf("Time Control %s %d moves %ld sec %d add %d depth\n",
335 (TCflag) ? "ON" : "OFF",
336 TimeControl.moves[black], TimeControl.clock[black] / 100,
337 TCadd/100, MaxSearchDepth);
342 * Set up a board position. Pieces are entered by typing the piece followed
343 * by the location. For example, Nf3 will place a knight on square f3.
348 short a, r, c, sq, i, found;
351 flag.regularstart = true;
354 Raw_UpdateDisplay(0, 0, 1, 0);
355 printf(". Exit to main\n");
356 printf("# Clear board\n");
357 printf("c Change sides\n");
358 printf("enter piece & location:\n");
372 for (sq = 0; sq < NO_SQUARES; sq++)
374 board[sq] = no_piece;
389 for (i = pawn; i <= king; i++)
391 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
399 printf("# Invalid piece type '%c'\n", s[0]);
406 if ((c < 0) || (c >= NO_COLS) || (r < 0) || (r >= NO_ROWS)) {
407 printf("# Out-of-board position '%c%c'\n", s[1], s[2]);
413 for (i = no_piece; i <= king; i++)
415 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
419 board[sq] = promoted[i];
429 printf("# Invalid piece type '%c'\n", s[0]);
432 for (sq = 0; sq < NO_SQUARES; sq++)
433 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
441 Raw_UpdateDisplay(0, 0, 1, 0);
446 * Set up a board position.
447 * Nine lines of nine characters are used to setup the board. 9a-1a is the
448 * first line. White pieces are represented by uppercase characters.
459 fgets(s, 80, stdin); /* skip "setup" command */
461 for (r = NO_ROWS - 1; r >= 0; r--)
465 for (c = 0; c <= (NO_COLS - 1); c++)
470 board[sq] = no_piece;
472 for (i = no_piece; i <= king; i++)
480 else if (ch == qxx[i])
490 for (sq = 0; sq < NO_SQUARES; sq++)
491 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
495 Raw_UpdateDisplay(0, 0, 1, 0);
496 fputs("Setup successful\n", stdout);
501 Raw_SearchStartStuff(short side)
505 printf("\nMove# %d Target = %ld Clock: %ld\n",
507 ResponseTime, TimeControl.clock[side]);
517 printf("Illegal position.\n");
521 if (mvstr[0][0] == '\0')
525 /* add remaining time in milliseconds to xshogi */
526 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
527 (TimeControl.clock[player] - et) * 10);
529 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
532 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
533 || (root->score == (SCORE_LIMIT + 998)))
546 if (Tree[t].f || Tree[t].t)
554 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
555 (et > 100) ? (NodeCnt / (et / 100)) : 0,
556 EADD, EGET, reminus, replus);
558 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
559 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
562 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
566 printf("My move is: %5s\n", mvstr[0]);
573 if (root->flags & draw)
574 fputs("Drawn game!\n", stdout);
575 else if (root->score == -(SCORE_LIMIT + 999))
576 printf("%s mates!\n", ColorStr[opponent]);
577 else if (root->score == (SCORE_LIMIT + 998))
578 printf("%s mates!\n", ColorStr[computer]);
580 else if (!XSHOGI && (root->score < -SCORE_LIMIT))
581 printf("%s%s has a forced mate in %d moves!\n", xboard ? "# " : "",
582 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
583 else if (!XSHOGI && (root->score > SCORE_LIMIT))
584 printf("%s%s has a forced mate in %d moves!\n", xboard ? "# " : "",
585 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
586 #endif /* VERYBUGGY */
591 Raw_UpdateClocks(void)
597 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
602 if (redraw && !XSHOGI)
605 r = (short)(TimeControl.clock[black] / 6000);
606 c = (short)((TimeControl.clock[black] % 6000) / 100);
607 l = (short)(TimeControl.clock[white] / 6000);
608 m = (short)((TimeControl.clock[white] % 6000) / 100);
609 printf("Black %d:%02d White %d:%02d\n", r, c, l, m);
612 for (r = (NO_ROWS - 1); r >= 0; r--)
614 for (c = 0; c <= (NO_COLS - 1); c++)
618 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
620 pc = (is_promoted[board[l]] ? '+' : ' ');
622 if (color[l] == neutral)
624 else if (color[l] == black)
625 printf("%c%c", pc, qxx[board[l]]);
627 printf("%c%c", pc, pxx[board[l]]);
637 for (side = black; side <= white; side++)
640 printf((side == black)?"black ":"white ");
642 for (piece = pawn; piece <= king; piece++)
644 if ((c = Captured[side][piece]))
645 printf("%i%c ", c, pxx[piece]);
656 Raw_ChangeAlphaWindow(void)
658 printf("WAwindow: ");
659 scanf("%hd", &WAwindow);
660 printf("BAwindow: ");
661 scanf("%hd", &BAwindow);
666 Raw_ChangeBetaWindow(void)
668 printf("WBwindow: ");
669 scanf("%hd", &WBwindow);
670 printf("BBwindow: ");
671 scanf("%hd", &BBwindow);
680 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
681 printf("Hint: %s\n", mvstr[0]);
684 fputs("I have no idea.\n", stdout);
689 Raw_SelectLevel(char *sx)
691 /* FIXME: NO_SQUARES is nonsense here */
692 char T[NO_SQUARES + 1], *p;
694 strncpy(T, sx, NO_SQUARES);
695 T[NO_SQUARES] = '\0';
699 /* if line empty, ask for input */
702 fputs("Enter #moves #minutes: ", stdout);
703 fgets(T, NO_SQUARES + 1, stdin);
706 /* skip blackspace */
707 for (p = T; *p == ' '; p++) ;
709 /* could be moves or a fischer clock */
712 /* its a fischer clock game */
715 TCminutes = (short)strtol(p, &q, 10);
716 TCadd = (short)strtol(q, NULL, 10) *100;
725 TCmoves = (short)strtol(p, &q, 10);
726 TCminutes = (short)strtol(q, &q, 10);
729 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
734 fputs("Operator time (hundredths) = ", stdout);
735 scanf("%hd", &OperatorTime);
741 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
742 TCminutes = TCseconds = 0;
751 int min, sec=0, inc, mps;
752 /* parse regular "level MPS TC INC" command of WB protocol */
753 sscanf(sx, "%d %d %d", &mps, &min, &inc) == 3 ||
754 sscanf(sx, "%d %d:%d %d", &mps, &min, &sec, &inc);
755 TCminutes = min; TCseconds = sec;
756 TCadd = inc*100; TCmoves = mps ? mps : 50;
757 MaxResponseTime = 0; TCflag = true;
760 TimeControl.clock[black] = TimeControl.clock[white] = 0;
765 printf("Clocks: %ld %ld\n",
766 TimeControl.clock[black] * 10,
767 TimeControl.clock[white] * 10);
773 Raw_ChangeSearchDepth(char *sx)
776 strncpy(buf, sx, 80); buf[80] = '\0';
777 /* if line empty, ask for input */
780 fgets(buf, 80+1, stdin);
782 sscanf(buf, "%hd", &MaxSearchDepth);
783 TCflag = !(MaxSearchDepth > 0);
788 Raw_ChangeHashDepth(void)
790 printf("hashdepth = ");
791 scanf("%hd", &HashDepth);
792 printf("MoveLimit = ");
793 scanf("%hd", &HashMoveLimit);
798 Raw_SetContempt(void)
800 printf("contempt = ");
801 scanf("%hd", &contempt);
806 Raw_ChangeXwindow(void)
809 scanf("%hd", &xwndw);
814 * Raw_ShowPostnValue(short sq)
815 * must have called ExaminePosition() first
818 Raw_ShowPostnValue(short sq)
820 (void) ScorePosition(color[sq]);
822 if (color[sq] != neutral)
824 #if defined SAVE_SVALUE
825 printf("???%c ", (color[sq] == white)?'b':'w');
827 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
840 short c, p, sq, tp, tc, tsq, score, j, k;
843 ExaminePosition(opponent);
844 Raw_ShowMessage("Enter piece: ");
848 if ((s[0] == 'b') || (s[0] == 'B'))
851 if ((s[0] == 'w') || (s[0] == 'W'))
854 for (p = king; p > no_piece; p--)
856 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
862 for (j = (NO_ROWS - 1); j >= 0; j--)
864 for (k = 0; k < (NO_COLS); k++)
866 sq = j*(NO_COLS) + k;
871 tsq = PieceList[c][1];
872 PieceList[c][1] = sq;
873 Raw_ShowPostnValue(sq);
874 PieceList[c][1] = tsq;
883 score = ScorePosition(opponent);
885 for (j = (NO_ROWS - 1); j >= 0; j--)
887 for (k = 0; k < (NO_COLS); k++)
889 sq = j*(NO_COLS) + k;
891 if (color[sq] != neutral)
893 #if defined SAVE_SVALUE
894 printf("%?????%c ", (color[sq] == white)?'b':'w');
896 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
908 printf("stage = %d\n", stage);
909 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
910 mtl[computer], pscore[computer], GameType[computer],
911 mtl[opponent], pscore[opponent], GameType[opponent]);
916 Raw_DoTable(short table[NO_SQUARES])
919 ExaminePosition(opponent);
921 for (j = (NO_ROWS - 1); j >= 0; j--)
923 for (k = 0; k < NO_COLS; k++)
925 sq = j*(NO_ROWS) + k;
926 printf("%3d ", table[sq]);
935 Raw_ShowPostnValues(void)
937 short sq, score, j, k;
938 ExaminePosition(opponent);
940 for (j = (NO_ROWS - 1); j >= 0; j--)
942 for (k = 0; k < NO_COLS; k++)
944 sq = j * NO_COLS + k;
945 Raw_ShowPostnValue(sq);
951 score = ScorePosition(opponent);
952 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
953 mtl[computer], pscore[computer], GameType[computer],
954 mtl[opponent], pscore[opponent], GameType[opponent]);
955 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
960 Raw_PollForInput(void)
964 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
967 static struct pollfd pollfds[1] = { [0] = { .fd = STDIN_FILENO,
968 .events = POLLIN } };
969 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
971 perror("polling standard input");
975 if (cnt) { /* if anything to read, or error occured */
977 flag.back = true; /* previous: flag.timeout = true; */
978 flag.bothsides = false;
982 struct display raw_display =
984 .ChangeAlphaWindow = Raw_ChangeAlphaWindow,
985 .ChangeBetaWindow = Raw_ChangeBetaWindow,
986 .ChangeHashDepth = Raw_ChangeHashDepth,
987 .ChangeSearchDepth = Raw_ChangeSearchDepth,
988 .ChangeXwindow = Raw_ChangeXwindow,
989 .ClearScreen = Raw_ClearScreen,
990 .DoDebug = Raw_DoDebug,
991 .DoTable = Raw_DoTable,
992 .EditBoard = Raw_EditBoard,
993 .ExitShogi = Raw_ExitShogi,
994 .GiveHint = Raw_GiveHint,
995 .Initialize = Raw_Initialize,
996 .ShowNodeCnt = Raw_ShowNodeCnt,
997 .OutputMove = Raw_OutputMove,
998 .PollForInput = Raw_PollForInput,
999 .SetContempt = Raw_SetContempt,
1000 .SearchStartStuff = Raw_SearchStartStuff,
1001 .SelectLevel = Raw_SelectLevel,
1002 .ShowCurrentMove = Raw_ShowCurrentMove,
1003 .ShowDepth = Raw_ShowDepth,
1004 .ShowGameType = Raw_ShowGameType,
1005 .ShowLine = Raw_ShowLine,
1006 .ShowMessage = Raw_ShowMessage,
1007 .AlwaysShowMessage = Raw_AlwaysShowMessage,
1008 .Printf = Raw_Printf,
1009 .doRequestInputString = Raw_doRequestInputString,
1010 .GetString = Raw_GetString,
1011 .SetupBoard = Raw_SetupBoard,
1012 .ShowPatternCount = Raw_ShowPatternCount,
1013 .ShowPostnValue = Raw_ShowPostnValue,
1014 .ShowPostnValues = Raw_ShowPostnValues,
1015 .ShowPrompt = Raw_ShowPrompt,
1016 .ShowResponseTime = Raw_ShowResponseTime,
1017 .ShowResults = Raw_ShowResults,
1018 .ShowSidetoMove = Raw_ShowSidetoMove,
1019 .ShowStage = Raw_ShowStage,
1020 .TerminateSearch = Raw_TerminateSearch,
1021 .UpdateClocks = Raw_UpdateClocks,
1022 .UpdateDisplay = Raw_UpdateDisplay,