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>
42 unsigned short MV[MAXDEPTH];
48 extern short pscore[];
51 /****************************************
52 * Trivial output functions.
53 ****************************************/
58 if (!barebones && !XSHOGI)
63 /* FIXME: change to ShowPrompt? */
67 if (!barebones && !XSHOGI)
69 /* printf("\nYour move is? "); */
70 fputs(CP[124], stdout);
76 Raw_ShowCurrentMove(short pnt, short f, short t)
82 Raw_ShowDepth(char ch)
84 if (!barebones && !XSHOGI)
86 printf(CP[53], Sdepth, ch); /* Depth = %d%c */
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_Printf(const char *format, ...)
130 va_start(ap, format);
136 Raw_doRequestInputString(const char* fmt, char* buffer)
143 Raw_ShowNodeCnt(long NodeCnt)
146 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
151 Raw_ShowPatternCount(short side, short n)
154 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
159 Raw_ShowResponseTime(void)
165 Raw_ShowResults(short score, unsigned short *bstline, char ch)
167 if (flag.post && !XSHOGI)
170 printf("%2d%c %6d %4ld %8ld ",
171 Sdepth, ch, score, et / 100, NodeCnt);
172 Raw_ShowLine(bstline);
178 Raw_ShowSidetoMove(void)
186 printf("stage = %d\n", stage);
187 printf("balance[black] = %d balance[white] = %d\n",
188 balance[black], balance[white]);
191 /****************************************
192 * End of trivial output routines.
193 ****************************************/
203 #ifdef HAVE_SETLINEBUF
207 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
209 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
212 printf("GNU Shogi %sp%s\n", version, patchlevel);
217 if (!TCflag && (MaxResponseTime == 0))
218 MaxResponseTime = 15L * 100L;
227 /* CHECKME: what purpose does this next statement serve? */
228 signal(SIGTERM, SIG_IGN);
242 Raw_ShowMessage(CP[31]); /* Abort? */
245 if (strcmp(s, CP[210]) == 0) /* yes */
252 Raw_TerminateSearch(int sig)
254 #ifdef INTERRUPT_TEST
255 ElapsedTime(INIT_INTERRUPT_MODE);
259 flag.back = true; /* previous: flag.timeout = true; */
261 flag.bothsides = false;
270 /* printf("SHOGI command summary\n"); */
271 printf(CP[40], version, patchlevel);
272 printf("----------------------------------"
273 "------------------------------\n");
274 /* printf("7g7f move from 7g to 7f quit
276 fputs(CP[158], stdout);
277 /* printf("S6h move silver to 6h beep
278 * turn %s\n", (flag.beep) ? "off" : "on"); */
279 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
280 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
281 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
282 /* printf("P*5e drop pawn to 5e easy
283 * turn %s\n", (flag.easy) ? "off" : "on"); */
284 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
286 * turn %s\n", (flag.hash) ? "off" : "on"); */
287 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
288 /* printf("bd redraw board reverse
289 * board display\n"); */
290 fputs(CP[130], stdout);
291 /* printf("list game to shogi.lst book
292 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
293 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
294 /* printf("undo undo last ply remove
295 * take back a move\n"); */
296 fputs(CP[200], stdout);
297 /* printf("edit edit board force
298 * enter game moves\n"); */
299 fputs(CP[153], stdout);
300 /* printf("switch sides with computer both
301 * computer match\n"); */
302 fputs(CP[194], stdout);
303 /* printf("black computer plays black white
304 * computer plays white\n"); */
305 fputs(CP[202], stdout);
306 /* printf("depth set search depth clock
307 * set time control\n"); */
308 fputs(CP[149], stdout);
309 /* printf("post principle variation hint
310 * suggest a move\n"); */
311 fputs(CP[177], stdout);
312 /* printf("save game to file get
313 * game from file\n"); */
314 fputs(CP[188], stdout);
315 printf("xsave pos. to xshogi file xget"
316 " pos. from xshogi file\n");
317 /* printf("random randomize play new
318 * start new game\n"); */
319 fputs(CP[181], stdout);
320 printf("--------------------------------"
321 "--------------------------------\n");
322 /* printf("Computer: %-12s Opponent: %s\n", */
324 ColorStr[computer], ColorStr[opponent]);
325 /* printf("Depth: %-12d Response time: %d sec\n", */
327 MaxSearchDepth, MaxResponseTime/100);
328 /* printf("Random: %-12s Easy mode: %s\n", */
330 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
331 /* printf("Beep: %-12s Transposition file: %s\n", */
333 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
334 /* printf("Tsume: %-12s Force: %s\n")*/
336 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
337 /* printf("Time Control %s %d moves %d seconds %d opr %d
338 * depth\n", (TCflag) ? "ON" : "OFF", */
340 (TCflag) ? CP[93] : CP[92],
341 TimeControl.moves[black], TimeControl.clock[black] / 100,
342 TCadd/100, MaxSearchDepth);
348 * Set up a board position. Pieces are entered by typing the piece followed
349 * 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 /* printf(". exit to main\n"); */
363 fputs(CP[29], stdout);
364 /* printf("# clear board\n"); */
365 fputs(CP[28], stdout);
366 /* printf("c change sides\n"); */
367 fputs(CP[136], stdout);
368 /* printf("enter piece & location: \n"); */
369 fputs(CP[155], stdout);
378 if (s[0] == CP[28][0]) /*#*/
380 for (sq = 0; sq < NO_SQUARES; sq++)
382 board[sq] = no_piece;
389 if (s[0] == CP[136][0]) /*c*/
394 for (i = pawn; i <= king; i++)
396 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
413 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
417 board[sq] = no_piece;
419 for (i = no_piece; i <= king; i++)
421 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
424 board[sq] = promoted[i];
437 while (s[0] != CP[29][0]);
439 for (sq = 0; sq < NO_SQUARES; sq++)
440 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
448 Raw_UpdateDisplay(0, 0, 1, 0);
455 * Set up a board position.
456 * Nine lines of nine characters are used to setup the board. 9a-1a is the
457 * first line. White pieces are represented by uppercase characters.
469 fgets(s, 80, stdin); /* skip "setup" command */
471 for (r = NO_ROWS - 1; r >= 0; r--)
475 for (c = 0; c <= (NO_COLS - 1); c++)
480 board[sq] = no_piece;
482 for (i = no_piece; i <= king; i++)
490 else if (ch == qxx[i])
500 for (sq = 0; sq < NO_SQUARES; sq++)
501 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
505 Raw_UpdateDisplay(0, 0, 1, 0);
506 /* printf("Setup successful\n"); */
507 fputs(CP[106], stdout);
512 Raw_SearchStartStuff(short side)
518 ResponseTime, TimeControl.clock[side]);
529 printf("%s\n", CP[225]);
533 if (mvstr[0][0] == '\0')
538 /* add remaining time in milliseconds to xshogi */
539 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
540 (TimeControl.clock[player] - et) * 10);
544 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
548 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
549 || (root->score == (SCORE_LIMIT + 998)))
562 if (Tree[t].f || Tree[t].t)
570 /* printf("Nodes %ld Tree %d Eval %ld
571 * Rate %ld RS high %ld low %ld\n", */
572 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
573 (et > 100) ? (NodeCnt / (et / 100)) : 0,
574 EADD, EGET, reminus, replus);
576 /* printf("Hin/Hout/Coll/Fin/Fout =
577 * %ld/%ld/%ld/%ld/%ld\n", */
579 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
582 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
586 /* printf("My move is: %s\n", mvstr[0]); */
587 printf(CP[83], mvstr[0]);
594 if (root->flags & draw)
596 /* printf("Drawn game!\n"); */
597 fputs(CP[57], stdout);
599 else if (root->score == -(SCORE_LIMIT + 999))
601 printf("%s mates!\n", ColorStr[opponent]);
603 else if (root->score == (SCORE_LIMIT + 998))
605 printf("%s mates!\n", ColorStr[computer]);
608 else if (!barebones && (root->score < -SCORE_LIMIT))
610 printf("%s has a forced mate in %d moves!\n",
611 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
613 else if (!barebones && (root->score > SCORE_LIMIT))
615 printf("%s has a forced mate in %d moves!\n",
616 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
618 #endif /* VERYBUGGY */
623 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
628 if (redraw && !XSHOGI)
631 r = (short)(TimeControl.clock[black] / 6000);
632 c = (short)((TimeControl.clock[black] % 6000) / 100);
633 l = (short)(TimeControl.clock[white] / 6000);
634 m = (short)((TimeControl.clock[white] % 6000) / 100);
635 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
636 printf(CP[116], r, c, l, m);
639 for (r = (NO_ROWS - 1); r >= 0; r--)
641 for (c = 0; c <= (NO_COLS - 1); c++)
645 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
647 pc = (is_promoted[board[l]] ? '+' : ' ');
649 if (color[l] == neutral)
651 else if (color[l] == black)
652 printf("%c%c", pc, qxx[board[l]]);
654 printf("%c%c", pc, pxx[board[l]]);
664 for (side = black; side <= white; side++)
667 printf((side == black)?"black ":"white ");
669 for (piece = pawn; piece <= king; piece++)
671 if ((c = Captured[side][piece]))
672 printf("%i%c ", c, pxx[piece]);
684 Raw_ChangeAlphaWindow(void)
686 printf("WAwindow: ");
687 scanf("%hd", &WAwindow);
688 printf("BAwindow: ");
689 scanf("%hd", &BAwindow);
695 Raw_ChangeBetaWindow(void)
697 printf("WBwindow: ");
698 scanf("%hd", &WBwindow);
699 printf("BBwindow: ");
700 scanf("%hd", &BBwindow);
710 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
711 printf(CP[72], mvstr[0]); /*hint*/
714 fputs(CP[223], stdout);
720 Raw_SelectLevel(char *sx)
723 char T[NO_SQUARES + 1], *p, *q;
725 if ((p = strstr(sx, CP[169])) != NULL)
726 p += strlen(CP[169]);
727 else if ((p = strstr(sx, CP[217])) != NULL)
728 p += strlen(CP[217]);
734 for (; *p != 'X'; *q++ = *p++);
738 /* line empty ask for input */
741 fputs(CP[61], stdout);
742 fgets(T, NO_SQUARES + 1, stdin);
746 /* skip blackspace */
747 for (p = T; *p == ' '; p++) ;
749 /* could be moves or a fischer clock */
752 /* its a fischer clock game */
754 TCminutes = (short)strtol(p, &q, 10);
755 TCadd = (short)strtol(q, NULL, 10) *100;
763 TCmoves = (short)strtol(p, &q, 10);
764 TCminutes = (short)strtol(q, &q, 10);
767 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
772 fputs(CP[94], stdout);
773 scanf("%hd", &OperatorTime);
779 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
780 TCminutes = TCseconds = 0;
789 TimeControl.clock[black] = TimeControl.clock[white] = 0;
794 printf("Clocks: %ld %ld\n",
795 TimeControl.clock[black] * 10,
796 TimeControl.clock[white] * 10);
804 Raw_ChangeSearchDepth(void)
807 scanf("%hd", &MaxSearchDepth);
808 TCflag = !(MaxSearchDepth > 0);
815 Raw_ChangeHashDepth(void)
817 printf("hashdepth = ");
818 scanf("%hd", &HashDepth);
819 printf("MoveLimit = ");
820 scanf("%hd", &HashMoveLimit);
826 Raw_SetContempt(void)
828 printf("contempt = ");
829 scanf("%hd", &contempt);
835 Raw_ChangeXwindow(void)
838 scanf("%hd", &xwndw);
843 * Raw_ShowPostnValue(short sq)
844 * must have called ExaminePosition() first
848 Raw_ShowPostnValue(short sq)
851 score = ScorePosition(color[sq]);
853 if (color[sq] != neutral)
855 #if defined SAVE_SVALUE
856 printf("???%c ", (color[sq] == white)?'b':'w');
858 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
872 short c, p, sq, tp, tc, tsq, score, j, k;
875 ExaminePosition(opponent);
876 Raw_ShowMessage(CP[65]);
880 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
883 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
886 for (p = king; p > no_piece; p--)
888 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
894 for (j = (NO_ROWS - 1); j >= 0; j--)
896 for (k = 0; k < (NO_COLS); k++)
898 sq = j*(NO_COLS) + k;
903 tsq = PieceList[c][1];
904 PieceList[c][1] = sq;
905 Raw_ShowPostnValue(sq);
906 PieceList[c][1] = tsq;
915 score = ScorePosition(opponent);
917 for (j = (NO_ROWS - 1); j >= 0; j--)
919 for (k = 0; k < (NO_COLS); k++)
921 sq = j*(NO_COLS) + k;
923 if (color[sq] != neutral)
925 #if defined SAVE_SVALUE
926 printf("%?????%c ", (color[sq] == white)?'b':'w');
928 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
940 printf("stage = %d\n", stage);
941 printf(CP[103], score,
942 mtl[computer], pscore[computer], GameType[computer],
943 mtl[opponent], pscore[opponent], GameType[opponent]);
949 Raw_DoTable(short table[NO_SQUARES])
952 ExaminePosition(opponent);
954 for (j = (NO_ROWS - 1); j >= 0; j--)
956 for (k = 0; k < NO_COLS; k++)
958 sq = j*(NO_ROWS) + k;
959 printf("%3d ", table[sq]);
969 Raw_ShowPostnValues(void)
971 short sq, score, j, k;
972 ExaminePosition(opponent);
974 for (j = (NO_ROWS - 1); j >= 0; j--)
976 for (k = 0; k < NO_COLS; k++)
978 sq = j * NO_COLS + k;
979 Raw_ShowPostnValue(sq);
985 score = ScorePosition(opponent);
986 printf(CP[103], score,
987 mtl[computer], pscore[computer], GameType[computer],
988 mtl[opponent], pscore[opponent], GameType[opponent]);
989 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);