Merge branch 'maint' into master
[gnushogi.git] / gnushogi / rawdsp.c
1 /*
2  * FILE: rawdsp.c
3  *
4  * ----------------------------------------------------------------------
5  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7  *
8  * GNU SHOGI is based on GNU CHESS
9  *
10  * Copyright (c) 1988, 1989, 1990 John Stanback
11  * Copyright (c) 1992 Free Software Foundation
12  *
13  * This file is part of GNU SHOGI.
14  *
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.
19  *
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.
24  *
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  * ----------------------------------------------------------------------
29  *
30  */
31
32 #include <ctype.h>
33 #include <signal.h>
34 #include <stdarg.h>
35 #include <stdio.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
43
44 #include "gnushogi.h"
45
46 unsigned short MV[MAXDEPTH];
47 int MSCORE;
48
49 int mycnt1, mycnt2;
50 char *DRAW;
51 extern char *InPtr;
52 extern short pscore[];
53
54 /****************************************
55  * forward declarations
56  ****************************************/
57
58 void Raw_UpdateDisplay(short f, short t, short redraw, short isspec);
59
60 /****************************************
61  * Trivial output functions.
62  ****************************************/
63
64 void
65 Raw_ClearScreen(void)
66 {
67     if (!barebones && !XSHOGI)
68         printf("\n");
69 }
70
71
72 void
73 Raw_ShowPrompt(void)
74 {
75     if (!barebones && !XSHOGI)
76     {
77         fputs("\nYour move is? ", stdout);
78     }
79 }
80
81
82 void
83 Raw_ShowCurrentMove(short pnt, short f, short t)
84 {
85 }
86
87
88 void
89 Raw_ShowDepth(char ch)
90 {
91     if (!barebones && !XSHOGI)
92     {
93         printf("Depth= %d%c ", Sdepth, ch);
94         printf("\n");
95     }
96 }
97
98
99 void
100 Raw_ShowGameType(void)
101 {
102     if (flag.post)
103         printf("%c vs. %c\n", GameType[black], GameType[white]);
104 }
105
106
107 void
108 Raw_ShowLine(unsigned short *bstline)
109 {
110     int i;
111
112     for (i = 1; bstline[i] > 0; i++)
113     {
114         if ((i > 1) && (i % 8 == 1))
115             printf("\n                          ");
116
117         algbr((short)(bstline[i] >> 8), (short)(bstline[i] & 0xFF), false);
118         printf("%5s ", mvstr[0]);
119     }
120
121     printf("\n");
122 }
123
124
125 void
126 Raw_ShowMessage(char *s)
127 {
128     if (!XSHOGI)
129         printf("%s\n", s);
130 }
131
132
133 void
134 Raw_AlwaysShowMessage(const char *format, ...)
135 {
136     va_list ap;
137     va_start(ap, format);
138     vprintf(format, ap);
139     printf("\n");
140     va_end(ap);
141 }
142
143
144 void
145 Raw_Printf(const char *format, ...)
146 {
147     va_list ap;
148     va_start(ap, format);
149     vprintf(format, ap);
150     va_end(ap);
151 }
152
153
154 void
155 Raw_doRequestInputString(const char* fmt, char* buffer)
156 {
157     scanf(fmt, buffer);
158 }
159
160
161 int
162 Raw_GetString(char* sx)
163 {
164     int eof = 0;
165     char *nl;
166     sx[0] = '\0';
167
168     while(!eof && !sx[0])
169         eof = (fgets(sx, 80, stdin) == NULL);
170
171     /* remove any trailing newline */
172     nl = strchr(sx, '\n');
173     if (nl)
174         nl[0] = '\0';
175
176     return eof;
177 }
178
179
180 void
181 Raw_ShowNodeCnt(long NodeCnt)
182 {
183     printf("Nodes = %ld Nodes/sec = %ld\n",
184            NodeCnt, (((et) ? ((NodeCnt * 100) / et) : 0)));
185 }
186
187
188 void
189 Raw_ShowPatternCount(short side, short n)
190 {
191     if (flag.post)
192         printf("%s matches %d pattern(s)\n", ColorStr[side], n);
193 }
194
195
196 void
197 Raw_ShowResponseTime(void)
198 {
199 }
200
201
202 void
203 Raw_ShowResults(short score, unsigned short *bstline, char ch)
204 {
205     if (flag.post  && !XSHOGI)
206     {
207         ElapsedTime(2);
208         printf("%2d%c %6d %4ld %8ld  ",
209                Sdepth, ch, score, et / 100, NodeCnt);
210         Raw_ShowLine(bstline);
211     }
212 }
213
214
215 void
216 Raw_ShowSidetoMove(void)
217 {
218 }
219
220
221 void
222 Raw_ShowStage(void)
223 {
224     printf("stage = %d\n", stage);
225     printf("balance[black] = %d balance[white] = %d\n",
226            balance[black], balance[white]);
227 }
228
229 /****************************************
230  * End of trivial output routines.
231  ****************************************/
232
233 void
234 Raw_Initialize(void)
235 {
236     mycnt1 = mycnt2 = 0;
237
238     if (XSHOGI)
239     {
240 #ifdef WIN32
241         /* needed because of inconsistency between MSVC run-time system and gcc includes */
242         setbuf(stdout, NULL);
243 #else
244 #ifdef HAVE_SETVBUF
245         setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
246 #else
247 #  ifdef HAVE_SETLINEBUF
248         setlinebuf(stdout);
249 #  else
250 #    error "Need setvbuf() or setlinebuf() to compile gnushogi!"
251 #  endif
252 #endif
253 #endif
254         printf("GNU Shogi %s\n", PACKAGE_VERSION);
255     }
256
257     if (hard_time_limit)
258     {
259         if (!TCflag && (MaxResponseTime == 0))
260             MaxResponseTime = 15L * 100L;
261     }
262 }
263
264
265 void
266 Raw_ExitShogi(void)
267 {
268     /* CHECKME: what purpose does this next statement serve? */
269     signal(SIGTERM, SIG_IGN);
270
271     if (!nolist)
272         ListGame();
273
274     exit(0);
275 }
276
277
278 void
279 Raw_Die(int sig)
280 {
281     char s[80];
282
283     Raw_ShowMessage("Abort? ");
284     scanf("%s", s);
285
286     if (strcmp(s, "yes") == 0)
287         Raw_ExitShogi();
288 }
289
290
291 void
292 Raw_TerminateSearch(int sig)
293 {
294 #ifdef INTERRUPT_TEST
295     ElapsedTime(INIT_INTERRUPT_MODE);
296 #endif
297
298     if (!flag.timeout)
299         flag.back = true; /* previous: flag.timeout = true; */
300
301     flag.bothsides = false;
302 }
303
304
305 void
306 Raw_help(void)
307 {
308     Raw_ClearScreen();
309     printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
310     printf("----------------------------------------------------------------\n");
311     printf("7g7f      move from 7g to 7f      quit      Exit Shogi\n");
312     printf("S6h       move silver to 6h       beep      turn %s\n",
313            (flag.beep) ? "OFF" : "ON");
314     printf("2d2c+     move to 2c and promote  material  turn %s\n",
315            (flag.material) ? "OFF" : "ON");
316     printf("P*5e      drop pawn to 5e         easy      turn %s\n",
317            (flag.easy) ? "OFF" : "ON");
318     printf("tsume     toggle tsume mode       hash      turn %s\n",
319            (flag.hash) ? "OFF" : "ON");
320     printf("bd        redraw board            reverse   board display\n");
321     printf("list      game to shogi.lst       book      turn %s used %d of %d\n",
322            (Book) ? "OFF" : "ON", bookcount, booksize);
323     printf("undo      undo last ply           remove    take back a move\n");
324     printf("edit      edit board              force     toggle manual move mode\n");
325     printf("switch    sides with computer     both      computer match\n");
326     printf("black     computer plays black    white     computer plays white\n");
327     printf("sd        set search depth        clock     set time control\n");
328     printf("post      principle variation     hint      suggest a move\n");
329     printf("save      game to file            get       game from file\n");
330     printf("xsave     pos. to xshogi file     xget      pos. from xshogi file\n");
331     printf("random    randomize play          new       start new game\n");
332     printf("setup                             first     \n");
333     printf("go        computer plays now      material  turn %s\n",
334            flag.material ? "OFF" : "ON");
335     printf("level     time control            gamein    \n");
336     printf("time      set engine clock        otime     set opponent clock\n");
337     printf("Awindow                           Bwindow     \n");
338     printf("rcptr     turn %3s                bsave     book save\n",
339            flag.rcptr ? "OFF" : "ON ");
340     printf("hashdepth                         hard      turn easy OFF\n");
341     printf("contempt                          xwndw                  \n");
342     printf("rv        turn %3s                coords    turn %s\n",
343            flag.rv ? "OFF" : "ON ", flag.coords ? "OFF" : "ON");
344     printf("stars     turn %3s                moves                  \n",
345            flag.stars ? "OFF" : "ON ");
346     printf("test                              p                      \n");
347     printf("debug                             depth     alias for 'sd'\n");
348     printf("----------------------------------------------------------------\n");
349     printf("Computer: %-12s Opponent:            %s\n",
350            ColorStr[computer], ColorStr[opponent]);
351     printf("Depth:    %-12d Response time:       %ld sec\n",
352            MaxSearchDepth, MaxResponseTime/100);
353     printf("Random:   %-12s Easy mode:           %s\n",
354            (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
355     printf("Beep:     %-12s Transposition file:  %s\n",
356            (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
357     printf("Tsume:    %-12s Force:               %s\n",
358            (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
359     printf("Time Control %s %d moves %ld sec %d add %d depth\n",
360            (TCflag) ? "ON" : "OFF",
361            TimeControl.moves[black], TimeControl.clock[black] / 100,
362            TCadd/100, MaxSearchDepth);
363 }
364
365
366 /*
367  * Set up a board position. Pieces are entered by typing the piece followed
368  * by the location. For example, Nf3 will place a knight on square f3.
369  */
370 void
371 Raw_EditBoard(void)
372 {
373     short a, r, c, sq, i, found;
374     char s[80];
375
376     flag.regularstart = true;
377     Book = BOOKFAIL;
378     Raw_ClearScreen();
379     Raw_UpdateDisplay(0, 0, 1, 0);
380     fputs(".   Exit to main\n", stdout);
381     fputs("#   Clear board\n", stdout);
382     fputs("c   Change sides\n", stdout);
383     fputs("enter piece & location: \n", stdout);
384
385     a = black;
386
387     do
388     {
389         scanf("%s", s);
390         found = 0;
391
392         if (s[0] == '#')
393         {
394             for (sq = 0; sq < NO_SQUARES; sq++)
395             {
396                 board[sq] = no_piece;
397                 color[sq] = neutral;
398             }
399
400             ClearCaptured();
401         }
402
403         if (s[0] == 'c')
404             a = otherside[a];
405
406         if (s[1] == '*')
407         {
408             for (i = pawn; i <= king; i++)
409             {
410                 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
411                 {
412                     Captured[a][i]++;
413                     found = 1;
414                     break;
415                 }
416             }
417
418             c = -1;
419             r = -1;
420         }
421         else
422         {
423             c = COL_NAME(s[1]);
424             r = ROW_NAME(s[2]);
425         }
426
427         if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
428         {
429             sq = locn(r, c);
430             color[sq] = a;
431             board[sq] = no_piece;
432
433             for (i = no_piece; i <= king; i++)
434             {
435                 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
436                 {
437                     if (s[3] == '+')
438                         board[sq] = promoted[i];
439                     else
440                         board[sq] = i;
441
442                     found = 1;
443                     break;
444                 }
445             }
446
447             if (found == 0)
448                 color[sq] = neutral;
449         }
450     }
451     while (s[0] != '.');
452
453     for (sq = 0; sq < NO_SQUARES; sq++)
454         Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
455
456     GameCnt = 0;
457     Game50 = 1;
458     ZeroRPT();
459     Sdepth = 0;
460     InitializeStats();
461     Raw_ClearScreen();
462     Raw_UpdateDisplay(0, 0, 1, 0);
463 }
464
465
466 /*
467  * Set up a board position.
468  * Nine lines of nine characters are used to setup the board. 9a-1a is the
469  * first line. White pieces are  represented  by  uppercase characters.
470  */
471 void
472 Raw_SetupBoard(void)
473 {
474     short r, c, sq, i;
475     char ch;
476     char s[80];
477
478     NewGame();
479
480     fgets(s, 80, stdin);            /* skip "setup" command */
481
482     for (r = NO_ROWS - 1; r >= 0; r--)
483     {
484         fgets(s, 80, stdin);
485
486         for (c = 0; c <= (NO_COLS - 1); c++)
487         {
488             ch = s[c];
489             sq = locn(r, c);
490             color[sq] = neutral;
491             board[sq] = no_piece;
492
493             for (i = no_piece; i <= king; i++)
494             {
495                 if (ch == pxx[i])
496                 {
497                     color[sq] = white;
498                     board[sq] = i;
499                     break;
500                 }
501                 else if (ch == qxx[i])
502                 {
503                     color[sq] = black;
504                     board[sq] = i;
505                     break;
506                 }
507             }
508         }
509     }
510
511     for (sq = 0; sq < NO_SQUARES; sq++)
512         Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
513
514     InitializeStats();
515     Raw_ClearScreen();
516     Raw_UpdateDisplay(0, 0, 1, 0);
517     fputs("Setup successful\n", stdout);
518 }
519
520
521 void
522 Raw_SearchStartStuff(short side)
523 {
524     if (flag.post)
525     {
526         printf("\nMove# %d    Target = %ld    Clock: %ld\n",
527                GameCnt/2 + 1,
528                ResponseTime, TimeControl.clock[side]);
529     }
530 }
531
532
533 void
534 Raw_OutputMove(void)
535 {
536     if (flag.illegal)
537     {
538         printf("Illegal position.\n");
539         return;
540     }
541
542     if (mvstr[0][0] == '\0')
543         goto nomove;
544
545     if (XSHOGI)
546     {
547         /* add remaining time in milliseconds to xshogi */
548         printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
549                (TimeControl.clock[player] - et) * 10);
550     }
551     else
552     {
553         printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
554     }
555
556  nomove:
557     if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
558         || (root->score == (SCORE_LIMIT + 998)))
559         goto summary;
560
561     if (flag.post)
562     {
563         short h, l, t;
564
565         h = TREE;
566         l = 0;
567         t = TREE >> 1;
568
569         while (l != t)
570         {
571             if (Tree[t].f || Tree[t].t)
572                 l = t;
573             else
574                 h = t;
575
576             t = (l + h) >> 1;
577         }
578
579         printf("Gen %ld Node %ld Tree %d Eval %ld Rate %ld EC %d/%d RS hi %ld lo %ld \n", GenCnt, NodeCnt, t, EvalNodes,
580                (et > 100) ? (NodeCnt / (et / 100)) : 0,
581                EADD, EGET, reminus, replus);
582
583         printf("Hin/Hout/Tcol/Coll/Fin/Fout = %ld/%ld/%ld/%ld/%ld/%ld\n",
584                HashAdd, HashCnt, THashCol, HashCol, FHashCnt, FHashAdd);
585     }
586
587     Raw_UpdateDisplay(root->f, root->t, 0, root->flags);
588
589     if (!XSHOGI)
590     {
591         printf("My move is: %5s\n", mvstr[0]);
592
593         if (flag.beep)
594             printf("%c", 7);
595     }
596
597  summary:
598     if (root->flags & draw)
599     {
600         fputs("Drawn game!\n", stdout);
601     }
602     else if (root->score == -(SCORE_LIMIT + 999))
603     {
604         printf("%s mates!\n", ColorStr[opponent]);
605     }
606     else if (root->score == (SCORE_LIMIT + 998))
607     {
608         printf("%s mates!\n", ColorStr[computer]);
609     }
610 #ifdef VERYBUGGY
611     else if (!barebones && (root->score < -SCORE_LIMIT))
612     {
613         printf("%s has a forced mate in %d moves!\n",
614                ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
615     }
616     else if (!barebones && (root->score > SCORE_LIMIT))
617     {
618         printf("%s has a forced mate in %d moves!\n",
619                ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
620     }
621 #endif /* VERYBUGGY */
622 }
623
624
625 void
626 Raw_UpdateClocks(void)
627 {
628 }
629
630
631 void
632 Raw_UpdateDisplay(short f, short t, short redraw, short isspec)
633 {
634
635     short r, c, l, m;
636
637     if (redraw && !XSHOGI)
638     {
639         printf("\n");
640         r = (short)(TimeControl.clock[black] / 6000);
641         c = (short)((TimeControl.clock[black] % 6000) / 100);
642         l = (short)(TimeControl.clock[white] / 6000);
643         m = (short)((TimeControl.clock[white] % 6000) / 100);
644         printf("Black %d:%02d  White %d:%02d\n", r, c, l, m);
645         printf("\n");
646
647         for (r = (NO_ROWS - 1); r >= 0; r--)
648         {
649             for (c = 0; c <= (NO_COLS - 1); c++)
650             {
651                 char pc;
652                 l = ((flag.reverse)
653                      ? locn((NO_ROWS - 1) - r, (NO_COLS - 1) - c)
654                      : locn(r, c));
655                 pc = (is_promoted[board[l]] ? '+' : ' ');
656
657                 if (color[l] == neutral)
658                     printf(" -");
659                 else if (color[l] == black)
660                     printf("%c%c", pc, qxx[board[l]]);
661                 else
662                     printf("%c%c", pc, pxx[board[l]]);
663             }
664
665             printf("\n");
666         }
667
668         printf("\n");
669         {
670             short side;
671
672             for (side = black; side <= white; side++)
673             {
674                 short piece, c;
675                 printf((side == black)?"black ":"white ");
676
677                 for (piece = pawn; piece <= king; piece++)
678                 {
679                     if ((c = Captured[side][piece]))
680                         printf("%i%c ", c, pxx[piece]);
681                 }
682
683                 printf("\n");
684             }
685         }
686     }
687 }
688
689
690 void
691 Raw_ChangeAlphaWindow(void)
692 {
693     printf("WAwindow: ");
694     scanf("%hd", &WAwindow);
695     printf("BAwindow: ");
696     scanf("%hd", &BAwindow);
697 }
698
699
700 void
701 Raw_ChangeBetaWindow(void)
702 {
703     printf("WBwindow: ");
704     scanf("%hd", &WBwindow);
705     printf("BBwindow: ");
706     scanf("%hd", &BBwindow);
707 }
708
709
710 void
711 Raw_GiveHint(void)
712 {
713     if (hint)
714     {
715         algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
716         printf("Hint: %s\n", mvstr[0]);
717     }
718     else
719         fputs("I have no idea.\n", stdout);
720 }
721
722
723 void
724 Raw_SelectLevel(char *sx)
725 {
726     /* FIXME: NO_SQUARES is nonsense here */
727     char T[NO_SQUARES + 1], *p;
728
729     strncpy(T, sx, NO_SQUARES);
730     T[NO_SQUARES] = '\0';
731
732     /* if line empty, ask for input */
733     if (!T[0])
734     {
735         fputs("Enter #moves #minutes: ", stdout);
736         fgets(T, NO_SQUARES + 1, stdin);
737     }
738
739     /* skip blackspace */
740     for (p = T; *p == ' '; p++) ;
741
742     /* could be moves or a fischer clock */
743     if (*p == 'f')
744     {
745         /* its a fischer clock game */
746         char *q;
747         p++;
748         TCminutes = (short)strtol(p, &q, 10);
749         TCadd = (short)strtol(q, NULL, 10) *100;
750         TCseconds = 0;
751         TCmoves = 50;
752     }
753     else
754     {
755         /* regular game */
756         char *q;
757         TCadd = 0;
758         TCmoves = (short)strtol(p, &q, 10);
759         TCminutes = (short)strtol(q, &q, 10);
760
761         if (*q == ':')
762             TCseconds = (short)strtol(q + 1, (char **) NULL, 10);
763         else
764             TCseconds = 0;
765
766 #ifdef OPERATORTIME
767         fputs("Operator time (hundredths) = ", stdout);
768         scanf("%hd", &OperatorTime);
769 #endif
770
771         if (TCmoves == 0)
772         {
773             TCflag = false;
774             MaxResponseTime = TCminutes*60L * 100L + TCseconds * 100L;
775             TCminutes = TCseconds = 0;
776         }
777         else
778         {
779             TCflag = true;
780             MaxResponseTime = 0;
781         }
782     }
783
784     TimeControl.clock[black] = TimeControl.clock[white] = 0;
785     SetTimeControl();
786
787     if (XSHOGI)
788     {
789         printf("Clocks: %ld %ld\n",
790                TimeControl.clock[black] * 10,
791                TimeControl.clock[white] * 10);
792     }
793 }
794
795
796 void
797 Raw_ChangeSearchDepth(char *sx)
798 {
799     char buf[80+1];
800     strncpy(buf, sx, 80); buf[80] = '\0';
801     /* if line empty, ask for input */
802     if (!buf[0]) {
803         printf("depth = ");
804         fgets(buf, 80+1, stdin);
805     }
806     sscanf(buf, "%hd", &MaxSearchDepth);
807     TCflag = !(MaxSearchDepth > 0);
808 }
809
810
811 void
812 Raw_ChangeHashDepth(void)
813 {
814     printf("hashdepth = ");
815     scanf("%hd", &HashDepth);
816     printf("MoveLimit = ");
817     scanf("%hd", &HashMoveLimit);
818 }
819
820
821 void
822 Raw_SetContempt(void)
823 {
824     printf("contempt = ");
825     scanf("%hd", &contempt);
826 }
827
828
829 void
830 Raw_ChangeXwindow(void)
831 {
832     printf("xwndw = ");
833     scanf("%hd", &xwndw);
834 }
835
836
837 /*
838  * Raw_ShowPostnValue(short sq)
839  * must have called ExaminePosition() first
840  */
841 void
842 Raw_ShowPostnValue(short sq)
843 {
844     (void) ScorePosition(color[sq]);
845
846     if (color[sq] != neutral)
847     {
848 #if defined SAVE_SVALUE
849         printf("???%c ", (color[sq] == white)?'b':'w');
850 #else
851         printf("%3d%c ", svalue[sq], (color[sq] == white)?'b':'w');
852 #endif
853     }
854     else
855     {
856         printf(" *   ");
857     }
858 }
859
860
861 void
862 Raw_DoDebug(void)
863 {
864     short c, p, sq, tp, tc, tsq, score, j, k;
865     char s[40];
866
867     ExaminePosition(opponent);
868     Raw_ShowMessage("Enter piece: ");
869     scanf("%s", s);
870     c = neutral;
871
872     if ((s[0] == 'b') || (s[0] == 'B'))
873         c = black;
874
875     if ((s[0] == 'w') || (s[0] == 'W'))
876         c = white;
877
878     for (p = king; p > no_piece; p--)
879     {
880         if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
881             break;
882     }
883
884     if (p > no_piece)
885     {
886         for (j = (NO_ROWS - 1); j >= 0; j--)
887         {
888             for (k = 0; k < (NO_COLS); k++)
889             {
890                 sq = j*(NO_COLS) + k;
891                 tp = board[sq];
892                 tc = color[sq];
893                 board[sq] = p;
894                 color[sq] = c;
895                 tsq = PieceList[c][1];
896                 PieceList[c][1] = sq;
897                 Raw_ShowPostnValue(sq);
898                 PieceList[c][1] = tsq;
899                 board[sq] = tp;
900                 color[sq] = tc;
901             }
902
903             printf("\n");
904         }
905     }
906
907     score = ScorePosition(opponent);
908
909     for (j = (NO_ROWS - 1); j >= 0; j--)
910     {
911         for (k = 0; k < (NO_COLS); k++)
912         {
913             sq = j*(NO_COLS) + k;
914
915             if (color[sq] != neutral)
916             {
917 #if defined SAVE_SVALUE
918                 printf("%?????%c ", (color[sq] == white)?'b':'w');
919 #else
920                 printf("%5d%c ", svalue[sq], (color[sq] == white)?'b':'w');
921 #endif
922             }
923             else
924             {
925                 printf("    *  ");
926             }
927         }
928
929         printf("\n");
930     }
931
932     printf("stage = %d\n", stage);
933     printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
934            mtl[computer], pscore[computer], GameType[computer],
935            mtl[opponent], pscore[opponent], GameType[opponent]);
936 }
937
938
939 void
940 Raw_DoTable(short table[NO_SQUARES])
941 {
942     short  sq, j, k;
943     ExaminePosition(opponent);
944
945     for (j = (NO_ROWS - 1); j >= 0; j--)
946     {
947         for (k = 0; k < NO_COLS; k++)
948         {
949             sq = j*(NO_ROWS) + k;
950             printf("%3d ", table[sq]);
951         }
952
953         printf("\n");
954     }
955 }
956
957
958 void
959 Raw_ShowPostnValues(void)
960 {
961     short sq, score, j, k;
962     ExaminePosition(opponent);
963
964     for (j = (NO_ROWS - 1); j >= 0; j--)
965     {
966         for (k = 0; k < NO_COLS; k++)
967         {
968             sq = j * NO_COLS + k;
969             Raw_ShowPostnValue(sq);
970         }
971
972         printf("\n");
973     }
974
975     score = ScorePosition(opponent);
976     printf("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
977            mtl[computer], pscore[computer], GameType[computer],
978            mtl[opponent], pscore[opponent], GameType[opponent]);
979     printf("\nhung black %d hung white %d\n", hung[black], hung[white]);
980 }
981
982
983 void
984 Raw_PollForInput(void)
985 {
986 #ifdef WIN32
987     DWORD cnt;
988     if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
989         cnt = 1;
990 #else
991     static struct pollfd pollfds[1] = { [0] = { .fd = STDIN_FILENO,
992                                                 .events = POLLIN } };
993     int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
994     if (cnt < 0) {
995         perror("polling standard input");
996         Raw_ExitShogi();
997     }
998 #endif
999     if (cnt) { /* if anything to read, or error occured */
1000         if (!flag.timeout)
1001             flag.back = true; /* previous: flag.timeout = true; */
1002         flag.bothsides = false;
1003     }
1004 }
1005
1006 struct display raw_display =
1007 {
1008     .ChangeAlphaWindow    = Raw_ChangeAlphaWindow,
1009     .ChangeBetaWindow     = Raw_ChangeBetaWindow,
1010     .ChangeHashDepth      = Raw_ChangeHashDepth,
1011     .ChangeSearchDepth    = Raw_ChangeSearchDepth,
1012     .ChangeXwindow        = Raw_ChangeXwindow,
1013     .ClearScreen          = Raw_ClearScreen,
1014     .DoDebug              = Raw_DoDebug,
1015     .DoTable              = Raw_DoTable,
1016     .EditBoard            = Raw_EditBoard,
1017     .ExitShogi            = Raw_ExitShogi,
1018     .GiveHint             = Raw_GiveHint,
1019     .Initialize           = Raw_Initialize,
1020     .ShowNodeCnt          = Raw_ShowNodeCnt,
1021     .OutputMove           = Raw_OutputMove,
1022     .PollForInput         = Raw_PollForInput,
1023     .SetContempt          = Raw_SetContempt,
1024     .SearchStartStuff     = Raw_SearchStartStuff,
1025     .SelectLevel          = Raw_SelectLevel,
1026     .ShowCurrentMove      = Raw_ShowCurrentMove,
1027     .ShowDepth            = Raw_ShowDepth,
1028     .ShowGameType         = Raw_ShowGameType,
1029     .ShowLine             = Raw_ShowLine,
1030     .ShowMessage          = Raw_ShowMessage,
1031     .AlwaysShowMessage    = Raw_AlwaysShowMessage,
1032     .Printf               = Raw_Printf,
1033     .doRequestInputString = Raw_doRequestInputString,
1034     .GetString            = Raw_GetString,
1035     .SetupBoard           = Raw_SetupBoard,
1036     .ShowPatternCount     = Raw_ShowPatternCount,
1037     .ShowPostnValue       = Raw_ShowPostnValue,
1038     .ShowPostnValues      = Raw_ShowPostnValues,
1039     .ShowPrompt           = Raw_ShowPrompt,
1040     .ShowResponseTime     = Raw_ShowResponseTime,
1041     .ShowResults          = Raw_ShowResults,
1042     .ShowSidetoMove       = Raw_ShowSidetoMove,
1043     .ShowStage            = Raw_ShowStage,
1044     .TerminateSearch      = Raw_TerminateSearch,
1045     .UpdateClocks         = Raw_UpdateClocks,
1046     .UpdateDisplay        = Raw_UpdateDisplay,
1047     .help                 = Raw_help,
1048 };