4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * Copyright (c) 1988, 1989, 1990 John Stanback
11 * Copyright (c) 1992 Free Software Foundation
13 * This file is part of GNU SHOGI.
15 * GNU Shogi is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 3 of the License,
18 * or (at your option) any later version.
20 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with GNU Shogi; see the file COPYING. If not, see
27 * <http://www.gnu.org/licenses/>.
28 * ----------------------------------------------------------------------
36 #include <sys/param.h>
37 #include <sys/types.h>
46 unsigned short MV[MAXDEPTH];
52 extern short pscore[];
54 /****************************************
55 * forward declarations
56 ****************************************/
58 void Raw_UpdateDisplay(short f, short t, short redraw, short isspec);
60 /****************************************
61 * Trivial output functions.
62 ****************************************/
67 if (!barebones && !XSHOGI)
75 if (!barebones && !XSHOGI)
77 fputs("\nYour move is? ", stdout);
83 Raw_ShowCurrentMove(short pnt, short f, short t)
89 Raw_ShowDepth(char ch)
91 if (!barebones && !XSHOGI)
93 printf("Depth= %d%c ", Sdepth, ch);
100 Raw_ShowGameType(void)
103 printf("%c vs. %c\n", GameType[black], GameType[white]);
108 Raw_ShowLine(unsigned short *bstline)
112 for (i = 1; bstline[i] > 0; i++)
114 if ((i > 1) && (i % 8 == 1))
117 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
118 printf("%5s ", mvstr[0]);
126 Raw_ShowMessage(char *s)
134 Raw_AlwaysShowMessage(const char *format, ...)
137 va_start(ap, format);
145 Raw_Printf(const char *format, ...)
148 va_start(ap, format);
155 Raw_doRequestInputString(const char* fmt, char* buffer)
162 Raw_GetString(char* sx)
168 while(!eof && !sx[0])
169 eof = (fgets(sx, 80, stdin) == NULL);
171 /* remove any trailing newline */
172 nl = strchr(sx, '\n');
181 Raw_ShowNodeCnt(long NodeCnt)
183 printf("Nodes = %ld Nodes/sec = %ld\n",
184 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
189 Raw_ShowPatternCount(short side, short n)
192 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
197 Raw_ShowResponseTime(void)
203 Raw_ShowResults(short score, unsigned short *bstline, char ch)
205 if (flag.post && !XSHOGI)
208 printf("%2d%c %6d %4ld %8ld ",
209 Sdepth, ch, score, et / 100, NodeCnt);
210 Raw_ShowLine(bstline);
216 Raw_ShowSidetoMove(void)
224 printf("stage = %d\n", stage);
225 printf("balance[black] = %d balance[white] = %d\n",
226 balance[black], balance[white]);
229 /****************************************
230 * End of trivial output routines.
231 ****************************************/
241 /* needed because of inconsistency between MSVC run-time system and gcc includes */
242 setbuf(stdout, NULL);
244 #ifdef HAVE_SETLINEBUF
248 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
250 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
254 printf("GNU Shogi %s\n", PACKAGE_VERSION);
259 if (!TCflag && (MaxResponseTime == 0))
260 MaxResponseTime = 15L * 100L;
268 /* CHECKME: what purpose does this next statement serve? */
269 signal(SIGTERM, SIG_IGN);
283 Raw_ShowMessage("Abort? ");
286 if (strcmp(s, "yes") == 0)
292 Raw_TerminateSearch(int sig)
294 #ifdef INTERRUPT_TEST
295 ElapsedTime(INIT_INTERRUPT_MODE);
299 flag.back = true; /* previous: flag.timeout = true; */
301 flag.bothsides = false;
309 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
310 printf("----------------------------------"
311 "------------------------------\n");
312 fputs ("7g7f move from 7g to 7f quit Exit Shogi\n", stdout);
313 printf("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
314 printf("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
315 printf("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
316 printf("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
317 fputs ("bd redraw board reverse board display\n", stdout);
318 printf("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, booksize);
319 fputs ("undo undo last ply remove take back a move\n", stdout);
320 fputs ("edit edit board force toggle manual move mode\n", stdout);
321 fputs ("switch sides with computer both computer match\n", stdout);
322 fputs ("black computer plays black white computer plays white\n", stdout);
323 fputs ("depth set search depth clock set time control\n", stdout);
324 fputs ("post principle variation hint suggest a move\n", stdout);
325 fputs ("save game to file get game from file\n", stdout);
326 printf("xsave pos. to xshogi file xget"
327 " pos. from xshogi file\n");
328 fputs("random randomize play new start new game\n", stdout);
329 printf("--------------------------------"
330 "--------------------------------\n");
331 printf("Computer: %-12s Opponent: %s\n",
332 ColorStr[computer], ColorStr[opponent]);
333 printf("Depth: %-12d Response time: %ld sec\n",
334 MaxSearchDepth, MaxResponseTime/100);
335 printf("Random: %-12s Easy mode: %s\n",
336 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
337 printf("Beep: %-12s Transposition file: %s\n",
338 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
339 printf("Tsume: %-12s Force: %s\n",
340 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
341 printf("Time Control %s %d moves %ld sec %d add %d depth\n",
342 (TCflag) ? "ON" : "OFF",
343 TimeControl.moves[black], TimeControl.clock[black] / 100,
344 TCadd/100, MaxSearchDepth);
349 * Set up a board position. Pieces are entered by typing the piece followed
350 * by the location. For example, Nf3 will place a knight on square f3.
355 short a, r, c, sq, i, found;
358 flag.regularstart = true;
361 Raw_UpdateDisplay(0, 0, 1, 0);
362 fputs(". Exit to main\n", stdout);
363 fputs("# Clear board\n", stdout);
364 fputs("c Change sides\n", stdout);
365 fputs("enter piece & location: \n", stdout);
376 for (sq = 0; sq < NO_SQUARES; sq++)
378 board[sq] = no_piece;
390 for (i = pawn; i <= king; i++)
392 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
409 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
413 board[sq] = no_piece;
415 for (i = no_piece; i <= king; i++)
417 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
420 board[sq] = promoted[i];
435 for (sq = 0; sq < NO_SQUARES; sq++)
436 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
444 Raw_UpdateDisplay(0, 0, 1, 0);
449 * Set up a board position.
450 * Nine lines of nine characters are used to setup the board. 9a-1a is the
451 * first line. White pieces are represented by uppercase characters.
462 fgets(s, 80, stdin); /* skip "setup" command */
464 for (r = NO_ROWS - 1; r >= 0; r--)
468 for (c = 0; c <= (NO_COLS - 1); c++)
473 board[sq] = no_piece;
475 for (i = no_piece; i <= king; i++)
483 else if (ch == qxx[i])
493 for (sq = 0; sq < NO_SQUARES; sq++)
494 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
498 Raw_UpdateDisplay(0, 0, 1, 0);
499 fputs("Setup successful\n", stdout);
504 Raw_SearchStartStuff(short side)
508 printf("\nMove# %d Target = %ld Clock: %ld\n",
510 ResponseTime, TimeControl.clock[side]);
520 printf("Illegal position.\n");
524 if (mvstr[0][0] == '\0')
529 /* add remaining time in milliseconds to xshogi */
530 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
531 (TimeControl.clock[player] - et) * 10);
535 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
539 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
540 || (root->score == (SCORE_LIMIT + 998)))
553 if (Tree[t].f || Tree[t].t)
561 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
562 (et > 100) ? (NodeCnt / (et / 100)) : 0,
563 EADD, EGET, reminus, replus);
565 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
566 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
569 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
573 printf("My move is: %5s\n", mvstr[0]);
580 if (root->flags & draw)
582 fputs("Drawn game!\n", stdout);
584 else if (root->score == -(SCORE_LIMIT + 999))
586 printf("%s mates!\n", ColorStr[opponent]);
588 else if (root->score == (SCORE_LIMIT + 998))
590 printf("%s mates!\n", ColorStr[computer]);
593 else if (!barebones && (root->score < -SCORE_LIMIT))
595 printf("%s has a forced mate in %d moves!\n",
596 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
598 else if (!barebones && (root->score > SCORE_LIMIT))
600 printf("%s has a forced mate in %d moves!\n",
601 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(void)
782 scanf("%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,