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 * ----------------------------------------------------------------------
35 #include <sys/param.h>
36 #include <sys/types.h>
46 unsigned short MV[MAXDEPTH];
52 extern short pscore[];
55 /****************************************
56 * Trivial output functions.
57 ****************************************/
62 if (!barebones && !XSHOGI)
70 if (!barebones && !XSHOGI)
72 fputs("\nYour move is? ", stdout);
78 Raw_ShowCurrentMove(short pnt, short f, short t)
84 Raw_ShowDepth(char ch)
86 if (!barebones && !XSHOGI)
88 printf("Depth= %d%c ", Sdepth, ch);
95 Raw_ShowGameType(void)
98 printf("%c vs. %c\n", GameType[black], GameType[white]);
103 Raw_ShowLine(unsigned short *bstline)
107 for (i = 1; bstline[i] > 0; i++)
109 if ((i > 1) && (i % 8 == 1))
112 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
113 printf("%5s ", mvstr[0]);
121 Raw_ShowMessage(char *s)
129 Raw_AlwaysShowMessage(const char *format, va_list ap)
137 Raw_Printf(const char *format, va_list ap)
144 Raw_doRequestInputString(const char* fmt, char* buffer)
151 Raw_GetString(char* sx)
156 while(!eof && !sx[0])
157 eof = (fgets(sx, 80, stdin) == NULL);
163 Raw_ShowNodeCnt(long NodeCnt)
165 printf("Nodes = %ld Nodes/sec = %ld\n",
166 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
171 Raw_ShowPatternCount(short side, short n)
174 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
179 Raw_ShowResponseTime(void)
185 Raw_ShowResults(short score, unsigned short *bstline, char ch)
187 if (flag.post && !XSHOGI)
190 printf("%2d%c %6d %4ld %8ld ",
191 Sdepth, ch, score, et / 100, NodeCnt);
192 Raw_ShowLine(bstline);
198 Raw_ShowSidetoMove(void)
206 printf("stage = %d\n", stage);
207 printf("balance[black] = %d balance[white] = %d\n",
208 balance[black], balance[white]);
211 /****************************************
212 * End of trivial output routines.
213 ****************************************/
223 /* needed because of inconsistency between MSVC run-time system and gcc includes */
224 setbuf(stdout, NULL);
227 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
229 # ifdef HAVE_SETLINEBUF
232 # error "Need setvbuf() or setlinebuf() to compile gnushogi!"
236 printf("GNU Shogi %s\n", PACKAGE_VERSION);
241 if (!TCflag && (MaxResponseTime == 0))
242 MaxResponseTime = 15L * 100L;
250 /* CHECKME: what purpose does this next statement serve? */
251 signal(SIGTERM, SIG_IGN);
265 Raw_ShowMessage("Abort? ");
268 if (strcmp(s, "yes") == 0)
274 Raw_TerminateSearch(int sig)
276 #ifdef INTERRUPT_TEST
277 ElapsedTime(INIT_INTERRUPT_MODE);
281 flag.back = true; /* previous: flag.timeout = true; */
283 flag.bothsides = false;
291 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
292 printf("----------------------------------"
293 "------------------------------\n");
294 fputs ("7g7f move from 7g to 7f quit Exit Shogi\n", stdout);
295 printf("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
296 printf("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
297 printf("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
298 printf("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
299 fputs ("bd redraw board reverse board display\n", stdout);
300 printf("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, booksize);
301 fputs ("undo undo last ply remove take back a move\n", stdout);
302 fputs ("edit edit board force toggle manual move mode\n", stdout);
303 fputs ("switch sides with computer both computer match\n", stdout);
304 fputs ("black computer plays black white computer plays white\n", stdout);
305 fputs ("depth set search depth clock set time control\n", stdout);
306 fputs ("post principle variation hint suggest a move\n", stdout);
307 fputs ("save game to file get game from file\n", stdout);
308 printf("xsave pos. to xshogi file xget"
309 " pos. from xshogi file\n");
310 fputs("random randomize play new start new game\n", stdout);
311 printf("--------------------------------"
312 "--------------------------------\n");
313 printf("Computer: %-12s Opponent: %s\n",
314 ColorStr[computer], ColorStr[opponent]);
315 printf("Depth: %-12d Response time: %d sec\n",
316 MaxSearchDepth, MaxResponseTime/100);
317 printf("Random: %-12s Easy mode: %s\n",
318 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
319 printf("Beep: %-12s Transposition file: %s\n",
320 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
321 printf("Tsume: %-12s Force: %s\n",
322 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
323 printf("Time Control %s %d moves %d sec %d add %d depth\n",
324 (TCflag) ? "ON" : "OFF",
325 TimeControl.moves[black], TimeControl.clock[black] / 100,
326 TCadd/100, MaxSearchDepth);
331 * Set up a board position. Pieces are entered by typing the piece followed
332 * by the location. For example, Nf3 will place a knight on square f3.
337 short a, r, c, sq, i, found;
340 flag.regularstart = true;
343 Raw_UpdateDisplay(0, 0, 1, 0);
344 fputs(". Exit to main\n", stdout);
345 fputs("# Clear board\n", stdout);
346 fputs("c Change sides\n", stdout);
347 fputs("enter piece & location: \n", stdout);
358 for (sq = 0; sq < NO_SQUARES; sq++)
360 board[sq] = no_piece;
372 for (i = pawn; i <= king; i++)
374 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
391 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
395 board[sq] = no_piece;
397 for (i = no_piece; i <= king; i++)
399 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
402 board[sq] = promoted[i];
417 for (sq = 0; sq < NO_SQUARES; sq++)
418 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
426 Raw_UpdateDisplay(0, 0, 1, 0);
431 * Set up a board position.
432 * Nine lines of nine characters are used to setup the board. 9a-1a is the
433 * first line. White pieces are represented by uppercase characters.
444 fgets(s, 80, stdin); /* skip "setup" command */
446 for (r = NO_ROWS - 1; r >= 0; r--)
450 for (c = 0; c <= (NO_COLS - 1); c++)
455 board[sq] = no_piece;
457 for (i = no_piece; i <= king; i++)
465 else if (ch == qxx[i])
475 for (sq = 0; sq < NO_SQUARES; sq++)
476 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
480 Raw_UpdateDisplay(0, 0, 1, 0);
481 fputs("Setup successful\n", stdout);
486 Raw_SearchStartStuff(short side)
490 printf("\nMove# %d Target = %ld Clock: %ld\n",
492 ResponseTime, TimeControl.clock[side]);
502 printf("Illegal position.\n");
506 if (mvstr[0][0] == '\0')
511 /* add remaining time in milliseconds to xshogi */
512 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
513 (TimeControl.clock[player] - et) * 10);
517 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
521 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
522 || (root->score == (SCORE_LIMIT + 998)))
535 if (Tree[t].f || Tree[t].t)
543 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
544 (et > 100) ? (NodeCnt / (et / 100)) : 0,
545 EADD, EGET, reminus, replus);
547 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
548 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
551 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
555 printf("My move is: %5s\n", mvstr[0]);
562 if (root->flags & draw)
564 fputs("Drawn game!\n", stdout);
566 else if (root->score == -(SCORE_LIMIT + 999))
568 printf("%s mates!\n", ColorStr[opponent]);
570 else if (root->score == (SCORE_LIMIT + 998))
572 printf("%s mates!\n", ColorStr[computer]);
575 else if (!barebones && (root->score < -SCORE_LIMIT))
577 printf("%s has a forced mate in %d moves!\n",
578 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
580 else if (!barebones && (root->score > SCORE_LIMIT))
582 printf("%s has a forced mate in %d moves!\n",
583 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
585 #endif /* VERYBUGGY */
590 Raw_UpdateClocks(void)
596 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
601 if (redraw && !XSHOGI)
604 r = (short)(TimeControl.clock[black] / 6000);
605 c = (short)((TimeControl.clock[black] % 6000) / 100);
606 l = (short)(TimeControl.clock[white] / 6000);
607 m = (short)((TimeControl.clock[white] % 6000) / 100);
608 printf("Black %d:%02d White %d:%02d\n", r, c, l, m);
611 for (r = (NO_ROWS - 1); r >= 0; r--)
613 for (c = 0; c <= (NO_COLS - 1); c++)
617 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
619 pc = (is_promoted[board[l]] ? '+' : ' ');
621 if (color[l] == neutral)
623 else if (color[l] == black)
624 printf("%c%c", pc, qxx[board[l]]);
626 printf("%c%c", pc, pxx[board[l]]);
636 for (side = black; side <= white; side++)
639 printf((side == black)?"black ":"white ");
641 for (piece = pawn; piece <= king; piece++)
643 if ((c = Captured[side][piece]))
644 printf("%i%c ", c, pxx[piece]);
655 Raw_ChangeAlphaWindow(void)
657 printf("WAwindow: ");
658 scanf("%hd", &WAwindow);
659 printf("BAwindow: ");
660 scanf("%hd", &BAwindow);
665 Raw_ChangeBetaWindow(void)
667 printf("WBwindow: ");
668 scanf("%hd", &WBwindow);
669 printf("BBwindow: ");
670 scanf("%hd", &BBwindow);
679 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
680 printf("Hint: %s\n", mvstr[0]);
683 fputs("I have no idea.\n", stdout);
688 Raw_SelectLevel(char *sx)
691 char T[NO_SQUARES + 1], *p, *q;
693 if ((p = strstr(sx, "level")) != NULL)
694 p += strlen("level");
695 else if ((p = strstr(sx, "clock")) != NULL)
696 p += strlen("clock");
702 for (; *p != 'X'; *q++ = *p++);
706 /* line empty ask for input */
709 fputs("Enter #moves #minutes: ", stdout);
710 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 */
722 TCminutes = (short)strtol(p, &q, 10);
723 TCadd = (short)strtol(q, NULL, 10) *100;
731 TCmoves = (short)strtol(p, &q, 10);
732 TCminutes = (short)strtol(q, &q, 10);
735 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
740 fputs("Operator time (hundredths) = ", stdout);
741 scanf("%hd", &OperatorTime);
747 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
748 TCminutes = TCseconds = 0;
757 TimeControl.clock[black] = TimeControl.clock[white] = 0;
762 printf("Clocks: %ld %ld\n",
763 TimeControl.clock[black] * 10,
764 TimeControl.clock[white] * 10);
770 Raw_ChangeSearchDepth(void)
773 scanf("%hd", &MaxSearchDepth);
774 TCflag = !(MaxSearchDepth > 0);
779 Raw_ChangeHashDepth(void)
781 printf("hashdepth = ");
782 scanf("%hd", &HashDepth);
783 printf("MoveLimit = ");
784 scanf("%hd", &HashMoveLimit);
789 Raw_SetContempt(void)
791 printf("contempt = ");
792 scanf("%hd", &contempt);
797 Raw_ChangeXwindow(void)
800 scanf("%hd", &xwndw);
805 * Raw_ShowPostnValue(short sq)
806 * must have called ExaminePosition() first
809 Raw_ShowPostnValue(short sq)
812 score = ScorePosition(color[sq]);
814 if (color[sq] != neutral)
816 #if defined SAVE_SVALUE
817 printf("???%c ", (color[sq] == white)?'b':'w');
819 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
832 short c, p, sq, tp, tc, tsq, score, j, k;
835 ExaminePosition(opponent);
836 Raw_ShowMessage("Enter piece: ");
840 if ((s[0] == 'b') || (s[0] == 'B'))
843 if ((s[0] == 'w') || (s[0] == 'W'))
846 for (p = king; p > no_piece; p--)
848 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
854 for (j = (NO_ROWS - 1); j >= 0; j--)
856 for (k = 0; k < (NO_COLS); k++)
858 sq = j*(NO_COLS) + k;
863 tsq = PieceList[c][1];
864 PieceList[c][1] = sq;
865 Raw_ShowPostnValue(sq);
866 PieceList[c][1] = tsq;
875 score = ScorePosition(opponent);
877 for (j = (NO_ROWS - 1); j >= 0; j--)
879 for (k = 0; k < (NO_COLS); k++)
881 sq = j*(NO_COLS) + k;
883 if (color[sq] != neutral)
885 #if defined SAVE_SVALUE
886 printf("%?????%c ", (color[sq] == white)?'b':'w');
888 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
900 printf("stage = %d\n", stage);
901 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
902 mtl[computer], pscore[computer], GameType[computer],
903 mtl[opponent], pscore[opponent], GameType[opponent]);
908 Raw_DoTable(short table[NO_SQUARES])
911 ExaminePosition(opponent);
913 for (j = (NO_ROWS - 1); j >= 0; j--)
915 for (k = 0; k < NO_COLS; k++)
917 sq = j*(NO_ROWS) + k;
918 printf("%3d ", table[sq]);
927 Raw_ShowPostnValues(void)
929 short sq, score, j, k;
930 ExaminePosition(opponent);
932 for (j = (NO_ROWS - 1); j >= 0; j--)
934 for (k = 0; k < NO_COLS; k++)
936 sq = j * NO_COLS + k;
937 Raw_ShowPostnValue(sq);
943 score = ScorePosition(opponent);
944 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
945 mtl[computer], pscore[computer], GameType[computer],
946 mtl[opponent], pscore[opponent], GameType[opponent]);
947 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
952 Raw_PollForInput(void)
956 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
959 static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
960 /* .events = */ POLLIN } };
961 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
963 perror("polling standard input");
967 if (cnt) { /* if anything to read, or error occured */
969 flag.back = true; /* previous: flag.timeout = true; */
970 flag.bothsides = false;