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 * ----------------------------------------------------------------------
34 #include <sys/param.h>
35 #include <sys/types.h>
41 unsigned short MV[MAXDEPTH];
47 extern short pscore[];
50 /****************************************
51 * Trivial output functions.
52 ****************************************/
57 if (!barebones && !XSHOGI)
62 /* FIXME: change to ShowPrompt? */
66 if (!barebones && !XSHOGI)
68 /* printf("\nYour move is? "); */
69 fputs(CP[124], stdout);
75 Raw_ShowCurrentMove(short pnt, short f, short t)
81 Raw_ShowDepth(char ch)
83 if (!barebones && !XSHOGI)
85 printf(CP[53], Sdepth, ch); /* Depth = %d%c */
92 Raw_ShowGameType(void)
95 printf("%c vs. %c\n", GameType[black], GameType[white]);
100 Raw_ShowLine(unsigned short *bstline)
104 for (i = 1; bstline[i] > 0; i++)
106 if ((i > 1) && (i % 8 == 1))
109 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
110 printf("%5s ", mvstr[0]);
118 Raw_ShowMessage(char *s)
126 Raw_ShowPatternCount(short side, short n)
129 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
134 Raw_ShowResponseTime(void)
140 Raw_ShowResults(short score, unsigned short *bstline, char ch)
142 if (flag.post && !XSHOGI)
145 printf("%2d%c %6d %4ld %8ld ",
146 Sdepth, ch, score, et / 100, NodeCnt);
147 Raw_ShowLine(bstline);
153 Raw_ShowSidetoMove(void)
161 printf("stage = %d\n", stage);
162 printf("balance[black] = %d balance[white] = %d\n",
163 balance[black], balance[white]);
166 /****************************************
167 * End of trivial output routines.
168 ****************************************/
178 #ifdef HAVE_SETLINEBUF
182 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
184 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
187 printf("GNU Shogi %sp%s\n", version, patchlevel);
192 if (!TCflag && (MaxResponseTime == 0))
193 MaxResponseTime = 15L * 100L;
202 /* CHECKME: what purpose does this next statement serve? */
203 signal(SIGTERM, SIG_IGN);
217 Raw_ShowMessage(CP[31]); /* Abort? */
220 if (strcmp(s, CP[210]) == 0) /* yes */
227 Raw_TerminateSearch(int sig)
229 #ifdef INTERRUPT_TEST
230 ElapsedTime(INIT_INTERRUPT_MODE);
234 flag.back = true; /* previous: flag.timeout = true; */
236 flag.bothsides = false;
245 /* printf("SHOGI command summary\n"); */
246 printf(CP[40], version, patchlevel);
247 printf("----------------------------------"
248 "------------------------------\n");
249 /* printf("7g7f move from 7g to 7f quit
251 fputs(CP[158], stdout);
252 /* printf("S6h move silver to 6h beep
253 * turn %s\n", (flag.beep) ? "off" : "on"); */
254 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
255 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
256 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
257 /* printf("P*5e drop pawn to 5e easy
258 * turn %s\n", (flag.easy) ? "off" : "on"); */
259 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
261 * turn %s\n", (flag.hash) ? "off" : "on"); */
262 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
263 /* printf("bd redraw board reverse
264 * board display\n"); */
265 fputs(CP[130], stdout);
266 /* printf("list game to shogi.lst book
267 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
268 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
269 /* printf("undo undo last ply remove
270 * take back a move\n"); */
271 fputs(CP[200], stdout);
272 /* printf("edit edit board force
273 * enter game moves\n"); */
274 fputs(CP[153], stdout);
275 /* printf("switch sides with computer both
276 * computer match\n"); */
277 fputs(CP[194], stdout);
278 /* printf("black computer plays black white
279 * computer plays white\n"); */
280 fputs(CP[202], stdout);
281 /* printf("depth set search depth clock
282 * set time control\n"); */
283 fputs(CP[149], stdout);
284 /* printf("post principle variation hint
285 * suggest a move\n"); */
286 fputs(CP[177], stdout);
287 /* printf("save game to file get
288 * game from file\n"); */
289 fputs(CP[188], stdout);
290 printf("xsave pos. to xshogi file xget"
291 " pos. from xshogi file\n");
292 /* printf("random randomize play new
293 * start new game\n"); */
294 fputs(CP[181], stdout);
295 printf("--------------------------------"
296 "--------------------------------\n");
297 /* printf("Computer: %-12s Opponent: %s\n", */
299 ColorStr[computer], ColorStr[opponent]);
300 /* printf("Depth: %-12d Response time: %d sec\n", */
302 MaxSearchDepth, MaxResponseTime/100);
303 /* printf("Random: %-12s Easy mode: %s\n", */
305 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
306 /* printf("Beep: %-12s Transposition file: %s\n", */
308 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
309 /* printf("Tsume: %-12s Force: %s\n")*/
311 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
312 /* printf("Time Control %s %d moves %d seconds %d opr %d
313 * depth\n", (TCflag) ? "ON" : "OFF", */
315 (TCflag) ? CP[93] : CP[92],
316 TimeControl.moves[black], TimeControl.clock[black] / 100,
317 TCadd/100, MaxSearchDepth);
319 signal(SIGINT, Raw_TerminateSearch);
320 signal(SIGQUIT, Raw_TerminateSearch);
326 * Set up a board position. Pieces are entered by typing the piece followed
327 * by the location. For example, Nf3 will place a knight on square f3.
333 short a, r, c, sq, i, found;
336 flag.regularstart = true;
339 Raw_UpdateDisplay(0, 0, 1, 0);
340 /* printf(". exit to main\n"); */
341 fputs(CP[29], stdout);
342 /* printf("# clear board\n"); */
343 fputs(CP[28], stdout);
344 /* printf("c change sides\n"); */
345 fputs(CP[136], stdout);
346 /* printf("enter piece & location: \n"); */
347 fputs(CP[155], stdout);
356 if (s[0] == CP[28][0]) /*#*/
358 for (sq = 0; sq < NO_SQUARES; sq++)
360 board[sq] = no_piece;
367 if (s[0] == CP[136][0]) /*c*/
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];
415 while (s[0] != CP[29][0]);
417 for (sq = 0; sq < NO_SQUARES; sq++)
418 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
426 Raw_UpdateDisplay(0, 0, 1, 0);
433 * Set up a board position.
434 * Nine lines of nine characters are used to setup the board. 9a-1a is the
435 * first line. White pieces are represented by uppercase characters.
447 fgets(s, 80, stdin); /* skip "setup" command */
449 for (r = NO_ROWS - 1; r >= 0; r--)
453 for (c = 0; c <= (NO_COLS - 1); c++)
458 board[sq] = no_piece;
460 for (i = no_piece; i <= king; i++)
468 else if (ch == qxx[i])
478 for (sq = 0; sq < NO_SQUARES; sq++)
479 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
483 Raw_UpdateDisplay(0, 0, 1, 0);
484 /* printf("Setup successful\n"); */
485 fputs(CP[106], stdout);
490 Raw_SearchStartStuff(short side)
492 signal(SIGINT, Raw_TerminateSearch);
493 signal(SIGQUIT, Raw_TerminateSearch);
499 ResponseTime, TimeControl.clock[side]);
510 printf("%s\n", CP[225]);
514 if (mvstr[0][0] == '\0')
519 /* add remaining time in milliseconds to xshogi */
520 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
521 (TimeControl.clock[player] - et) * 10);
525 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
529 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
530 || (root->score == (SCORE_LIMIT + 998)))
543 if (Tree[t].f || Tree[t].t)
551 /* printf("Nodes %ld Tree %d Eval %ld
552 * Rate %ld RS high %ld low %ld\n", */
553 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
554 (et > 100) ? (NodeCnt / (et / 100)) : 0,
555 EADD, EGET, reminus, replus);
557 /* printf("Hin/Hout/Coll/Fin/Fout =
558 * %ld/%ld/%ld/%ld/%ld\n", */
560 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
563 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
567 /* printf("My move is: %s\n", mvstr[0]); */
568 printf(CP[83], mvstr[0]);
575 if (root->flags & draw)
577 /* printf("Drawn game!\n"); */
578 fputs(CP[57], stdout);
580 else if (root->score == -(SCORE_LIMIT + 999))
582 printf("%s mates!\n", ColorStr[opponent]);
584 else if (root->score == (SCORE_LIMIT + 998))
586 printf("%s mates!\n", ColorStr[computer]);
589 else if (!barebones && (root->score < -SCORE_LIMIT))
591 printf("%s has a forced mate in %d moves!\n",
592 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
594 else if (!barebones && (root->score > SCORE_LIMIT))
596 printf("%s has a forced mate in %d moves!\n",
597 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
599 #endif /* VERYBUGGY */
604 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
609 if (redraw && !XSHOGI)
612 r = (short)(TimeControl.clock[black] / 6000);
613 c = (short)((TimeControl.clock[black] % 6000) / 100);
614 l = (short)(TimeControl.clock[white] / 6000);
615 m = (short)((TimeControl.clock[white] % 6000) / 100);
616 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
617 printf(CP[116], r, c, l, m);
620 for (r = (NO_ROWS - 1); r >= 0; r--)
622 for (c = 0; c <= (NO_COLS - 1); c++)
626 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
628 pc = (is_promoted[board[l]] ? '+' : ' ');
630 if (color[l] == neutral)
632 else if (color[l] == black)
633 printf("%c%c", pc, qxx[board[l]]);
635 printf("%c%c", pc, pxx[board[l]]);
645 for (side = black; side <= white; side++)
648 printf((side == black)?"black ":"white ");
650 for (piece = pawn; piece <= king; piece++)
652 if ((c = Captured[side][piece]))
653 printf("%i%c ", c, pxx[piece]);
665 Raw_ChangeAlphaWindow(void)
667 printf("WAwindow: ");
668 scanf("%hd", &WAwindow);
669 printf("BAwindow: ");
670 scanf("%hd", &BAwindow);
676 Raw_ChangeBetaWindow(void)
678 printf("WBwindow: ");
679 scanf("%hd", &WBwindow);
680 printf("BBwindow: ");
681 scanf("%hd", &BBwindow);
691 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
692 printf(CP[72], mvstr[0]); /*hint*/
695 fputs(CP[223], stdout);
701 Raw_SelectLevel(char *sx)
704 char T[NO_SQUARES + 1], *p, *q;
706 if ((p = strstr(sx, CP[169])) != NULL)
707 p += strlen(CP[169]);
708 else if ((p = strstr(sx, CP[217])) != NULL)
709 p += strlen(CP[217]);
715 for (; *p != 'X'; *q++ = *p++);
719 /* line empty ask for input */
722 fputs(CP[61], stdout);
723 fgets(T, NO_SQUARES + 1, stdin);
727 /* skip blackspace */
728 for (p = T; *p == ' '; p++) ;
730 /* could be moves or a fischer clock */
733 /* its a fischer clock game */
735 TCminutes = (short)strtol(p, &q, 10);
736 TCadd = (short)strtol(q, NULL, 10) *100;
744 TCmoves = (short)strtol(p, &q, 10);
745 TCminutes = (short)strtol(q, &q, 10);
748 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
753 fputs(CP[94], stdout);
754 scanf("%hd", &OperatorTime);
760 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
761 TCminutes = TCseconds = 0;
770 TimeControl.clock[black] = TimeControl.clock[white] = 0;
775 printf("Clocks: %ld %ld\n",
776 TimeControl.clock[black] * 10,
777 TimeControl.clock[white] * 10);
785 Raw_ChangeSearchDepth(void)
788 scanf("%hd", &MaxSearchDepth);
789 TCflag = !(MaxSearchDepth > 0);
796 Raw_ChangeHashDepth(void)
798 printf("hashdepth = ");
799 scanf("%hd", &HashDepth);
800 printf("MoveLimit = ");
801 scanf("%hd", &HashMoveLimit);
807 Raw_SetContempt(void)
809 printf("contempt = ");
810 scanf("%hd", &contempt);
816 Raw_ChangeXwindow(void)
819 scanf("%hd", &xwndw);
824 * Raw_ShowPostnValue(short sq)
825 * must have called ExaminePosition() first
829 Raw_ShowPostnValue(short sq)
832 score = ScorePosition(color[sq]);
834 if (color[sq] != neutral)
836 #if defined SAVE_SVALUE
837 printf("???%c ", (color[sq] == white)?'b':'w');
839 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
853 short c, p, sq, tp, tc, tsq, score, j, k;
856 ExaminePosition(opponent);
857 Raw_ShowMessage(CP[65]);
861 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
864 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
867 for (p = king; p > no_piece; p--)
869 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
875 for (j = (NO_ROWS - 1); j >= 0; j--)
877 for (k = 0; k < (NO_COLS); k++)
879 sq = j*(NO_COLS) + k;
884 tsq = PieceList[c][1];
885 PieceList[c][1] = sq;
886 Raw_ShowPostnValue(sq);
887 PieceList[c][1] = tsq;
896 score = ScorePosition(opponent);
898 for (j = (NO_ROWS - 1); j >= 0; j--)
900 for (k = 0; k < (NO_COLS); k++)
902 sq = j*(NO_COLS) + k;
904 if (color[sq] != neutral)
906 #if defined SAVE_SVALUE
907 printf("%?????%c ", (color[sq] == white)?'b':'w');
909 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
921 printf("stage = %d\n", stage);
922 printf(CP[103], score,
923 mtl[computer], pscore[computer], GameType[computer],
924 mtl[opponent], pscore[opponent], GameType[opponent]);
930 Raw_DoTable(short table[NO_SQUARES])
933 ExaminePosition(opponent);
935 for (j = (NO_ROWS - 1); j >= 0; j--)
937 for (k = 0; k < NO_COLS; k++)
939 sq = j*(NO_ROWS) + k;
940 printf("%3d ", table[sq]);
950 Raw_ShowPostnValues(void)
952 short sq, score, j, k;
953 ExaminePosition(opponent);
955 for (j = (NO_ROWS - 1); j >= 0; j--)
957 for (k = 0; k < NO_COLS; k++)
959 sq = j * NO_COLS + k;
960 Raw_ShowPostnValue(sq);
966 score = ScorePosition(opponent);
967 printf(CP[103], score,
968 mtl[computer], pscore[computer], GameType[computer],
969 mtl[opponent], pscore[opponent], GameType[opponent]);
970 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);