Restore previous copyright information that got removed by error.
[gnushogi.git] / gnushogi / rawdsp.c
blob7d172874bff72e540f7b56e9efe0dd866c8d9b4e
1 /*
2 * FILE: rawdsp.c
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
23 * for more details.
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 * ----------------------------------------------------------------------
32 #include <ctype.h>
33 #include <signal.h>
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/file.h>
38 #include "gnushogi.h"
39 #include "rawdsp.h"
41 unsigned short MV[MAXDEPTH];
42 int MSCORE;
44 int mycnt1, mycnt2;
45 char *DRAW;
46 extern char *InPtr;
47 extern short pscore[];
50 /****************************************
51 * Trivial output functions.
52 ****************************************/
54 void
55 Raw_ClearScreen(void)
57 if (!barebones && !XSHOGI)
58 printf("\n");
62 /* FIXME: change to ShowPrompt? */
63 void
64 PromptForMove(void)
66 if (!barebones && !XSHOGI)
68 /* printf("\nYour move is? "); */
69 fputs(CP[124], stdout);
74 void
75 Raw_ShowCurrentMove(short pnt, short f, short t)
80 void
81 Raw_ShowDepth(char ch)
83 if (!barebones && !XSHOGI)
85 printf(CP[53], Sdepth, ch); /* Depth = %d%c */
86 printf("\n");
91 void
92 Raw_ShowGameType(void)
94 if (flag.post)
95 printf("%c vs. %c\n", GameType[black], GameType[white]);
99 void
100 Raw_ShowLine(unsigned short *bstline)
102 int i;
104 for (i = 1; bstline[i] > 0; i++)
106 if ((i > 1) && (i % 8 == 1))
107 printf("\n ");
109 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
110 printf("%5s ", mvstr[0]);
113 printf("\n");
117 void
118 Raw_ShowMessage(char *s)
120 if (!XSHOGI)
121 printf("%s\n", s);
125 void
126 Raw_ShowPatternCount(short side, short n)
128 if (flag.post)
129 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
133 void
134 Raw_ShowResponseTime(void)
139 void
140 Raw_ShowResults(short score, unsigned short *bstline, char ch)
142 if (flag.post && !XSHOGI)
144 ElapsedTime(2);
145 printf("%2d%c %6d %4ld %8ld ",
146 Sdepth, ch, score, et / 100, NodeCnt);
147 Raw_ShowLine(bstline);
152 void
153 Raw_ShowSidetoMove(void)
158 void
159 Raw_ShowStage(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 ****************************************/
171 void
172 Raw_Initialize(void)
174 mycnt1 = mycnt2 = 0;
176 if (XSHOGI)
178 #ifdef HAVE_SETLINEBUF
179 setlinebuf(stdout);
180 #else
181 # ifdef HAVE_SETVBUF
182 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
183 # else
184 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
185 # endif
186 #endif
187 printf("GNU Shogi %sp%s\n", version, patchlevel);
190 if (hard_time_limit)
192 if (!TCflag && (MaxResponseTime == 0))
193 MaxResponseTime = 15L * 100L;
199 void
200 Raw_ExitShogi(void)
202 /* CHECKME: what purpose does this next statement serve? */
203 signal(SIGTERM, SIG_IGN);
205 if (!nolist)
206 ListGame();
210 void
211 Raw_Die(int sig)
213 char s[80];
215 Raw_ShowMessage(CP[31]); /* Abort? */
216 scanf("%s", s);
218 if (strcmp(s, CP[210]) == 0) /* yes */
219 Raw_ExitShogi();
224 void
225 Raw_TerminateSearch(int sig)
227 #ifdef INTERRUPT_TEST
228 ElapsedTime(INIT_INTERRUPT_MODE);
229 #endif
231 if (!flag.timeout)
232 flag.back = true; /* previous: flag.timeout = true; */
234 flag.bothsides = false;
239 void
240 Raw_help(void)
242 Raw_ClearScreen();
243 /* printf("SHOGI command summary\n"); */
244 printf(CP[40], version, patchlevel);
245 printf("----------------------------------"
246 "------------------------------\n");
247 /* printf("7g7f move from 7g to 7f quit
248 * Exit Shogi\n"); */
249 fputs(CP[158], stdout);
250 /* printf("S6h move silver to 6h beep
251 * turn %s\n", (flag.beep) ? "off" : "on"); */
252 printf(CP[86], (flag.beep) ? CP[92] : CP[93]);
253 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
254 printf(CP[128], (flag.material) ? CP[92] : CP[93]);
255 /* printf("P*5e drop pawn to 5e easy
256 * turn %s\n", (flag.easy) ? "off" : "on"); */
257 printf(CP[173], (flag.easy) ? CP[92] : CP[93]);
258 /* printf(" hash
259 * turn %s\n", (flag.hash) ? "off" : "on"); */
260 printf(CP[174], (flag.hash) ? CP[92] : CP[93]);
261 /* printf("bd redraw board reverse
262 * board display\n"); */
263 fputs(CP[130], stdout);
264 /* printf("list game to shogi.lst book
265 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
266 printf(CP[170], (Book) ? CP[92] : CP[93], bookcount, booksize);
267 /* printf("undo undo last ply remove
268 * take back a move\n"); */
269 fputs(CP[200], stdout);
270 /* printf("edit edit board force
271 * enter game moves\n"); */
272 fputs(CP[153], stdout);
273 /* printf("switch sides with computer both
274 * computer match\n"); */
275 fputs(CP[194], stdout);
276 /* printf("black computer plays black white
277 * computer plays white\n"); */
278 fputs(CP[202], stdout);
279 /* printf("depth set search depth clock
280 * set time control\n"); */
281 fputs(CP[149], stdout);
282 /* printf("post principle variation hint
283 * suggest a move\n"); */
284 fputs(CP[177], stdout);
285 /* printf("save game to file get
286 * game from file\n"); */
287 fputs(CP[188], stdout);
288 printf("xsave pos. to xshogi file xget"
289 " pos. from xshogi file\n");
290 /* printf("random randomize play new
291 * start new game\n"); */
292 fputs(CP[181], stdout);
293 printf("--------------------------------"
294 "--------------------------------\n");
295 /* printf("Computer: %-12s Opponent: %s\n", */
296 printf(CP[46],
297 ColorStr[computer], ColorStr[opponent]);
298 /* printf("Depth: %-12d Response time: %d sec\n", */
299 printf(CP[51],
300 MaxSearchDepth, MaxResponseTime/100);
301 /* printf("Random: %-12s Easy mode: %s\n", */
302 printf(CP[99],
303 (dither) ? CP[93] : CP[92], (flag.easy) ? CP[93] : CP[92]);
304 /* printf("Beep: %-12s Transposition file: %s\n", */
305 printf(CP[36],
306 (flag.beep) ? CP[93] : CP[92], (flag.hash) ? CP[93] : CP[92]);
307 /* printf("Tsume: %-12s Force: %s\n")*/
308 printf(CP[232],
309 (flag.tsume) ? CP[93] : CP[92], (flag.force) ? CP[93] : CP[92]);
310 /* printf("Time Control %s %d moves %d seconds %d opr %d
311 * depth\n", (TCflag) ? "ON" : "OFF", */
312 printf(CP[110],
313 (TCflag) ? CP[93] : CP[92],
314 TimeControl.moves[black], TimeControl.clock[black] / 100,
315 TCadd/100, MaxSearchDepth);
317 signal(SIGUSR1, Raw_TerminateSearch);
323 * Set up a board position. Pieces are entered by typing the piece followed
324 * by the location. For example, Nf3 will place a knight on square f3.
327 void
328 Raw_EditBoard(void)
330 short a, r, c, sq, i, found;
331 char s[80];
333 flag.regularstart = true;
334 Book = BOOKFAIL;
335 Raw_ClearScreen();
336 Raw_UpdateDisplay(0, 0, 1, 0);
337 /* printf(". exit to main\n"); */
338 fputs(CP[29], stdout);
339 /* printf("# clear board\n"); */
340 fputs(CP[28], stdout);
341 /* printf("c change sides\n"); */
342 fputs(CP[136], stdout);
343 /* printf("enter piece & location: \n"); */
344 fputs(CP[155], stdout);
346 a = black;
350 scanf("%s", s);
351 found = 0;
353 if (s[0] == CP[28][0]) /*#*/
355 for (sq = 0; sq < NO_SQUARES; sq++)
357 board[sq] = no_piece;
358 color[sq] = neutral;
361 ClearCaptured();
364 if (s[0] == CP[136][0]) /*c*/
365 a = otherside[a];
367 if (s[1] == '*')
369 for (i = pawn; i <= king; i++)
371 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
373 Captured[a][i]++;
374 found = 1;
375 break;
379 c = -1;
380 r = -1;
382 else
384 c = COL_NAME(s[1]);
385 r = ROW_NAME(s[2]);
388 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
390 sq = locn(r, c);
391 color[sq] = a;
392 board[sq] = no_piece;
394 for (i = no_piece; i <= king; i++)
396 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
398 if (s[3] == '+')
399 board[sq] = promoted[i];
400 else
401 board[sq] = i;
403 found = 1;
404 break;
408 if (found == 0)
409 color[sq] = neutral;
412 while (s[0] != CP[29][0]);
414 for (sq = 0; sq < NO_SQUARES; sq++)
415 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
417 GameCnt = 0;
418 Game50 = 1;
419 ZeroRPT();
420 Sdepth = 0;
421 InitializeStats();
422 Raw_ClearScreen();
423 Raw_UpdateDisplay(0, 0, 1, 0);
430 * Set up a board position.
431 * Nine lines of nine characters are used to setup the board. 9a-1a is the
432 * first line. White pieces are represented by uppercase characters.
435 void
436 SetupBoard(void)
438 short r, c, sq, i;
439 char ch;
440 char s[80];
442 NewGame();
444 fgets(s, 80, stdin); /* skip "setup" command */
446 for (r = NO_ROWS - 1; r >= 0; r--)
448 fgets(s, 80, stdin);
450 for (c = 0; c <= (NO_COLS - 1); c++)
452 ch = s[c];
453 sq = locn(r, c);
454 color[sq] = neutral;
455 board[sq] = no_piece;
457 for (i = no_piece; i <= king; i++)
459 if (ch == pxx[i])
461 color[sq] = white;
462 board[sq] = i;
463 break;
465 else if (ch == qxx[i])
467 color[sq] = black;
468 board[sq] = i;
469 break;
475 for (sq = 0; sq < NO_SQUARES; sq++)
476 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
478 InitializeStats();
479 Raw_ClearScreen();
480 Raw_UpdateDisplay(0, 0, 1, 0);
481 /* printf("Setup successful\n"); */
482 fputs(CP[106], stdout);
486 void
487 Raw_SearchStartStuff(short side)
489 signal(SIGUSR1, Raw_TerminateSearch);
491 if (flag.post)
493 printf(CP[123],
494 GameCnt/2 + 1,
495 ResponseTime, TimeControl.clock[side]);
501 void
502 Raw_OutputMove(void)
504 if (flag.illegal)
506 printf("%s\n", CP[225]);
507 return;
510 if (mvstr[0][0] == '\0')
511 goto nomove;
513 if (XSHOGI)
515 /* add remaining time in milliseconds to xshogi */
516 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
517 (TimeControl.clock[player] - et) * 10);
519 else
521 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
524 nomove:
525 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
526 || (root->score == (SCORE_LIMIT + 998)))
527 goto summary;
529 if (flag.post)
531 short h, l, t;
533 h = TREE;
534 l = 0;
535 t = TREE >> 1;
537 while (l != t)
539 if (Tree[t].f || Tree[t].t)
540 l = t;
541 else
542 h = t;
544 t = (l + h) >> 1;
547 /* printf("Nodes %ld Tree %d Eval %ld
548 * Rate %ld RS high %ld low %ld\n", */
549 printf(CP[89], GenCnt, NodeCnt, t, EvalNodes,
550 (et > 100) ? (NodeCnt / (et / 100)) : 0,
551 EADD, EGET, reminus, replus);
553 /* printf("Hin/Hout/Coll/Fin/Fout =
554 * %ld/%ld/%ld/%ld/%ld\n", */
555 printf(CP[71],
556 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
559 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
561 if (!XSHOGI)
563 /* printf("My move is: %s\n", mvstr[0]); */
564 printf(CP[83], mvstr[0]);
566 if (flag.beep)
567 printf("%c", 7);
570 summary:
571 if (root->flags & draw)
573 /* printf("Drawn game!\n"); */
574 fputs(CP[57], stdout);
576 else if (root->score == -(SCORE_LIMIT + 999))
578 printf("%s mates!\n", ColorStr[opponent]);
580 else if (root->score == (SCORE_LIMIT + 998))
582 printf("%s mates!\n", ColorStr[computer]);
584 #ifdef VERYBUGGY
585 else if (!barebones && (root->score < -SCORE_LIMIT))
587 printf("%s has a forced mate in %d moves!\n",
588 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
590 else if (!barebones && (root->score > SCORE_LIMIT))
592 printf("%s has a forced mate in %d moves!\n",
593 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
595 #endif /* VERYBUGGY */
599 void
600 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
603 short r, c, l, m;
605 if (redraw && !XSHOGI)
607 printf("\n");
608 r = (short)(TimeControl.clock[black] / 6000);
609 c = (short)((TimeControl.clock[black] % 6000) / 100);
610 l = (short)(TimeControl.clock[white] / 6000);
611 m = (short)((TimeControl.clock[white] % 6000) / 100);
612 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
613 printf(CP[116], r, c, l, m);
614 printf("\n");
616 for (r = (NO_ROWS - 1); r >= 0; r--)
618 for (c = 0; c <= (NO_COLS - 1); c++)
620 char pc;
621 l = ((flag.reverse)
622 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
623 : locn(r, c));
624 pc = (is_promoted[board[l]] ? '+' : ' ');
626 if (color[l] == neutral)
627 printf(" -");
628 else if (color[l] == black)
629 printf("%c%c", pc, qxx[board[l]]);
630 else
631 printf("%c%c", pc, pxx[board[l]]);
634 printf("\n");
637 printf("\n");
639 short side;
641 for (side = black; side <= white; side++)
643 short piece, c;
644 printf((side == black)?"black ":"white ");
646 for (piece = pawn; piece <= king; piece++)
648 if ((c = Captured[side][piece]))
649 printf("%i%c ", c, pxx[piece]);
652 printf("\n");
660 void
661 Raw_ChangeAlphaWindow(void)
663 printf("WAwindow: ");
664 scanf("%hd", &WAwindow);
665 printf("BAwindow: ");
666 scanf("%hd", &BAwindow);
671 void
672 Raw_ChangeBetaWindow(void)
674 printf("WBwindow: ");
675 scanf("%hd", &WBwindow);
676 printf("BBwindow: ");
677 scanf("%hd", &BBwindow);
682 void
683 Raw_GiveHint(void)
685 if (hint)
687 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
688 printf(CP[72], mvstr[0]); /*hint*/
690 else
691 fputs(CP[223], stdout);
696 void
697 Raw_SelectLevel(char *sx)
700 char T[NO_SQUARES + 1], *p, *q;
702 if ((p = strstr(sx, CP[169])) != NULL)
703 p += strlen(CP[169]);
704 else if ((p = strstr(sx, CP[217])) != NULL)
705 p += strlen(CP[217]);
707 strcat(sx, "XX");
708 q = T;
709 *q = '\0';
711 for (; *p != 'X'; *q++ = *p++);
713 *q = '\0';
715 /* line empty ask for input */
716 if (!T[0])
718 fputs(CP[61], stdout);
719 fgets(T, NO_SQUARES + 1, stdin);
720 strcat(T, "XX");
723 /* skip blackspace */
724 for (p = T; *p == ' '; p++) ;
726 /* could be moves or a fischer clock */
727 if (*p == 'f')
729 /* its a fischer clock game */
730 p++;
731 TCminutes = (short)strtol(p, &q, 10);
732 TCadd = (short)strtol(q, NULL, 10) *100;
733 TCseconds = 0;
734 TCmoves = 50;
736 else
738 /* regular game */
739 TCadd = 0;
740 TCmoves = (short)strtol(p, &q, 10);
741 TCminutes = (short)strtol(q, &q, 10);
743 if (*q == ':')
744 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
745 else
746 TCseconds = 0;
748 #ifdef OPERATORTIME
749 fputs(CP[94], stdout);
750 scanf("%hd", &OperatorTime);
751 #endif
753 if (TCmoves == 0)
755 TCflag = false;
756 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
757 TCminutes = TCseconds = 0;
759 else
761 TCflag = true;
762 MaxResponseTime = 0;
766 TimeControl.clock[black] = TimeControl.clock[white] = 0;
767 SetTimeControl();
769 if (XSHOGI)
771 printf("Clocks: %ld %ld\n",
772 TimeControl.clock[black] * 10,
773 TimeControl.clock[white] * 10);
780 void
781 Raw_ChangeSearchDepth(void)
783 printf("depth = ");
784 scanf("%hd", &MaxSearchDepth);
785 TCflag = !(MaxSearchDepth > 0);
791 void
792 Raw_ChangeHashDepth(void)
794 printf("hashdepth = ");
795 scanf("%hd", &HashDepth);
796 printf("MoveLimit = ");
797 scanf("%hd", &HashMoveLimit);
802 void
803 Raw_SetContempt(void)
805 printf("contempt = ");
806 scanf("%hd", &contempt);
811 void
812 Raw_ChangeXwindow(void)
814 printf("xwndw = ");
815 scanf("%hd", &xwndw);
820 * Raw_ShowPostnValue(short sq)
821 * must have called ExaminePosition() first
824 void
825 Raw_ShowPostnValue(short sq)
827 short score;
828 score = ScorePosition(color[sq]);
830 if (color[sq] != neutral)
832 #if defined SAVE_SVALUE
833 printf("???%c ", (color[sq] == white)?'b':'w');
834 #else
835 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
836 #endif
838 else
840 printf(" * ");
846 void
847 Raw_DoDebug(void)
849 short c, p, sq, tp, tc, tsq, score, j, k;
850 char s[40];
852 ExaminePosition(opponent);
853 Raw_ShowMessage(CP[65]);
854 scanf("%s", s);
855 c = neutral;
857 if ((s[0] == CP[9][0]) || (s[0] == CP[9][1])) /* w W */
858 c = black;
860 if ((s[0] == CP[9][2]) || (s[0] == CP[9][3])) /* b B */
861 c = white;
863 for (p = king; p > no_piece; p--)
865 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
866 break;
869 if (p > no_piece)
871 for (j = (NO_ROWS - 1); j >= 0; j--)
873 for (k = 0; k < (NO_COLS); k++)
875 sq = j*(NO_COLS) + k;
876 tp = board[sq];
877 tc = color[sq];
878 board[sq] = p;
879 color[sq] = c;
880 tsq = PieceList[c][1];
881 PieceList[c][1] = sq;
882 Raw_ShowPostnValue(sq);
883 PieceList[c][1] = tsq;
884 board[sq] = tp;
885 color[sq] = tc;
888 printf("\n");
892 score = ScorePosition(opponent);
894 for (j = (NO_ROWS - 1); j >= 0; j--)
896 for (k = 0; k < (NO_COLS); k++)
898 sq = j*(NO_COLS) + k;
900 if (color[sq] != neutral)
902 #if defined SAVE_SVALUE
903 printf("%?????%c ", (color[sq] == white)?'b':'w');
904 #else
905 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
906 #endif
908 else
910 printf(" * ");
914 printf("\n");
917 printf("stage = %d\n", stage);
918 printf(CP[103], score,
919 mtl[computer], pscore[computer], GameType[computer],
920 mtl[opponent], pscore[opponent], GameType[opponent]);
925 void
926 Raw_DoTable(short table[NO_SQUARES])
928 short sq, j, k;
929 ExaminePosition(opponent);
931 for (j = (NO_ROWS - 1); j >= 0; j--)
933 for (k = 0; k < NO_COLS; k++)
935 sq = j*(NO_ROWS) + k;
936 printf("%3d ", table[sq]);
939 printf("\n");
945 void
946 Raw_ShowPostnValues(void)
948 short sq, score, j, k;
949 ExaminePosition(opponent);
951 for (j = (NO_ROWS - 1); j >= 0; j--)
953 for (k = 0; k < NO_COLS; k++)
955 sq = j * NO_COLS + k;
956 Raw_ShowPostnValue(sq);
959 printf("\n");
962 score = ScorePosition(opponent);
963 printf(CP[103], score,
964 mtl[computer], pscore[computer], GameType[computer],
965 mtl[opponent], pscore[opponent], GameType[opponent]);
966 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);