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_ShowPatternCount(short side, short n)
146 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
151 Raw_ShowResponseTime(void)
157 Raw_ShowResults(short score, unsigned short *bstline, char ch)
159 if (flag.post && !XSHOGI)
162 printf("%2d%c %6d %4ld %8ld ",
163 Sdepth, ch, score, et / 100, NodeCnt);
164 Raw_ShowLine(bstline);
170 Raw_ShowSidetoMove(void)
178 printf("stage = %d\n", stage);
179 printf("balance[black] = %d balance[white] = %d\n",
180 balance[black], balance[white]);
183 /****************************************
184 * End of trivial output routines.
185 ****************************************/
195 #ifdef HAVE_SETLINEBUF
199 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
201 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
204 printf("GNU Shogi %sp%s\n", version, patchlevel);
209 if (!TCflag && (MaxResponseTime == 0))
210 MaxResponseTime = 15L * 100L;
219 /* CHECKME: what purpose does this next statement serve? */
220 signal(SIGTERM, SIG_IGN);
234 Raw_ShowMessage(CP[31]); /* Abort? */
237 if (strcmp(s, CP[210]) == 0) /* yes */
244 Raw_TerminateSearch(int sig)
246 #ifdef INTERRUPT_TEST
247 ElapsedTime(INIT_INTERRUPT_MODE);
251 flag.back = true; /* previous: flag.timeout = true; */
253 flag.bothsides = false;
262 /* printf("SHOGI command summary\n"); */
263 printf(CP[40], version, patchlevel);
264 printf("----------------------------------"
265 "------------------------------\n");
266 /* printf("7g7f move from 7g to 7f quit
268 fputs(CP[158], stdout);
269 /* printf("S6h move silver to 6h beep
270 * turn %s\n", (flag.beep) ? "off" : "on"); */
271 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
272 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
273 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
274 /* printf("P*5e drop pawn to 5e easy
275 * turn %s\n", (flag.easy) ? "off" : "on"); */
276 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
278 * turn %s\n", (flag.hash) ? "off" : "on"); */
279 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
280 /* printf("bd redraw board reverse
281 * board display\n"); */
282 fputs(CP[130], stdout);
283 /* printf("list game to shogi.lst book
284 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
285 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
286 /* printf("undo undo last ply remove
287 * take back a move\n"); */
288 fputs(CP[200], stdout);
289 /* printf("edit edit board force
290 * enter game moves\n"); */
291 fputs(CP[153], stdout);
292 /* printf("switch sides with computer both
293 * computer match\n"); */
294 fputs(CP[194], stdout);
295 /* printf("black computer plays black white
296 * computer plays white\n"); */
297 fputs(CP[202], stdout);
298 /* printf("depth set search depth clock
299 * set time control\n"); */
300 fputs(CP[149], stdout);
301 /* printf("post principle variation hint
302 * suggest a move\n"); */
303 fputs(CP[177], stdout);
304 /* printf("save game to file get
305 * game from file\n"); */
306 fputs(CP[188], stdout);
307 printf("xsave pos. to xshogi file xget"
308 " pos. from xshogi file\n");
309 /* printf("random randomize play new
310 * start new game\n"); */
311 fputs(CP[181], stdout);
312 printf("--------------------------------"
313 "--------------------------------\n");
314 /* printf("Computer: %-12s Opponent: %s\n", */
316 ColorStr[computer], ColorStr[opponent]);
317 /* printf("Depth: %-12d Response time: %d sec\n", */
319 MaxSearchDepth, MaxResponseTime/100);
320 /* printf("Random: %-12s Easy mode: %s\n", */
322 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
323 /* printf("Beep: %-12s Transposition file: %s\n", */
325 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
326 /* printf("Tsume: %-12s Force: %s\n")*/
328 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
329 /* printf("Time Control %s %d moves %d seconds %d opr %d
330 * depth\n", (TCflag) ? "ON" : "OFF", */
332 (TCflag) ? CP[93] : CP[92],
333 TimeControl.moves[black], TimeControl.clock[black] / 100,
334 TCadd/100, MaxSearchDepth);
340 * Set up a board position. Pieces are entered by typing the piece followed
341 * by the location. For example, Nf3 will place a knight on square f3.
347 short a, r, c, sq, i, found;
350 flag.regularstart = true;
353 Raw_UpdateDisplay(0, 0, 1, 0);
354 /* printf(". exit to main\n"); */
355 fputs(CP[29], stdout);
356 /* printf("# clear board\n"); */
357 fputs(CP[28], stdout);
358 /* printf("c change sides\n"); */
359 fputs(CP[136], stdout);
360 /* printf("enter piece & location: \n"); */
361 fputs(CP[155], stdout);
370 if (s[0] == CP[28][0]) /*#*/
372 for (sq = 0; sq < NO_SQUARES; sq++)
374 board[sq] = no_piece;
381 if (s[0] == CP[136][0]) /*c*/
386 for (i = pawn; i <= king; i++)
388 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
405 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
409 board[sq] = no_piece;
411 for (i = no_piece; i <= king; i++)
413 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
416 board[sq] = promoted[i];
429 while (s[0] != CP[29][0]);
431 for (sq = 0; sq < NO_SQUARES; sq++)
432 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
440 Raw_UpdateDisplay(0, 0, 1, 0);
447 * Set up a board position.
448 * Nine lines of nine characters are used to setup the board. 9a-1a is the
449 * first line. White pieces are represented by uppercase characters.
461 fgets(s, 80, stdin); /* skip "setup" command */
463 for (r = NO_ROWS - 1; r >= 0; r--)
467 for (c = 0; c <= (NO_COLS - 1); c++)
472 board[sq] = no_piece;
474 for (i = no_piece; i <= king; i++)
482 else if (ch == qxx[i])
492 for (sq = 0; sq < NO_SQUARES; sq++)
493 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
497 Raw_UpdateDisplay(0, 0, 1, 0);
498 /* printf("Setup successful\n"); */
499 fputs(CP[106], stdout);
504 Raw_SearchStartStuff(short side)
510 ResponseTime, TimeControl.clock[side]);
521 printf("%s\n", CP[225]);
525 if (mvstr[0][0] == '\0')
530 /* add remaining time in milliseconds to xshogi */
531 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
532 (TimeControl.clock[player] - et) * 10);
536 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
540 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
541 || (root->score == (SCORE_LIMIT + 998)))
554 if (Tree[t].f || Tree[t].t)
562 /* printf("Nodes %ld Tree %d Eval %ld
563 * Rate %ld RS high %ld low %ld\n", */
564 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
565 (et > 100) ? (NodeCnt / (et / 100)) : 0,
566 EADD, EGET, reminus, replus);
568 /* printf("Hin/Hout/Coll/Fin/Fout =
569 * %ld/%ld/%ld/%ld/%ld\n", */
571 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
574 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
578 /* printf("My move is: %s\n", mvstr[0]); */
579 printf(CP[83], mvstr[0]);
586 if (root->flags & draw)
588 /* printf("Drawn game!\n"); */
589 fputs(CP[57], stdout);
591 else if (root->score == -(SCORE_LIMIT + 999))
593 printf("%s mates!\n", ColorStr[opponent]);
595 else if (root->score == (SCORE_LIMIT + 998))
597 printf("%s mates!\n", ColorStr[computer]);
600 else if (!barebones && (root->score < -SCORE_LIMIT))
602 printf("%s has a forced mate in %d moves!\n",
603 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
605 else if (!barebones && (root->score > SCORE_LIMIT))
607 printf("%s has a forced mate in %d moves!\n",
608 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
610 #endif /* VERYBUGGY */
615 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
620 if (redraw && !XSHOGI)
623 r = (short)(TimeControl.clock[black] / 6000);
624 c = (short)((TimeControl.clock[black] % 6000) / 100);
625 l = (short)(TimeControl.clock[white] / 6000);
626 m = (short)((TimeControl.clock[white] % 6000) / 100);
627 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
628 printf(CP[116], r, c, l, m);
631 for (r = (NO_ROWS - 1); r >= 0; r--)
633 for (c = 0; c <= (NO_COLS - 1); c++)
637 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
639 pc = (is_promoted[board[l]] ? '+' : ' ');
641 if (color[l] == neutral)
643 else if (color[l] == black)
644 printf("%c%c", pc, qxx[board[l]]);
646 printf("%c%c", pc, pxx[board[l]]);
656 for (side = black; side <= white; side++)
659 printf((side == black)?"black ":"white ");
661 for (piece = pawn; piece <= king; piece++)
663 if ((c = Captured[side][piece]))
664 printf("%i%c ", c, pxx[piece]);
676 Raw_ChangeAlphaWindow(void)
678 printf("WAwindow: ");
679 scanf("%hd", &WAwindow);
680 printf("BAwindow: ");
681 scanf("%hd", &BAwindow);
687 Raw_ChangeBetaWindow(void)
689 printf("WBwindow: ");
690 scanf("%hd", &WBwindow);
691 printf("BBwindow: ");
692 scanf("%hd", &BBwindow);
702 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
703 printf(CP[72], mvstr[0]); /*hint*/
706 fputs(CP[223], stdout);
712 Raw_SelectLevel(char *sx)
715 char T[NO_SQUARES + 1], *p, *q;
717 if ((p = strstr(sx, CP[169])) != NULL)
718 p += strlen(CP[169]);
719 else if ((p = strstr(sx, CP[217])) != NULL)
720 p += strlen(CP[217]);
726 for (; *p != 'X'; *q++ = *p++);
730 /* line empty ask for input */
733 fputs(CP[61], stdout);
734 fgets(T, NO_SQUARES + 1, stdin);
738 /* skip blackspace */
739 for (p = T; *p == ' '; p++) ;
741 /* could be moves or a fischer clock */
744 /* its a fischer clock game */
746 TCminutes = (short)strtol(p, &q, 10);
747 TCadd = (short)strtol(q, NULL, 10) *100;
755 TCmoves = (short)strtol(p, &q, 10);
756 TCminutes = (short)strtol(q, &q, 10);
759 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
764 fputs(CP[94], stdout);
765 scanf("%hd", &OperatorTime);
771 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
772 TCminutes = TCseconds = 0;
781 TimeControl.clock[black] = TimeControl.clock[white] = 0;
786 printf("Clocks: %ld %ld\n",
787 TimeControl.clock[black] * 10,
788 TimeControl.clock[white] * 10);
796 Raw_ChangeSearchDepth(void)
799 scanf("%hd", &MaxSearchDepth);
800 TCflag = !(MaxSearchDepth > 0);
807 Raw_ChangeHashDepth(void)
809 printf("hashdepth = ");
810 scanf("%hd", &HashDepth);
811 printf("MoveLimit = ");
812 scanf("%hd", &HashMoveLimit);
818 Raw_SetContempt(void)
820 printf("contempt = ");
821 scanf("%hd", &contempt);
827 Raw_ChangeXwindow(void)
830 scanf("%hd", &xwndw);
835 * Raw_ShowPostnValue(short sq)
836 * must have called ExaminePosition() first
840 Raw_ShowPostnValue(short sq)
843 score = ScorePosition(color[sq]);
845 if (color[sq] != neutral)
847 #if defined SAVE_SVALUE
848 printf("???%c ", (color[sq] == white)?'b':'w');
850 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
864 short c, p, sq, tp, tc, tsq, score, j, k;
867 ExaminePosition(opponent);
868 Raw_ShowMessage(CP[65]);
872 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
875 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
878 for (p = king; p > no_piece; p--)
880 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
886 for (j = (NO_ROWS - 1); j >= 0; j--)
888 for (k = 0; k < (NO_COLS); k++)
890 sq = j*(NO_COLS) + k;
895 tsq = PieceList[c][1];
896 PieceList[c][1] = sq;
897 Raw_ShowPostnValue(sq);
898 PieceList[c][1] = tsq;
907 score = ScorePosition(opponent);
909 for (j = (NO_ROWS - 1); j >= 0; j--)
911 for (k = 0; k < (NO_COLS); k++)
913 sq = j*(NO_COLS) + k;
915 if (color[sq] != neutral)
917 #if defined SAVE_SVALUE
918 printf("%?????%c ", (color[sq] == white)?'b':'w');
920 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
932 printf("stage = %d\n", stage);
933 printf(CP[103], score,
934 mtl[computer], pscore[computer], GameType[computer],
935 mtl[opponent], pscore[opponent], GameType[opponent]);
941 Raw_DoTable(short table[NO_SQUARES])
944 ExaminePosition(opponent);
946 for (j = (NO_ROWS - 1); j >= 0; j--)
948 for (k = 0; k < NO_COLS; k++)
950 sq = j*(NO_ROWS) + k;
951 printf("%3d ", table[sq]);
961 Raw_ShowPostnValues(void)
963 short sq, score, j, k;
964 ExaminePosition(opponent);
966 for (j = (NO_ROWS - 1); j >= 0; j--)
968 for (k = 0; k < NO_COLS; k++)
970 sq = j * NO_COLS + k;
971 Raw_ShowPostnValue(sq);
977 score = ScorePosition(opponent);
978 printf(CP[103], score,
979 mtl[computer], pscore[computer], GameType[computer],
980 mtl[opponent], pscore[opponent], GameType[opponent]);
981 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);