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_GetString(char* sx)
157 while(!eof && !sx[0])
158 eof = (fgets(sx, 80, stdin) == NULL);
164 Raw_ShowNodeCnt(long NodeCnt)
167 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
172 Raw_ShowPatternCount(short side, short n)
175 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
180 Raw_ShowResponseTime(void)
186 Raw_ShowResults(short score, unsigned short *bstline, char ch)
188 if (flag.post && !XSHOGI)
191 printf("%2d%c %6d %4ld %8ld ",
192 Sdepth, ch, score, et / 100, NodeCnt);
193 Raw_ShowLine(bstline);
199 Raw_ShowSidetoMove(void)
207 printf("stage = %d\n", stage);
208 printf("balance[black] = %d balance[white] = %d\n",
209 balance[black], balance[white]);
212 /****************************************
213 * End of trivial output routines.
214 ****************************************/
224 #ifdef HAVE_SETLINEBUF
228 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
230 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
233 printf("GNU Shogi %sp%s\n", version, patchlevel);
238 if (!TCflag && (MaxResponseTime == 0))
239 MaxResponseTime = 15L * 100L;
248 /* CHECKME: what purpose does this next statement serve? */
249 signal(SIGTERM, SIG_IGN);
263 Raw_ShowMessage(CP[31]); /* Abort? */
266 if (strcmp(s, CP[210]) == 0) /* yes */
273 Raw_TerminateSearch(int sig)
275 #ifdef INTERRUPT_TEST
276 ElapsedTime(INIT_INTERRUPT_MODE);
280 flag.back = true; /* previous: flag.timeout = true; */
282 flag.bothsides = false;
291 /* printf("SHOGI command summary\n"); */
292 printf(CP[40], version, patchlevel);
293 printf("----------------------------------"
294 "------------------------------\n");
295 /* printf("7g7f move from 7g to 7f quit
297 fputs(CP[158], stdout);
298 /* printf("S6h move silver to 6h beep
299 * turn %s\n", (flag.beep) ? "off" : "on"); */
300 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
301 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
302 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
303 /* printf("P*5e drop pawn to 5e easy
304 * turn %s\n", (flag.easy) ? "off" : "on"); */
305 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
307 * turn %s\n", (flag.hash) ? "off" : "on"); */
308 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
309 /* printf("bd redraw board reverse
310 * board display\n"); */
311 fputs(CP[130], stdout);
312 /* printf("list game to shogi.lst book
313 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
314 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
315 /* printf("undo undo last ply remove
316 * take back a move\n"); */
317 fputs(CP[200], stdout);
318 /* printf("edit edit board force
319 * enter game moves\n"); */
320 fputs(CP[153], stdout);
321 /* printf("switch sides with computer both
322 * computer match\n"); */
323 fputs(CP[194], stdout);
324 /* printf("black computer plays black white
325 * computer plays white\n"); */
326 fputs(CP[202], stdout);
327 /* printf("depth set search depth clock
328 * set time control\n"); */
329 fputs(CP[149], stdout);
330 /* printf("post principle variation hint
331 * suggest a move\n"); */
332 fputs(CP[177], stdout);
333 /* printf("save game to file get
334 * game from file\n"); */
335 fputs(CP[188], stdout);
336 printf("xsave pos. to xshogi file xget"
337 " pos. from xshogi file\n");
338 /* printf("random randomize play new
339 * start new game\n"); */
340 fputs(CP[181], stdout);
341 printf("--------------------------------"
342 "--------------------------------\n");
343 /* printf("Computer: %-12s Opponent: %s\n", */
345 ColorStr[computer], ColorStr[opponent]);
346 /* printf("Depth: %-12d Response time: %d sec\n", */
348 MaxSearchDepth, MaxResponseTime/100);
349 /* printf("Random: %-12s Easy mode: %s\n", */
351 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
352 /* printf("Beep: %-12s Transposition file: %s\n", */
354 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
355 /* printf("Tsume: %-12s Force: %s\n")*/
357 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
358 /* printf("Time Control %s %d moves %d seconds %d opr %d
359 * depth\n", (TCflag) ? "ON" : "OFF", */
361 (TCflag) ? CP[93] : CP[92],
362 TimeControl.moves[black], TimeControl.clock[black] / 100,
363 TCadd/100, MaxSearchDepth);
369 * Set up a board position. Pieces are entered by typing the piece followed
370 * by the location. For example, Nf3 will place a knight on square f3.
376 short a, r, c, sq, i, found;
379 flag.regularstart = true;
382 Raw_UpdateDisplay(0, 0, 1, 0);
383 /* printf(". exit to main\n"); */
384 fputs(CP[29], stdout);
385 /* printf("# clear board\n"); */
386 fputs(CP[28], stdout);
387 /* printf("c change sides\n"); */
388 fputs(CP[136], stdout);
389 /* printf("enter piece & location: \n"); */
390 fputs(CP[155], stdout);
399 if (s[0] == CP[28][0]) /*#*/
401 for (sq = 0; sq < NO_SQUARES; sq++)
403 board[sq] = no_piece;
410 if (s[0] == CP[136][0]) /*c*/
415 for (i = pawn; i <= king; i++)
417 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
434 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
438 board[sq] = no_piece;
440 for (i = no_piece; i <= king; i++)
442 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
445 board[sq] = promoted[i];
458 while (s[0] != CP[29][0]);
460 for (sq = 0; sq < NO_SQUARES; sq++)
461 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
469 Raw_UpdateDisplay(0, 0, 1, 0);
476 * Set up a board position.
477 * Nine lines of nine characters are used to setup the board. 9a-1a is the
478 * first line. White pieces are represented by uppercase characters.
490 fgets(s, 80, stdin); /* skip "setup" command */
492 for (r = NO_ROWS - 1; r >= 0; r--)
496 for (c = 0; c <= (NO_COLS - 1); c++)
501 board[sq] = no_piece;
503 for (i = no_piece; i <= king; i++)
511 else if (ch == qxx[i])
521 for (sq = 0; sq < NO_SQUARES; sq++)
522 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
526 Raw_UpdateDisplay(0, 0, 1, 0);
527 /* printf("Setup successful\n"); */
528 fputs(CP[106], stdout);
533 Raw_SearchStartStuff(short side)
539 ResponseTime, TimeControl.clock[side]);
550 printf("%s\n", CP[225]);
554 if (mvstr[0][0] == '\0')
559 /* add remaining time in milliseconds to xshogi */
560 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
561 (TimeControl.clock[player] - et) * 10);
565 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
569 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
570 || (root->score == (SCORE_LIMIT + 998)))
583 if (Tree[t].f || Tree[t].t)
591 /* printf("Nodes %ld Tree %d Eval %ld
592 * Rate %ld RS high %ld low %ld\n", */
593 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
594 (et > 100) ? (NodeCnt / (et / 100)) : 0,
595 EADD, EGET, reminus, replus);
597 /* printf("Hin/Hout/Coll/Fin/Fout =
598 * %ld/%ld/%ld/%ld/%ld\n", */
600 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
603 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
607 /* printf("My move is: %s\n", mvstr[0]); */
608 printf(CP[83], mvstr[0]);
615 if (root->flags & draw)
617 /* printf("Drawn game!\n"); */
618 fputs(CP[57], stdout);
620 else if (root->score == -(SCORE_LIMIT + 999))
622 printf("%s mates!\n", ColorStr[opponent]);
624 else if (root->score == (SCORE_LIMIT + 998))
626 printf("%s mates!\n", ColorStr[computer]);
629 else if (!barebones && (root->score < -SCORE_LIMIT))
631 printf("%s has a forced mate in %d moves!\n",
632 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
634 else if (!barebones && (root->score > SCORE_LIMIT))
636 printf("%s has a forced mate in %d moves!\n",
637 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
639 #endif /* VERYBUGGY */
644 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
649 if (redraw && !XSHOGI)
652 r = (short)(TimeControl.clock[black] / 6000);
653 c = (short)((TimeControl.clock[black] % 6000) / 100);
654 l = (short)(TimeControl.clock[white] / 6000);
655 m = (short)((TimeControl.clock[white] % 6000) / 100);
656 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
657 printf(CP[116], r, c, l, m);
660 for (r = (NO_ROWS - 1); r >= 0; r--)
662 for (c = 0; c <= (NO_COLS - 1); c++)
666 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
668 pc = (is_promoted[board[l]] ? '+' : ' ');
670 if (color[l] == neutral)
672 else if (color[l] == black)
673 printf("%c%c", pc, qxx[board[l]]);
675 printf("%c%c", pc, pxx[board[l]]);
685 for (side = black; side <= white; side++)
688 printf((side == black)?"black ":"white ");
690 for (piece = pawn; piece <= king; piece++)
692 if ((c = Captured[side][piece]))
693 printf("%i%c ", c, pxx[piece]);
705 Raw_ChangeAlphaWindow(void)
707 printf("WAwindow: ");
708 scanf("%hd", &WAwindow);
709 printf("BAwindow: ");
710 scanf("%hd", &BAwindow);
716 Raw_ChangeBetaWindow(void)
718 printf("WBwindow: ");
719 scanf("%hd", &WBwindow);
720 printf("BBwindow: ");
721 scanf("%hd", &BBwindow);
731 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
732 printf(CP[72], mvstr[0]); /*hint*/
735 fputs(CP[223], stdout);
741 Raw_SelectLevel(char *sx)
744 char T[NO_SQUARES + 1], *p, *q;
746 if ((p = strstr(sx, CP[169])) != NULL)
747 p += strlen(CP[169]);
748 else if ((p = strstr(sx, CP[217])) != NULL)
749 p += strlen(CP[217]);
755 for (; *p != 'X'; *q++ = *p++);
759 /* line empty ask for input */
762 fputs(CP[61], stdout);
763 fgets(T, NO_SQUARES + 1, stdin);
767 /* skip blackspace */
768 for (p = T; *p == ' '; p++) ;
770 /* could be moves or a fischer clock */
773 /* its a fischer clock game */
775 TCminutes = (short)strtol(p, &q, 10);
776 TCadd = (short)strtol(q, NULL, 10) *100;
784 TCmoves = (short)strtol(p, &q, 10);
785 TCminutes = (short)strtol(q, &q, 10);
788 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
793 fputs(CP[94], stdout);
794 scanf("%hd", &OperatorTime);
800 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
801 TCminutes = TCseconds = 0;
810 TimeControl.clock[black] = TimeControl.clock[white] = 0;
815 printf("Clocks: %ld %ld\n",
816 TimeControl.clock[black] * 10,
817 TimeControl.clock[white] * 10);
825 Raw_ChangeSearchDepth(void)
828 scanf("%hd", &MaxSearchDepth);
829 TCflag = !(MaxSearchDepth > 0);
836 Raw_ChangeHashDepth(void)
838 printf("hashdepth = ");
839 scanf("%hd", &HashDepth);
840 printf("MoveLimit = ");
841 scanf("%hd", &HashMoveLimit);
847 Raw_SetContempt(void)
849 printf("contempt = ");
850 scanf("%hd", &contempt);
856 Raw_ChangeXwindow(void)
859 scanf("%hd", &xwndw);
864 * Raw_ShowPostnValue(short sq)
865 * must have called ExaminePosition() first
869 Raw_ShowPostnValue(short sq)
872 score = ScorePosition(color[sq]);
874 if (color[sq] != neutral)
876 #if defined SAVE_SVALUE
877 printf("???%c ", (color[sq] == white)?'b':'w');
879 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
893 short c, p, sq, tp, tc, tsq, score, j, k;
896 ExaminePosition(opponent);
897 Raw_ShowMessage(CP[65]);
901 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
904 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
907 for (p = king; p > no_piece; p--)
909 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
915 for (j = (NO_ROWS - 1); j >= 0; j--)
917 for (k = 0; k < (NO_COLS); k++)
919 sq = j*(NO_COLS) + k;
924 tsq = PieceList[c][1];
925 PieceList[c][1] = sq;
926 Raw_ShowPostnValue(sq);
927 PieceList[c][1] = tsq;
936 score = ScorePosition(opponent);
938 for (j = (NO_ROWS - 1); j >= 0; j--)
940 for (k = 0; k < (NO_COLS); k++)
942 sq = j*(NO_COLS) + k;
944 if (color[sq] != neutral)
946 #if defined SAVE_SVALUE
947 printf("%?????%c ", (color[sq] == white)?'b':'w');
949 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
961 printf("stage = %d\n", stage);
962 printf(CP[103], score,
963 mtl[computer], pscore[computer], GameType[computer],
964 mtl[opponent], pscore[opponent], GameType[opponent]);
970 Raw_DoTable(short table[NO_SQUARES])
973 ExaminePosition(opponent);
975 for (j = (NO_ROWS - 1); j >= 0; j--)
977 for (k = 0; k < NO_COLS; k++)
979 sq = j*(NO_ROWS) + k;
980 printf("%3d ", table[sq]);
990 Raw_ShowPostnValues(void)
992 short sq, score, j, k;
993 ExaminePosition(opponent);
995 for (j = (NO_ROWS - 1); j >= 0; j--)
997 for (k = 0; k < NO_COLS; k++)
999 sq = j * NO_COLS + k;
1000 Raw_ShowPostnValue(sq);
1006 score = ScorePosition(opponent);
1007 printf(CP[103], score,
1008 mtl[computer], pscore[computer], GameType[computer],
1009 mtl[opponent], pscore[opponent], GameType[opponent]);
1010 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);