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)
78 fputs("\nYour move is? ", stdout);
84 Raw_ShowCurrentMove(short pnt, short f, short t)
90 Raw_ShowDepth(char ch)
92 if (!barebones && !XSHOGI)
94 printf("Depth= %d%c ", Sdepth, ch);
101 Raw_ShowGameType(void)
104 printf("%c vs. %c\n", GameType[black], GameType[white]);
109 Raw_ShowLine(unsigned short *bstline)
113 for (i = 1; bstline[i] > 0; i++)
115 if ((i > 1) && (i % 8 == 1))
118 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
119 printf("%5s ", mvstr[0]);
127 Raw_ShowMessage(char *s)
135 Raw_AlwaysShowMessage(const char *format, ...)
138 va_start(ap, format);
146 Raw_Printf(const char *format, ...)
149 va_start(ap, format);
156 Raw_doRequestInputString(const char* fmt, char* buffer)
163 Raw_GetString(char* sx)
169 while(!eof && !sx[0])
170 eof = (fgets(sx, 80, stdin) == NULL);
172 /* remove any trailing newline */
173 nl = strchr(sx, '\n');
182 Raw_ShowNodeCnt(long NodeCnt)
184 printf("Nodes = %ld Nodes/sec = %ld\n",
185 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
190 Raw_ShowPatternCount(short side, short n)
193 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
198 Raw_ShowResponseTime(void)
204 Raw_ShowResults(short score, unsigned short *bstline, char ch)
206 if (flag.post && !XSHOGI)
209 printf("%2d%c %6d %4ld %8ld ",
210 Sdepth, ch, score, et / 100, NodeCnt);
211 Raw_ShowLine(bstline);
217 Raw_ShowSidetoMove(void)
225 printf("stage = %d\n", stage);
226 printf("balance[black] = %d balance[white] = %d\n",
227 balance[black], balance[white]);
230 /****************************************
231 * End of trivial output routines.
232 ****************************************/
242 /* needed because of inconsistency between MSVC run-time system and gcc includes */
243 setbuf(stdout, NULL);
246 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
248 # ifdef HAVE_SETLINEBUF
251 # error "Need setvbuf() or setlinebuf() to compile gnushogi!"
255 printf("GNU Shogi %s\n", PACKAGE_VERSION);
260 if (!TCflag && (MaxResponseTime == 0))
261 MaxResponseTime = 15L * 100L;
269 /* CHECKME: what purpose does this next statement serve? */
270 signal(SIGTERM, SIG_IGN);
284 Raw_ShowMessage("Abort? ");
287 if (strcmp(s, "yes") == 0)
293 Raw_TerminateSearch(int sig)
295 #ifdef INTERRUPT_TEST
296 ElapsedTime(INIT_INTERRUPT_MODE);
300 flag.back = true; /* previous: flag.timeout = true; */
302 flag.bothsides = false;
310 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
311 printf("----------------------------------------------------------------\n");
312 printf("7g7f move from 7g to 7f quit Exit Shogi\n");
313 printf("S6h move silver to 6h beep turn %s\n",
314 (flag.beep) ? "OFF" : "ON");
315 printf("2d2c+ move to 2c and promote material turn %s\n",
316 (flag.material) ? "OFF" : "ON");
317 printf("P*5e drop pawn to 5e easy turn %s\n",
318 (flag.easy) ? "OFF" : "ON");
319 printf("tsume toggle tsume mode hash turn %s\n",
320 (flag.hash) ? "OFF" : "ON");
321 printf("bd redraw board reverse board display\n");
322 printf("list game to shogi.lst book turn %s used %d of %d\n",
323 (Book) ? "OFF" : "ON", bookcount, booksize);
324 printf("undo undo last ply remove take back a move\n");
325 printf("edit edit board force toggle manual move mode\n");
326 printf("switch sides with computer both computer match\n");
327 printf("black computer plays black white computer plays white\n");
328 printf("sd set search depth clock set time control\n");
329 printf("post principle variation hint suggest a move\n");
330 printf("save game to file get game from file\n");
331 printf("xsave pos. to xshogi file xget pos. from xshogi file\n");
332 printf("random randomize play new start new game\n");
333 printf("setup first \n");
334 printf("go computer plays now material turn %s\n",
335 flag.material ? "OFF" : "ON");
336 printf("level time control gamein \n");
337 printf("time set engine clock otime set opponent clock\n");
338 printf("Awindow Bwindow \n");
339 printf("rcptr turn %3s bsave book save\n",
340 flag.rcptr ? "OFF" : "ON ");
341 printf("hashdepth hard turn easy OFF\n");
342 printf("contempt xwndw \n");
343 printf("rv turn %3s coords turn %s\n",
344 flag.rv ? "OFF" : "ON ", flag.coords ? "OFF" : "ON");
345 printf("stars turn %3s moves \n",
346 flag.stars ? "OFF" : "ON ");
348 printf("debug depth alias for 'sd'\n");
349 printf("----------------------------------------------------------------\n");
350 printf("Computer: %-12s Opponent: %s\n",
351 ColorStr[computer], ColorStr[opponent]);
352 printf("Depth: %-12d Response time: %ld sec\n",
353 MaxSearchDepth, MaxResponseTime/100);
354 printf("Random: %-12s Easy mode: %s\n",
355 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
356 printf("Beep: %-12s Transposition file: %s\n",
357 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
358 printf("Tsume: %-12s Force: %s\n",
359 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
360 printf("Time Control %s %d moves %ld sec %d add %d depth\n",
361 (TCflag) ? "ON" : "OFF",
362 TimeControl.moves[black], TimeControl.clock[black] / 100,
363 TCadd/100, MaxSearchDepth);
368 * Set up a board position. Pieces are entered by typing the piece followed
369 * by the location. For example, Nf3 will place a knight on square f3.
374 short a, r, c, sq, i, found;
377 flag.regularstart = true;
380 Raw_UpdateDisplay(0, 0, 1, 0);
381 fputs(". Exit to main\n", stdout);
382 fputs("# Clear board\n", stdout);
383 fputs("c Change sides\n", stdout);
384 fputs("enter piece & location: \n", stdout);
395 for (sq = 0; sq < NO_SQUARES; sq++)
397 board[sq] = no_piece;
409 for (i = pawn; i <= king; i++)
411 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
428 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
432 board[sq] = no_piece;
434 for (i = no_piece; i <= king; i++)
436 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
439 board[sq] = promoted[i];
454 for (sq = 0; sq < NO_SQUARES; sq++)
455 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
463 Raw_UpdateDisplay(0, 0, 1, 0);
468 * Set up a board position.
469 * Nine lines of nine characters are used to setup the board. 9a-1a is the
470 * first line. White pieces are represented by uppercase characters.
481 fgets(s, 80, stdin); /* skip "setup" command */
483 for (r = NO_ROWS - 1; r >= 0; r--)
487 for (c = 0; c <= (NO_COLS - 1); c++)
492 board[sq] = no_piece;
494 for (i = no_piece; i <= king; i++)
502 else if (ch == qxx[i])
512 for (sq = 0; sq < NO_SQUARES; sq++)
513 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
517 Raw_UpdateDisplay(0, 0, 1, 0);
518 fputs("Setup successful\n", stdout);
523 Raw_SearchStartStuff(short side)
527 printf("\nMove# %d Target = %ld Clock: %ld\n",
529 ResponseTime, TimeControl.clock[side]);
539 printf("Illegal position.\n");
543 if (mvstr[0][0] == '\0')
548 /* add remaining time in milliseconds to xshogi */
549 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
550 (TimeControl.clock[player] - et) * 10);
554 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
558 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
559 || (root->score == (SCORE_LIMIT + 998)))
572 if (Tree[t].f || Tree[t].t)
580 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
581 (et > 100) ? (NodeCnt / (et / 100)) : 0,
582 EADD, EGET, reminus, replus);
584 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
585 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
588 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
592 printf("My move is: %5s\n", mvstr[0]);
599 if (root->flags & draw)
601 fputs("Drawn game!\n", stdout);
603 else if (root->score == -(SCORE_LIMIT + 999))
605 printf("%s mates!\n", ColorStr[opponent]);
607 else if (root->score == (SCORE_LIMIT + 998))
609 printf("%s mates!\n", ColorStr[computer]);
612 else if (!barebones && (root->score < -SCORE_LIMIT))
614 printf("%s has a forced mate in %d moves!\n",
615 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
617 else if (!barebones && (root->score > SCORE_LIMIT))
619 printf("%s has a forced mate in %d moves!\n",
620 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
622 #endif /* VERYBUGGY */
627 Raw_UpdateClocks(void)
633 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
638 if (redraw && !XSHOGI)
641 r = (short)(TimeControl.clock[black] / 6000);
642 c = (short)((TimeControl.clock[black] % 6000) / 100);
643 l = (short)(TimeControl.clock[white] / 6000);
644 m = (short)((TimeControl.clock[white] % 6000) / 100);
645 printf("Black %d:%02d White %d:%02d\n", r, c, l, m);
648 for (r = (NO_ROWS - 1); r >= 0; r--)
650 for (c = 0; c <= (NO_COLS - 1); c++)
654 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
656 pc = (is_promoted[board[l]] ? '+' : ' ');
658 if (color[l] == neutral)
660 else if (color[l] == black)
661 printf("%c%c", pc, qxx[board[l]]);
663 printf("%c%c", pc, pxx[board[l]]);
673 for (side = black; side <= white; side++)
676 printf((side == black)?"black ":"white ");
678 for (piece = pawn; piece <= king; piece++)
680 if ((c = Captured[side][piece]))
681 printf("%i%c ", c, pxx[piece]);
692 Raw_ChangeAlphaWindow(void)
694 printf("WAwindow: ");
695 scanf("%hd", &WAwindow);
696 printf("BAwindow: ");
697 scanf("%hd", &BAwindow);
702 Raw_ChangeBetaWindow(void)
704 printf("WBwindow: ");
705 scanf("%hd", &WBwindow);
706 printf("BBwindow: ");
707 scanf("%hd", &BBwindow);
716 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
717 printf("Hint: %s\n", mvstr[0]);
720 fputs("I have no idea.\n", stdout);
725 Raw_SelectLevel(char *sx)
727 /* FIXME: NO_SQUARES is nonsense here */
728 char T[NO_SQUARES + 1], *p;
730 strncpy(T, sx, NO_SQUARES);
731 T[NO_SQUARES] = '\0';
733 /* if line empty, ask for input */
736 fputs("Enter #moves #minutes: ", stdout);
737 fgets(T, NO_SQUARES + 1, stdin);
740 /* skip blackspace */
741 for (p = T; *p == ' '; p++) ;
743 /* could be moves or a fischer clock */
746 /* its a fischer clock game */
749 TCminutes = (short)strtol(p, &q, 10);
750 TCadd = (short)strtol(q, NULL, 10) *100;
759 TCmoves = (short)strtol(p, &q, 10);
760 TCminutes = (short)strtol(q, &q, 10);
763 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
768 fputs("Operator time (hundredths) = ", stdout);
769 scanf("%hd", &OperatorTime);
775 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
776 TCminutes = TCseconds = 0;
785 TimeControl.clock[black] = TimeControl.clock[white] = 0;
790 printf("Clocks: %ld %ld\n",
791 TimeControl.clock[black] * 10,
792 TimeControl.clock[white] * 10);
798 Raw_ChangeSearchDepth(char *sx)
801 strncpy(buf, sx, 80); buf[80] = '\0';
802 /* if line empty, ask for input */
805 fgets(buf, 80+1, stdin);
807 sscanf(buf, "%hd", &MaxSearchDepth);
808 TCflag = !(MaxSearchDepth > 0);
813 Raw_ChangeHashDepth(void)
815 printf("hashdepth = ");
816 scanf("%hd", &HashDepth);
817 printf("MoveLimit = ");
818 scanf("%hd", &HashMoveLimit);
823 Raw_SetContempt(void)
825 printf("contempt = ");
826 scanf("%hd", &contempt);
831 Raw_ChangeXwindow(void)
834 scanf("%hd", &xwndw);
839 * Raw_ShowPostnValue(short sq)
840 * must have called ExaminePosition() first
843 Raw_ShowPostnValue(short sq)
845 (void) ScorePosition(color[sq]);
847 if (color[sq] != neutral)
849 #if defined SAVE_SVALUE
850 printf("???%c ", (color[sq] == white)?'b':'w');
852 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
865 short c, p, sq, tp, tc, tsq, score, j, k;
868 ExaminePosition(opponent);
869 Raw_ShowMessage("Enter piece: ");
873 if ((s[0] == 'b') || (s[0] == 'B'))
876 if ((s[0] == 'w') || (s[0] == 'W'))
879 for (p = king; p > no_piece; p--)
881 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
887 for (j = (NO_ROWS - 1); j >= 0; j--)
889 for (k = 0; k < (NO_COLS); k++)
891 sq = j*(NO_COLS) + k;
896 tsq = PieceList[c][1];
897 PieceList[c][1] = sq;
898 Raw_ShowPostnValue(sq);
899 PieceList[c][1] = tsq;
908 score = ScorePosition(opponent);
910 for (j = (NO_ROWS - 1); j >= 0; j--)
912 for (k = 0; k < (NO_COLS); k++)
914 sq = j*(NO_COLS) + k;
916 if (color[sq] != neutral)
918 #if defined SAVE_SVALUE
919 printf("%?????%c ", (color[sq] == white)?'b':'w');
921 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
933 printf("stage = %d\n", stage);
934 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
935 mtl[computer], pscore[computer], GameType[computer],
936 mtl[opponent], pscore[opponent], GameType[opponent]);
941 Raw_DoTable(short table[NO_SQUARES])
944 ExaminePosition(opponent);
946 for (j = (NO_ROWS - 1); j >= 0; j--)
948 for (k = 0; k < NO_COLS; k++)
950 sq = j*(NO_ROWS) + k;
951 printf("%3d ", table[sq]);
960 Raw_ShowPostnValues(void)
962 short sq, score, j, k;
963 ExaminePosition(opponent);
965 for (j = (NO_ROWS - 1); j >= 0; j--)
967 for (k = 0; k < NO_COLS; k++)
969 sq = j * NO_COLS + k;
970 Raw_ShowPostnValue(sq);
976 score = ScorePosition(opponent);
977 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
978 mtl[computer], pscore[computer], GameType[computer],
979 mtl[opponent], pscore[opponent], GameType[opponent]);
980 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
985 Raw_PollForInput(void)
989 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
992 static struct pollfd pollfds[1] = { [0] = { .fd = STDIN_FILENO,
993 .events = POLLIN } };
994 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
996 perror("polling standard input");
1000 if (cnt) { /* if anything to read, or error occured */
1002 flag.back = true; /* previous: flag.timeout = true; */
1003 flag.bothsides = false;
1007 struct display raw_display =
1009 .ChangeAlphaWindow = Raw_ChangeAlphaWindow,
1010 .ChangeBetaWindow = Raw_ChangeBetaWindow,
1011 .ChangeHashDepth = Raw_ChangeHashDepth,
1012 .ChangeSearchDepth = Raw_ChangeSearchDepth,
1013 .ChangeXwindow = Raw_ChangeXwindow,
1014 .ClearScreen = Raw_ClearScreen,
1015 .DoDebug = Raw_DoDebug,
1016 .DoTable = Raw_DoTable,
1017 .EditBoard = Raw_EditBoard,
1018 .ExitShogi = Raw_ExitShogi,
1019 .GiveHint = Raw_GiveHint,
1020 .Initialize = Raw_Initialize,
1021 .ShowNodeCnt = Raw_ShowNodeCnt,
1022 .OutputMove = Raw_OutputMove,
1023 .PollForInput = Raw_PollForInput,
1024 .SetContempt = Raw_SetContempt,
1025 .SearchStartStuff = Raw_SearchStartStuff,
1026 .SelectLevel = Raw_SelectLevel,
1027 .ShowCurrentMove = Raw_ShowCurrentMove,
1028 .ShowDepth = Raw_ShowDepth,
1029 .ShowGameType = Raw_ShowGameType,
1030 .ShowLine = Raw_ShowLine,
1031 .ShowMessage = Raw_ShowMessage,
1032 .AlwaysShowMessage = Raw_AlwaysShowMessage,
1033 .Printf = Raw_Printf,
1034 .doRequestInputString = Raw_doRequestInputString,
1035 .GetString = Raw_GetString,
1036 .SetupBoard = Raw_SetupBoard,
1037 .ShowPatternCount = Raw_ShowPatternCount,
1038 .ShowPostnValue = Raw_ShowPostnValue,
1039 .ShowPostnValues = Raw_ShowPostnValues,
1040 .ShowPrompt = Raw_ShowPrompt,
1041 .ShowResponseTime = Raw_ShowResponseTime,
1042 .ShowResults = Raw_ShowResults,
1043 .ShowSidetoMove = Raw_ShowSidetoMove,
1044 .ShowStage = Raw_ShowStage,
1045 .TerminateSearch = Raw_TerminateSearch,
1046 .UpdateClocks = Raw_UpdateClocks,
1047 .UpdateDisplay = Raw_UpdateDisplay,