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