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>
44 unsigned short MV[MAXDEPTH];
50 extern short pscore[];
53 /****************************************
54 * Trivial output functions.
55 ****************************************/
60 if (!barebones && !XSHOGI)
68 if (!barebones && !XSHOGI)
70 fputs("\nYour move is? ", stdout);
76 Raw_ShowCurrentMove(short pnt, short f, short t)
82 Raw_ShowDepth(char ch)
84 if (!barebones && !XSHOGI)
86 printf("Depth= %d%c ", Sdepth, ch);
93 Raw_ShowGameType(void)
96 printf("%c vs. %c\n", GameType[black], GameType[white]);
101 Raw_ShowLine(unsigned short *bstline)
105 for (i = 1; bstline[i] > 0; i++)
107 if ((i > 1) && (i % 8 == 1))
110 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
111 printf("%5s ", mvstr[0]);
119 Raw_ShowMessage(char *s)
127 Raw_AlwaysShowMessage(const char *format, va_list ap)
135 Raw_Printf(const char *format, va_list ap)
142 Raw_doRequestInputString(const char* fmt, char* buffer)
149 Raw_GetString(char* sx)
154 while(!eof && !sx[0])
155 eof = (fgets(sx, 80, stdin) == NULL);
161 Raw_ShowNodeCnt(long NodeCnt)
163 printf("Nodes = %ld Nodes/sec = %ld\n",
164 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
169 Raw_ShowPatternCount(short side, short n)
172 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
177 Raw_ShowResponseTime(void)
183 Raw_ShowResults(short score, unsigned short *bstline, char ch)
185 if (flag.post && !XSHOGI)
188 printf("%2d%c %6d %4ld %8ld ",
189 Sdepth, ch, score, et / 100, NodeCnt);
190 Raw_ShowLine(bstline);
196 Raw_ShowSidetoMove(void)
204 printf("stage = %d\n", stage);
205 printf("balance[black] = %d balance[white] = %d\n",
206 balance[black], balance[white]);
209 /****************************************
210 * End of trivial output routines.
211 ****************************************/
220 #ifdef HAVE_SETLINEBUF
224 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
226 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
229 printf("GNU Shogi %sp%s\n", version, patchlevel);
234 if (!TCflag && (MaxResponseTime == 0))
235 MaxResponseTime = 15L * 100L;
243 /* CHECKME: what purpose does this next statement serve? */
244 signal(SIGTERM, SIG_IGN);
258 Raw_ShowMessage("Abort? ");
261 if (strcmp(s, "yes") == 0)
267 Raw_TerminateSearch(int sig)
269 #ifdef INTERRUPT_TEST
270 ElapsedTime(INIT_INTERRUPT_MODE);
274 flag.back = true; /* previous: flag.timeout = true; */
276 flag.bothsides = false;
284 printf("GNU Shogi %sp%s command summary\n", version, patchlevel);
285 printf("----------------------------------"
286 "------------------------------\n");
287 fputs ("7g7f move from 7g to 7f quit Exit Shogi\n", stdout);
288 printf("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
289 printf("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
290 printf("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
291 printf("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
292 fputs ("bd redraw board reverse board display\n", stdout);
293 printf("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, booksize);
294 fputs ("undo undo last ply remove take back a move\n", stdout);
295 fputs ("edit edit board force toggle manual move mode\n", stdout);
296 fputs ("switch sides with computer both computer match\n", stdout);
297 fputs ("black computer plays black white computer plays white\n", stdout);
298 fputs ("depth set search depth clock set time control\n", stdout);
299 fputs ("post principle variation hint suggest a move\n", stdout);
300 fputs ("save game to file get game from file\n", stdout);
301 printf("xsave pos. to xshogi file xget"
302 " pos. from xshogi file\n");
303 fputs("random randomize play new start new game\n", stdout);
304 printf("--------------------------------"
305 "--------------------------------\n");
306 printf("Computer: %-12s Opponent: %s\n",
307 ColorStr[computer], ColorStr[opponent]);
308 printf("Depth: %-12d Response time: %d sec\n",
309 MaxSearchDepth, MaxResponseTime/100);
310 printf("Random: %-12s Easy mode: %s\n",
311 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
312 printf("Beep: %-12s Transposition file: %s\n",
313 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
314 printf("Tsume: %-12s Force: %s\n",
315 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
316 printf("Time Control %s %d moves %d sec %d add %d depth\n",
317 (TCflag) ? "ON" : "OFF",
318 TimeControl.moves[black], TimeControl.clock[black] / 100,
319 TCadd/100, MaxSearchDepth);
324 * Set up a board position. Pieces are entered by typing the piece followed
325 * by the location. For example, Nf3 will place a knight on square f3.
330 short a, r, c, sq, i, found;
333 flag.regularstart = true;
336 Raw_UpdateDisplay(0, 0, 1, 0);
337 fputs(". Exit to main\n", stdout);
338 fputs("# Clear board\n", stdout);
339 fputs("c Change sides\n", stdout);
340 fputs("enter piece & location: \n", stdout);
351 for (sq = 0; sq < NO_SQUARES; sq++)
353 board[sq] = no_piece;
365 for (i = pawn; i <= king; i++)
367 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
384 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
388 board[sq] = no_piece;
390 for (i = no_piece; i <= king; i++)
392 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
395 board[sq] = promoted[i];
410 for (sq = 0; sq < NO_SQUARES; sq++)
411 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
419 Raw_UpdateDisplay(0, 0, 1, 0);
424 * Set up a board position.
425 * Nine lines of nine characters are used to setup the board. 9a-1a is the
426 * first line. White pieces are represented by uppercase characters.
437 fgets(s, 80, stdin); /* skip "setup" command */
439 for (r = NO_ROWS - 1; r >= 0; r--)
443 for (c = 0; c <= (NO_COLS - 1); c++)
448 board[sq] = no_piece;
450 for (i = no_piece; i <= king; i++)
458 else if (ch == qxx[i])
468 for (sq = 0; sq < NO_SQUARES; sq++)
469 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
473 Raw_UpdateDisplay(0, 0, 1, 0);
474 fputs("Setup successful\n", stdout);
479 Raw_SearchStartStuff(short side)
483 printf("\nMove# %d Target = %ld Clock: %ld\n",
485 ResponseTime, TimeControl.clock[side]);
495 printf("Illegal position.\n");
499 if (mvstr[0][0] == '\0')
504 /* add remaining time in milliseconds to xshogi */
505 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
506 (TimeControl.clock[player] - et) * 10);
510 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
514 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
515 || (root->score == (SCORE_LIMIT + 998)))
528 if (Tree[t].f || Tree[t].t)
536 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
537 (et > 100) ? (NodeCnt / (et / 100)) : 0,
538 EADD, EGET, reminus, replus);
540 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
541 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
544 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
548 printf("My move is: %5s\n", mvstr[0]);
555 if (root->flags & draw)
557 fputs("Drawn game!\n", stdout);
559 else if (root->score == -(SCORE_LIMIT + 999))
561 printf("%s mates!\n", ColorStr[opponent]);
563 else if (root->score == (SCORE_LIMIT + 998))
565 printf("%s mates!\n", ColorStr[computer]);
568 else if (!barebones && (root->score < -SCORE_LIMIT))
570 printf("%s has a forced mate in %d moves!\n",
571 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
573 else if (!barebones && (root->score > SCORE_LIMIT))
575 printf("%s has a forced mate in %d moves!\n",
576 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
578 #endif /* VERYBUGGY */
583 Raw_UpdateClocks(void)
589 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
594 if (redraw && !XSHOGI)
597 r = (short)(TimeControl.clock[black] / 6000);
598 c = (short)((TimeControl.clock[black] % 6000) / 100);
599 l = (short)(TimeControl.clock[white] / 6000);
600 m = (short)((TimeControl.clock[white] % 6000) / 100);
601 printf("Black %d:%02d White %d:%02d\n", r, c, l, m);
604 for (r = (NO_ROWS - 1); r >= 0; r--)
606 for (c = 0; c <= (NO_COLS - 1); c++)
610 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
612 pc = (is_promoted[board[l]] ? '+' : ' ');
614 if (color[l] == neutral)
616 else if (color[l] == black)
617 printf("%c%c", pc, qxx[board[l]]);
619 printf("%c%c", pc, pxx[board[l]]);
629 for (side = black; side <= white; side++)
632 printf((side == black)?"black ":"white ");
634 for (piece = pawn; piece <= king; piece++)
636 if ((c = Captured[side][piece]))
637 printf("%i%c ", c, pxx[piece]);
648 Raw_ChangeAlphaWindow(void)
650 printf("WAwindow: ");
651 scanf("%hd", &WAwindow);
652 printf("BAwindow: ");
653 scanf("%hd", &BAwindow);
658 Raw_ChangeBetaWindow(void)
660 printf("WBwindow: ");
661 scanf("%hd", &WBwindow);
662 printf("BBwindow: ");
663 scanf("%hd", &BBwindow);
672 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
673 printf("Hint: %s\n", mvstr[0]);
676 fputs("I have no idea.\n", stdout);
681 Raw_SelectLevel(char *sx)
684 char T[NO_SQUARES + 1], *p, *q;
686 if ((p = strstr(sx, "level")) != NULL)
687 p += strlen("level");
688 else if ((p = strstr(sx, "clock")) != NULL)
689 p += strlen("clock");
695 for (; *p != 'X'; *q++ = *p++);
699 /* line empty ask for input */
702 fputs("Enter #moves #minutes: ", stdout);
703 fgets(T, NO_SQUARES + 1, stdin);
707 /* skip blackspace */
708 for (p = T; *p == ' '; p++) ;
710 /* could be moves or a fischer clock */
713 /* its a fischer clock game */
715 TCminutes = (short)strtol(p, &q, 10);
716 TCadd = (short)strtol(q, NULL, 10) *100;
724 TCmoves = (short)strtol(p, &q, 10);
725 TCminutes = (short)strtol(q, &q, 10);
728 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
733 fputs("Operator time (hundredths) = ", stdout);
734 scanf("%hd", &OperatorTime);
740 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
741 TCminutes = TCseconds = 0;
750 TimeControl.clock[black] = TimeControl.clock[white] = 0;
755 printf("Clocks: %ld %ld\n",
756 TimeControl.clock[black] * 10,
757 TimeControl.clock[white] * 10);
763 Raw_ChangeSearchDepth(void)
766 scanf("%hd", &MaxSearchDepth);
767 TCflag = !(MaxSearchDepth > 0);
772 Raw_ChangeHashDepth(void)
774 printf("hashdepth = ");
775 scanf("%hd", &HashDepth);
776 printf("MoveLimit = ");
777 scanf("%hd", &HashMoveLimit);
782 Raw_SetContempt(void)
784 printf("contempt = ");
785 scanf("%hd", &contempt);
790 Raw_ChangeXwindow(void)
793 scanf("%hd", &xwndw);
798 * Raw_ShowPostnValue(short sq)
799 * must have called ExaminePosition() first
802 Raw_ShowPostnValue(short sq)
805 score = ScorePosition(color[sq]);
807 if (color[sq] != neutral)
809 #if defined SAVE_SVALUE
810 printf("???%c ", (color[sq] == white)?'b':'w');
812 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
825 short c, p, sq, tp, tc, tsq, score, j, k;
828 ExaminePosition(opponent);
829 Raw_ShowMessage("Enter piece: ");
833 if ((s[0] == 'b') || (s[0] == 'B'))
836 if ((s[0] == 'w') || (s[0] == 'W'))
839 for (p = king; p > no_piece; p--)
841 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
847 for (j = (NO_ROWS - 1); j >= 0; j--)
849 for (k = 0; k < (NO_COLS); k++)
851 sq = j*(NO_COLS) + k;
856 tsq = PieceList[c][1];
857 PieceList[c][1] = sq;
858 Raw_ShowPostnValue(sq);
859 PieceList[c][1] = tsq;
868 score = ScorePosition(opponent);
870 for (j = (NO_ROWS - 1); j >= 0; j--)
872 for (k = 0; k < (NO_COLS); k++)
874 sq = j*(NO_COLS) + k;
876 if (color[sq] != neutral)
878 #if defined SAVE_SVALUE
879 printf("%?????%c ", (color[sq] == white)?'b':'w');
881 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
893 printf("stage = %d\n", stage);
894 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
895 mtl[computer], pscore[computer], GameType[computer],
896 mtl[opponent], pscore[opponent], GameType[opponent]);
901 Raw_DoTable(short table[NO_SQUARES])
904 ExaminePosition(opponent);
906 for (j = (NO_ROWS - 1); j >= 0; j--)
908 for (k = 0; k < NO_COLS; k++)
910 sq = j*(NO_ROWS) + k;
911 printf("%3d ", table[sq]);
920 Raw_ShowPostnValues(void)
922 short sq, score, j, k;
923 ExaminePosition(opponent);
925 for (j = (NO_ROWS - 1); j >= 0; j--)
927 for (k = 0; k < NO_COLS; k++)
929 sq = j * NO_COLS + k;
930 Raw_ShowPostnValue(sq);
936 score = ScorePosition(opponent);
937 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
938 mtl[computer], pscore[computer], GameType[computer],
939 mtl[opponent], pscore[opponent], GameType[opponent]);
940 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
945 Raw_PollForInput(void)
947 static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
948 /* .events = */ POLLIN } };
949 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
951 perror("polling standard input");
954 if (cnt) { /* if anything to read, or error occured */
956 flag.back = true; /* previous: flag.timeout = true; */
957 flag.bothsides = false;