Changing license to GPL3 (and bumping version to 1.4.0).
[gnushogi.git] / gnushogi / rawdsp.c
blob1f9e4b340b6ac2b7a114b900dd25c6190e26f69a
1 /*
2 * FILE: rawdsp.c
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
20 * for more details.
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 * ----------------------------------------------------------------------
29 #include <ctype.h>
30 #include <signal.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/file.h>
35 #include "gnushogi.h"
36 #include "rawdsp.h"
38 unsigned short MV[MAXDEPTH];
39 int MSCORE;
41 int mycnt1, mycnt2;
42 char *DRAW;
43 extern char *InPtr;
44 extern short pscore[];
47 /****************************************
48 * Trivial output functions.
49 ****************************************/
51 void
52 Raw_ClearScreen(void)
54 if (!barebones && !XSHOGI)
55 printf("\n");
59 /* FIXME: change to ShowPrompt? */
60 void
61 PromptForMove(void)
63 if (!barebones && !XSHOGI)
65 /* printf("\nYour move is? "); */
66 printf(CP[124]);
71 void
72 Raw_ShowCurrentMove(short pnt, short f, short t)
77 void
78 Raw_ShowDepth(char ch)
80 if (!barebones && !XSHOGI)
82 printf(CP[53], Sdepth, ch); /* Depth = %d%c */
83 printf("\n");
88 void
89 Raw_ShowGameType(void)
91 if (flag.post)
92 printf("%c vs. %c\n", GameType[black], GameType[white]);
96 void
97 Raw_ShowLine(unsigned short *bstline)
99 int i;
101 for (i = 1; bstline[i] > 0; i++)
103 if ((i > 1) && (i % 8 == 1))
104 printf("\n ");
106 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
107 printf("%5s ", mvstr[0]);
110 printf("\n");
114 void
115 Raw_ShowMessage(char *s)
117 if (!XSHOGI)
118 printf("%s\n", s);
122 void
123 Raw_ShowPatternCount(short side, short n)
125 if (flag.post)
126 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
130 void
131 Raw_ShowResponseTime(void)
136 void
137 Raw_ShowResults(short score, unsigned short *bstline, char ch)
139 if (flag.post && !XSHOGI)
141 ElapsedTime(2);
142 printf("%2d%c %6d %4ld %8ld ",
143 Sdepth, ch, score, et / 100, NodeCnt);
144 Raw_ShowLine(bstline);
149 void
150 Raw_ShowSidetoMove(void)
155 void
156 Raw_ShowStage(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 ****************************************/
168 void
169 Raw_Initialize(void)
171 mycnt1 = mycnt2 = 0;
173 if (XSHOGI)
175 #ifdef HAVE_SETLINEBUF
176 setlinebuf(stdout);
177 #else
178 # ifdef HAVE_SETVBUF
179 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
180 # else
181 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
182 # endif
183 #endif
184 printf("GNU Shogi %sp%s\n", version, patchlevel);
187 if (hard_time_limit)
189 if (!TCflag && (MaxResponseTime == 0))
190 MaxResponseTime = 15L * 100L;
196 void
197 Raw_ExitShogi(void)
199 /* CHECKME: what purpose does this next statement serve? */
200 signal(SIGTERM, SIG_IGN);
202 if (!nolist)
203 ListGame();
207 void
208 Raw_Die(int sig)
210 char s[80];
212 Raw_ShowMessage(CP[31]); /* Abort? */
213 scanf("%s", s);
215 if (strcmp(s, CP[210]) == 0) /* yes */
216 Raw_ExitShogi();
221 void
222 Raw_TerminateSearch(int sig)
224 #ifdef INTERRUPT_TEST
225 ElapsedTime(INIT_INTERRUPT_MODE);
226 #endif
228 if (!flag.timeout)
229 flag.back = true; /* previous: flag.timeout = true; */
231 flag.bothsides = false;
236 void
237 Raw_help(void)
239 Raw_ClearScreen();
240 /* printf("SHOGI command summary\n"); */
241 printf(CP[40]);
242 printf("----------------------------------"
243 "------------------------------\n");
244 /* printf("7g7f move from 7g to 7f quit
245 * Exit Shogi\n"); */
246 printf(CP[158]);
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]);
255 /* printf(" hash
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 printf(CP[130]);
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 printf(CP[200]);
267 /* printf("edit edit board force
268 * enter game moves\n"); */
269 printf(CP[153]);
270 /* printf("switch sides with computer both
271 * computer match\n"); */
272 printf(CP[194]);
273 /* printf("black computer plays black white
274 * computer plays white\n"); */
275 printf(CP[202]);
276 /* printf("depth set search depth clock
277 * set time control\n"); */
278 printf(CP[149]);
279 /* printf("post principle variation hint
280 * suggest a move\n"); */
281 printf(CP[177]);
282 /* printf("save game to file get
283 * game from file\n"); */
284 printf(CP[188]);
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 printf(CP[181]);
290 printf("--------------------------------"
291 "--------------------------------\n");
292 /* printf("Computer: %-12s Opponent: %s\n", */
293 printf(CP[46],
294 ColorStr[computer], ColorStr[opponent]);
295 /* printf("Depth: %-12d Response time: %d sec\n", */
296 printf(CP[51],
297 MaxSearchDepth, MaxResponseTime/100);
298 /* printf("Random: %-12s Easy mode: %s\n", */
299 printf(CP[99],
300 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
301 /* printf("Beep: %-12s Transposition file: %s\n", */
302 printf(CP[36],
303 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
304 /* printf("Tsume: %-12s Force: %s\n")*/
305 printf(CP[232],
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", */
309 printf(CP[110],
310 (TCflag) ? CP[93] : CP[92],
311 TimeControl.moves[black], TimeControl.clock[black] / 100,
312 TCadd/100, MaxSearchDepth);
314 signal(SIGINT, Raw_TerminateSearch);
315 signal(SIGQUIT, Raw_TerminateSearch);
321 * Set up a board position. Pieces are entered by typing the piece followed
322 * by the location. For example, Nf3 will place a knight on square f3.
325 void
326 Raw_EditBoard(void)
328 short a, r, c, sq, i, found;
329 char s[80];
331 flag.regularstart = true;
332 Book = BOOKFAIL;
333 Raw_ClearScreen();
334 Raw_UpdateDisplay(0, 0, 1, 0);
335 /* printf(". exit to main\n"); */
336 printf(CP[29]);
337 /* printf("# clear board\n"); */
338 printf(CP[28]);
339 /* printf("c change sides\n"); */
340 printf(CP[136]);
341 /* printf("enter piece & location: \n"); */
342 printf(CP[155]);
344 a = black;
348 scanf("%s", s);
349 found = 0;
351 if (s[0] == CP[28][0]) /*#*/
353 for (sq = 0; sq < NO_SQUARES; sq++)
355 board[sq] = no_piece;
356 color[sq] = neutral;
359 ClearCaptured();
362 if (s[0] == CP[136][0]) /*c*/
363 a = otherside[a];
365 if (s[1] == '*')
367 for (i = pawn; i <= king; i++)
369 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
371 Captured[a][i]++;
372 found = 1;
373 break;
377 c = -1;
378 r = -1;
380 else
382 c = '9' - s[1];
383 r = 'i' - s[2];
386 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
388 sq = locn(r, c);
389 color[sq] = a;
390 board[sq] = no_piece;
392 for (i = no_piece; i <= king; i++)
394 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
396 if (s[3] == '+')
397 board[sq] = promoted[i];
398 else
399 board[sq] = i;
401 found = 1;
402 break;
406 if (found == 0)
407 color[sq] = neutral;
410 while (s[0] != CP[29][0]);
412 for (sq = 0; sq < NO_SQUARES; sq++)
413 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
415 GameCnt = 0;
416 Game50 = 1;
417 ZeroRPT();
418 Sdepth = 0;
419 InitializeStats();
420 Raw_ClearScreen();
421 Raw_UpdateDisplay(0, 0, 1, 0);
428 * Set up a board position.
429 * Nine lines of nine characters are used to setup the board. 9a-1a is the
430 * first line. White pieces are represented by uppercase characters.
433 void
434 SetupBoard(void)
436 short r, c, sq, i;
437 char ch;
438 char s[80];
440 NewGame();
442 fgets(s, 80, stdin); /* skip "setup" command */
444 for (r = NO_ROWS - 1; r >= 0; r--)
446 fgets(s, 80, stdin);
448 for (c = 0; c <= (NO_COLS - 1); c++)
450 ch = s[c];
451 sq = locn(r, c);
452 color[sq] = neutral;
453 board[sq] = no_piece;
455 for (i = no_piece; i <= king; i++)
457 if (ch == pxx[i])
459 color[sq] = white;
460 board[sq] = i;
461 break;
463 else if (ch == qxx[i])
465 color[sq] = black;
466 board[sq] = i;
467 break;
473 for (sq = 0; sq < NO_SQUARES; sq++)
474 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
476 InitializeStats();
477 Raw_ClearScreen();
478 Raw_UpdateDisplay(0, 0, 1, 0);
479 /* printf("Setup successful\n"); */
480 printf(CP[106]);
484 void
485 Raw_SearchStartStuff(short side)
487 signal(SIGINT, Raw_TerminateSearch);
488 signal(SIGQUIT, Raw_TerminateSearch);
490 if (flag.post)
492 printf(CP[123],
493 GameCnt/2 + 1,
494 ResponseTime, TimeControl.clock[side]);
500 void
501 Raw_OutputMove(void)
503 if (flag.illegal)
505 printf("%s\n", CP[225]);
506 return;
509 if (mvstr[0][0] == '\0')
510 goto nomove;
512 if (XSHOGI)
514 /* add remaining time in milliseconds to xshogi */
515 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
516 (TimeControl.clock[player] - et) * 10);
518 else
520 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
523 nomove:
524 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
525 || (root->score == (SCORE_LIMIT + 998)))
526 goto summary;
528 if (flag.post)
530 short h, l, t;
532 h = TREE;
533 l = 0;
534 t = TREE >> 1;
536 while (l != t)
538 if (Tree[t].f || Tree[t].t)
539 l = t;
540 else
541 h = t;
543 t = (l + h) >> 1;
546 /* printf("Nodes %ld Tree %d Eval %ld
547 * Rate %ld RS high %ld low %ld\n", */
548 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
549 (et > 100) ? (NodeCnt / (et / 100)) : 0,
550 EADD, EGET, reminus, replus);
552 /* printf("Hin/Hout/Coll/Fin/Fout =
553 * %ld/%ld/%ld/%ld/%ld\n", */
554 printf(CP[71],
555 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
558 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
560 if (!XSHOGI)
562 /* printf("My move is: %s\n", mvstr[0]); */
563 printf(CP[83], mvstr[0]);
565 if (flag.beep)
566 printf("%c", 7);
569 summary:
570 if (root->flags & draw)
572 /* printf("Drawn game!\n"); */
573 printf(CP[57]);
575 else if (root->score == -(SCORE_LIMIT + 999))
577 printf("%s mates!\n", ColorStr[opponent]);
579 else if (root->score == (SCORE_LIMIT + 998))
581 printf("%s mates!\n", ColorStr[computer]);
583 #ifdef VERYBUGGY
584 else if (!barebones && (root->score < -SCORE_LIMIT))
586 printf("%s has a forced mate in %d moves!\n",
587 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
589 else if (!barebones && (root->score > SCORE_LIMIT))
591 printf("%s has a forced mate in %d moves!\n",
592 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
594 #endif /* VERYBUGGY */
598 void
599 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
602 short r, c, l, m;
604 if (redraw && !XSHOGI)
606 printf("\n");
607 r = (short)(TimeControl.clock[black] / 6000);
608 c = (short)((TimeControl.clock[black] % 6000) / 100);
609 l = (short)(TimeControl.clock[white] / 6000);
610 m = (short)((TimeControl.clock[white] % 6000) / 100);
611 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
612 printf(CP[116], r, c, l, m);
613 printf("\n");
615 for (r = (NO_ROWS - 1); r >= 0; r--)
617 for (c = 0; c <= (NO_COLS - 1); c++)
619 char pc;
620 l = ((flag.reverse)
621 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
622 : locn(r, c));
623 pc = (is_promoted[board[l]] ? '+' : ' ');
625 if (color[l] == neutral)
626 printf(" -");
627 else if (color[l] == black)
628 printf("%c%c", pc, qxx[board[l]]);
629 else
630 printf("%c%c", pc, pxx[board[l]]);
633 printf("\n");
636 printf("\n");
638 short side;
640 for (side = black; side <= white; side++)
642 short piece, c;
643 printf((side == black)?"black ":"white ");
645 for (piece = pawn; piece <= king; piece++)
647 if ((c = Captured[side][piece]))
648 printf("%i%c ", c, pxx[piece]);
651 printf("\n");
659 void
660 Raw_ChangeAlphaWindow(void)
662 printf("WAwindow: ");
663 scanf("%hd", &WAwindow);
664 printf("BAwindow: ");
665 scanf("%hd", &BAwindow);
670 void
671 Raw_ChangeBetaWindow(void)
673 printf("WBwindow: ");
674 scanf("%hd", &WBwindow);
675 printf("BBwindow: ");
676 scanf("%hd", &BBwindow);
681 void
682 Raw_GiveHint(void)
684 if (hint)
686 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
687 printf(CP[72], mvstr[0]); /*hint*/
689 else
690 printf(CP[223]);
695 void
696 Raw_SelectLevel(char *sx)
699 char T[NO_SQUARES + 1], *p, *q;
701 if ((p = strstr(sx, CP[169])) != NULL)
702 p += strlen(CP[169]);
703 else if ((p = strstr(sx, CP[217])) != NULL)
704 p += strlen(CP[217]);
706 strcat(sx, "XX");
707 q = T;
708 *q = '\0';
710 for (; *p != 'X'; *q++ = *p++);
712 *q = '\0';
714 /* line empty ask for input */
715 if (!T[0])
717 printf(CP[61]);
718 fgets(T, NO_SQUARES + 1, stdin);
719 strcat(T, "XX");
722 /* skip blackspace */
723 for (p = T; *p == ' '; p++) ;
725 /* could be moves or a fischer clock */
726 if (*p == 'f')
728 /* its a fischer clock game */
729 p++;
730 TCminutes = (short)strtol(p, &q, 10);
731 TCadd = (short)strtol(q, NULL, 10) *100;
732 TCseconds = 0;
733 TCmoves = 50;
735 else
737 /* regular game */
738 TCadd = 0;
739 TCmoves = (short)strtol(p, &q, 10);
740 TCminutes = (short)strtol(q, &q, 10);
742 if (*q == ':')
743 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
744 else
745 TCseconds = 0;
747 #ifdef OPERATORTIME
748 printf(CP[94]);
749 scanf("%hd", &OperatorTime);
750 #endif
752 if (TCmoves == 0)
754 TCflag = false;
755 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
756 TCminutes = TCseconds = 0;
758 else
760 TCflag = true;
761 MaxResponseTime = 0;
765 TimeControl.clock[black] = TimeControl.clock[white] = 0;
766 SetTimeControl();
768 if (XSHOGI)
770 printf("Clocks: %ld %ld\n",
771 TimeControl.clock[black] * 10,
772 TimeControl.clock[white] * 10);
779 void
780 Raw_ChangeSearchDepth(void)
782 printf("depth = ");
783 scanf("%hd", &MaxSearchDepth);
784 TCflag = !(MaxSearchDepth > 0);
790 void
791 Raw_ChangeHashDepth(void)
793 printf("hashdepth = ");
794 scanf("%hd", &HashDepth);
795 printf("MoveLimit = ");
796 scanf("%hd", &HashMoveLimit);
801 void
802 Raw_SetContempt(void)
804 printf("contempt = ");
805 scanf("%hd", &contempt);
810 void
811 Raw_ChangeXwindow(void)
813 printf("xwndw = ");
814 scanf("%hd", &xwndw);
819 * Raw_ShowPostnValue(short sq)
820 * must have called ExaminePosition() first
823 void
824 Raw_ShowPostnValue(short sq)
826 short score;
827 score = ScorePosition(color[sq]);
829 if (color[sq] != neutral)
831 #if defined SAVE_SVALUE
832 printf("???%c ", (color[sq] == white)?'b':'w');
833 #else
834 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
835 #endif
837 else
839 printf(" * ");
845 void
846 Raw_DoDebug(void)
848 short c, p, sq, tp, tc, tsq, score, j, k;
849 char s[40];
851 ExaminePosition(opponent);
852 Raw_ShowMessage(CP[65]);
853 scanf("%s", s);
854 c = neutral;
856 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
857 c = black;
859 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
860 c = white;
862 for (p = king; p > no_piece; p--)
864 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
865 break;
868 if (p > no_piece)
870 for (j = (NO_ROWS - 1); j >= 0; j--)
872 for (k = 0; k < (NO_COLS); k++)
874 sq = j*(NO_COLS) + k;
875 tp = board[sq];
876 tc = color[sq];
877 board[sq] = p;
878 color[sq] = c;
879 tsq = PieceList[c][1];
880 PieceList[c][1] = sq;
881 Raw_ShowPostnValue(sq);
882 PieceList[c][1] = tsq;
883 board[sq] = tp;
884 color[sq] = tc;
887 printf("\n");
891 score = ScorePosition(opponent);
893 for (j = (NO_ROWS - 1); j >= 0; j--)
895 for (k = 0; k < (NO_COLS); k++)
897 sq = j*(NO_COLS) + k;
899 if (color[sq] != neutral)
901 #if defined SAVE_SVALUE
902 printf("%?????%c ", (color[sq] == white)?'b':'w');
903 #else
904 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
905 #endif
907 else
909 printf(" * ");
913 printf("\n");
916 printf("stage = %d\n", stage);
917 printf(CP[103], score,
918 mtl[computer], pscore[computer], GameType[computer],
919 mtl[opponent], pscore[opponent], GameType[opponent]);
924 void
925 Raw_DoTable(short table[NO_SQUARES])
927 short sq, j, k;
928 ExaminePosition(opponent);
930 for (j = (NO_ROWS - 1); j >= 0; j--)
932 for (k = 0; k < NO_COLS; k++)
934 sq = j*(NO_ROWS) + k;
935 printf("%3d ", table[sq]);
938 printf("\n");
944 void
945 Raw_ShowPostnValues(void)
947 short sq, score, j, k;
948 ExaminePosition(opponent);
950 for (j = (NO_ROWS - 1); j >= 0; j--)
952 for (k = 0; k < NO_COLS; k++)
954 sq = j * NO_COLS + k;
955 Raw_ShowPostnValue(sq);
958 printf("\n");
961 score = ScorePosition(opponent);
962 printf(CP[103], score,
963 mtl[computer], pscore[computer], GameType[computer],
964 mtl[opponent], pscore[opponent], GameType[opponent]);
965 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);