Prepare v1.4.2.
[gnushogi.git] / gnushogi / rawdsp.c
blob54bb7a1e16fee4205959e6e899a903252edde1cb
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
7 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
16 * GNU Shogi is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 3 of the License,
19 * or (at your option) any later version.
21 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with GNU Shogi; see the file COPYING. If not, see
28 * <http://www.gnu.org/licenses/>.
29 * ----------------------------------------------------------------------
33 #include <ctype.h>
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/file.h>
39 #ifndef WIN32
40 #include <poll.h>
41 #include <unistd.h>
42 #endif
44 #include "gnushogi.h"
45 #include "rawdsp.h"
47 unsigned short MV[MAXDEPTH];
48 int MSCORE;
50 int mycnt1, mycnt2;
51 char *DRAW;
52 extern char *InPtr;
53 extern short pscore[];
56 /****************************************
57 * Trivial output functions.
58 ****************************************/
60 void
61 Raw_ClearScreen(void)
63 if (!barebones && !XSHOGI)
64 printf("\n");
68 void
69 Raw_ShowPrompt(void)
71 if (!barebones && !XSHOGI)
73 fputs("\nYour move is? ", stdout);
78 void
79 Raw_ShowCurrentMove(short pnt, short f, short t)
84 void
85 Raw_ShowDepth(char ch)
87 if (!barebones && !XSHOGI)
89 printf("Depth= %d%c ", Sdepth, ch);
90 printf("\n");
95 void
96 Raw_ShowGameType(void)
98 if (flag.post)
99 printf("%c vs. %c\n", GameType[black], GameType[white]);
103 void
104 Raw_ShowLine(unsigned short *bstline)
106 int i;
108 for (i = 1; bstline[i] > 0; i++)
110 if ((i > 1) && (i % 8 == 1))
111 printf("\n ");
113 algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
114 printf("%5s ", mvstr[0]);
117 printf("\n");
121 void
122 Raw_ShowMessage(char *s)
124 if (!XSHOGI)
125 printf("%s\n", s);
129 void
130 Raw_AlwaysShowMessage(const char *format, va_list ap)
132 vprintf(format, ap);
133 printf("\n");
137 void
138 Raw_Printf(const char *format, va_list ap)
140 vprintf(format, ap);
144 void
145 Raw_doRequestInputString(const char* fmt, char* buffer)
147 scanf(fmt, buffer);
152 Raw_GetString(char* sx)
154 int eof = 0;
155 sx[0] = '\0';
157 while(!eof && !sx[0])
158 eof = (fgets(sx, 80, stdin) == NULL);
159 return eof;
163 void
164 Raw_ShowNodeCnt(long NodeCnt)
166 printf("Nodes = %ld Nodes/sec = %ld\n",
167 NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
171 void
172 Raw_ShowPatternCount(short side, short n)
174 if (flag.post)
175 printf("%s matches %d pattern(s)\n", ColorStr[side], n);
179 void
180 Raw_ShowResponseTime(void)
185 void
186 Raw_ShowResults(short score, unsigned short *bstline, char ch)
188 if (flag.post && !XSHOGI)
190 ElapsedTime(2);
191 printf("%2d%c %6d %4ld %8ld ",
192 Sdepth, ch, score, et / 100, NodeCnt);
193 Raw_ShowLine(bstline);
198 void
199 Raw_ShowSidetoMove(void)
204 void
205 Raw_ShowStage(void)
207 printf("stage = %d\n", stage);
208 printf("balance[black] = %d balance[white] = %d\n",
209 balance[black], balance[white]);
212 /****************************************
213 * End of trivial output routines.
214 ****************************************/
216 void
217 Raw_Initialize(void)
219 mycnt1 = mycnt2 = 0;
221 if (XSHOGI)
223 #ifdef WIN32
224 /* needed because of inconsistency between MSVC run-time system and gcc includes */
225 setbuf(stdout, NULL);
226 #else
227 #ifdef HAVE_SETVBUF
228 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
229 #else
230 # ifdef HAVE_SETLINEBUF
231 setlinebuf(stdout);
232 # else
233 # error "Need setvbuf() or setlinebuf() to compile gnushogi!"
234 # endif
235 #endif
236 #endif
237 printf("GNU Shogi %s\n", PACKAGE_VERSION);
240 if (hard_time_limit)
242 if (!TCflag && (MaxResponseTime == 0))
243 MaxResponseTime = 15L * 100L;
248 void
249 Raw_ExitShogi(void)
251 /* CHECKME: what purpose does this next statement serve? */
252 signal(SIGTERM, SIG_IGN);
254 if (!nolist)
255 ListGame();
257 exit(0);
261 void
262 Raw_Die(int sig)
264 char s[80];
266 Raw_ShowMessage("Abort? ");
267 scanf("%s", s);
269 if (strcmp(s, "yes") == 0)
270 Raw_ExitShogi();
274 void
275 Raw_TerminateSearch(int sig)
277 #ifdef INTERRUPT_TEST
278 ElapsedTime(INIT_INTERRUPT_MODE);
279 #endif
281 if (!flag.timeout)
282 flag.back = true; /* previous: flag.timeout = true; */
284 flag.bothsides = false;
288 void
289 Raw_help(void)
291 Raw_ClearScreen();
292 printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
293 printf("----------------------------------"
294 "------------------------------\n");
295 fputs ("7g7f move from 7g to 7f quit Exit Shogi\n", stdout);
296 printf("S6h move silver to 6h beep turn %s\n", (flag.beep) ? "OFF" : "ON");
297 printf("2d2c+ move to 2c and promote material turn %s\n", (flag.material) ? "OFF" : "ON");
298 printf("P*5e drop pawn to 5e easy turn %s\n", (flag.easy) ? "OFF" : "ON");
299 printf("tsume toggle tsume mode hash turn %s\n", (flag.hash) ? "OFF" : "ON");
300 fputs ("bd redraw board reverse board display\n", stdout);
301 printf("list game to shogi.lst book turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, booksize);
302 fputs ("undo undo last ply remove take back a move\n", stdout);
303 fputs ("edit edit board force toggle manual move mode\n", stdout);
304 fputs ("switch sides with computer both computer match\n", stdout);
305 fputs ("black computer plays black white computer plays white\n", stdout);
306 fputs ("depth set search depth clock set time control\n", stdout);
307 fputs ("post principle variation hint suggest a move\n", stdout);
308 fputs ("save game to file get game from file\n", stdout);
309 printf("xsave pos. to xshogi file xget"
310 " pos. from xshogi file\n");
311 fputs("random randomize play new start new game\n", stdout);
312 printf("--------------------------------"
313 "--------------------------------\n");
314 printf("Computer: %-12s Opponent: %s\n",
315 ColorStr[computer], ColorStr[opponent]);
316 printf("Depth: %-12d Response time: %d sec\n",
317 MaxSearchDepth, MaxResponseTime/100);
318 printf("Random: %-12s Easy mode: %s\n",
319 (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
320 printf("Beep: %-12s Transposition file: %s\n",
321 (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
322 printf("Tsume: %-12s Force: %s\n",
323 (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
324 printf("Time Control %s %d moves %d sec %d add %d depth\n",
325 (TCflag) ? "ON" : "OFF",
326 TimeControl.moves[black], TimeControl.clock[black] / 100,
327 TCadd/100, MaxSearchDepth);
332 * Set up a board position. Pieces are entered by typing the piece followed
333 * by the location. For example, Nf3 will place a knight on square f3.
335 void
336 Raw_EditBoard(void)
338 short a, r, c, sq, i, found;
339 char s[80];
341 flag.regularstart = true;
342 Book = BOOKFAIL;
343 Raw_ClearScreen();
344 Raw_UpdateDisplay(0, 0, 1, 0);
345 printf(". Exit to main\n");
346 printf("# Clear board\n");
347 printf("c Change sides\n");
348 printf("enter piece & location:\n");
350 a = black;
352 while(1)
354 scanf("%s", s);
355 found = 0;
357 if (s[0] == '.')
358 break;
360 if (s[0] == '#')
362 for (sq = 0; sq < NO_SQUARES; sq++)
364 board[sq] = no_piece;
365 color[sq] = neutral;
368 ClearCaptured();
369 continue;
372 if (s[0] == 'c') {
373 a = otherside[a];
374 continue;
377 if (s[1] == '*')
379 for (i = pawn; i <= king; i++)
381 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
383 Captured[a][i]++;
384 found = 1;
385 break;
388 if (!found)
389 printf("# Invalid piece type '%c'\n", s[0]);
390 continue;
393 c = COL_NUM(s[1]);
394 r = ROW_NUM(s[2]);
396 if ((c < 0) || (c >= NO_COLS) || (r < 0) || (r >= NO_ROWS)) {
397 printf("# Out-of-board position '%c%c'\n", s[1], s[2]);
398 continue;
401 sq = locn(r, c);
403 for (i = no_piece; i <= king; i++)
405 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
407 color[sq] = a;
408 if (s[3] == '+')
409 board[sq] = promoted[i];
410 else
411 board[sq] = i;
413 found = 1;
414 break;
418 if (!found)
419 printf("# Invalid piece type '%c'\n", s[0]);
422 for (sq = 0; sq < NO_SQUARES; sq++)
423 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
425 GameCnt = 0;
426 Game50 = 1;
427 ZeroRPT();
428 Sdepth = 0;
429 InitializeStats();
430 Raw_ClearScreen();
431 Raw_UpdateDisplay(0, 0, 1, 0);
436 * Set up a board position.
437 * Nine lines of nine characters are used to setup the board. 9a-1a is the
438 * first line. White pieces are represented by uppercase characters.
440 void
441 Raw_SetupBoard(void)
443 short r, c, sq, i;
444 char ch;
445 char s[80];
447 NewGame();
449 fgets(s, 80, stdin); /* skip "setup" command */
451 for (r = NO_ROWS - 1; r >= 0; r--)
453 fgets(s, 80, stdin);
455 for (c = 0; c <= (NO_COLS - 1); c++)
457 ch = s[c];
458 sq = locn(r, c);
459 color[sq] = neutral;
460 board[sq] = no_piece;
462 for (i = no_piece; i <= king; i++)
464 if (ch == pxx[i])
466 color[sq] = white;
467 board[sq] = i;
468 break;
470 else if (ch == qxx[i])
472 color[sq] = black;
473 board[sq] = i;
474 break;
480 for (sq = 0; sq < NO_SQUARES; sq++)
481 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
483 InitializeStats();
484 Raw_ClearScreen();
485 Raw_UpdateDisplay(0, 0, 1, 0);
486 fputs("Setup successful\n", stdout);
490 void
491 Raw_SearchStartStuff(short side)
493 if (flag.post)
495 printf("\nMove# %d Target = %ld Clock: %ld\n",
496 GameCnt/2 + 1,
497 ResponseTime, TimeControl.clock[side]);
502 void
503 Raw_OutputMove(void)
505 if (flag.illegal)
507 printf("Illegal position.\n");
508 return;
511 if (mvstr[0][0] == '\0')
512 goto nomove;
514 if (XSHOGI)
516 /* add remaining time in milliseconds to xshogi */
517 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
518 (TimeControl.clock[player] - et) * 10);
520 else
522 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
525 nomove:
526 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
527 || (root->score == (SCORE_LIMIT + 998)))
528 goto summary;
530 if (flag.post)
532 short h, l, t;
534 h = TREE;
535 l = 0;
536 t = TREE >> 1;
538 while (l != t)
540 if (Tree[t].f || Tree[t].t)
541 l = t;
542 else
543 h = t;
545 t = (l + h) >> 1;
548 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
549 (et > 100) ? (NodeCnt / (et / 100)) : 0,
550 EADD, EGET, reminus, replus);
552 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
553 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
556 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
558 if (!XSHOGI)
560 printf("My move is: %5s\n", mvstr[0]);
562 if (flag.beep)
563 printf("%c", 7);
566 summary:
567 if (root->flags & draw)
569 fputs("Drawn game!\n", stdout);
571 else if (root->score == -(SCORE_LIMIT + 999))
573 printf("%s mates!\n", ColorStr[opponent]);
575 else if (root->score == (SCORE_LIMIT + 998))
577 printf("%s mates!\n", ColorStr[computer]);
579 #ifdef VERYBUGGY
580 else if (!barebones && (root->score < -SCORE_LIMIT))
582 printf("%s has a forced mate in %d moves!\n",
583 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
585 else if (!barebones && (root->score > SCORE_LIMIT))
587 printf("%s has a forced mate in %d moves!\n",
588 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
590 #endif /* VERYBUGGY */
594 void
595 Raw_UpdateClocks(void)
600 void
601 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
604 short r, c, l, m;
606 if (redraw && !XSHOGI)
608 printf("\n");
609 r = (short)(TimeControl.clock[black] / 6000);
610 c = (short)((TimeControl.clock[black] % 6000) / 100);
611 l = (short)(TimeControl.clock[white] / 6000);
612 m = (short)((TimeControl.clock[white] % 6000) / 100);
613 printf("Black %d:%02d White %d:%02d\n", 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");
659 void
660 Raw_ChangeAlphaWindow(void)
662 printf("WAwindow: ");
663 scanf("%hd", &WAwindow);
664 printf("BAwindow: ");
665 scanf("%hd", &BAwindow);
669 void
670 Raw_ChangeBetaWindow(void)
672 printf("WBwindow: ");
673 scanf("%hd", &WBwindow);
674 printf("BBwindow: ");
675 scanf("%hd", &BBwindow);
679 void
680 Raw_GiveHint(void)
682 if (hint)
684 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
685 printf("Hint: %s\n", mvstr[0]);
687 else
688 fputs("I have no idea.\n", stdout);
692 void
693 Raw_SelectLevel(char *sx)
696 char T[NO_SQUARES + 1], *p, *q;
698 if ((p = strstr(sx, "level")) != NULL)
699 p += strlen("level");
700 else if ((p = strstr(sx, "clock")) != NULL)
701 p += strlen("clock");
703 strcat(sx, "XX");
704 q = T;
705 *q = '\0';
707 for (; *p != 'X'; *q++ = *p++);
709 *q = '\0';
711 /* line empty ask for input */
712 if (!T[0])
714 fputs("Enter #moves #minutes: ", stdout);
715 fgets(T, NO_SQUARES + 1, stdin);
716 strcat(T, "XX");
719 /* skip blackspace */
720 for (p = T; *p == ' '; p++) ;
722 /* could be moves or a fischer clock */
723 if (*p == 'f')
725 /* its a fischer clock game */
726 p++;
727 TCminutes = (short)strtol(p, &q, 10);
728 TCadd = (short)strtol(q, NULL, 10) *100;
729 TCseconds = 0;
730 TCmoves = 50;
732 else
734 /* regular game */
735 TCadd = 0;
736 TCmoves = (short)strtol(p, &q, 10);
737 TCminutes = (short)strtol(q, &q, 10);
739 if (*q == ':')
740 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
741 else
742 TCseconds = 0;
744 #ifdef OPERATORTIME
745 fputs("Operator time (hundredths) = ", stdout);
746 scanf("%hd", &OperatorTime);
747 #endif
749 if (TCmoves == 0)
751 TCflag = false;
752 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
753 TCminutes = TCseconds = 0;
755 else
757 TCflag = true;
758 MaxResponseTime = 0;
762 TimeControl.clock[black] = TimeControl.clock[white] = 0;
763 SetTimeControl();
765 if (XSHOGI)
767 printf("Clocks: %ld %ld\n",
768 TimeControl.clock[black] * 10,
769 TimeControl.clock[white] * 10);
774 void
775 Raw_ChangeSearchDepth(void)
777 printf("depth = ");
778 scanf("%hd", &MaxSearchDepth);
779 TCflag = !(MaxSearchDepth > 0);
783 void
784 Raw_ChangeHashDepth(void)
786 printf("hashdepth = ");
787 scanf("%hd", &HashDepth);
788 printf("MoveLimit = ");
789 scanf("%hd", &HashMoveLimit);
793 void
794 Raw_SetContempt(void)
796 printf("contempt = ");
797 scanf("%hd", &contempt);
801 void
802 Raw_ChangeXwindow(void)
804 printf("xwndw = ");
805 scanf("%hd", &xwndw);
810 * Raw_ShowPostnValue(short sq)
811 * must have called ExaminePosition() first
813 void
814 Raw_ShowPostnValue(short sq)
816 short score;
817 score = ScorePosition(color[sq]);
819 if (color[sq] != neutral)
821 #if defined SAVE_SVALUE
822 printf("???%c ", (color[sq] == white)?'b':'w');
823 #else
824 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
825 #endif
827 else
829 printf(" * ");
834 void
835 Raw_DoDebug(void)
837 short c, p, sq, tp, tc, tsq, score, j, k;
838 char s[40];
840 ExaminePosition(opponent);
841 Raw_ShowMessage("Enter piece: ");
842 scanf("%s", s);
843 c = neutral;
845 if ((s[0] == 'b') || (s[0] == 'B'))
846 c = black;
848 if ((s[0] == 'w') || (s[0] == 'W'))
849 c = white;
851 for (p = king; p > no_piece; p--)
853 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
854 break;
857 if (p > no_piece)
859 for (j = (NO_ROWS - 1); j >= 0; j--)
861 for (k = 0; k < (NO_COLS); k++)
863 sq = j*(NO_COLS) + k;
864 tp = board[sq];
865 tc = color[sq];
866 board[sq] = p;
867 color[sq] = c;
868 tsq = PieceList[c][1];
869 PieceList[c][1] = sq;
870 Raw_ShowPostnValue(sq);
871 PieceList[c][1] = tsq;
872 board[sq] = tp;
873 color[sq] = tc;
876 printf("\n");
880 score = ScorePosition(opponent);
882 for (j = (NO_ROWS - 1); j >= 0; j--)
884 for (k = 0; k < (NO_COLS); k++)
886 sq = j*(NO_COLS) + k;
888 if (color[sq] != neutral)
890 #if defined SAVE_SVALUE
891 printf("%?????%c ", (color[sq] == white)?'b':'w');
892 #else
893 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
894 #endif
896 else
898 printf(" * ");
902 printf("\n");
905 printf("stage = %d\n", stage);
906 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
907 mtl[computer], pscore[computer], GameType[computer],
908 mtl[opponent], pscore[opponent], GameType[opponent]);
912 void
913 Raw_DoTable(short table[NO_SQUARES])
915 short sq, j, k;
916 ExaminePosition(opponent);
918 for (j = (NO_ROWS - 1); j >= 0; j--)
920 for (k = 0; k < NO_COLS; k++)
922 sq = j*(NO_ROWS) + k;
923 printf("%3d ", table[sq]);
926 printf("\n");
931 void
932 Raw_ShowPostnValues(void)
934 short sq, score, j, k;
935 ExaminePosition(opponent);
937 for (j = (NO_ROWS - 1); j >= 0; j--)
939 for (k = 0; k < NO_COLS; k++)
941 sq = j * NO_COLS + k;
942 Raw_ShowPostnValue(sq);
945 printf("\n");
948 score = ScorePosition(opponent);
949 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
950 mtl[computer], pscore[computer], GameType[computer],
951 mtl[opponent], pscore[opponent], GameType[opponent]);
952 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
956 void
957 Raw_PollForInput(void)
959 #ifdef WIN32
960 DWORD cnt;
961 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
962 cnt = 1;
963 #else
964 static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
965 /* .events = */ POLLIN } };
966 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
967 if (cnt < 0) {
968 perror("polling standard input");
969 ExitShogi();
971 #endif
972 if (cnt) { /* if anything to read, or error occured */
973 if (!flag.timeout)
974 flag.back = true; /* previous: flag.timeout = true; */
975 flag.bothsides = false;