EditBoard: restructure loop for consistency.
[gnushogi.git] / gnushogi / cursesdsp.c
1 /*
2  * FILE: cursesdsp.c
3  *
4  *     Curses interface for GNU Shogi
5  *
6  * ----------------------------------------------------------------------
7  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
9  * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
10  *
11  * GNU SHOGI is based on GNU CHESS
12  *
13  * Copyright (c) 1988, 1989, 1990 John Stanback
14  * Copyright (c) 1992 Free Software Foundation
15  *
16  * This file is part of GNU SHOGI.
17  *
18  * GNU Shogi is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 3 of the License,
21  * or (at your option) any later version.
22  *
23  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with GNU Shogi; see the file COPYING. If not, see
30  * <http://www.gnu.org/licenses/>.
31  * ----------------------------------------------------------------------
32  */
33
34 /* request *snprintf prototypes*/
35 #define _POSIX_C_SOURCE 200112L
36
37 #include <ctype.h>
38 #include <signal.h>
39 #include <stdio.h>
40
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/file.h>
44 #include <curses.h>
45
46 #include "gnushogi.h"
47 #include "cursesdsp.h"
48
49 #if HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #if HAVE_SYS_FILIO_H
54 /* Definition of FIONREAD */
55 #include <sys/filio.h>
56 #endif
57
58 #if HAVE_ERRNO_H
59 /* Definition of errno(). */
60 #include <errno.h>
61 #endif
62
63 #define FLUSH_SCANW fflush(stdout), scanw
64
65 int mycnt1, mycnt2;
66
67 #define TAB (58)
68
69 #define VIR_C(s)  ((flag.reverse) ? (NO_COLS - 1 - column(s)) : column(s))
70 #define VIR_R(s)  ((flag.reverse) ? (NO_ROWS - 1 - row(s)) : row(s))
71
72 unsigned short MV[MAXDEPTH];
73 int MSCORE;
74 char *DRAW;
75
76 /* Forward declarations. */
77 /* FIXME: change this name, puh-leeze! */
78
79 static void UpdateCatched(void);
80 static void DrawPiece(short sq);
81 static void ShowScore(short score);
82
83 /****************************************
84  * Trivial output functions.
85  ****************************************/
86
87 static void
88 ClearEoln(void)
89 {
90     clrtoeol();
91     refresh();
92 }
93
94
95 void
96 Curses_ClearScreen(void)
97 {
98     clear();
99     refresh();
100 }
101
102
103 static void
104 gotoXY(short x, short y)
105 {
106     move(y - 1, x - 1);
107 }
108
109
110 static void
111 ClearMessage(void)
112 {
113     gotoXY(TAB, 6);
114     ClearEoln();
115 }
116
117
118 void
119 Curses_ShowCurrentMove(short pnt, short f, short t)
120 {
121     algbr(f, t, false);
122     gotoXY(TAB, 7);
123     printw("(%2d) %5s ", pnt, mvstr[0]);
124 }
125
126
127 void
128 Curses_ShowDepth(char ch)
129 {
130     gotoXY(TAB, 4);
131     printw("Depth= %d%c ", Sdepth, ch);
132     ClearEoln();
133 }
134
135
136 void
137 Curses_ShowGameType(void)
138 {
139     if (flag.post)
140     {
141         gotoXY(TAB, 20);
142         printw("%c vs. %c", GameType[black], GameType[white]);
143     }
144 }
145
146
147 void
148 ShowHeader(void)
149 {
150     gotoXY(TAB, 2);
151     printw("GNU Shogi %s", PACKAGE_VERSION);
152 }
153
154
155 void
156 Curses_ShowLine(unsigned short *bstline)
157 {
158 }
159
160
161 void
162 Curses_ShowMessage(char *s)
163 {
164     gotoXY(TAB, 6);
165     printw("%s", s);
166     ClearEoln();
167 }
168
169
170 void
171 Curses_AlwaysShowMessage(const char *format, va_list ap)
172 {
173     static char buffer[60];
174     vsnprintf(buffer, sizeof(buffer), format, ap);
175     Curses_ShowMessage(buffer);
176 }
177
178
179 void
180 Curses_Printf(const char *format, va_list ap)
181 {
182     static char buffer[60];
183     vsnprintf(buffer, sizeof(buffer), format, ap);
184     printw("%s", buffer);
185 }
186
187
188 void
189 Curses_doRequestInputString(const char* fmt, char* buffer)
190 {
191     FLUSH_SCANW(fmt, buffer);
192 }
193
194
195 int
196 Curses_GetString(char* sx)
197 {
198     fflush(stdout);
199     return (getstr(sx) == ERR);
200 }
201
202
203 void
204 Curses_ShowNodeCnt(long NodeCnt)
205 {
206     gotoXY(TAB, 22);
207     /* printw("Nodes = %8ld, Nodes/Sec = %5ld", NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0); */
208     printw("n = %ld n/s = %ld", 
209            NodeCnt, (et > 100) ? NodeCnt / (et / 100) : 0);
210     ClearEoln();
211 }
212
213
214 void
215 Curses_ShowPatternCount(short side, short n)
216 {
217     if (flag.post)
218     {
219         gotoXY(TAB + 10 + 3 * side, 20);          /* CHECKME */
220
221         if (n >= 0)
222             printw("%3d", n);
223         else
224             printw("   ");
225     }
226 }
227
228
229 static void
230 ShowPlayers(void)
231 {
232     gotoXY(5, ((flag.reverse) ? (5 + 2*NO_ROWS) : 2));
233     printw("%s", (computer == white) ? "Computer" : "Human   ");
234     gotoXY(5, ((flag.reverse) ? 2 : (5 + 2*NO_ROWS)));
235     printw("%s", (computer == black) ? "Computer" : "Human   ");
236 }
237
238
239 void
240 Curses_ShowPrompt(void)
241 {
242     Curses_ShowSidetoMove();
243     gotoXY(TAB, 17);
244     printw("Your move is? ");
245     ClearEoln();
246 }
247
248
249 void
250 Curses_ShowResponseTime(void)
251 {
252     if (flag.post)
253     {
254         short TCC = TCcount;
255         gotoXY(TAB, 21);
256         printw("%ld, %d, %ld, %ld, %ld, %d",
257                ResponseTime, TCC, TCleft, ExtraTime, et, flag.timeout);
258         ClearEoln();
259     }
260 }
261
262
263 void
264 Curses_ShowResults(short score, unsigned short *bstline, char ch)
265 {
266     unsigned char d, ply;
267
268     if (flag.post)
269     {
270         Curses_ShowDepth(ch);
271         ShowScore(score);
272         d = 7;
273
274         for (ply = 1; bstline[ply] > 0; ply++)
275         {
276             if (ply % 2 == 1)
277             {
278                 gotoXY(TAB, ++d);
279                 ClearEoln();
280             }
281
282             algbr((short) bstline[ply] >> 8, 
283                   (short) bstline[ply] & 0xFF, false);
284             printw("%5s ", mvstr[0]);
285         }
286
287         ClearEoln();
288
289         while (d < 13)
290         {
291             gotoXY(TAB, ++d);
292             ClearEoln();
293         }
294     }
295 }
296
297
298 static void
299 ShowScore(short score)
300 {
301     gotoXY(TAB, 5);
302     printw("Score= %d", score);
303     ClearEoln();
304 }
305
306
307 void
308 Curses_ShowSidetoMove(void)
309 {
310     gotoXY(TAB, 14);
311     printw("%2d:   %s", 1 + GameCnt / 2, ColorStr[player]);
312     ClearEoln();
313 }
314
315
316 void
317 Curses_ShowStage(void)
318 {
319     gotoXY(TAB, 19);
320     printw("Stage= %2d%c B= %2d W= %2d",
321            stage, flag.tsume?'T':' ', balance[black], balance[white]);
322     ClearEoln();
323 }
324
325 /****************************************
326  * End of trivial output routines.
327  ****************************************/
328
329 void
330 Curses_Initialize(void)
331 {
332     signal(SIGINT, Curses_Die);
333     signal(SIGQUIT, Curses_Die);
334     initscr();
335     crmode();
336 }
337
338
339 void
340 Curses_ExitShogi(void)
341
342     if (!nolist)
343         ListGame();
344
345     gotoXY(1, 24);
346
347     refresh();
348     nocrmode();
349     endwin();
350
351     exit(0);
352 }
353
354
355 void
356 Curses_Die(int sig)
357 {
358     char s[80];
359
360     signal(SIGINT, SIG_IGN);
361     signal(SIGQUIT, SIG_IGN);
362
363     Curses_ShowMessage("Abort? ");
364     FLUSH_SCANW("%s", s);
365
366     if (strcmp(s, "yes") == 0)
367         Curses_ExitShogi();
368
369     signal(SIGINT, Curses_Die);
370     signal(SIGQUIT, Curses_Die);
371 }
372
373
374 void
375 Curses_TerminateSearch(int sig)
376 {
377     signal(SIGINT, SIG_IGN);
378     signal(SIGQUIT, SIG_IGN);
379
380     if (!flag.timeout)
381         flag.musttimeout = true;
382
383     Curses_ShowMessage("Terminate Search");
384     flag.bothsides = false;
385     signal(SIGINT, Curses_Die);
386     signal(SIGQUIT, Curses_Die);
387 }
388
389
390 void
391 Curses_help(void)
392 {
393     Curses_ClearScreen();
394     printw("GNU Shogi %s command summary\n", PACKAGE_VERSION);
395     printw("-------------------------------"
396            "---------------------------------\n");
397     printw("7g7f      move from 7g to 7f      quit      Exit Shogi\n");
398     printw("S6h       move silver to 6h       beep      turn %s\n", (flag.beep) ? "OFF" : "ON");
399     printw("2d2c+     move to 2c and promote  material  turn %s\n", (flag.material) ? "OFF" : "ON");
400     printw("P*5e      drop pawn to 5e         easy      turn %s\n", (flag.easy) ? "OFF" : "ON");
401     printw("tsume     toggle tsume mode       hash      turn %s\n", (flag.hash) ? "OFF" : "ON");
402     printw("bd        redraw board            reverse   board display\n");
403     printw("list      game to shogi.lst       book      turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, BOOKSIZE);
404     printw("undo      undo last ply           remove    take back a move\n");
405     printw("edit      edit board              force     toggle manual move mode\n");
406     printw("switch    sides with computer     both      computer match\n");
407     printw("black     computer plays black    white     computer plays white\n");
408     printw("depth     set search depth        clock     set time control\n");
409     printw("post      principle variation     hint      suggest a move\n", (flag.post) ? "OFF" : "ON");
410     printw("save      game to file            get       game from file\n");
411     printw("random    randomize play          new       start new game\n");
412     gotoXY(10, 20);
413     printw("Computer: %s", ColorStr[computer]);
414     gotoXY(10, 21);
415     printw("Opponent: %s", ColorStr[opponent]);
416     gotoXY(10, 22);
417     printw("Level: %ld", MaxResponseTime/100);
418     gotoXY(10, 23);
419     printw("Easy mode: %s", (flag.easy) ? "ON" : "OFF");
420     gotoXY(25, 23);
421     printw("Tsume: %s", (flag.tsume) ? "ON" : "OFF");
422     gotoXY(40, 20);
423     printw("Depth: %d", MaxSearchDepth);
424     gotoXY(40, 21);
425     printw("Random: %s", (dither) ? "ON" : "OFF");
426     gotoXY(40, 22);
427     printw("Transposition table: %s", (flag.hash) ? "ON" : "OFF");
428     gotoXY(40, 23);
429     printw("Hit <RET> to return: ");
430     gotoXY(10, 24);
431     printw("Time Control %s %d moves %d sec %d add %d depth\n", (TCflag) ? "ON" : "OFF",
432            TimeControl.moves[black], 
433            TimeControl.clock[black] / 100, 
434            OperatorTime, MaxSearchDepth);
435
436     refresh();
437
438 #ifdef BOGUS
439     fflush(stdin); /* what is this supposed to do?? */
440 #endif /* BOGUS */
441
442     getchar();
443     Curses_ClearScreen();
444     Curses_UpdateDisplay(0, 0, 1, 0);
445 }
446
447
448 static const short x0[2] = { 54, 2 };
449 static const short y0[2] = { 20, 4 };
450
451
452 /*
453  * Set up a board position. Pieces are entered by typing the piece followed
454  * by the location. For example, N3f will place a knight on square 3f.
455  * P* will put a pawn to the captured pieces.
456  */
457
458 void
459 Curses_EditBoard(void)
460 {
461     short a, c, sq, i, found;
462     short r = 0;
463     char s[80];
464
465     flag.regularstart = true;
466     Book = BOOKFAIL;
467     Curses_ClearScreen();
468     Curses_UpdateDisplay(0, 0, 1, 0);
469     gotoXY(TAB, 3);
470     printw(".   Exit to main\n");
471     gotoXY(TAB, 4);
472     printw("#   Clear board\n");
473     gotoXY(TAB, 5);
474     printw("c   Change sides\n");
475     gotoXY(TAB, 7);
476     printw("Enter piece & location: ");
477     a = black;
478
479     while(1)
480     {
481         gotoXY(TAB, 6);
482         printw("Editing: %s", ColorStr[a]);
483         gotoXY(TAB + 24, 7);
484         ClearEoln();
485         FLUSH_SCANW("%s", s);
486         found = 0;
487
488         if (s[0] == '.')
489             break;
490
491         if (s[0] == '#')
492         {
493             for (sq = 0; sq < NO_SQUARES; sq++)
494             {
495                 board[sq] = no_piece;
496                 color[sq] = neutral;
497                 DrawPiece(sq);
498             }
499
500             ClearCaptured();
501             UpdateCatched();
502         }
503
504         if (s[0] == 'c')
505             a = otherside[a];
506
507         if (s[1] == '*')
508         {
509             for (i = NO_PIECES; i > no_piece; i--)
510             {
511                 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
512                 {
513                     Captured[a][unpromoted[i]]++;
514                     UpdateCatched();
515                     found = 1;
516                     break;
517                 }
518             }
519
520             c = -1;
521         }
522         else
523         {
524             c = COL_NUM(s[1]);
525             r = ROW_NUM(s[2]);
526         }
527
528         if ((c >= 0) && (c < NO_COLS) && (r >= 0) && (r < NO_ROWS))
529         {
530             sq = locn(r, c);
531             color[sq] = a;
532             board[sq] = no_piece;
533     
534             for (i = NO_PIECES; i > no_piece; i--)
535             {
536                 if ((s[0] == pxx[i]) || (s[0] == qxx[i]))
537                 {
538                     if (s[3] == '+')
539                         board[sq] = promoted[i];
540                     else
541                         board[sq] = unpromoted[i];
542
543                     found = 1;
544                     break;
545                 }
546             }
547     
548
549             if (found == 0)
550                 color[sq] = neutral;
551
552             DrawPiece(sq);
553         }
554     }
555
556     for (sq = 0; sq < NO_SQUARES; sq++)
557         Mvboard[sq] = ((board[sq] != Stboard[sq]) ? 10 : 0);
558
559     GameCnt = 0;
560     Game50 = 1;
561     ZeroRPT();
562     Sdepth = 0;
563     InitializeStats();
564     Curses_ClearScreen();
565     Curses_UpdateDisplay(0, 0, 1, 0);
566 }
567
568
569 static void 
570 UpdateCatched()
571 {
572     short side;
573
574     for (side = black; side <= white; side++)
575     { 
576         short x, y, piece, cside, k;
577
578         cside = flag.reverse ? (side ^ 1) : side;
579         x = x0[cside];
580         y = y0[cside];
581         k = 0;
582
583         for (piece = pawn; piece <= king; piece++)
584         {
585             short n;
586
587             if ((n = Captured[side][piece]))
588             {
589                 gotoXY(x, y); 
590                 printw("%i%c", n, pxx[piece]);
591
592                 if (cside == black) 
593                     y--; 
594                 else 
595                     y++;
596             }
597             else
598             {
599                 k++;
600             }
601         }
602
603         while (k)
604         {
605             k--;
606             gotoXY(x, y);
607             printw("  ");
608
609             if (cside == black) 
610                 y--; 
611             else 
612                 y++;
613         }
614     }
615
616     refresh();
617 }
618
619
620 void
621 Curses_SearchStartStuff(short side)
622 {
623     short i;
624
625     signal(SIGINT, Curses_TerminateSearch);
626     signal(SIGQUIT, Curses_TerminateSearch);
627
628     for (i = 4; i < 14; i++)                      /* CHECKME */
629     {
630         gotoXY(TAB, i);
631         ClearEoln();
632     }
633 }
634
635
636 void
637 Curses_OutputMove(void)
638 {
639
640     Curses_UpdateDisplay(root->f, root->t, 0, (short) root->flags);
641     gotoXY(TAB, 16);
642
643     if (flag.illegal) 
644     {
645         printw("Illegal position.");
646         return;
647     }
648
649     printw("My move is: %5s", mvstr[0]);
650
651     if (flag.beep)
652         putchar(7);
653
654     ClearEoln();
655
656     gotoXY(TAB, 18);
657
658     if (root->flags & draw)
659         printw("Drawn game!");
660     else if (root->score == -(SCORE_LIMIT + 999))
661         printw("Opponent mates!");
662     else if (root->score == SCORE_LIMIT + 998)
663         printw("Computer mates!");
664 #ifdef VERYBUGGY
665     else if (root->score < -SCORE_LIMIT)
666         printw("Opp: mate in %d!", SCORE_LIMIT + 999 + root->score - 1);
667     else if (root->score > SCORE_LIMIT)
668         printw("Comp: mate in %d!", SCORE_LIMIT + 998 - root->score - 1);
669 #endif /* VERYBUGGY */
670
671     ClearEoln();
672
673     if (flag.post)
674     {
675         short h, l, t;
676
677         h = TREE;
678         l = 0;
679         t = TREE >> 1;
680
681         while (l != t)
682         {
683             if (Tree[t].f || Tree[t].t)
684                 l = t;
685             else
686                 h = t;
687
688             t = (l + h) >> 1;
689         }
690
691         ShowNodeCnt(NodeCnt);
692         gotoXY(TAB, 23);
693         printw("Max Tree = %5d", t);
694         ClearEoln();
695     }
696
697     Curses_ShowSidetoMove();
698 }
699
700
701 void
702 Curses_UpdateClocks(void)
703 {
704     short m, s;
705     long dt;
706
707     if (TCflag)
708     {
709         m = (short) ((dt = (TimeControl.clock[player] - et)) / 6000);
710         s = (short) ((dt - 6000 * (long) m) / 100);
711     }
712     else
713     {
714         m = (short) ((dt = et) / 6000);
715         s = (short) (et - 6000 * (long) m) / 100;
716     }
717
718     if (m < 0)
719         m = 0;
720
721     if (s < 0)
722         s = 0;
723
724     if (player == black)
725         gotoXY(20, (flag.reverse) ? 2 : 23);
726     else
727         gotoXY(20, (flag.reverse) ? 23 : 2);
728
729     /* printw("%d:%02d %ld  ", m, s, dt); */
730     printw("%d:%02d  ", m, s); 
731
732     if (flag.post)
733         ShowNodeCnt(NodeCnt);
734
735     refresh();
736 }
737
738
739 static void
740 DrawPiece(short sq)
741 {
742     char y;
743     char piece, l, r, p; 
744
745     if (color[sq] == neutral)
746     {
747         l = r = ' ';
748     }
749     else if (flag.reverse ^ (color[sq] == black))
750     {
751         l = '/';
752         r = '\\';
753     } 
754     else
755     {
756         l = '\\', r = '/';
757     }
758
759     piece = board[sq];
760
761     if (is_promoted[(int)piece])
762     {
763         p = '+';
764         y = pxx[unpromoted[(int)piece]];
765     } 
766     else
767     {
768         p = ' ';
769         y = pxx[(int)piece];
770     }
771
772     gotoXY(8 + 5 * VIR_C(sq), 4 + 2 * ((NO_ROWS - 1) - VIR_R(sq)));
773     printw("%c%c%c%c", l, p, y, r);
774 }
775
776
777 /*
778  * Curses_ShowPostnValue(): must have called ExaminePosition() first
779  */
780 void
781 Curses_ShowPostnValue(short sq)
782 {
783     short score;
784
785     gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq))); /* CHECKME */
786     score = ScorePosition(color[sq]);
787
788     if (color[sq] != neutral)
789 #if defined SAVE_SVALUE
790     {
791         printw("??? ");
792     }
793 #else
794     {
795         printw("%3d ", svalue[sq]);
796     }
797 #endif
798     else
799     {
800         printw("   ");
801     }
802 }
803
804
805 void
806 Curses_ShowPostnValues(void)
807 {
808     short sq, score;
809
810     ExaminePosition(opponent);
811
812     for (sq = 0; sq < NO_SQUARES; sq++)
813         Curses_ShowPostnValue(sq);
814
815     score = ScorePosition(opponent);
816     gotoXY(TAB, 5);
817     printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
818            mtl[computer], pscore[computer], GameType[computer],
819            mtl[opponent], pscore[opponent], GameType[opponent]);
820
821     ClearEoln();
822 }
823
824
825 void
826 Curses_UpdateDisplay(short f, short t, short redraw, short isspec)
827 {
828     short i, sq, z;
829     int j;
830
831     if (redraw)
832     {
833         ShowHeader();
834         ShowPlayers();
835
836         i = 2;
837         gotoXY(3, ++i);
838
839         printw("    +");
840         for (j=0; j<NO_COLS; j++)
841             printw("----+");
842
843         while (i <= 1 + 2*NO_ROWS)
844         {
845             gotoXY(1, ++i);
846
847             if (flag.reverse)
848                 z = (i / 2) - 1;
849             else
850                 z = NO_ROWS + 2 - ((i + 1) / 2);
851
852             printw("    %c |", ROW_NAME(z+1));
853             for (j=0; j<NO_COLS; j++)
854                 printw("    |");
855
856             gotoXY(3, ++i);
857
858             if (i < 2 + 2*NO_ROWS)
859             {
860                 printw("    +");
861                 for (j=0; j<NO_COLS; j++)
862                     printw("----+");
863             }
864         }
865
866         printw("    +");
867         for (j=0; j<NO_COLS; j++)
868             printw("----+");
869
870         gotoXY(3, 4 + 2*NO_ROWS);
871         printw("    ");
872
873 #ifndef MINISHOGI
874         if (flag.reverse)
875             printw("  1    2    3    4    5    6    7    8    9");
876         else
877             printw("  9    8    7    6    5    4    3    2    1");
878 #else
879         if (flag.reverse)
880             printw("  1    2    3    4    5");
881         else
882             printw("  1    2    3    4    5");
883 #endif
884
885         for (sq = 0; sq < NO_SQUARES; sq++)
886             DrawPiece(sq);
887     }
888     else /* not redraw */
889     {
890         if (f < NO_SQUARES)
891             DrawPiece(f);
892
893         DrawPiece(t & 0x7f);
894     }
895
896     if ((isspec & capture) || (isspec & dropmask) || redraw)
897     {
898         short side;
899
900         for (side = black; side <= white; side++)
901         {
902             short x, y, piece, cside, k;
903             cside = flag.reverse ? (side ^ 1) : side;
904             x = x0[cside];
905             y = y0[cside];
906             k = 0;
907
908             for (piece = pawn; piece <= king; piece++)
909             {
910                 short n;
911
912                 if ((n = Captured[side][piece]))
913                 {
914                     gotoXY(x, y); 
915                     printw("%i%c", n, pxx[piece]);
916
917                     if (cside == black) y--; else y++;
918                 }
919                 else
920                 {
921                     k++;
922                 }
923             }
924
925             while (k)
926             {
927                 k--;
928                 gotoXY(x, y);
929                 printw("  ");
930
931                 if (cside == black) 
932                     y--;
933                 else 
934                     y++;
935             }
936         }
937     }
938
939     refresh();
940 }
941
942
943 void
944 Curses_ChangeAlphaWindow(void)
945 {
946     Curses_ShowMessage("WAwindow = ");
947     FLUSH_SCANW("%hd", &WAwindow);
948     Curses_ShowMessage("BAwindow = ");
949     FLUSH_SCANW("%hd", &BAwindow);
950 }
951
952
953 void
954 Curses_ChangeBetaWindow(void)
955 {
956     Curses_ShowMessage("WBwindow = ");
957     FLUSH_SCANW("%hd", &WBwindow);
958     Curses_ShowMessage("BBwindow = ");
959     FLUSH_SCANW("%hd", &BBwindow);
960 }
961
962
963 void
964 Curses_GiveHint(void)
965 {
966     char s[40];
967
968     if (hint)
969     {
970         algbr((short) (hint >> 8), (short) (hint & 0xFF), false);
971         strcpy(s, "try ");
972         strcat(s, mvstr[0]);
973         Curses_ShowMessage(s);
974     }
975     else
976     {
977         Curses_ShowMessage("I have no idea.\n");
978     }
979 }
980
981
982 void
983 Curses_ChangeSearchDepth(void)
984 {
985     Curses_ShowMessage("depth = ");
986     FLUSH_SCANW("%hd", &MaxSearchDepth);
987     TCflag = !(MaxSearchDepth > 0);
988 }
989
990
991 void
992 Curses_ChangeHashDepth(void)
993 {
994     Curses_ShowMessage("hashdepth = ");
995     FLUSH_SCANW("%hd", &HashDepth);
996     Curses_ShowMessage("MoveLimit = ");
997     FLUSH_SCANW("%hd", &HashMoveLimit);
998 }
999
1000
1001 void
1002 Curses_SetContempt(void)
1003 {
1004     Curses_ShowMessage("contempt = ");
1005     FLUSH_SCANW("%hd", &contempt);
1006 }
1007
1008
1009 void
1010 Curses_ChangeXwindow(void)
1011 {
1012     Curses_ShowMessage("xwndw= ");
1013     FLUSH_SCANW("%hd", &xwndw);
1014 }
1015
1016
1017 void
1018 Curses_SelectLevel(char *sx)
1019 {
1020     int item;
1021
1022     Curses_ClearScreen();
1023     gotoXY(32, 2);
1024     printw("GNU Shogi %s", PACKAGE_VERSION);
1025     gotoXY(20, 4);
1026     printw(" 1.   40 moves in   5 minutes");
1027     gotoXY(20, 5);
1028     printw(" 2.   40 moves in  15 minutes");
1029     gotoXY(20, 6);
1030     printw(" 3.   40 moves in  30 minutes");
1031     gotoXY(20, 7);
1032     printw(" 4.  all moves in  15 minutes");
1033     gotoXY(20, 8);
1034     printw(" 5.  all moves in  30 minutes");
1035     gotoXY(20, 9);
1036     printw(" 6.  all moves in  15 minutes, 30 seconds fischer clock");
1037     gotoXY(20, 10);
1038     printw(" 7.  all moves in  30 minutes, 30 seconds fischer clock");
1039     gotoXY(20, 11);
1040     printw(" 8.    1 move  in   1 minute");
1041     gotoXY(20, 12);
1042     printw(" 9.    1 move  in  15 minutes");
1043     gotoXY(20, 13);
1044     printw("10.    1 move  in  30 minutes");
1045
1046     OperatorTime = 0;
1047     TCmoves = 40;
1048     TCminutes = 5;
1049     TCseconds = 0;
1050
1051     gotoXY(20, 17);
1052     printw("Enter Level: ");
1053     refresh();
1054     FLUSH_SCANW("%d", &item);
1055
1056     switch(item)
1057     {
1058     case 1:
1059         TCmoves = 40;
1060         TCminutes = 5;
1061         break;
1062
1063     case 2:
1064         TCmoves = 40;
1065         TCminutes = 15;
1066         break;
1067
1068     case 3:
1069         TCmoves = 40;
1070         TCminutes = 30;
1071         break;
1072
1073     case 4:
1074         TCmoves = 80;
1075         TCminutes = 15;
1076         flag.gamein = true;
1077         break;
1078
1079     case 5:
1080         TCmoves = 80;
1081         TCminutes = 30;
1082         flag.gamein = true;
1083         break;
1084
1085     case 6:
1086         TCmoves = 80;
1087         TCminutes = 15;
1088         TCadd = 3000;
1089         flag.gamein = true;
1090         break;
1091
1092     case 7:
1093         TCmoves = 80;
1094         TCminutes = 30;
1095         TCadd = 3000;
1096         break;
1097
1098     case 8:
1099         TCmoves = 1;
1100         TCminutes = 1;
1101         flag.onemove = true;
1102         break;
1103
1104     case 9:
1105         TCmoves = 1;
1106         TCminutes = 15;
1107         flag.onemove = true;
1108         break;
1109
1110     case 10:
1111         TCmoves = 1;
1112         TCminutes = 30;
1113         flag.onemove = true;
1114         break;
1115     }
1116
1117     TCflag = (TCmoves > 0);
1118
1119     TimeControl.clock[black] = TimeControl.clock[white] = 0; 
1120
1121     SetTimeControl();
1122     Curses_ClearScreen();
1123     Curses_UpdateDisplay(0, 0, 1, 0);
1124 }
1125
1126
1127 void
1128 Curses_DoDebug(void)
1129 {
1130     short c, p, sq, tp, tc, tsq, score;
1131     char s[40];
1132
1133     ExaminePosition(opponent);
1134     Curses_ShowMessage("Enter piece: ");
1135     FLUSH_SCANW("%s", s);
1136     c = neutral;
1137
1138     if ((s[0] == 'b') || (s[0] == 'B'))
1139         c = black;
1140
1141     if ((s[0] == 'w') || (s[0] == 'W'))
1142         c = white;
1143
1144     for (p = king; p > no_piece; p--)
1145     {
1146         if ((s[1] == pxx[p]) || (s[1] == qxx[p]))
1147             break;
1148     }
1149
1150     for (sq = 0; sq < NO_SQUARES; sq++)
1151     {
1152         tp = board[sq];
1153         tc = color[sq];
1154         board[sq] = p;
1155         color[sq] = c;
1156         tsq = PieceList[c][1];
1157         PieceList[c][1] = sq;
1158         Curses_ShowPostnValue(sq);
1159         PieceList[c][1] = tsq;
1160         board[sq] = tp;
1161         color[sq] = tc;
1162     }
1163
1164     score = ScorePosition(opponent);
1165     gotoXY(TAB, 5);
1166     printw("S%d m%d ps%d gt%c m%d ps%d gt%c", score,
1167            mtl[computer], pscore[computer], GameType[computer],
1168            mtl[opponent], pscore[opponent], GameType[opponent]);
1169
1170     ClearEoln();
1171 }
1172
1173
1174 void
1175 Curses_DoTable(short table[NO_SQUARES])
1176 {
1177     short  sq;
1178     ExaminePosition(opponent);
1179
1180     for (sq = 0; sq < NO_SQUARES; sq++)
1181     {
1182         gotoXY(4 + 5 * VIR_C(sq), 5 + 2 * (7 - VIR_R(sq)));
1183         printw("%3d ", table[sq]);
1184     }
1185
1186
1187
1188 void
1189 Curses_PollForInput(void)
1190 {
1191     int  i;
1192     int  nchar;
1193
1194     if ((i = ioctl((int) 0, FIONREAD, &nchar)))
1195     {
1196         perror("FIONREAD");
1197         fprintf(stderr,
1198                 "You probably have a non-ANSI <ioctl.h>; "
1199                 "see README. %d %d %x\n",
1200                 i, errno, FIONREAD);
1201         exit(1);
1202     }
1203
1204     if (nchar)
1205     {
1206         if (!flag.timeout)
1207             flag.back = true;
1208
1209         flag.bothsides = false;
1210     }
1211 }
1212
1213
1214 void
1215 Curses_SetupBoard(void)
1216 {
1217     Curses_ShowMessage("'setup' command is not supported in Cursesmode");
1218 }