Fix use of (COL|ROW)_NAME macros to compute numbers from names.
[gnushogi.git] / gnushogi / rawdsp.c
blobd4ac301bf01673ed68aed6c8c2ebc892c8f12f60
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 fputs(". Exit to main\n", stdout);
346 fputs("# Clear board\n", stdout);
347 fputs("c Change sides\n", stdout);
348 fputs("enter piece & location: \n", stdout);
350 a = black;
354 scanf("%s", s);
355 found = 0;
357 if (s[0] == '#')
359 for (sq = 0; sq < NO_SQUARES; sq++)
361 board[sq] = no_piece;
362 color[sq] = neutral;
365 ClearCaptured();
368 if (s[0] == 'c')
369 a = otherside[a];
371 if (s[1] == '*')
373 for (i = pawn; i <= king; i++)
375 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
377 Captured[a][i]++;
378 found = 1;
379 break;
383 c = -1;
384 r = -1;
386 else
388 c = COL_NUM(s[1]);
389 r = ROW_NUM(s[2]);
392 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
394 sq = locn(r, c);
395 color[sq] = a;
396 board[sq] = no_piece;
398 for (i = no_piece; i <= king; i++)
400 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
402 if (s[3] == '+')
403 board[sq] = promoted[i];
404 else
405 board[sq] = i;
407 found = 1;
408 break;
412 if (found == 0)
413 color[sq] = neutral;
416 while (s[0] != '.');
418 for (sq = 0; sq < NO_SQUARES; sq++)
419 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
421 GameCnt = 0;
422 Game50 = 1;
423 ZeroRPT();
424 Sdepth = 0;
425 InitializeStats();
426 Raw_ClearScreen();
427 Raw_UpdateDisplay(0, 0, 1, 0);
432 * Set up a board position.
433 * Nine lines of nine characters are used to setup the board. 9a-1a is the
434 * first line. White pieces are represented by uppercase characters.
436 void
437 Raw_SetupBoard(void)
439 short r, c, sq, i;
440 char ch;
441 char s[80];
443 NewGame();
445 fgets(s, 80, stdin); /* skip "setup" command */
447 for (r = NO_ROWS - 1; r >= 0; r--)
449 fgets(s, 80, stdin);
451 for (c = 0; c <= (NO_COLS - 1); c++)
453 ch = s[c];
454 sq = locn(r, c);
455 color[sq] = neutral;
456 board[sq] = no_piece;
458 for (i = no_piece; i <= king; i++)
460 if (ch == pxx[i])
462 color[sq] = white;
463 board[sq] = i;
464 break;
466 else if (ch == qxx[i])
468 color[sq] = black;
469 board[sq] = i;
470 break;
476 for (sq = 0; sq < NO_SQUARES; sq++)
477 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
479 InitializeStats();
480 Raw_ClearScreen();
481 Raw_UpdateDisplay(0, 0, 1, 0);
482 fputs("Setup successful\n", stdout);
486 void
487 Raw_SearchStartStuff(short side)
489 if (flag.post)
491 printf("\nMove# %d Target = %ld Clock: %ld\n",
492 GameCnt/2 + 1,
493 ResponseTime, TimeControl.clock[side]);
498 void
499 Raw_OutputMove(void)
501 if (flag.illegal)
503 printf("Illegal position.\n");
504 return;
507 if (mvstr[0][0] == '\0')
508 goto nomove;
510 if (XSHOGI)
512 /* add remaining time in milliseconds to xshogi */
513 printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
514 (TimeControl.clock[player] - et) * 10);
516 else
518 printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
521 nomove:
522 if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
523 || (root->score == (SCORE_LIMIT + 998)))
524 goto summary;
526 if (flag.post)
528 short h, l, t;
530 h = TREE;
531 l = 0;
532 t = TREE >> 1;
534 while (l != t)
536 if (Tree[t].f || Tree[t].t)
537 l = t;
538 else
539 h = t;
541 t = (l + h) >> 1;
544 printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
545 (et > 100) ? (NodeCnt / (et / 100)) : 0,
546 EADD, EGET, reminus, replus);
548 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
549 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
552 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
554 if (!XSHOGI)
556 printf("My move is: %5s\n", mvstr[0]);
558 if (flag.beep)
559 printf("%c", 7);
562 summary:
563 if (root->flags & draw)
565 fputs("Drawn game!\n", stdout);
567 else if (root->score == -(SCORE_LIMIT + 999))
569 printf("%s mates!\n", ColorStr[opponent]);
571 else if (root->score == (SCORE_LIMIT + 998))
573 printf("%s mates!\n", ColorStr[computer]);
575 #ifdef VERYBUGGY
576 else if (!barebones && (root->score < -SCORE_LIMIT))
578 printf("%s has a forced mate in %d moves!\n",
579 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
581 else if (!barebones && (root->score > SCORE_LIMIT))
583 printf("%s has a forced mate in %d moves!\n",
584 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
586 #endif /* VERYBUGGY */
590 void
591 Raw_UpdateClocks(void)
596 void
597 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
600 short r, c, l, m;
602 if (redraw && !XSHOGI)
604 printf("\n");
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("\n");
612 for (r = (NO_ROWS - 1); r >= 0; r--)
614 for (c = 0; c <= (NO_COLS - 1); c++)
616 char pc;
617 l = ((flag.reverse)
618 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
619 : locn(r, c));
620 pc = (is_promoted[board[l]] ? '+' : ' ');
622 if (color[l] == neutral)
623 printf(" -");
624 else if (color[l] == black)
625 printf("%c%c", pc, qxx[board[l]]);
626 else
627 printf("%c%c", pc, pxx[board[l]]);
630 printf("\n");
633 printf("\n");
635 short side;
637 for (side = black; side <= white; side++)
639 short piece, c;
640 printf((side == black)?"black ":"white ");
642 for (piece = pawn; piece <= king; piece++)
644 if ((c = Captured[side][piece]))
645 printf("%i%c ", c, pxx[piece]);
648 printf("\n");
655 void
656 Raw_ChangeAlphaWindow(void)
658 printf("WAwindow: ");
659 scanf("%hd", &WAwindow);
660 printf("BAwindow: ");
661 scanf("%hd", &BAwindow);
665 void
666 Raw_ChangeBetaWindow(void)
668 printf("WBwindow: ");
669 scanf("%hd", &WBwindow);
670 printf("BBwindow: ");
671 scanf("%hd", &BBwindow);
675 void
676 Raw_GiveHint(void)
678 if (hint)
680 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
681 printf("Hint: %s\n", mvstr[0]);
683 else
684 fputs("I have no idea.\n", stdout);
688 void
689 Raw_SelectLevel(char *sx)
692 char T[NO_SQUARES + 1], *p, *q;
694 if ((p = strstr(sx, "level")) != NULL)
695 p += strlen("level");
696 else if ((p = strstr(sx, "clock")) != NULL)
697 p += strlen("clock");
699 strcat(sx, "XX");
700 q = T;
701 *q = '\0';
703 for (; *p != 'X'; *q++ = *p++);
705 *q = '\0';
707 /* line empty ask for input */
708 if (!T[0])
710 fputs("Enter #moves #minutes: ", stdout);
711 fgets(T, NO_SQUARES + 1, stdin);
712 strcat(T, "XX");
715 /* skip blackspace */
716 for (p = T; *p == ' '; p++) ;
718 /* could be moves or a fischer clock */
719 if (*p == 'f')
721 /* its a fischer clock game */
722 p++;
723 TCminutes = (short)strtol(p, &q, 10);
724 TCadd = (short)strtol(q, NULL, 10) *100;
725 TCseconds = 0;
726 TCmoves = 50;
728 else
730 /* regular game */
731 TCadd = 0;
732 TCmoves = (short)strtol(p, &q, 10);
733 TCminutes = (short)strtol(q, &q, 10);
735 if (*q == ':')
736 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
737 else
738 TCseconds = 0;
740 #ifdef OPERATORTIME
741 fputs("Operator time (hundredths) = ", stdout);
742 scanf("%hd", &OperatorTime);
743 #endif
745 if (TCmoves == 0)
747 TCflag = false;
748 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
749 TCminutes = TCseconds = 0;
751 else
753 TCflag = true;
754 MaxResponseTime = 0;
758 TimeControl.clock[black] = TimeControl.clock[white] = 0;
759 SetTimeControl();
761 if (XSHOGI)
763 printf("Clocks: %ld %ld\n",
764 TimeControl.clock[black] * 10,
765 TimeControl.clock[white] * 10);
770 void
771 Raw_ChangeSearchDepth(void)
773 printf("depth = ");
774 scanf("%hd", &MaxSearchDepth);
775 TCflag = !(MaxSearchDepth > 0);
779 void
780 Raw_ChangeHashDepth(void)
782 printf("hashdepth = ");
783 scanf("%hd", &HashDepth);
784 printf("MoveLimit = ");
785 scanf("%hd", &HashMoveLimit);
789 void
790 Raw_SetContempt(void)
792 printf("contempt = ");
793 scanf("%hd", &contempt);
797 void
798 Raw_ChangeXwindow(void)
800 printf("xwndw = ");
801 scanf("%hd", &xwndw);
806 * Raw_ShowPostnValue(short sq)
807 * must have called ExaminePosition() first
809 void
810 Raw_ShowPostnValue(short sq)
812 short score;
813 score = ScorePosition(color[sq]);
815 if (color[sq] != neutral)
817 #if defined SAVE_SVALUE
818 printf("???%c ", (color[sq] == white)?'b':'w');
819 #else
820 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
821 #endif
823 else
825 printf(" * ");
830 void
831 Raw_DoDebug(void)
833 short c, p, sq, tp, tc, tsq, score, j, k;
834 char s[40];
836 ExaminePosition(opponent);
837 Raw_ShowMessage("Enter piece: ");
838 scanf("%s", s);
839 c = neutral;
841 if ((s[0] == 'b') || (s[0] == 'B'))
842 c = black;
844 if ((s[0] == 'w') || (s[0] == 'W'))
845 c = white;
847 for (p = king; p > no_piece; p--)
849 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
850 break;
853 if (p > no_piece)
855 for (j = (NO_ROWS - 1); j >= 0; j--)
857 for (k = 0; k < (NO_COLS); k++)
859 sq = j*(NO_COLS) + k;
860 tp = board[sq];
861 tc = color[sq];
862 board[sq] = p;
863 color[sq] = c;
864 tsq = PieceList[c][1];
865 PieceList[c][1] = sq;
866 Raw_ShowPostnValue(sq);
867 PieceList[c][1] = tsq;
868 board[sq] = tp;
869 color[sq] = tc;
872 printf("\n");
876 score = ScorePosition(opponent);
878 for (j = (NO_ROWS - 1); j >= 0; j--)
880 for (k = 0; k < (NO_COLS); k++)
882 sq = j*(NO_COLS) + k;
884 if (color[sq] != neutral)
886 #if defined SAVE_SVALUE
887 printf("%?????%c ", (color[sq] == white)?'b':'w');
888 #else
889 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
890 #endif
892 else
894 printf(" * ");
898 printf("\n");
901 printf("stage = %d\n", stage);
902 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
903 mtl[computer], pscore[computer], GameType[computer],
904 mtl[opponent], pscore[opponent], GameType[opponent]);
908 void
909 Raw_DoTable(short table[NO_SQUARES])
911 short sq, j, k;
912 ExaminePosition(opponent);
914 for (j = (NO_ROWS - 1); j >= 0; j--)
916 for (k = 0; k < NO_COLS; k++)
918 sq = j*(NO_ROWS) + k;
919 printf("%3d ", table[sq]);
922 printf("\n");
927 void
928 Raw_ShowPostnValues(void)
930 short sq, score, j, k;
931 ExaminePosition(opponent);
933 for (j = (NO_ROWS - 1); j >= 0; j--)
935 for (k = 0; k < NO_COLS; k++)
937 sq = j * NO_COLS + k;
938 Raw_ShowPostnValue(sq);
941 printf("\n");
944 score = ScorePosition(opponent);
945 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
946 mtl[computer], pscore[computer], GameType[computer],
947 mtl[opponent], pscore[opponent], GameType[opponent]);
948 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
952 void
953 Raw_PollForInput(void)
955 #ifdef WIN32
956 DWORD cnt;
957 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
958 cnt = 1;
959 #else
960 static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
961 /* .events = */ POLLIN } };
962 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
963 if (cnt < 0) {
964 perror("polling standard input");
965 ExitShogi();
967 #endif
968 if (cnt) { /* if anything to read, or error occured */
969 if (!flag.timeout)
970 flag.back = true; /* previous: flag.timeout = true; */
971 flag.bothsides = false;