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