EditBoard: restructure loop for consistency.
[gnushogi.git] / gnushogi / rawdsp.c
blob287e578e4146033dd345ad17afe4e67d6acaf2fc
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;
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();
371 if (s[0] == 'c')
372 a = otherside[a];
374 if (s[1] == '*')
376 for (i = pawn; i <= king; i++)
378 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
380 Captured[a][i]++;
381 found = 1;
382 break;
386 c = -1;
387 r = -1;
389 else
391 c = COL_NUM(s[1]);
392 r = ROW_NUM(s[2]);
395 if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
397 sq = locn(r, c);
398 color[sq] = a;
399 board[sq] = no_piece;
401 for (i = no_piece; i <= king; i++)
403 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
405 if (s[3] == '+')
406 board[sq] = promoted[i];
407 else
408 board[sq] = i;
410 found = 1;
411 break;
415 if (found == 0)
416 color[sq] = neutral;
420 for (sq = 0; sq < NO_SQUARES; sq++)
421 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
423 GameCnt = 0;
424 Game50 = 1;
425 ZeroRPT();
426 Sdepth = 0;
427 InitializeStats();
428 Raw_ClearScreen();
429 Raw_UpdateDisplay(0, 0, 1, 0);
434 * Set up a board position.
435 * Nine lines of nine characters are used to setup the board. 9a-1a is the
436 * first line. White pieces are represented by uppercase characters.
438 void
439 Raw_SetupBoard(void)
441 short r, c, sq, i;
442 char ch;
443 char s[80];
445 NewGame();
447 fgets(s, 80, stdin); /* skip "setup" command */
449 for (r = NO_ROWS - 1; r >= 0; r--)
451 fgets(s, 80, stdin);
453 for (c = 0; c <= (NO_COLS - 1); c++)
455 ch = s[c];
456 sq = locn(r, c);
457 color[sq] = neutral;
458 board[sq] = no_piece;
460 for (i = no_piece; i <= king; i++)
462 if (ch == pxx[i])
464 color[sq] = white;
465 board[sq] = i;
466 break;
468 else if (ch == qxx[i])
470 color[sq] = black;
471 board[sq] = i;
472 break;
478 for (sq = 0; sq < NO_SQUARES; sq++)
479 Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
481 InitializeStats();
482 Raw_ClearScreen();
483 Raw_UpdateDisplay(0, 0, 1, 0);
484 fputs("Setup successful\n", stdout);
488 void
489 Raw_SearchStartStuff(short side)
491 if (flag.post)
493 printf("\nMove# %d Target = %ld Clock: %ld\n",
494 GameCnt/2 + 1,
495 ResponseTime, TimeControl.clock[side]);
500 void
501 Raw_OutputMove(void)
503 if (flag.illegal)
505 printf("Illegal position.\n");
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("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
547 (et > 100) ? (NodeCnt / (et / 100)) : 0,
548 EADD, EGET, reminus, replus);
550 printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
551 HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
554 Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
556 if (!XSHOGI)
558 printf("My move is: %5s\n", mvstr[0]);
560 if (flag.beep)
561 printf("%c", 7);
564 summary:
565 if (root->flags & draw)
567 fputs("Drawn game!\n", stdout);
569 else if (root->score == -(SCORE_LIMIT + 999))
571 printf("%s mates!\n", ColorStr[opponent]);
573 else if (root->score == (SCORE_LIMIT + 998))
575 printf("%s mates!\n", ColorStr[computer]);
577 #ifdef VERYBUGGY
578 else if (!barebones && (root->score < -SCORE_LIMIT))
580 printf("%s has a forced mate in %d moves!\n",
581 ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
583 else if (!barebones && (root->score > SCORE_LIMIT))
585 printf("%s has a forced mate in %d moves!\n",
586 ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
588 #endif /* VERYBUGGY */
592 void
593 Raw_UpdateClocks(void)
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("\n");
614 for (r = (NO_ROWS - 1); r >= 0; r--)
616 for (c = 0; c <= (NO_COLS - 1); c++)
618 char pc;
619 l = ((flag.reverse)
620 ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
621 : locn(r, c));
622 pc = (is_promoted[board[l]] ? '+' : ' ');
624 if (color[l] == neutral)
625 printf(" -");
626 else if (color[l] == black)
627 printf("%c%c", pc, qxx[board[l]]);
628 else
629 printf("%c%c", pc, pxx[board[l]]);
632 printf("\n");
635 printf("\n");
637 short side;
639 for (side = black; side <= white; side++)
641 short piece, c;
642 printf((side == black)?"black ":"white ");
644 for (piece = pawn; piece <= king; piece++)
646 if ((c = Captured[side][piece]))
647 printf("%i%c ", c, pxx[piece]);
650 printf("\n");
657 void
658 Raw_ChangeAlphaWindow(void)
660 printf("WAwindow: ");
661 scanf("%hd", &WAwindow);
662 printf("BAwindow: ");
663 scanf("%hd", &BAwindow);
667 void
668 Raw_ChangeBetaWindow(void)
670 printf("WBwindow: ");
671 scanf("%hd", &WBwindow);
672 printf("BBwindow: ");
673 scanf("%hd", &BBwindow);
677 void
678 Raw_GiveHint(void)
680 if (hint)
682 algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
683 printf("Hint: %s\n", mvstr[0]);
685 else
686 fputs("I have no idea.\n", stdout);
690 void
691 Raw_SelectLevel(char *sx)
694 char T[NO_SQUARES + 1], *p, *q;
696 if ((p = strstr(sx, "level")) != NULL)
697 p += strlen("level");
698 else if ((p = strstr(sx, "clock")) != NULL)
699 p += strlen("clock");
701 strcat(sx, "XX");
702 q = T;
703 *q = '\0';
705 for (; *p != 'X'; *q++ = *p++);
707 *q = '\0';
709 /* line empty ask for input */
710 if (!T[0])
712 fputs("Enter #moves #minutes: ", stdout);
713 fgets(T, NO_SQUARES + 1, stdin);
714 strcat(T, "XX");
717 /* skip blackspace */
718 for (p = T; *p == ' '; p++) ;
720 /* could be moves or a fischer clock */
721 if (*p == 'f')
723 /* its a fischer clock game */
724 p++;
725 TCminutes = (short)strtol(p, &q, 10);
726 TCadd = (short)strtol(q, NULL, 10) *100;
727 TCseconds = 0;
728 TCmoves = 50;
730 else
732 /* regular game */
733 TCadd = 0;
734 TCmoves = (short)strtol(p, &q, 10);
735 TCminutes = (short)strtol(q, &q, 10);
737 if (*q == ':')
738 TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
739 else
740 TCseconds = 0;
742 #ifdef OPERATORTIME
743 fputs("Operator time (hundredths) = ", stdout);
744 scanf("%hd", &OperatorTime);
745 #endif
747 if (TCmoves == 0)
749 TCflag = false;
750 MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
751 TCminutes = TCseconds = 0;
753 else
755 TCflag = true;
756 MaxResponseTime = 0;
760 TimeControl.clock[black] = TimeControl.clock[white] = 0;
761 SetTimeControl();
763 if (XSHOGI)
765 printf("Clocks: %ld %ld\n",
766 TimeControl.clock[black] * 10,
767 TimeControl.clock[white] * 10);
772 void
773 Raw_ChangeSearchDepth(void)
775 printf("depth = ");
776 scanf("%hd", &MaxSearchDepth);
777 TCflag = !(MaxSearchDepth > 0);
781 void
782 Raw_ChangeHashDepth(void)
784 printf("hashdepth = ");
785 scanf("%hd", &HashDepth);
786 printf("MoveLimit = ");
787 scanf("%hd", &HashMoveLimit);
791 void
792 Raw_SetContempt(void)
794 printf("contempt = ");
795 scanf("%hd", &contempt);
799 void
800 Raw_ChangeXwindow(void)
802 printf("xwndw = ");
803 scanf("%hd", &xwndw);
808 * Raw_ShowPostnValue(short sq)
809 * must have called ExaminePosition() first
811 void
812 Raw_ShowPostnValue(short sq)
814 short score;
815 score = ScorePosition(color[sq]);
817 if (color[sq] != neutral)
819 #if defined SAVE_SVALUE
820 printf("???%c ", (color[sq] == white)?'b':'w');
821 #else
822 printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
823 #endif
825 else
827 printf(" * ");
832 void
833 Raw_DoDebug(void)
835 short c, p, sq, tp, tc, tsq, score, j, k;
836 char s[40];
838 ExaminePosition(opponent);
839 Raw_ShowMessage("Enter piece: ");
840 scanf("%s", s);
841 c = neutral;
843 if ((s[0] == 'b') || (s[0] == 'B'))
844 c = black;
846 if ((s[0] == 'w') || (s[0] == 'W'))
847 c = white;
849 for (p = king; p > no_piece; p--)
851 if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
852 break;
855 if (p > no_piece)
857 for (j = (NO_ROWS - 1); j >= 0; j--)
859 for (k = 0; k < (NO_COLS); k++)
861 sq = j*(NO_COLS) + k;
862 tp = board[sq];
863 tc = color[sq];
864 board[sq] = p;
865 color[sq] = c;
866 tsq = PieceList[c][1];
867 PieceList[c][1] = sq;
868 Raw_ShowPostnValue(sq);
869 PieceList[c][1] = tsq;
870 board[sq] = tp;
871 color[sq] = tc;
874 printf("\n");
878 score = ScorePosition(opponent);
880 for (j = (NO_ROWS - 1); j >= 0; j--)
882 for (k = 0; k < (NO_COLS); k++)
884 sq = j*(NO_COLS) + k;
886 if (color[sq] != neutral)
888 #if defined SAVE_SVALUE
889 printf("%?????%c ", (color[sq] == white)?'b':'w');
890 #else
891 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
892 #endif
894 else
896 printf(" * ");
900 printf("\n");
903 printf("stage = %d\n", stage);
904 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
905 mtl[computer], pscore[computer], GameType[computer],
906 mtl[opponent], pscore[opponent], GameType[opponent]);
910 void
911 Raw_DoTable(short table[NO_SQUARES])
913 short sq, j, k;
914 ExaminePosition(opponent);
916 for (j = (NO_ROWS - 1); j >= 0; j--)
918 for (k = 0; k < NO_COLS; k++)
920 sq = j*(NO_ROWS) + k;
921 printf("%3d ", table[sq]);
924 printf("\n");
929 void
930 Raw_ShowPostnValues(void)
932 short sq, score, j, k;
933 ExaminePosition(opponent);
935 for (j = (NO_ROWS - 1); j >= 0; j--)
937 for (k = 0; k < NO_COLS; k++)
939 sq = j * NO_COLS + k;
940 Raw_ShowPostnValue(sq);
943 printf("\n");
946 score = ScorePosition(opponent);
947 printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
948 mtl[computer], pscore[computer], GameType[computer],
949 mtl[opponent], pscore[opponent], GameType[opponent]);
950 printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
954 void
955 Raw_PollForInput(void)
957 #ifdef WIN32
958 DWORD cnt;
959 if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
960 cnt = 1;
961 #else
962 static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
963 /* .events = */ POLLIN } };
964 int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
965 if (cnt < 0) {
966 perror("polling standard input");
967 ExitShogi();
969 #endif
970 if (cnt) { /* if anything to read, or error occured */
971 if (!flag.timeout)
972 flag.back = true; /* previous: flag.timeout = true; */
973 flag.bothsides = false;