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