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