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)
126 Raw_AlwaysShowMessage(const char *format, ...)
129 va_start(ap, format);
136 Raw_Printf(const char *format, ...)
139 va_start(ap, format);
145 Raw_doRequestInputString(const char* fmt, char* buffer)
152 Raw_ShowNodeCnt(long NodeCnt)
155 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
160 Raw_ShowPatternCount(short side, short n)
163 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
168 Raw_ShowResponseTime(void)
174 Raw_ShowResults(short score, unsigned short *bstline, char ch)
176 if (flag.post && !XSHOGI)
179 printf("%2d%c %6d %4ld %8ld ",
180 Sdepth, ch, score, et / 100, NodeCnt);
181 Raw_ShowLine(bstline);
187 Raw_ShowSidetoMove(void)
195 printf("stage = %d\n", stage);
196 printf("balance[black] = %d balance[white] = %d\n",
197 balance[black], balance[white]);
200 /****************************************
201 * End of trivial output routines.
202 ****************************************/
212 #ifdef HAVE_SETLINEBUF
216 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
218 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
221 printf("GNU Shogi %sp%s\n", version, patchlevel);
226 if (!TCflag && (MaxResponseTime == 0))
227 MaxResponseTime = 15L * 100L;
236 /* CHECKME: what purpose does this next statement serve? */
237 signal(SIGTERM, SIG_IGN);
251 Raw_ShowMessage(CP[31]); /* Abort? */
254 if (strcmp(s, CP[210]) == 0) /* yes */
261 Raw_TerminateSearch(int sig)
263 #ifdef INTERRUPT_TEST
264 ElapsedTime(INIT_INTERRUPT_MODE);
268 flag.back = true; /* previous: flag.timeout = true; */
270 flag.bothsides = false;
279 /* printf("SHOGI command summary\n"); */
280 printf(CP[40], version, patchlevel);
281 printf("----------------------------------"
282 "------------------------------\n");
283 /* printf("7g7f move from 7g to 7f quit
285 fputs(CP[158], stdout);
286 /* printf("S6h move silver to 6h beep
287 * turn %s\n", (flag.beep) ? "off" : "on"); */
288 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
289 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
290 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
291 /* printf("P*5e drop pawn to 5e easy
292 * turn %s\n", (flag.easy) ? "off" : "on"); */
293 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
295 * turn %s\n", (flag.hash) ? "off" : "on"); */
296 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
297 /* printf("bd redraw board reverse
298 * board display\n"); */
299 fputs(CP[130], stdout);
300 /* printf("list game to shogi.lst book
301 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
302 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
303 /* printf("undo undo last ply remove
304 * take back a move\n"); */
305 fputs(CP[200], stdout);
306 /* printf("edit edit board force
307 * enter game moves\n"); */
308 fputs(CP[153], stdout);
309 /* printf("switch sides with computer both
310 * computer match\n"); */
311 fputs(CP[194], stdout);
312 /* printf("black computer plays black white
313 * computer plays white\n"); */
314 fputs(CP[202], stdout);
315 /* printf("depth set search depth clock
316 * set time control\n"); */
317 fputs(CP[149], stdout);
318 /* printf("post principle variation hint
319 * suggest a move\n"); */
320 fputs(CP[177], stdout);
321 /* printf("save game to file get
322 * game from file\n"); */
323 fputs(CP[188], stdout);
324 printf("xsave pos. to xshogi file xget"
325 " pos. from xshogi file\n");
326 /* printf("random randomize play new
327 * start new game\n"); */
328 fputs(CP[181], stdout);
329 printf("--------------------------------"
330 "--------------------------------\n");
331 /* printf("Computer: %-12s Opponent: %s\n", */
333 ColorStr[computer], ColorStr[opponent]);
334 /* printf("Depth: %-12d Response time: %d sec\n", */
336 MaxSearchDepth, MaxResponseTime/100);
337 /* printf("Random: %-12s Easy mode: %s\n", */
339 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
340 /* printf("Beep: %-12s Transposition file: %s\n", */
342 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
343 /* printf("Tsume: %-12s Force: %s\n")*/
345 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
346 /* printf("Time Control %s %d moves %d seconds %d opr %d
347 * depth\n", (TCflag) ? "ON" : "OFF", */
349 (TCflag) ? CP[93] : CP[92],
350 TimeControl.moves[black], TimeControl.clock[black] / 100,
351 TCadd/100, MaxSearchDepth);
357 * Set up a board position. Pieces are entered by typing the piece followed
358 * by the location. For example, Nf3 will place a knight on square f3.
364 short a, r, c, sq, i, found;
367 flag.regularstart = true;
370 Raw_UpdateDisplay(0, 0, 1, 0);
371 /* printf(". exit to main\n"); */
372 fputs(CP[29], stdout);
373 /* printf("# clear board\n"); */
374 fputs(CP[28], stdout);
375 /* printf("c change sides\n"); */
376 fputs(CP[136], stdout);
377 /* printf("enter piece & location: \n"); */
378 fputs(CP[155], stdout);
387 if (s[0] == CP[28][0]) /*#*/
389 for (sq = 0; sq < NO_SQUARES; sq++)
391 board[sq] = no_piece;
398 if (s[0] == CP[136][0]) /*c*/
403 for (i = pawn; i <= king; i++)
405 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
422 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
426 board[sq] = no_piece;
428 for (i = no_piece; i <= king; i++)
430 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
433 board[sq] = promoted[i];
446 while (s[0] != CP[29][0]);
448 for (sq = 0; sq < NO_SQUARES; sq++)
449 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
457 Raw_UpdateDisplay(0, 0, 1, 0);
464 * Set up a board position.
465 * Nine lines of nine characters are used to setup the board. 9a-1a is the
466 * first line. White pieces are represented by uppercase characters.
478 fgets(s, 80, stdin); /* skip "setup" command */
480 for (r = NO_ROWS - 1; r >= 0; r--)
484 for (c = 0; c <= (NO_COLS - 1); c++)
489 board[sq] = no_piece;
491 for (i = no_piece; i <= king; i++)
499 else if (ch == qxx[i])
509 for (sq = 0; sq < NO_SQUARES; sq++)
510 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
514 Raw_UpdateDisplay(0, 0, 1, 0);
515 /* printf("Setup successful\n"); */
516 fputs(CP[106], stdout);
521 Raw_SearchStartStuff(short side)
527 ResponseTime, TimeControl.clock[side]);
538 printf("%s\n", CP[225]);
542 if (mvstr[0][0] == '\0')
547 /* add remaining time in milliseconds to xshogi */
548 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
549 (TimeControl.clock[player] - et) * 10);
553 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
557 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
558 || (root->score == (SCORE_LIMIT + 998)))
571 if (Tree[t].f || Tree[t].t)
579 /* printf("Nodes %ld Tree %d Eval %ld
580 * Rate %ld RS high %ld low %ld\n", */
581 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
582 (et > 100) ? (NodeCnt / (et / 100)) : 0,
583 EADD, EGET, reminus, replus);
585 /* printf("Hin/Hout/Coll/Fin/Fout =
586 * %ld/%ld/%ld/%ld/%ld\n", */
588 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
591 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
595 /* printf("My move is: %s\n", mvstr[0]); */
596 printf(CP[83], mvstr[0]);
603 if (root->flags & draw)
605 /* printf("Drawn game!\n"); */
606 fputs(CP[57], stdout);
608 else if (root->score == -(SCORE_LIMIT + 999))
610 printf("%s mates!\n", ColorStr[opponent]);
612 else if (root->score == (SCORE_LIMIT + 998))
614 printf("%s mates!\n", ColorStr[computer]);
617 else if (!barebones && (root->score < -SCORE_LIMIT))
619 printf("%s has a forced mate in %d moves!\n",
620 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
622 else if (!barebones && (root->score > SCORE_LIMIT))
624 printf("%s has a forced mate in %d moves!\n",
625 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
627 #endif /* VERYBUGGY */
632 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
637 if (redraw && !XSHOGI)
640 r = (short)(TimeControl.clock[black] / 6000);
641 c = (short)((TimeControl.clock[black] % 6000) / 100);
642 l = (short)(TimeControl.clock[white] / 6000);
643 m = (short)((TimeControl.clock[white] % 6000) / 100);
644 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
645 printf(CP[116], 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]);
693 Raw_ChangeAlphaWindow(void)
695 printf("WAwindow: ");
696 scanf("%hd", &WAwindow);
697 printf("BAwindow: ");
698 scanf("%hd", &BAwindow);
704 Raw_ChangeBetaWindow(void)
706 printf("WBwindow: ");
707 scanf("%hd", &WBwindow);
708 printf("BBwindow: ");
709 scanf("%hd", &BBwindow);
719 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
720 printf(CP[72], mvstr[0]); /*hint*/
723 fputs(CP[223], stdout);
729 Raw_SelectLevel(char *sx)
732 char T[NO_SQUARES + 1], *p, *q;
734 if ((p = strstr(sx, CP[169])) != NULL)
735 p += strlen(CP[169]);
736 else if ((p = strstr(sx, CP[217])) != NULL)
737 p += strlen(CP[217]);
743 for (; *p != 'X'; *q++ = *p++);
747 /* line empty ask for input */
750 fputs(CP[61], stdout);
751 fgets(T, NO_SQUARES + 1, stdin);
755 /* skip blackspace */
756 for (p = T; *p == ' '; p++) ;
758 /* could be moves or a fischer clock */
761 /* its a fischer clock game */
763 TCminutes = (short)strtol(p, &q, 10);
764 TCadd = (short)strtol(q, NULL, 10) *100;
772 TCmoves = (short)strtol(p, &q, 10);
773 TCminutes = (short)strtol(q, &q, 10);
776 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
781 fputs(CP[94], stdout);
782 scanf("%hd", &OperatorTime);
788 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
789 TCminutes = TCseconds = 0;
798 TimeControl.clock[black] = TimeControl.clock[white] = 0;
803 printf("Clocks: %ld %ld\n",
804 TimeControl.clock[black] * 10,
805 TimeControl.clock[white] * 10);
813 Raw_ChangeSearchDepth(void)
816 scanf("%hd", &MaxSearchDepth);
817 TCflag = !(MaxSearchDepth > 0);
824 Raw_ChangeHashDepth(void)
826 printf("hashdepth = ");
827 scanf("%hd", &HashDepth);
828 printf("MoveLimit = ");
829 scanf("%hd", &HashMoveLimit);
835 Raw_SetContempt(void)
837 printf("contempt = ");
838 scanf("%hd", &contempt);
844 Raw_ChangeXwindow(void)
847 scanf("%hd", &xwndw);
852 * Raw_ShowPostnValue(short sq)
853 * must have called ExaminePosition() first
857 Raw_ShowPostnValue(short sq)
860 score = ScorePosition(color[sq]);
862 if (color[sq] != neutral)
864 #if defined SAVE_SVALUE
865 printf("???%c ", (color[sq] == white)?'b':'w');
867 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
881 short c, p, sq, tp, tc, tsq, score, j, k;
884 ExaminePosition(opponent);
885 Raw_ShowMessage(CP[65]);
889 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
892 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
895 for (p = king; p > no_piece; p--)
897 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
903 for (j = (NO_ROWS - 1); j >= 0; j--)
905 for (k = 0; k < (NO_COLS); k++)
907 sq = j*(NO_COLS) + k;
912 tsq = PieceList[c][1];
913 PieceList[c][1] = sq;
914 Raw_ShowPostnValue(sq);
915 PieceList[c][1] = tsq;
924 score = ScorePosition(opponent);
926 for (j = (NO_ROWS - 1); j >= 0; j--)
928 for (k = 0; k < (NO_COLS); k++)
930 sq = j*(NO_COLS) + k;
932 if (color[sq] != neutral)
934 #if defined SAVE_SVALUE
935 printf("%?????%c ", (color[sq] == white)?'b':'w');
937 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
949 printf("stage = %d\n", stage);
950 printf(CP[103], score,
951 mtl[computer], pscore[computer], GameType[computer],
952 mtl[opponent], pscore[opponent], GameType[opponent]);
958 Raw_DoTable(short table[NO_SQUARES])
961 ExaminePosition(opponent);
963 for (j = (NO_ROWS - 1); j >= 0; j--)
965 for (k = 0; k < NO_COLS; k++)
967 sq = j*(NO_ROWS) + k;
968 printf("%3d ", table[sq]);
978 Raw_ShowPostnValues(void)
980 short sq, score, j, k;
981 ExaminePosition(opponent);
983 for (j = (NO_ROWS - 1); j >= 0; j--)
985 for (k = 0; k < NO_COLS; k++)
987 sq = j * NO_COLS + k;
988 Raw_ShowPostnValue(sq);
994 score = ScorePosition(opponent);
995 printf(CP[103], score,
996 mtl[computer], pscore[computer], GameType[computer],
997 mtl[opponent], pscore[opponent], GameType[opponent]);
998 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);