4 * ----------------------------------------------------------------------
6 * Copyright (c) 2012 Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * This file is part of GNU SHOGI.
12 * GNU Shogi is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License,
15 * or (at your option) any later version.
17 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with GNU Shogi; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
25 * ----------------------------------------------------------------------
31 #include <sys/param.h>
32 #include <sys/types.h>
38 unsigned short MV[MAXDEPTH];
44 extern short pscore[];
47 /****************************************
48 * Trivial output functions.
49 ****************************************/
54 if (!barebones && !XSHOGI)
59 /* FIXME: change to ShowPrompt? */
63 if (!barebones && !XSHOGI)
65 /* printf("\nYour move is? "); */
66 fputs(CP[124], stdout);
72 Raw_ShowCurrentMove(short pnt, short f, short t)
78 Raw_ShowDepth(char ch)
80 if (!barebones && !XSHOGI)
82 printf(CP[53], Sdepth, ch); /* Depth = %d%c */
89 Raw_ShowGameType(void)
92 printf("%c vs. %c\n", GameType[black], GameType[white]);
97 Raw_ShowLine(unsigned short *bstline)
101 for (i = 1; bstline[i] > 0; i++)
103 if ((i > 1) && (i % 8 == 1))
106 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
107 printf("%5s ", mvstr[0]);
115 Raw_ShowMessage(char *s)
123 Raw_ShowPatternCount(short side, short n)
126 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
131 Raw_ShowResponseTime(void)
137 Raw_ShowResults(short score, unsigned short *bstline, char ch)
139 if (flag.post && !XSHOGI)
142 printf("%2d%c %6d %4ld %8ld ",
143 Sdepth, ch, score, et / 100, NodeCnt);
144 Raw_ShowLine(bstline);
150 Raw_ShowSidetoMove(void)
158 printf("stage = %d\n", stage);
159 printf("balance[black] = %d balance[white] = %d\n",
160 balance[black], balance[white]);
163 /****************************************
164 * End of trivial output routines.
165 ****************************************/
175 #ifdef HAVE_SETLINEBUF
179 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
181 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
184 printf("GNU Shogi %sp%s\n", version, patchlevel);
189 if (!TCflag && (MaxResponseTime == 0))
190 MaxResponseTime = 15L * 100L;
199 /* CHECKME: what purpose does this next statement serve? */
200 signal(SIGTERM, SIG_IGN);
212 Raw_ShowMessage(CP[31]); /* Abort? */
215 if (strcmp(s, CP[210]) == 0) /* yes */
222 Raw_TerminateSearch(int sig)
224 #ifdef INTERRUPT_TEST
225 ElapsedTime(INIT_INTERRUPT_MODE);
229 flag.back = true; /* previous: flag.timeout = true; */
231 flag.bothsides = false;
240 /* printf("SHOGI command summary\n"); */
241 printf(CP[40], version, patchlevel);
242 printf("----------------------------------"
243 "------------------------------\n");
244 /* printf("7g7f move from 7g to 7f quit
246 fputs(CP[158], stdout);
247 /* printf("S6h move silver to 6h beep
248 * turn %s\n", (flag.beep) ? "off" : "on"); */
249 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
250 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
251 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
252 /* printf("P*5e drop pawn to 5e easy
253 * turn %s\n", (flag.easy) ? "off" : "on"); */
254 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
256 * turn %s\n", (flag.hash) ? "off" : "on"); */
257 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
258 /* printf("bd redraw board reverse
259 * board display\n"); */
260 fputs(CP[130], stdout);
261 /* printf("list game to shogi.lst book
262 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
263 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
264 /* printf("undo undo last ply remove
265 * take back a move\n"); */
266 fputs(CP[200], stdout);
267 /* printf("edit edit board force
268 * enter game moves\n"); */
269 fputs(CP[153], stdout);
270 /* printf("switch sides with computer both
271 * computer match\n"); */
272 fputs(CP[194], stdout);
273 /* printf("black computer plays black white
274 * computer plays white\n"); */
275 fputs(CP[202], stdout);
276 /* printf("depth set search depth clock
277 * set time control\n"); */
278 fputs(CP[149], stdout);
279 /* printf("post principle variation hint
280 * suggest a move\n"); */
281 fputs(CP[177], stdout);
282 /* printf("save game to file get
283 * game from file\n"); */
284 fputs(CP[188], stdout);
285 printf("xsave pos. to xshogi file xget"
286 " pos. from xshogi file\n");
287 /* printf("random randomize play new
288 * start new game\n"); */
289 fputs(CP[181], stdout);
290 printf("--------------------------------"
291 "--------------------------------\n");
292 /* printf("Computer: %-12s Opponent: %s\n", */
294 ColorStr[computer], ColorStr[opponent]);
295 /* printf("Depth: %-12d Response time: %d sec\n", */
297 MaxSearchDepth, MaxResponseTime/100);
298 /* printf("Random: %-12s Easy mode: %s\n", */
300 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
301 /* printf("Beep: %-12s Transposition file: %s\n", */
303 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
304 /* printf("Tsume: %-12s Force: %s\n")*/
306 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
307 /* printf("Time Control %s %d moves %d seconds %d opr %d
308 * depth\n", (TCflag) ? "ON" : "OFF", */
310 (TCflag) ? CP[93] : CP[92],
311 TimeControl.moves[black], TimeControl.clock[black] / 100,
312 TCadd/100, MaxSearchDepth);
314 signal(SIGUSR1, Raw_TerminateSearch);
320 * Set up a board position. Pieces are entered by typing the piece followed
321 * by the location. For example, Nf3 will place a knight on square f3.
327 short a, r, c, sq, i, found;
330 flag.regularstart = true;
333 Raw_UpdateDisplay(0, 0, 1, 0);
334 /* printf(". exit to main\n"); */
335 fputs(CP[29], stdout);
336 /* printf("# clear board\n"); */
337 fputs(CP[28], stdout);
338 /* printf("c change sides\n"); */
339 fputs(CP[136], stdout);
340 /* printf("enter piece & location: \n"); */
341 fputs(CP[155], stdout);
350 if (s[0] == CP[28][0]) /*#*/
352 for (sq = 0; sq < NO_SQUARES; sq++)
354 board[sq] = no_piece;
361 if (s[0] == CP[136][0]) /*c*/
366 for (i = pawn; i <= king; i++)
368 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
385 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
389 board[sq] = no_piece;
391 for (i = no_piece; i <= king; i++)
393 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
396 board[sq] = promoted[i];
409 while (s[0] != CP[29][0]);
411 for (sq = 0; sq < NO_SQUARES; sq++)
412 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
420 Raw_UpdateDisplay(0, 0, 1, 0);
427 * Set up a board position.
428 * Nine lines of nine characters are used to setup the board. 9a-1a is the
429 * first line. White pieces are represented by uppercase characters.
441 fgets(s, 80, stdin); /* skip "setup" command */
443 for (r = NO_ROWS - 1; r >= 0; r--)
447 for (c = 0; c <= (NO_COLS - 1); c++)
452 board[sq] = no_piece;
454 for (i = no_piece; i <= king; i++)
462 else if (ch == qxx[i])
472 for (sq = 0; sq < NO_SQUARES; sq++)
473 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
477 Raw_UpdateDisplay(0, 0, 1, 0);
478 /* printf("Setup successful\n"); */
479 fputs(CP[106], stdout);
484 Raw_SearchStartStuff(short side)
486 signal(SIGUSR1, Raw_TerminateSearch);
492 ResponseTime, TimeControl.clock[side]);
503 printf("%s\n", CP[225]);
507 if (mvstr[0][0] == '\0')
512 /* add remaining time in milliseconds to xshogi */
513 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
514 (TimeControl.clock[player] - et) * 10);
518 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
522 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
523 || (root->score == (SCORE_LIMIT + 998)))
536 if (Tree[t].f || Tree[t].t)
544 /* printf("Nodes %ld Tree %d Eval %ld
545 * Rate %ld RS high %ld low %ld\n", */
546 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
547 (et > 100) ? (NodeCnt / (et / 100)) : 0,
548 EADD, EGET, reminus, replus);
550 /* printf("Hin/Hout/Coll/Fin/Fout =
551 * %ld/%ld/%ld/%ld/%ld\n", */
553 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
556 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
560 /* printf("My move is: %s\n", mvstr[0]); */
561 printf(CP[83], mvstr[0]);
568 if (root->flags & draw)
570 /* printf("Drawn game!\n"); */
571 fputs(CP[57], stdout);
573 else if (root->score == -(SCORE_LIMIT + 999))
575 printf("%s mates!\n", ColorStr[opponent]);
577 else if (root->score == (SCORE_LIMIT + 998))
579 printf("%s mates!\n", ColorStr[computer]);
582 else if (!barebones && (root->score < -SCORE_LIMIT))
584 printf("%s has a forced mate in %d moves!\n",
585 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
587 else if (!barebones && (root->score > SCORE_LIMIT))
589 printf("%s has a forced mate in %d moves!\n",
590 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
592 #endif /* VERYBUGGY */
597 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
602 if (redraw && !XSHOGI)
605 r = (short)(TimeControl.clock[black] / 6000);
606 c = (short)((TimeControl.clock[black] % 6000) / 100);
607 l = (short)(TimeControl.clock[white] / 6000);
608 m = (short)((TimeControl.clock[white] % 6000) / 100);
609 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
610 printf(CP[116], r, c, l, m);
613 for (r = (NO_ROWS - 1); r >= 0; r--)
615 for (c = 0; c <= (NO_COLS - 1); c++)
619 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
621 pc = (is_promoted[board[l]] ? '+' : ' ');
623 if (color[l] == neutral)
625 else if (color[l] == black)
626 printf("%c%c", pc, qxx[board[l]]);
628 printf("%c%c", pc, pxx[board[l]]);
638 for (side = black; side <= white; side++)
641 printf((side == black)?"black ":"white ");
643 for (piece = pawn; piece <= king; piece++)
645 if ((c = Captured[side][piece]))
646 printf("%i%c ", c, pxx[piece]);
658 Raw_ChangeAlphaWindow(void)
660 printf("WAwindow: ");
661 scanf("%hd", &WAwindow);
662 printf("BAwindow: ");
663 scanf("%hd", &BAwindow);
669 Raw_ChangeBetaWindow(void)
671 printf("WBwindow: ");
672 scanf("%hd", &WBwindow);
673 printf("BBwindow: ");
674 scanf("%hd", &BBwindow);
684 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
685 printf(CP[72], mvstr[0]); /*hint*/
688 fputs(CP[223], stdout);
694 Raw_SelectLevel(char *sx)
697 char T[NO_SQUARES + 1], *p, *q;
699 if ((p = strstr(sx, CP[169])) != NULL)
700 p += strlen(CP[169]);
701 else if ((p = strstr(sx, CP[217])) != NULL)
702 p += strlen(CP[217]);
708 for (; *p != 'X'; *q++ = *p++);
712 /* line empty ask for input */
715 fputs(CP[61], stdout);
716 fgets(T, NO_SQUARES + 1, stdin);
720 /* skip blackspace */
721 for (p = T; *p == ' '; p++) ;
723 /* could be moves or a fischer clock */
726 /* its a fischer clock game */
728 TCminutes = (short)strtol(p, &q, 10);
729 TCadd = (short)strtol(q, NULL, 10) *100;
737 TCmoves = (short)strtol(p, &q, 10);
738 TCminutes = (short)strtol(q, &q, 10);
741 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
746 fputs(CP[94], stdout);
747 scanf("%hd", &OperatorTime);
753 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
754 TCminutes = TCseconds = 0;
763 TimeControl.clock[black] = TimeControl.clock[white] = 0;
768 printf("Clocks: %ld %ld\n",
769 TimeControl.clock[black] * 10,
770 TimeControl.clock[white] * 10);
778 Raw_ChangeSearchDepth(void)
781 scanf("%hd", &MaxSearchDepth);
782 TCflag = !(MaxSearchDepth > 0);
789 Raw_ChangeHashDepth(void)
791 printf("hashdepth = ");
792 scanf("%hd", &HashDepth);
793 printf("MoveLimit = ");
794 scanf("%hd", &HashMoveLimit);
800 Raw_SetContempt(void)
802 printf("contempt = ");
803 scanf("%hd", &contempt);
809 Raw_ChangeXwindow(void)
812 scanf("%hd", &xwndw);
817 * Raw_ShowPostnValue(short sq)
818 * must have called ExaminePosition() first
822 Raw_ShowPostnValue(short sq)
825 score = ScorePosition(color[sq]);
827 if (color[sq] != neutral)
829 #if defined SAVE_SVALUE
830 printf("???%c ", (color[sq] == white)?'b':'w');
832 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
846 short c, p, sq, tp, tc, tsq, score, j, k;
849 ExaminePosition(opponent);
850 Raw_ShowMessage(CP[65]);
854 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
857 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
860 for (p = king; p > no_piece; p--)
862 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
868 for (j = (NO_ROWS - 1); j >= 0; j--)
870 for (k = 0; k < (NO_COLS); k++)
872 sq = j*(NO_COLS) + k;
877 tsq = PieceList[c][1];
878 PieceList[c][1] = sq;
879 Raw_ShowPostnValue(sq);
880 PieceList[c][1] = tsq;
889 score = ScorePosition(opponent);
891 for (j = (NO_ROWS - 1); j >= 0; j--)
893 for (k = 0; k < (NO_COLS); k++)
895 sq = j*(NO_COLS) + k;
897 if (color[sq] != neutral)
899 #if defined SAVE_SVALUE
900 printf("%?????%c ", (color[sq] == white)?'b':'w');
902 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
914 printf("stage = %d\n", stage);
915 printf(CP[103], score,
916 mtl[computer], pscore[computer], GameType[computer],
917 mtl[opponent], pscore[opponent], GameType[opponent]);
923 Raw_DoTable(short table[NO_SQUARES])
926 ExaminePosition(opponent);
928 for (j = (NO_ROWS - 1); j >= 0; j--)
930 for (k = 0; k < NO_COLS; k++)
932 sq = j*(NO_ROWS) + k;
933 printf("%3d ", table[sq]);
943 Raw_ShowPostnValues(void)
945 short sq, score, j, k;
946 ExaminePosition(opponent);
948 for (j = (NO_ROWS - 1); j >= 0; j--)
950 for (k = 0; k < NO_COLS; k++)
952 sq = j * NO_COLS + k;
953 Raw_ShowPostnValue(sq);
959 score = ScorePosition(opponent);
960 printf(CP[103], score,
961 mtl[computer], pscore[computer], GameType[computer],
962 mtl[opponent], pscore[opponent], GameType[opponent]);
963 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);