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 *format, ...)
119 va_start(ap, format);
127 Raw_AlwaysShowMessage(const char *format, ...)
130 va_start(ap, format);
138 Raw_Printf(const char *format, ...)
141 va_start(ap, format);
148 Raw_doRequestInputString(const char* fmt, char* buffer)
155 Raw_GetString(char* sx)
161 while(!eof && !sx[0])
162 eof = (fgets(sx, 80, stdin) == NULL);
164 /* remove any trailing newline */
165 nl = strchr(sx, '\n');
174 Raw_ShowNodeCnt(long NodeCnt)
176 printf("Nodes = %ld Nodes/sec = %ld\n",
177 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
182 Raw_ShowPatternCount(short side, short n)
185 printf("%s%s matches %d pattern(s)\n", xboard ? "# " : "" , ColorStr[side], n);
190 Raw_ShowResponseTime(void)
196 Raw_ShowResults(short score, unsigned short *bstline, char ch)
198 if (flag.post && (xboard || !XSHOGI))
201 printf("%2d%c %6d %4ld %8ld ",
202 Sdepth, ch, score, et / 100, NodeCnt);
203 Raw_ShowLine(bstline);
209 Raw_ShowSidetoMove(void)
217 printf("stage = %d\n", stage);
218 printf("balance[black] = %d balance[white] = %d\n",
219 balance[black], balance[white]);
222 /****************************************
223 * End of trivial output routines.
224 ****************************************/
234 /* needed because of inconsistency between MSVC run-time system and gcc includes */
235 setbuf(stdout, NULL);
238 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
240 # ifdef HAVE_SETLINEBUF
243 # error "Need setvbuf() or setlinebuf() to compile gnushogi!"
247 printf("GNU Shogi %s\n", PACKAGE_VERSION);
252 if (!TCflag && (MaxResponseTime == 0))
253 MaxResponseTime = 15L * 100L;
261 /* CHECKME: what purpose does this next statement serve? */
262 signal(SIGTERM, SIG_IGN);
272 Raw_TerminateSearch(int sig)
274 #ifdef INTERRUPT_TEST
275 ElapsedTime(INIT_INTERRUPT_MODE);
279 flag.back = true; /* previous: flag.timeout = true; */
281 flag.bothsides = false;
289 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
290 printf("----------------------------------------------------------------\n");
291 printf("7g7f move from 7g to 7f quit Exit Shogi\n");
292 printf("S6h move silver to 6h beep turn %s\n",
293 (flag.beep) ? "OFF" : "ON");
294 printf("2d2c+ move to 2c and promote material turn %s\n",
295 (flag.material) ? "OFF" : "ON");
296 printf("P*5e drop pawn to 5e easy turn %s\n",
297 (flag.easy) ? "OFF" : "ON");
298 printf("tsume toggle tsume mode hash turn %s\n",
299 (flag.hash) ? "OFF" : "ON");
300 printf("bd redraw board reverse board display\n");
301 printf("list game to shogi.lst book turn %s used %d of %d\n",
302 (Book) ? "OFF" : "ON", bookcount, booksize);
303 printf("undo undo last ply remove take back a move\n");
304 printf("edit edit board force toggle manual move mode\n");
305 printf("switch sides with computer both computer match\n");
306 printf("black computer plays black white computer plays white\n");
307 printf("sd set search depth clock set time control\n");
308 printf("post principle variation hint suggest a move\n");
309 printf("save game to file get game from file\n");
310 printf("xsave pos. to xshogi file xget pos. from xshogi file\n");
311 printf("random randomize play new start new game\n");
312 printf("setup first \n");
313 printf("go computer plays now material turn %s\n",
314 flag.material ? "OFF" : "ON");
315 printf("level time control gamein \n");
316 printf("time set engine clock otime set opponent clock\n");
317 printf("Awindow Bwindow \n");
318 printf("rcptr turn %3s bsave book save\n",
319 flag.rcptr ? "OFF" : "ON ");
320 printf("hashdepth hard turn easy OFF\n");
321 printf("contempt xwndw \n");
322 printf("rv turn %3s coords turn %s\n",
323 flag.rv ? "OFF" : "ON ", flag.coords ? "OFF" : "ON");
324 printf("stars turn %3s moves \n",
325 flag.stars ? "OFF" : "ON ");
327 printf("debug depth alias for 'sd'\n");
328 printf("----------------------------------------------------------------\n");
329 printf("Computer: %-12s Opponent: %s\n",
330 ColorStr[computer], ColorStr[opponent]);
331 printf("Depth: %-12d Response time: %ld sec\n",
332 MaxSearchDepth, MaxResponseTime/100);
333 printf("Random: %-12s Easy mode: %s\n",
334 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
335 printf("Beep: %-12s Transposition file: %s\n",
336 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
337 printf("Tsume: %-12s Force: %s\n",
338 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
339 printf("Time Control %s %d moves %ld sec %d add %d depth\n",
340 (TCflag) ? "ON" : "OFF",
341 TimeControl.moves[black], TimeControl.clock[black] / 100,
342 TCadd/100, MaxSearchDepth);
347 * Set up a board position. Pieces are entered by typing the piece followed
348 * by the location. For example, Nf3 will place a knight on square f3.
353 short a, r, c, sq, i, found;
356 flag.regularstart = true;
359 Raw_UpdateDisplay(0, 0, 1, 0);
360 printf(". Exit to main\n");
361 printf("# Clear board\n");
362 printf("c Change sides\n");
363 printf("enter piece & location:\n");
377 for (sq = 0; sq < NO_SQUARES; sq++)
379 board[sq] = no_piece;
394 for (i = pawn; i <= king; i++)
396 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
404 printf("# Invalid piece type '%c'\n", s[0]);
411 if ((c < 0) || (c >= NO_COLS) || (r < 0) || (r >= NO_ROWS)) {
412 printf("# Out-of-board position '%c%c'\n", s[1], s[2]);
418 for (i = no_piece; i <= king; i++)
420 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
424 board[sq] = promoted[i];
434 printf("# Invalid piece type '%c'\n", s[0]);
437 for (sq = 0; sq < NO_SQUARES; sq++)
438 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
446 Raw_UpdateDisplay(0, 0, 1, 0);
451 * Set up a board position.
452 * Nine lines of nine characters are used to setup the board. 9a-1a is the
453 * first line. White pieces are represented by uppercase characters.
464 fgets(s, 80, stdin); /* skip "setup" command */
466 for (r = NO_ROWS - 1; r >= 0; r--)
470 for (c = 0; c <= (NO_COLS - 1); c++)
475 board[sq] = no_piece;
477 for (i = no_piece; i <= king; i++)
485 else if (ch == qxx[i])
495 for (sq = 0; sq < NO_SQUARES; sq++)
496 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
500 Raw_UpdateDisplay(0, 0, 1, 0);
501 fputs("Setup successful\n", stdout);
506 Raw_SearchStartStuff(short side)
510 printf("\nMove# %d Target = %ld Clock: %ld\n",
512 ResponseTime, TimeControl.clock[side]);
522 printf("Illegal position.\n");
526 if (mvstr[0][0] == '\0')
530 if (XSHOGI && xboard) /* xboard: print move in XBoard format, with 'move' prefix */
531 printf("move %s\n", mvstr[0]);
533 /* add remaining time in milliseconds to xshogi */
534 printf("%d. ... %s %ld\n", mycnt1, mvstr[0],
535 (TimeControl.clock[player] - et) * 10);
537 printf("%d. ... %s\n", mycnt1, mvstr[0]);
540 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
541 || (root->score == (SCORE_LIMIT + 998)))
554 if (Tree[t].f || Tree[t].t)
562 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
563 (et > 100) ? (NodeCnt / (et / 100)) : 0,
564 EADD, EGET, reminus, replus);
566 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
567 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
570 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
574 printf("My move is: %5s\n", mvstr[0]);
581 if (root->flags & draw)
582 fputs("Drawn game!\n", stdout);
583 else if (root->score == -(SCORE_LIMIT + 999))
584 printf("%s mates!\n", ColorStr[opponent]);
585 else if (root->score == (SCORE_LIMIT + 998))
586 printf("%s mates!\n", ColorStr[computer]);
588 else if (!XSHOGI && (root->score < -SCORE_LIMIT))
589 printf("%s%s has a forced mate in %d moves!\n", xboard ? "# " : "",
590 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
591 else if (!XSHOGI && (root->score > SCORE_LIMIT))
592 printf("%s%s has a forced mate in %d moves!\n", xboard ? "# " : "",
593 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
594 #endif /* VERYBUGGY */
599 Raw_UpdateClocks(void)
605 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
610 if (redraw && !XSHOGI)
613 r = (short)(TimeControl.clock[black] / 6000);
614 c = (short)((TimeControl.clock[black] % 6000) / 100);
615 l = (short)(TimeControl.clock[white] / 6000);
616 m = (short)((TimeControl.clock[white] % 6000) / 100);
617 printf("Black %d:%02d White %d:%02d\n", r, c, l, m);
620 for (r = (NO_ROWS - 1); r >= 0; r--)
622 for (c = 0; c <= (NO_COLS - 1); c++)
626 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
628 pc = (is_promoted[board[l]] ? '+' : ' ');
630 if (color[l] == neutral)
632 else if (color[l] == black)
633 printf("%c%c", pc, qxx[board[l]]);
635 printf("%c%c", pc, pxx[board[l]]);
645 for (side = black; side <= white; side++)
648 printf((side == black)?"black ":"white ");
650 for (piece = pawn; piece <= king; piece++)
652 if ((c = Captured[side][piece]))
653 printf("%i%c ", c, pxx[piece]);
664 Raw_ChangeAlphaWindow(void)
666 printf("WAwindow: ");
667 scanf("%hd", &WAwindow);
668 printf("BAwindow: ");
669 scanf("%hd", &BAwindow);
674 Raw_ChangeBetaWindow(void)
676 printf("WBwindow: ");
677 scanf("%hd", &WBwindow);
678 printf("BBwindow: ");
679 scanf("%hd", &BBwindow);
688 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
689 printf("Hint: %s\n", mvstr[0]);
692 fputs("I have no idea.\n", stdout);
697 Raw_SelectLevel(char *sx)
699 /* FIXME: NO_SQUARES is nonsense here */
700 char T[NO_SQUARES + 1], *p;
702 strncpy(T, sx, NO_SQUARES);
703 T[NO_SQUARES] = '\0';
707 /* if line empty, ask for input */
710 fputs("Enter #moves #minutes: ", stdout);
711 fgets(T, NO_SQUARES + 1, stdin);
714 /* skip blackspace */
715 for (p = T; *p == ' '; p++) ;
717 /* could be moves or a fischer clock */
720 /* its a fischer clock game */
723 TCminutes = (short)strtol(p, &q, 10);
724 TCadd = (short)strtol(q, NULL, 10) *100;
733 TCmoves = (short)strtol(p, &q, 10);
734 TCminutes = (short)strtol(q, &q, 10);
737 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
742 fputs("Operator time (hundredths) = ", stdout);
743 scanf("%hd", &OperatorTime);
749 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
750 TCminutes = TCseconds = 0;
759 int min, sec=0, inc, mps;
760 /* parse regular "level MPS TC INC" command of WB protocol */
761 if (sscanf(sx, "%d %d %d", &mps, &min, &inc) != 3)
762 sscanf(sx, "%d %d:%d %d", &mps, &min, &sec, &inc);
763 TCminutes = min; TCseconds = sec;
764 TCadd = inc*100; TCmoves = mps;
765 MaxResponseTime = 0; TCflag = true;
766 if (!mps) /* Fischer TC or sudden death */
769 TCflag = 2; /* kludge to requests special calculation of ResponseTime */
773 TimeControl.clock[black] = TimeControl.clock[white] = 0;
778 printf("Clocks: %ld %ld\n",
779 TimeControl.clock[black] * 10,
780 TimeControl.clock[white] * 10);
786 Raw_ChangeSearchDepth(char *sx)
789 strncpy(buf, sx, 80); buf[80] = '\0';
790 /* if line empty, ask for input */
793 fgets(buf, 80+1, stdin);
795 sscanf(buf, "%hd", &MaxSearchDepth);
796 TCflag = !(MaxSearchDepth > 0);
801 Raw_ChangeHashDepth(void)
803 printf("hashdepth = ");
804 scanf("%hd", &HashDepth);
805 printf("MoveLimit = ");
806 scanf("%hd", &HashMoveLimit);
811 Raw_SetContempt(void)
813 printf("contempt = ");
814 scanf("%hd", &contempt);
819 Raw_ChangeXwindow(void)
822 scanf("%hd", &xwndw);
827 * Raw_ShowPostnValue(short sq)
828 * must have called ExaminePosition() first
831 Raw_ShowPostnValue(short sq)
833 (void) ScorePosition(color[sq]);
835 if (color[sq] != neutral)
837 #if defined SAVE_SVALUE
838 printf("???%c ", (color[sq] == white)?'b':'w');
840 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
853 short c, p, sq, tp, tc, tsq, score, j, k;
856 ExaminePosition(opponent);
857 Raw_ShowMessage("Enter piece: ");
861 if ((s[0] == 'b') || (s[0] == 'B'))
864 if ((s[0] == 'w') || (s[0] == 'W'))
867 for (p = king; p > no_piece; p--)
869 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
875 for (j = (NO_ROWS - 1); j >= 0; j--)
877 for (k = 0; k < (NO_COLS); k++)
879 sq = j*(NO_COLS) + k;
884 tsq = PieceList[c][1];
885 PieceList[c][1] = sq;
886 Raw_ShowPostnValue(sq);
887 PieceList[c][1] = tsq;
896 score = ScorePosition(opponent);
898 for (j = (NO_ROWS - 1); j >= 0; j--)
900 for (k = 0; k < (NO_COLS); k++)
902 sq = j*(NO_COLS) + k;
904 if (color[sq] != neutral)
906 #if defined SAVE_SVALUE
907 printf("%?????%c ", (color[sq] == white)?'b':'w');
909 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
921 printf("stage = %d\n", stage);
922 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
923 mtl[computer], pscore[computer], GameType[computer],
924 mtl[opponent], pscore[opponent], GameType[opponent]);
929 Raw_DoTable(short table[NO_SQUARES])
932 ExaminePosition(opponent);
934 for (j = (NO_ROWS - 1); j >= 0; j--)
936 for (k = 0; k < NO_COLS; k++)
938 sq = j*(NO_ROWS) + k;
939 printf("%3d ", table[sq]);
948 Raw_ShowPostnValues(void)
950 short sq, score, j, k;
951 ExaminePosition(opponent);
953 for (j = (NO_ROWS - 1); j >= 0; j--)
955 for (k = 0; k < NO_COLS; k++)
957 sq = j * NO_COLS + k;
958 Raw_ShowPostnValue(sq);
964 score = ScorePosition(opponent);
965 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
966 mtl[computer], pscore[computer], GameType[computer],
967 mtl[opponent], pscore[opponent], GameType[opponent]);
968 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
973 Raw_PollForInput(void)
977 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
980 static struct pollfd pollfds[1] = { [0] = { .fd = STDIN_FILENO,
981 .events = POLLIN } };
982 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
984 perror("polling standard input");
988 if (cnt && InputCommand(false)) { /* if anything to read, or error occured */
990 flag.back = true; /* previous: flag.timeout = true; */
991 flag.bothsides = false;
995 struct display raw_display =
997 .ChangeAlphaWindow = Raw_ChangeAlphaWindow,
998 .ChangeBetaWindow = Raw_ChangeBetaWindow,
999 .ChangeHashDepth = Raw_ChangeHashDepth,
1000 .ChangeSearchDepth = Raw_ChangeSearchDepth,
1001 .ChangeXwindow = Raw_ChangeXwindow,
1002 .ClearScreen = Raw_ClearScreen,
1003 .DoDebug = Raw_DoDebug,
1004 .DoTable = Raw_DoTable,
1005 .EditBoard = Raw_EditBoard,
1006 .ExitShogi = Raw_ExitShogi,
1007 .GiveHint = Raw_GiveHint,
1008 .Initialize = Raw_Initialize,
1009 .ShowNodeCnt = Raw_ShowNodeCnt,
1010 .OutputMove = Raw_OutputMove,
1011 .PollForInput = Raw_PollForInput,
1012 .SetContempt = Raw_SetContempt,
1013 .SearchStartStuff = Raw_SearchStartStuff,
1014 .SelectLevel = Raw_SelectLevel,
1015 .ShowCurrentMove = Raw_ShowCurrentMove,
1016 .ShowDepth = Raw_ShowDepth,
1017 .ShowGameType = Raw_ShowGameType,
1018 .ShowLine = Raw_ShowLine,
1019 .ShowMessage = Raw_ShowMessage,
1020 .AlwaysShowMessage = Raw_AlwaysShowMessage,
1021 .Printf = Raw_Printf,
1022 .doRequestInputString = Raw_doRequestInputString,
1023 .GetString = Raw_GetString,
1024 .SetupBoard = Raw_SetupBoard,
1025 .ShowPatternCount = Raw_ShowPatternCount,
1026 .ShowPostnValue = Raw_ShowPostnValue,
1027 .ShowPostnValues = Raw_ShowPostnValues,
1028 .ShowPrompt = Raw_ShowPrompt,
1029 .ShowResponseTime = Raw_ShowResponseTime,
1030 .ShowResults = Raw_ShowResults,
1031 .ShowSidetoMove = Raw_ShowSidetoMove,
1032 .ShowStage = Raw_ShowStage,
1033 .TerminateSearch = Raw_TerminateSearch,
1034 .UpdateClocks = Raw_UpdateClocks,
1035 .UpdateDisplay = Raw_UpdateDisplay,