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 unsigned short MV[MAXDEPTH];
53 extern short pscore[];
55 /****************************************
56 * forward declarations
57 ****************************************/
59 void Raw_UpdateDisplay(short f, short t, short redraw, short isspec);
61 /****************************************
62 * Trivial output functions.
63 ****************************************/
68 if (!barebones && !XSHOGI)
76 if (!barebones && !XSHOGI)
77 fputs("\nYour move is? ", stdout);
82 Raw_ShowCurrentMove(short pnt, short f, short t)
88 Raw_ShowDepth(char ch)
90 if (!barebones && !XSHOGI)
91 printf("Depth= %d%c \n", Sdepth, ch);
96 Raw_ShowGameType(void)
99 printf("%c vs. %c\n", GameType[black], GameType[white]);
104 Raw_ShowLine(unsigned short *bstline)
108 for (i = 1; bstline[i] > 0; i++)
110 if ((i > 1) && (i % 8 == 1))
113 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
114 printf("%5s ", mvstr[0]);
122 Raw_ShowMessage(char *s)
130 Raw_AlwaysShowMessage(const char *format, ...)
133 va_start(ap, format);
141 Raw_Printf(const char *format, ...)
144 va_start(ap, format);
151 Raw_doRequestInputString(const char* fmt, char* buffer)
158 Raw_GetString(char* sx)
164 while(!eof && !sx[0])
165 eof = (fgets(sx, 80, stdin) == NULL);
167 /* remove any trailing newline */
168 nl = strchr(sx, '\n');
177 Raw_ShowNodeCnt(long NodeCnt)
179 printf("Nodes = %ld Nodes/sec = %ld\n",
180 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
185 Raw_ShowPatternCount(short side, short n)
188 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
193 Raw_ShowResponseTime(void)
199 Raw_ShowResults(short score, unsigned short *bstline, char ch)
201 if (flag.post && !XSHOGI)
204 printf("%2d%c %6d %4ld %8ld ",
205 Sdepth, ch, score, et / 100, NodeCnt);
206 Raw_ShowLine(bstline);
212 Raw_ShowSidetoMove(void)
220 printf("stage = %d\n", stage);
221 printf("balance[black] = %d balance[white] = %d\n",
222 balance[black], balance[white]);
225 /****************************************
226 * End of trivial output routines.
227 ****************************************/
237 /* needed because of inconsistency between MSVC run-time system and gcc includes */
238 setbuf(stdout, NULL);
241 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
243 # ifdef HAVE_SETLINEBUF
246 # error "Need setvbuf() or setlinebuf() to compile gnushogi!"
250 printf("GNU Shogi %s\n", PACKAGE_VERSION);
255 if (!TCflag && (MaxResponseTime == 0))
256 MaxResponseTime = 15L * 100L;
264 /* CHECKME: what purpose does this next statement serve? */
265 signal(SIGTERM, SIG_IGN);
279 Raw_ShowMessage("Abort? ");
282 if (strcmp(s, "yes") == 0)
288 Raw_TerminateSearch(int sig)
290 #ifdef INTERRUPT_TEST
291 ElapsedTime(INIT_INTERRUPT_MODE);
295 flag.back = true; /* previous: flag.timeout = true; */
297 flag.bothsides = false;
305 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
306 printf("----------------------------------------------------------------\n");
307 printf("7g7f move from 7g to 7f quit Exit Shogi\n");
308 printf("S6h move silver to 6h beep turn %s\n",
309 (flag.beep) ? "OFF" : "ON");
310 printf("2d2c+ move to 2c and promote material turn %s\n",
311 (flag.material) ? "OFF" : "ON");
312 printf("P*5e drop pawn to 5e easy turn %s\n",
313 (flag.easy) ? "OFF" : "ON");
314 printf("tsume toggle tsume mode hash turn %s\n",
315 (flag.hash) ? "OFF" : "ON");
316 printf("bd redraw board reverse board display\n");
317 printf("list game to shogi.lst book turn %s used %d of %d\n",
318 (Book) ? "OFF" : "ON", bookcount, booksize);
319 printf("undo undo last ply remove take back a move\n");
320 printf("edit edit board force toggle manual move mode\n");
321 printf("switch sides with computer both computer match\n");
322 printf("black computer plays black white computer plays white\n");
323 printf("sd set search depth clock set time control\n");
324 printf("post principle variation hint suggest a move\n");
325 printf("save game to file get game from file\n");
326 printf("xsave pos. to xshogi file xget pos. from xshogi file\n");
327 printf("random randomize play new start new game\n");
328 printf("setup first \n");
329 printf("go computer plays now material turn %s\n",
330 flag.material ? "OFF" : "ON");
331 printf("level time control gamein \n");
332 printf("time set engine clock otime set opponent clock\n");
333 printf("Awindow Bwindow \n");
334 printf("rcptr turn %3s bsave book save\n",
335 flag.rcptr ? "OFF" : "ON ");
336 printf("hashdepth hard turn easy OFF\n");
337 printf("contempt xwndw \n");
338 printf("rv turn %3s coords turn %s\n",
339 flag.rv ? "OFF" : "ON ", flag.coords ? "OFF" : "ON");
340 printf("stars turn %3s moves \n",
341 flag.stars ? "OFF" : "ON ");
343 printf("debug depth alias for 'sd'\n");
344 printf("----------------------------------------------------------------\n");
345 printf("Computer: %-12s Opponent: %s\n",
346 ColorStr[computer], ColorStr[opponent]);
347 printf("Depth: %-12d Response time: %ld sec\n",
348 MaxSearchDepth, MaxResponseTime/100);
349 printf("Random: %-12s Easy mode: %s\n",
350 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
351 printf("Beep: %-12s Transposition file: %s\n",
352 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
353 printf("Tsume: %-12s Force: %s\n",
354 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
355 printf("Time Control %s %d moves %ld sec %d add %d depth\n",
356 (TCflag) ? "ON" : "OFF",
357 TimeControl.moves[black], TimeControl.clock[black] / 100,
358 TCadd/100, MaxSearchDepth);
363 * Set up a board position. Pieces are entered by typing the piece followed
364 * by the location. For example, Nf3 will place a knight on square f3.
369 short a, r, c, sq, i, found;
372 flag.regularstart = true;
375 Raw_UpdateDisplay(0, 0, 1, 0);
376 fputs(". Exit to main\n", stdout);
377 fputs("# Clear board\n", stdout);
378 fputs("c Change sides\n", stdout);
379 fputs("enter piece & location: \n", stdout);
390 for (sq = 0; sq < NO_SQUARES; sq++)
392 board[sq] = no_piece;
404 for (i = pawn; i <= king; i++)
406 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
423 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
427 board[sq] = no_piece;
429 for (i = no_piece; i <= king; i++)
431 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
434 board[sq] = promoted[i];
449 for (sq = 0; sq < NO_SQUARES; sq++)
450 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
458 Raw_UpdateDisplay(0, 0, 1, 0);
463 * Set up a board position.
464 * Nine lines of nine characters are used to setup the board. 9a-1a is the
465 * first line. White pieces are represented by uppercase characters.
476 fgets(s, 80, stdin); /* skip "setup" command */
478 for (r = NO_ROWS - 1; r >= 0; r--)
482 for (c = 0; c <= (NO_COLS - 1); c++)
487 board[sq] = no_piece;
489 for (i = no_piece; i <= king; i++)
497 else if (ch == qxx[i])
507 for (sq = 0; sq < NO_SQUARES; sq++)
508 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
512 Raw_UpdateDisplay(0, 0, 1, 0);
513 fputs("Setup successful\n", stdout);
518 Raw_SearchStartStuff(short side)
522 printf("\nMove# %d Target = %ld Clock: %ld\n",
524 ResponseTime, TimeControl.clock[side]);
534 printf("Illegal position.\n");
538 if (mvstr[0][0] == '\0')
542 /* add remaining time in milliseconds to xshogi */
543 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
544 (TimeControl.clock[player] - et) * 10);
546 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
549 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
550 || (root->score == (SCORE_LIMIT + 998)))
563 if (Tree[t].f || Tree[t].t)
571 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
572 (et > 100) ? (NodeCnt / (et / 100)) : 0,
573 EADD, EGET, reminus, replus);
575 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
576 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
579 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
583 printf("My move is: %5s\n", mvstr[0]);
590 if (root->flags & draw)
591 fputs("Drawn game!\n", stdout);
592 else if (root->score == -(SCORE_LIMIT + 999))
593 printf("%s mates!\n", ColorStr[opponent]);
594 else if (root->score == (SCORE_LIMIT + 998))
595 printf("%s mates!\n", ColorStr[computer]);
597 else if (!barebones && (root->score < -SCORE_LIMIT))
598 printf("%s has a forced mate in %d moves!\n",
599 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
600 else if (!barebones && (root->score > SCORE_LIMIT))
601 printf("%s has a forced mate in %d moves!\n",
602 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
603 #endif /* VERYBUGGY */
608 Raw_UpdateClocks(void)
614 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
619 if (redraw && !XSHOGI)
622 r = (short)(TimeControl.clock[black] / 6000);
623 c = (short)((TimeControl.clock[black] % 6000) / 100);
624 l = (short)(TimeControl.clock[white] / 6000);
625 m = (short)((TimeControl.clock[white] % 6000) / 100);
626 printf("Black %d:%02d White %d:%02d\n", r, c, l, m);
629 for (r = (NO_ROWS - 1); r >= 0; r--)
631 for (c = 0; c <= (NO_COLS - 1); c++)
635 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
637 pc = (is_promoted[board[l]] ? '+' : ' ');
639 if (color[l] == neutral)
641 else if (color[l] == black)
642 printf("%c%c", pc, qxx[board[l]]);
644 printf("%c%c", pc, pxx[board[l]]);
654 for (side = black; side <= white; side++)
657 printf((side == black)?"black ":"white ");
659 for (piece = pawn; piece <= king; piece++)
661 if ((c = Captured[side][piece]))
662 printf("%i%c ", c, pxx[piece]);
673 Raw_ChangeAlphaWindow(void)
675 printf("WAwindow: ");
676 scanf("%hd", &WAwindow);
677 printf("BAwindow: ");
678 scanf("%hd", &BAwindow);
683 Raw_ChangeBetaWindow(void)
685 printf("WBwindow: ");
686 scanf("%hd", &WBwindow);
687 printf("BBwindow: ");
688 scanf("%hd", &BBwindow);
697 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
698 printf("Hint: %s\n", mvstr[0]);
701 fputs("I have no idea.\n", stdout);
706 Raw_SelectLevel(char *sx)
708 /* FIXME: NO_SQUARES is nonsense here */
709 char T[NO_SQUARES + 1], *p;
711 strncpy(T, sx, NO_SQUARES);
712 T[NO_SQUARES] = '\0';
714 /* if line empty, ask for input */
717 fputs("Enter #moves #minutes: ", stdout);
718 fgets(T, NO_SQUARES + 1, stdin);
721 /* skip blackspace */
722 for (p = T; *p == ' '; p++) ;
724 /* could be moves or a fischer clock */
727 /* its a fischer clock game */
730 TCminutes = (short)strtol(p, &q, 10);
731 TCadd = (short)strtol(q, NULL, 10) *100;
740 TCmoves = (short)strtol(p, &q, 10);
741 TCminutes = (short)strtol(q, &q, 10);
744 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
749 fputs("Operator time (hundredths) = ", stdout);
750 scanf("%hd", &OperatorTime);
756 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
757 TCminutes = TCseconds = 0;
766 TimeControl.clock[black] = TimeControl.clock[white] = 0;
771 printf("Clocks: %ld %ld\n",
772 TimeControl.clock[black] * 10,
773 TimeControl.clock[white] * 10);
779 Raw_ChangeSearchDepth(char *sx)
782 strncpy(buf, sx, 80); buf[80] = '\0';
783 /* if line empty, ask for input */
786 fgets(buf, 80+1, stdin);
788 sscanf(buf, "%hd", &MaxSearchDepth);
789 TCflag = !(MaxSearchDepth > 0);
794 Raw_ChangeHashDepth(void)
796 printf("hashdepth = ");
797 scanf("%hd", &HashDepth);
798 printf("MoveLimit = ");
799 scanf("%hd", &HashMoveLimit);
804 Raw_SetContempt(void)
806 printf("contempt = ");
807 scanf("%hd", &contempt);
812 Raw_ChangeXwindow(void)
815 scanf("%hd", &xwndw);
820 * Raw_ShowPostnValue(short sq)
821 * must have called ExaminePosition() first
824 Raw_ShowPostnValue(short sq)
826 (void) ScorePosition(color[sq]);
828 if (color[sq] != neutral)
830 #if defined SAVE_SVALUE
831 printf("???%c ", (color[sq] == white)?'b':'w');
833 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
846 short c, p, sq, tp, tc, tsq, score, j, k;
849 ExaminePosition(opponent);
850 Raw_ShowMessage("Enter piece: ");
854 if ((s[0] == 'b') || (s[0] == 'B'))
857 if ((s[0] == 'w') || (s[0] == 'W'))
860 for (p = king; p > no_piece; p--)
862 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
868 for (j = (NO_ROWS - 1); j >= 0; j--)
870 for (k = 0; k < (NO_COLS); k++)
872 sq = j*(NO_COLS) + k;
877 tsq = PieceList[c][1];
878 PieceList[c][1] = sq;
879 Raw_ShowPostnValue(sq);
880 PieceList[c][1] = tsq;
889 score = ScorePosition(opponent);
891 for (j = (NO_ROWS - 1); j >= 0; j--)
893 for (k = 0; k < (NO_COLS); k++)
895 sq = j*(NO_COLS) + k;
897 if (color[sq] != neutral)
899 #if defined SAVE_SVALUE
900 printf("%?????%c ", (color[sq] == white)?'b':'w');
902 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
914 printf("stage = %d\n", stage);
915 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
916 mtl[computer], pscore[computer], GameType[computer],
917 mtl[opponent], pscore[opponent], GameType[opponent]);
922 Raw_DoTable(short table[NO_SQUARES])
925 ExaminePosition(opponent);
927 for (j = (NO_ROWS - 1); j >= 0; j--)
929 for (k = 0; k < NO_COLS; k++)
931 sq = j*(NO_ROWS) + k;
932 printf("%3d ", table[sq]);
941 Raw_ShowPostnValues(void)
943 short sq, score, j, k;
944 ExaminePosition(opponent);
946 for (j = (NO_ROWS - 1); j >= 0; j--)
948 for (k = 0; k < NO_COLS; k++)
950 sq = j * NO_COLS + k;
951 Raw_ShowPostnValue(sq);
957 score = ScorePosition(opponent);
958 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
959 mtl[computer], pscore[computer], GameType[computer],
960 mtl[opponent], pscore[opponent], GameType[opponent]);
961 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
966 Raw_PollForInput(void)
970 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
973 static struct pollfd pollfds[1] = { [0] = { .fd = STDIN_FILENO,
974 .events = POLLIN } };
975 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
977 perror("polling standard input");
981 if (cnt) { /* if anything to read, or error occured */
983 flag.back = true; /* previous: flag.timeout = true; */
984 flag.bothsides = false;
988 struct display raw_display =
990 .ChangeAlphaWindow = Raw_ChangeAlphaWindow,
991 .ChangeBetaWindow = Raw_ChangeBetaWindow,
992 .ChangeHashDepth = Raw_ChangeHashDepth,
993 .ChangeSearchDepth = Raw_ChangeSearchDepth,
994 .ChangeXwindow = Raw_ChangeXwindow,
995 .ClearScreen = Raw_ClearScreen,
996 .DoDebug = Raw_DoDebug,
997 .DoTable = Raw_DoTable,
998 .EditBoard = Raw_EditBoard,
999 .ExitShogi = Raw_ExitShogi,
1000 .GiveHint = Raw_GiveHint,
1001 .Initialize = Raw_Initialize,
1002 .ShowNodeCnt = Raw_ShowNodeCnt,
1003 .OutputMove = Raw_OutputMove,
1004 .PollForInput = Raw_PollForInput,
1005 .SetContempt = Raw_SetContempt,
1006 .SearchStartStuff = Raw_SearchStartStuff,
1007 .SelectLevel = Raw_SelectLevel,
1008 .ShowCurrentMove = Raw_ShowCurrentMove,
1009 .ShowDepth = Raw_ShowDepth,
1010 .ShowGameType = Raw_ShowGameType,
1011 .ShowLine = Raw_ShowLine,
1012 .ShowMessage = Raw_ShowMessage,
1013 .AlwaysShowMessage = Raw_AlwaysShowMessage,
1014 .Printf = Raw_Printf,
1015 .doRequestInputString = Raw_doRequestInputString,
1016 .GetString = Raw_GetString,
1017 .SetupBoard = Raw_SetupBoard,
1018 .ShowPatternCount = Raw_ShowPatternCount,
1019 .ShowPostnValue = Raw_ShowPostnValue,
1020 .ShowPostnValues = Raw_ShowPostnValues,
1021 .ShowPrompt = Raw_ShowPrompt,
1022 .ShowResponseTime = Raw_ShowResponseTime,
1023 .ShowResults = Raw_ShowResults,
1024 .ShowSidetoMove = Raw_ShowSidetoMove,
1025 .ShowStage = Raw_ShowStage,
1026 .TerminateSearch = Raw_TerminateSearch,
1027 .UpdateClocks = Raw_UpdateClocks,
1028 .UpdateDisplay = Raw_UpdateDisplay,