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