4 * ----------------------------------------------------------------------
6 * Copyright (c) 2012 Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * This file is part of GNU SHOGI.
12 * GNU Shogi is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License,
15 * or (at your option) any later version.
17 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with GNU Shogi; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
25 * ----------------------------------------------------------------------
31 #include <sys/param.h>
32 #include <sys/types.h>
38 unsigned short MV
[MAXDEPTH
];
44 extern short pscore
[];
47 /****************************************
48 * Trivial output functions.
49 ****************************************/
54 if (!barebones
&& !XSHOGI
)
59 /* FIXME: change to ShowPrompt? */
63 if (!barebones
&& !XSHOGI
)
65 /* printf("\nYour move is? "); */
72 Raw_ShowCurrentMove(short pnt
, short f
, short t
)
78 Raw_ShowDepth(char ch
)
80 if (!barebones
&& !XSHOGI
)
82 printf(CP
[53], Sdepth
, ch
); /* Depth = %d%c */
89 Raw_ShowGameType(void)
92 printf("%c vs. %c\n", GameType
[black
], GameType
[white
]);
97 Raw_ShowLine(unsigned short *bstline
)
101 for (i
= 1; bstline
[i
] > 0; i
++)
103 if ((i
> 1) && (i
% 8 == 1))
106 algbr((short)(bstline
[i
] >> 8), (short)(bstline
[i
] & 0xFF), false);
107 printf("%5s ", mvstr
[0]);
115 Raw_ShowMessage(char *s
)
123 Raw_ShowPatternCount(short side
, short n
)
126 printf("%s matches %d pattern(s)\n", ColorStr
[side
], n
);
131 Raw_ShowResponseTime(void)
137 Raw_ShowResults(short score
, unsigned short *bstline
, char ch
)
139 if (flag
.post
&& !XSHOGI
)
142 printf("%2d%c %6d %4ld %8ld ",
143 Sdepth
, ch
, score
, et
/ 100, NodeCnt
);
144 Raw_ShowLine(bstline
);
150 Raw_ShowSidetoMove(void)
158 printf("stage = %d\n", stage
);
159 printf("balance[black] = %d balance[white] = %d\n",
160 balance
[black
], balance
[white
]);
163 /****************************************
164 * End of trivial output routines.
165 ****************************************/
175 #ifdef HAVE_SETLINEBUF
179 setvbuf(stdout
, NULL
, _IOLBF
, BUFSIZ
);
181 # error "Need setlinebuf() or setvbuf() to compile gnushogi!"
184 printf("GNU Shogi %sp%s\n", version
, patchlevel
);
189 if (!TCflag
&& (MaxResponseTime
== 0))
190 MaxResponseTime
= 15L * 100L;
199 /* CHECKME: what purpose does this next statement serve? */
200 signal(SIGTERM
, SIG_IGN
);
212 Raw_ShowMessage(CP
[31]); /* Abort? */
215 if (strcmp(s
, CP
[210]) == 0) /* yes */
222 Raw_TerminateSearch(int sig
)
224 #ifdef INTERRUPT_TEST
225 ElapsedTime(INIT_INTERRUPT_MODE
);
229 flag
.back
= true; /* previous: flag.timeout = true; */
231 flag
.bothsides
= false;
240 /* printf("SHOGI command summary\n"); */
242 printf("----------------------------------"
243 "------------------------------\n");
244 /* printf("7g7f move from 7g to 7f quit
247 /* printf("S6h move silver to 6h beep
248 * turn %s\n", (flag.beep) ? "off" : "on"); */
249 printf(CP
[86], (flag
.beep
) ? CP
[92] : CP
[93]);
250 /* printf("2d2c+ move from 2d to 2c and promote\n"); */
251 printf(CP
[128], (flag
.material
) ? CP
[92] : CP
[93]);
252 /* printf("P*5e drop pawn to 5e easy
253 * turn %s\n", (flag.easy) ? "off" : "on"); */
254 printf(CP
[173], (flag
.easy
) ? CP
[92] : CP
[93]);
256 * turn %s\n", (flag.hash) ? "off" : "on"); */
257 printf(CP
[174], (flag
.hash
) ? CP
[92] : CP
[93]);
258 /* printf("bd redraw board reverse
259 * board display\n"); */
261 /* printf("list game to shogi.lst book
262 * turn %s used %d of %d\n", (Book) ? "off" : "on", bookcount); */
263 printf(CP
[170], (Book
) ? CP
[92] : CP
[93], bookcount
, booksize
);
264 /* printf("undo undo last ply remove
265 * take back a move\n"); */
267 /* printf("edit edit board force
268 * enter game moves\n"); */
270 /* printf("switch sides with computer both
271 * computer match\n"); */
273 /* printf("black computer plays black white
274 * computer plays white\n"); */
276 /* printf("depth set search depth clock
277 * set time control\n"); */
279 /* printf("post principle variation hint
280 * suggest a move\n"); */
282 /* printf("save game to file get
283 * game from file\n"); */
285 printf("xsave pos. to xshogi file xget"
286 " pos. from xshogi file\n");
287 /* printf("random randomize play new
288 * start new game\n"); */
290 printf("--------------------------------"
291 "--------------------------------\n");
292 /* printf("Computer: %-12s Opponent: %s\n", */
294 ColorStr
[computer
], ColorStr
[opponent
]);
295 /* printf("Depth: %-12d Response time: %d sec\n", */
297 MaxSearchDepth
, MaxResponseTime
/100);
298 /* printf("Random: %-12s Easy mode: %s\n", */
300 (dither
) ? CP
[93] : CP
[92], (flag
.easy
) ? CP
[93] : CP
[92]);
301 /* printf("Beep: %-12s Transposition file: %s\n", */
303 (flag
.beep
) ? CP
[93] : CP
[92], (flag
.hash
) ? CP
[93] : CP
[92]);
304 /* printf("Tsume: %-12s Force: %s\n")*/
306 (flag
.tsume
) ? CP
[93] : CP
[92], (flag
.force
) ? CP
[93] : CP
[92]);
307 /* printf("Time Control %s %d moves %d seconds %d opr %d
308 * depth\n", (TCflag) ? "ON" : "OFF", */
310 (TCflag
) ? CP
[93] : CP
[92],
311 TimeControl
.moves
[black
], TimeControl
.clock
[black
] / 100,
312 TCadd
/100, MaxSearchDepth
);
314 signal(SIGINT
, Raw_TerminateSearch
);
315 signal(SIGQUIT
, Raw_TerminateSearch
);
321 * Set up a board position. Pieces are entered by typing the piece followed
322 * by the location. For example, Nf3 will place a knight on square f3.
328 short a
, r
, c
, sq
, i
, found
;
331 flag
.regularstart
= true;
334 Raw_UpdateDisplay(0, 0, 1, 0);
335 /* printf(". exit to main\n"); */
337 /* printf("# clear board\n"); */
339 /* printf("c change sides\n"); */
341 /* printf("enter piece & location: \n"); */
351 if (s
[0] == CP
[28][0]) /*#*/
353 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
355 board
[sq
] = no_piece
;
362 if (s
[0] == CP
[136][0]) /*c*/
367 for (i
= pawn
; i
<= king
; i
++)
369 if ((s
[0] == pxx
[i
]) || (s
[0] == qxx
[i
]))
386 if ((c
>= 0) && (c
< NO_COLS
) && (r
>= 0) && (r
< NO_ROWS
))
390 board
[sq
] = no_piece
;
392 for (i
= no_piece
; i
<= king
; i
++)
394 if ((s
[0] == pxx
[i
]) || (s
[0] == qxx
[i
]))
397 board
[sq
] = promoted
[i
];
410 while (s
[0] != CP
[29][0]);
412 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
413 Mvboard
[sq
] = ((board
[sq
] != Stboard
[sq
]) ? 10 : 0);
421 Raw_UpdateDisplay(0, 0, 1, 0);
428 * Set up a board position.
429 * Nine lines of nine characters are used to setup the board. 9a-1a is the
430 * first line. White pieces are represented by uppercase characters.
442 fgets(s
, 80, stdin
); /* skip "setup" command */
444 for (r
= NO_ROWS
- 1; r
>= 0; r
--)
448 for (c
= 0; c
<= (NO_COLS
- 1); c
++)
453 board
[sq
] = no_piece
;
455 for (i
= no_piece
; i
<= king
; i
++)
463 else if (ch
== qxx
[i
])
473 for (sq
= 0; sq
< NO_SQUARES
; sq
++)
474 Mvboard
[sq
] = ((board
[sq
] != Stboard
[sq
]) ? 10 : 0);
478 Raw_UpdateDisplay(0, 0, 1, 0);
479 /* printf("Setup successful\n"); */
485 Raw_SearchStartStuff(short side
)
487 signal(SIGINT
, Raw_TerminateSearch
);
488 signal(SIGQUIT
, Raw_TerminateSearch
);
494 ResponseTime
, TimeControl
.clock
[side
]);
505 printf("%s\n", CP
[225]);
509 if (mvstr
[0][0] == '\0')
514 /* add remaining time in milliseconds to xshogi */
515 printf("%d. ... %s %ld\n", ++mycnt1
, mvstr
[0],
516 (TimeControl
.clock
[player
] - et
) * 10);
520 printf("%d. ... %s\n", ++mycnt1
, mvstr
[0]);
524 if ((root
->flags
& draw
) || (root
->score
== -(SCORE_LIMIT
+ 999))
525 || (root
->score
== (SCORE_LIMIT
+ 998)))
538 if (Tree
[t
].f
|| Tree
[t
].t
)
546 /* printf("Nodes %ld Tree %d Eval %ld
547 * Rate %ld RS high %ld low %ld\n", */
548 printf(CP
[89], GenCnt
, NodeCnt
, t
, EvalNodes
,
549 (et
> 100) ? (NodeCnt
/ (et
/ 100)) : 0,
550 EADD
, EGET
, reminus
, replus
);
552 /* printf("Hin/Hout/Coll/Fin/Fout =
553 * %ld/%ld/%ld/%ld/%ld\n", */
555 HashAdd
, HashCnt
, THashCol
, HashCol
, FHashCnt
, FHashAdd
);
558 Raw_UpdateDisplay(root
->f
, root
->t
, 0, root
->flags
);
562 /* printf("My move is: %s\n", mvstr[0]); */
563 printf(CP
[83], mvstr
[0]);
570 if (root
->flags
& draw
)
572 /* printf("Drawn game!\n"); */
575 else if (root
->score
== -(SCORE_LIMIT
+ 999))
577 printf("%s mates!\n", ColorStr
[opponent
]);
579 else if (root
->score
== (SCORE_LIMIT
+ 998))
581 printf("%s mates!\n", ColorStr
[computer
]);
584 else if (!barebones
&& (root
->score
< -SCORE_LIMIT
))
586 printf("%s has a forced mate in %d moves!\n",
587 ColorStr
[opponent
], SCORE_LIMIT
+ 999 + root
->score
- 1);
589 else if (!barebones
&& (root
->score
> SCORE_LIMIT
))
591 printf("%s has a forced mate in %d moves!\n",
592 ColorStr
[computer
], SCORE_LIMIT
+ 998 - root
->score
- 1);
594 #endif /* VERYBUGGY */
599 Raw_UpdateDisplay(short f
, short t
, short redraw
, short isspec
)
604 if (redraw
&& !XSHOGI
)
607 r
= (short)(TimeControl
.clock
[black
] / 6000);
608 c
= (short)((TimeControl
.clock
[black
] % 6000) / 100);
609 l
= (short)(TimeControl
.clock
[white
] / 6000);
610 m
= (short)((TimeControl
.clock
[white
] % 6000) / 100);
611 /* printf("Black %d:%02d White %d:%02d\n", r, c, l, m); */
612 printf(CP
[116], r
, c
, l
, m
);
615 for (r
= (NO_ROWS
- 1); r
>= 0; r
--)
617 for (c
= 0; c
<= (NO_COLS
- 1); c
++)
621 ? locn((NO_ROWS
- 1) - r
, (NO_COLS
- 1) - c
)
623 pc
= (is_promoted
[board
[l
]] ? '+' : ' ');
625 if (color
[l
] == neutral
)
627 else if (color
[l
] == black
)
628 printf("%c%c", pc
, qxx
[board
[l
]]);
630 printf("%c%c", pc
, pxx
[board
[l
]]);
640 for (side
= black
; side
<= white
; side
++)
643 printf((side
== black
)?"black ":"white ");
645 for (piece
= pawn
; piece
<= king
; piece
++)
647 if ((c
= Captured
[side
][piece
]))
648 printf("%i%c ", c
, pxx
[piece
]);
660 Raw_ChangeAlphaWindow(void)
662 printf("WAwindow: ");
663 scanf("%hd", &WAwindow
);
664 printf("BAwindow: ");
665 scanf("%hd", &BAwindow
);
671 Raw_ChangeBetaWindow(void)
673 printf("WBwindow: ");
674 scanf("%hd", &WBwindow
);
675 printf("BBwindow: ");
676 scanf("%hd", &BBwindow
);
686 algbr((short) (hint
>> 8), (short) (hint
& 0xFF), false);
687 printf(CP
[72], mvstr
[0]); /*hint*/
696 Raw_SelectLevel(char *sx
)
699 char T
[NO_SQUARES
+ 1], *p
, *q
;
701 if ((p
= strstr(sx
, CP
[169])) != NULL
)
702 p
+= strlen(CP
[169]);
703 else if ((p
= strstr(sx
, CP
[217])) != NULL
)
704 p
+= strlen(CP
[217]);
710 for (; *p
!= 'X'; *q
++ = *p
++);
714 /* line empty ask for input */
718 fgets(T
, NO_SQUARES
+ 1, stdin
);
722 /* skip blackspace */
723 for (p
= T
; *p
== ' '; p
++) ;
725 /* could be moves or a fischer clock */
728 /* its a fischer clock game */
730 TCminutes
= (short)strtol(p
, &q
, 10);
731 TCadd
= (short)strtol(q
, NULL
, 10) *100;
739 TCmoves
= (short)strtol(p
, &q
, 10);
740 TCminutes
= (short)strtol(q
, &q
, 10);
743 TCseconds
= (short)strtol(q
+ 1, (char **) NULL
, 10);
749 scanf("%hd", &OperatorTime
);
755 MaxResponseTime
= TCminutes
*60L * 100L + TCseconds
* 100L;
756 TCminutes
= TCseconds
= 0;
765 TimeControl
.clock
[black
] = TimeControl
.clock
[white
] = 0;
770 printf("Clocks: %ld %ld\n",
771 TimeControl
.clock
[black
] * 10,
772 TimeControl
.clock
[white
] * 10);
780 Raw_ChangeSearchDepth(void)
783 scanf("%hd", &MaxSearchDepth
);
784 TCflag
= !(MaxSearchDepth
> 0);
791 Raw_ChangeHashDepth(void)
793 printf("hashdepth = ");
794 scanf("%hd", &HashDepth
);
795 printf("MoveLimit = ");
796 scanf("%hd", &HashMoveLimit
);
802 Raw_SetContempt(void)
804 printf("contempt = ");
805 scanf("%hd", &contempt
);
811 Raw_ChangeXwindow(void)
814 scanf("%hd", &xwndw
);
819 * Raw_ShowPostnValue(short sq)
820 * must have called ExaminePosition() first
824 Raw_ShowPostnValue(short sq
)
827 score
= ScorePosition(color
[sq
]);
829 if (color
[sq
] != neutral
)
831 #if defined SAVE_SVALUE
832 printf("???%c ", (color
[sq
] == white
)?'b':'w');
834 printf("%3d%c ", svalue
[sq
], (color
[sq
] == white
)?'b':'w');
848 short c
, p
, sq
, tp
, tc
, tsq
, score
, j
, k
;
851 ExaminePosition(opponent
);
852 Raw_ShowMessage(CP
[65]);
856 if ((s
[0] == CP
[9][0]) || (s
[0] == CP
[9][1])) /* w W */
859 if ((s
[0] == CP
[9][2]) || (s
[0] == CP
[9][3])) /* b B */
862 for (p
= king
; p
> no_piece
; p
--)
864 if ((s
[1] == pxx
[p
]) || (s
[1] == qxx
[p
]))
870 for (j
= (NO_ROWS
- 1); j
>= 0; j
--)
872 for (k
= 0; k
< (NO_COLS
); k
++)
874 sq
= j
*(NO_COLS
) + k
;
879 tsq
= PieceList
[c
][1];
880 PieceList
[c
][1] = sq
;
881 Raw_ShowPostnValue(sq
);
882 PieceList
[c
][1] = tsq
;
891 score
= ScorePosition(opponent
);
893 for (j
= (NO_ROWS
- 1); j
>= 0; j
--)
895 for (k
= 0; k
< (NO_COLS
); k
++)
897 sq
= j
*(NO_COLS
) + k
;
899 if (color
[sq
] != neutral
)
901 #if defined SAVE_SVALUE
902 printf("%?????%c ", (color
[sq
] == white
)?'b':'w');
904 printf("%5d%c ", svalue
[sq
], (color
[sq
] == white
)?'b':'w');
916 printf("stage = %d\n", stage
);
917 printf(CP
[103], score
,
918 mtl
[computer
], pscore
[computer
], GameType
[computer
],
919 mtl
[opponent
], pscore
[opponent
], GameType
[opponent
]);
925 Raw_DoTable(short table
[NO_SQUARES
])
928 ExaminePosition(opponent
);
930 for (j
= (NO_ROWS
- 1); j
>= 0; j
--)
932 for (k
= 0; k
< NO_COLS
; k
++)
934 sq
= j
*(NO_ROWS
) + k
;
935 printf("%3d ", table
[sq
]);
945 Raw_ShowPostnValues(void)
947 short sq
, score
, j
, k
;
948 ExaminePosition(opponent
);
950 for (j
= (NO_ROWS
- 1); j
>= 0; j
--)
952 for (k
= 0; k
< NO_COLS
; k
++)
954 sq
= j
* NO_COLS
+ k
;
955 Raw_ShowPostnValue(sq
);
961 score
= ScorePosition(opponent
);
962 printf(CP
[103], score
,
963 mtl
[computer
], pscore
[computer
], GameType
[computer
],
964 mtl
[opponent
], pscore
[opponent
], GameType
[opponent
]);
965 printf("\nhung black %d hung white %d\n", hung
[black
], hung
[white
]);