4 * Common display routines for GNU Shogi.
6 * ----------------------------------------------------------------------
7 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
8 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
10 * GNU SHOGI is based on GNU CHESS
12 * Copyright (c) 1988, 1989, 1990 John Stanback
13 * Copyright (c) 1992 Free Software Foundation
15 * This file is part of GNU SHOGI.
17 * GNU Shogi is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 3 of the License,
20 * or (at your option) any later version.
22 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License along
28 * with GNU Shogi; see the file COPYING. If not, see
29 * <http://www.gnu.org/licenses/>.
30 * ----------------------------------------------------------------------
34 #if defined HAVE_GETTIMEOFDAY
41 #include <sys/param.h>
42 #include <sys/types.h>
50 int InBackground
= false;
56 movealgbr(short m
, char *s
)
59 short piece
= 0, flag
= 0;
72 piece
= f
- NO_SQUARES
;
74 if (piece
> NO_PIECES
)
77 flag
= (dropmask
| piece
);
124 #endif /* BOOKTEST */
130 * Generate move strings in different formats.
133 * - f piece to be moved
134 * - 0 < f < NO_SQUARES source square
135 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
136 * - t & 0x7f target square
137 * - t & 0x80 promotion flag
138 * - flag FIXME: must be zero ?
145 algbr(short f
, short t
, short flag
)
151 piece
= f
- NO_SQUARES
;
153 if (f
> (NO_SQUARES
+ NO_PIECES
))
156 flag
= (dropmask
| piece
);
165 if ((f
== t
) && ((f
!= 0) || (t
!= 0)))
170 printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t
, flag
);
172 printw("error in algbr: FROM=TO=%d, flag=0x%4x\n", t
, flag
);
175 mvstr
[0][0] = mvstr
[1][0] = mvstr
[2][0] = mvstr
[3][0] = '\0';
177 else if ((flag
& dropmask
) != 0)
179 short piece
= flag
& pmask
;
181 mvstr
[0][0] = pxx
[piece
];
183 mvstr
[0][2] = cxx
[column(t
)];
184 mvstr
[0][3] = rxx
[row(t
)];
186 strcpy(mvstr
[1], mvstr
[0]);
187 strcpy(mvstr
[2], mvstr
[0]);
188 strcpy(mvstr
[3], mvstr
[0]);
190 else if ((f
!= 0) || (t
!= 0))
192 /* algebraic notation */
193 mvstr
[0][0] = cxx
[column(f
)];
194 mvstr
[0][1] = rxx
[row(f
)];
195 mvstr
[0][2] = cxx
[column(t
)];
196 mvstr
[0][3] = rxx
[row(t
)];
197 mvstr
[0][4] = mvstr
[3][0] = '\0';
198 mvstr
[1][0] = pxx
[board
[f
]];
200 mvstr
[2][0] = mvstr
[1][0];
201 mvstr
[2][1] = mvstr
[0][1];
203 mvstr
[2][2] = mvstr
[1][1] = mvstr
[0][2]; /* to column */
204 mvstr
[2][3] = mvstr
[1][2] = mvstr
[0][3]; /* to row */
205 mvstr
[2][4] = mvstr
[1][3] = '\0';
206 strcpy(mvstr
[3], mvstr
[2]);
207 mvstr
[3][1] = mvstr
[0][0];
211 strcat(mvstr
[0], "+");
212 strcat(mvstr
[1], "+");
213 strcat(mvstr
[2], "+");
214 strcat(mvstr
[3], "+");
219 mvstr
[0][0] = mvstr
[1][0] = mvstr
[2][0] = mvstr
[3][0] = '\0';
226 * Compare the string 's' to the list of legal moves available for the
227 * opponent. If a match is found, make the move on the board.
231 VerifyMove(char *s
, VerifyMove_mode iop
, unsigned short *mv
)
233 static short pnt
, tempb
, tempc
, tempsf
, tempst
, cnt
;
234 static struct leaf xnode
;
236 short i
, l
, local_flags
;
239 /* check and remove quality flags */
240 for (i
= local_flags
= 0, l
= strlen(s
); i
< l
; i
++)
245 local_flags
|= badmove
;
250 local_flags
|= goodmove
;
256 local_flags
|= difficult
;
265 if (iop
== UNMAKE_MODE
)
267 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
273 if (iop
== VERIFY_AND_MAKE_MODE
)
274 generate_move_flags
= true;
276 MoveList(opponent
, 2, -1, true);
277 generate_move_flags
= false;
280 while (pnt
< TrPnt
[3])
283 algbr(node
->f
, node
->t
, (short) node
->flags
);
285 if ((strcmp(s
, mvstr
[0]) == 0)
286 || (strcmp(s
, mvstr
[1]) == 0)
287 || (strcmp(s
, mvstr
[2]) == 0)
288 || (strcmp(s
, mvstr
[3]) == 0))
295 if ((cnt
== 1) && (xnode
.score
> DONTUSE
))
299 MakeMove(opponent
, &xnode
, &tempb
, &tempc
,
300 &tempsf
, &tempst
, &INCscore
);
302 if (SqAttacked(PieceList
[opponent
][0], computer
, &blocked
))
304 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
308 /* Illegal move in check */
309 printf(CP
[77], mvstr
[0]);
314 /* Illegal move in check */
315 sprintf(buffer
, CP
[77], s
);
323 if (iop
== VERIFY_AND_TRY_MODE
)
326 UpdateDisplay(xnode
.f
, xnode
.t
, 0, (short) xnode
.flags
);
327 GameList
[GameCnt
].depth
= GameList
[GameCnt
].score
= 0;
328 GameList
[GameCnt
].nodes
= 0;
329 ElapsedTime(COMPUTE_AND_INIT_MODE
);
330 GameList
[GameCnt
].time
= (short) (et
+ 50)/100;
331 GameList
[GameCnt
].flags
|= local_flags
;
335 TimeControl
.clock
[opponent
] -= et
;
336 timeopp
[oppptr
] = et
;
337 --TimeControl
.moves
[opponent
];
340 *mv
= (xnode
.f
<< 8) | xnode
.t
;
341 algbr(xnode
.f
, xnode
.t
, false);
343 /* in force mode, check for mate conditions */
346 if (IsCheckmate(opponent
^ 1, -1, -1))
350 sprintf(buf
, "%s mates!\n", ColorStr
[opponent
]);
368 sprintf(buffer
, CP
[76], s
);
372 if (!barebones
&& (cnt
> 1))
374 sprintf(buffer
, CP
[32], s
);
384 parser(char *f
, int side
, short *fpiece
)
392 for (i
= 1, *fpiece
= no_piece
; i
< NO_PIECES
; i
++)
394 if (f
[0] == pxx
[i
] || f
[0] == qxx
[i
])
396 *fpiece
= (p
? promoted
[i
] : unpromoted
[i
]);
401 if (f
[1] == '*' || f
[1] == '\'')
406 return ((NO_SQUARES
+ *fpiece
) << 8) | locn(r2
, c2
);
414 p
= (f
[5] == '+') ? 0x80 : 0;
416 return (locn(r1
, c1
) << 8) | locn(r2
, c2
) | p
;
424 while (*InPtr
!= ' ')
427 while (*InPtr
== ' ')
436 while (*InPtr
== ' ')
453 strcpy(fname
, savefile
);
457 /* Enter file name */
472 if (fname
[0] == '\0')
473 strcpy(fname
, CP
[137]);
475 if ((fd
= fopen(fname
, "r")) != NULL
)
478 fgets(fname
, 256, fd
);
479 computer
= opponent
= black
;
488 /* FIXME: write a skipn() function so that we can get
489 * 3 skips by doing skipn(3) */
493 Game50
= atoi(InPtr
);
495 flag
.force
= (*InPtr
== 'f');
496 fgets(fname
, 256, fd
); /* empty */
497 fgets(fname
, 256, fd
);
500 TCflag
= atoi(InPtr
);
504 OperatorTime
= atoi(InPtr
);
505 fgets(fname
, 256, fd
);
508 TimeControl
.clock
[black
] = atol(InPtr
);
511 TimeControl
.moves
[black
] = atoi(InPtr
);
512 fgets(fname
, 256, fd
);
515 TimeControl
.clock
[white
] = atol(InPtr
);
518 TimeControl
.moves
[white
] = atoi(InPtr
);
519 fgets(fname
, 256, fd
); /* empty */
521 for (i
= NO_ROWS
- 1; i
> -1; i
--)
523 fgets(fname
, 256, fd
);
527 for (j
= 0; j
< NO_COLS
; j
++)
529 sq
= i
* NO_COLS
+ j
;
535 board
[sq
] = no_piece
;
540 for (c
= 0; c
< NO_PIECES
; c
++)
545 board
[sq
] = promoted
[c
];
547 board
[sq
] = unpromoted
[c
];
553 for (c
= 0; c
< NO_PIECES
; c
++)
558 board
[sq
] = promoted
[c
];
560 board
[sq
] = unpromoted
[c
];
568 Mvboard
[sq
] = atoi(InPtr
);
573 fgets(fname
, 256, fd
); /* empty */
574 fgets(fname
, 256, fd
); /* 9 8 7 ... */
575 fgets(fname
, 256, fd
); /* empty */
576 fgets(fname
, 256, fd
); /* p l n ... */
579 for (side
= 0; side
<= 1; side
++)
581 fgets(fname
, 256, fd
);
585 Captured
[side
][pawn
] = atoi(InPtr
);
587 Captured
[side
][lance
] = atoi(InPtr
);
589 Captured
[side
][knight
] = atoi(InPtr
);
591 Captured
[side
][silver
] = atoi(InPtr
);
593 Captured
[side
][gold
] = atoi(InPtr
);
595 Captured
[side
][bishop
] = atoi(InPtr
);
597 Captured
[side
][rook
] = atoi(InPtr
);
599 Captured
[side
][king
] = atoi(InPtr
);
603 flag
.regularstart
= true;
605 fgets(fname
, 256, fd
); /* empty */
606 fgets(fname
, 256, fd
); /* move score ... */
608 while (fgets(fname
, 256, fd
))
617 g
= &GameList
[GameCnt
];
618 g
->gmove
= parser(InPtr
, side
, &g
->fpiece
);
620 g
->score
= atoi(InPtr
);
622 g
->depth
= atoi(InPtr
);
624 g
->nodes
= atol(InPtr
);
626 g
->time
= atol(InPtr
);
628 g
->flags
= c
= atoi(InPtr
);
630 g
->hashkey
= strtol(InPtr
, (char **) NULL
, 16);
632 g
->hashbd
= strtol(InPtr
, (char **) NULL
, 16);
640 for (piece
= no_piece
, i
= 0; i
< NO_PIECES
; i
++)
642 if (pxx
[i
] == *InPtr
)
650 g
->color
= ((*InPtr
== CP
[119][0]) ? white
: black
);
652 g
->piece
= (*InPtr
== '+'
654 : unpromoted
[piece
]);
663 if (TimeControl
.clock
[black
] > 0)
671 UpdateDisplay(0, 0, 1, 0);
683 short sq
, i
, c
, f
, t
;
686 char empty
[2] = "\n";
690 strcpy(fname
, savefile
);
694 /* Enter file name */
708 if (fname
[0] == '\0') /* shogi.000 */
709 strcpy(fname
, CP
[137]);
711 if ((fd
= fopen(fname
, "w")) != NULL
)
716 if (computer
== white
)
719 if (computer
== black
)
722 fprintf(fd
, CP
[37], w
, b
, Game50
,
723 flag
.force
? "force" : "");
725 fprintf(fd
, CP
[111], TCflag
, OperatorTime
);
727 TimeControl
.clock
[black
], TimeControl
.moves
[black
],
728 TimeControl
.clock
[white
], TimeControl
.moves
[white
]);
731 for (i
= NO_ROWS
- 1; i
> -1; i
--)
733 fprintf(fd
, "%c ", ROW_NAME(i
));
735 for (c
= 0; c
< NO_COLS
; c
++)
737 sq
= i
* NO_COLS
+ c
;
739 p
= is_promoted
[piece
] ? '+' : ' ';
740 fprintf(fd
, "%c", p
);
756 fprintf(fd
, "%c", p
);
761 for (f
= i
* NO_COLS
; f
< i
* NO_COLS
+ NO_ROWS
; f
++)
762 fprintf(fd
, " %d", Mvboard
[f
]);
768 fprintf(fd
, " 9 8 7 6 5 4 3 2 1\n"); /* FIXME */
770 fprintf(fd
, " p l n s g b r k\n");
772 for (side
= 0; side
<= 1; side
++)
774 fprintf(fd
, "%c", (side
== black
) ? 'B' : 'W');
775 fprintf(fd
, " %2d", Captured
[side
][pawn
]);
776 fprintf(fd
, " %2d", Captured
[side
][lance
]);
777 fprintf(fd
, " %2d", Captured
[side
][knight
]);
778 fprintf(fd
, " %2d", Captured
[side
][silver
]);
779 fprintf(fd
, " %2d", Captured
[side
][gold
]);
780 fprintf(fd
, " %2d", Captured
[side
][bishop
]);
781 fprintf(fd
, " %2d", Captured
[side
][rook
]);
782 fprintf(fd
, " %2d", Captured
[side
][king
]);
789 for (i
= 1; i
<= GameCnt
; i
++)
791 struct GameRec
*g
= &GameList
[i
];
794 t
= (g
->gmove
& 0xFF);
795 algbr(f
, t
, g
->flags
);
797 fprintf(fd
, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
800 : (is_promoted
[g
->fpiece
] ? '+' : ' ')),
802 ((f
> NO_SQUARES
) ? &mvstr
[0][1] : mvstr
[0]),
804 g
->nodes
, g
->time
, g
->flags
,
805 g
->hashkey
, g
->hashbd
);
807 if (g
->piece
!= no_piece
)
809 fprintf(fd
, " %c %s %c\n",
810 pxx
[g
->piece
], ColorStr
[g
->color
],
811 (is_promoted
[g
->piece
] ? '+' : ' '));
826 /* ShowMessage("Could not open file"); */
834 * GetXGame, SaveXGame and BookGame used to only be defined if
835 * xshogi wasn't defined -- wonder why?
847 /* Enter file name */
860 if (fname
[0] == '\0') /* XSHOGI.position.read */
861 strcpy(fname
, CP
[205]);
863 if ((fd
= fopen(fname
, "r")) != NULL
)
866 flag
.regularstart
= false;
869 /* xshogi position file ... */
870 fgets(fname
, 256, fd
);
875 if (strcmp(fname
, CP
[206]))
879 /* -- empty line -- */
880 fgets(fname
, 256, fd
);
881 /* -- empty line -- */
882 fgets(fname
, 256, fd
);
884 for (i
= NO_ROWS
- 1; i
> -1; i
--)
886 fgets(fname
, 256, fd
);
889 for (j
= 0; j
< NO_COLS
; j
++)
891 sq
= i
* NO_COLS
+ j
;
897 board
[sq
] = no_piece
;
902 for (c
= 0; c
< NO_PIECES
; c
++)
907 board
[sq
] = promoted
[c
];
909 board
[sq
] = unpromoted
[c
];
915 for (c
= 0; c
< NO_PIECES
; c
++)
920 board
[sq
] = promoted
[c
];
922 board
[sq
] = unpromoted
[c
];
935 for (side
= 0; side
<= 1; side
++)
937 fgets(fname
, 256, fd
);
939 Captured
[side
][pawn
] = atoi(InPtr
);
941 Captured
[side
][lance
] = atoi(InPtr
);
943 Captured
[side
][knight
] = atoi(InPtr
);
945 Captured
[side
][silver
] = atoi(InPtr
);
947 Captured
[side
][gold
] = atoi(InPtr
);
949 Captured
[side
][bishop
] = atoi(InPtr
);
951 Captured
[side
][rook
] = atoi(InPtr
);
953 Captured
[side
][king
] = atoi(InPtr
);
956 if (fgets(fname
, 256, fd
) != NULL
&& strncmp(fname
, "white", 5) == 0)
969 UpdateDisplay(0, 0, 1, 0);
984 /* Enter file name */
997 if (fname
[0] == '\0') /* XSHOGI.position.read */
998 strcpy(fname
, CP
[205]);
1000 if ((fd
= fopen(fname
, "w")) != NULL
)
1002 /* xshogi position file ... */
1003 fputs("# xshogi position file -- \n", fd
);
1004 /* -- empty line -- */
1006 /* -- empty line -- */
1009 for (i
= NO_ROWS
- 1; i
> -1; i
--)
1013 for (j
= 0; j
< NO_COLS
; j
++)
1015 sq
= i
* NO_COLS
+ j
;
1017 isp
= is_promoted
[piece
];
1018 *p
= (isp
? '+' : ' ');
1021 if (piece
== no_piece
)
1023 else if (color
[sq
] == white
)
1036 for (side
= 0; side
<= 1; side
++)
1038 sprintf(fname
, "%d %d %d %d %d %d %d %d\n",
1039 Captured
[side
][pawn
],
1040 Captured
[side
][lance
],
1041 Captured
[side
][knight
],
1042 Captured
[side
][silver
],
1043 Captured
[side
][gold
],
1044 Captured
[side
][bishop
],
1045 Captured
[side
][rook
],
1046 Captured
[side
][king
]);
1051 if (computer
== black
)
1052 fputs("white to play\n", fd
);
1054 fputs("black to play\n", fd
);
1065 char fname
[256], sflags
[4];
1070 strcpy(fname
, savefile
);
1074 /* Enter file name */
1075 ShowMessage(CP
[63]);
1088 if (fname
[0] == '\0')
1091 if ((fd
= fopen(fname
, "a")) != NULL
)
1095 for (i
= 1; i
<= GameCnt
; i
++)
1097 struct GameRec
*g
= &GameList
[i
];
1098 char mvnr
[20], mvs
[20];
1101 sprintf(mvnr
, "%d.", (i
+ 1)/2);
1106 t
= (g
->gmove
& 0xFF);
1107 algbr(f
, t
, g
->flags
);
1110 /* determine move quality string */
1111 if (g
->flags
& goodmove
)
1114 if (g
->flags
& badmove
)
1117 #ifdef EASY_OPENINGS
1118 if (g
->flags
& difficult
)
1124 /* determine move string */
1127 sprintf(mvs
, "%s%s ", &mvstr
[0][1], sflags
);
1131 sprintf(mvs
, "%c%c%c%c%c%s%s ",
1132 mvstr
[0][0], mvstr
[0][1],
1133 (g
->flags
& capture
) ? 'x' : '-',
1134 mvstr
[0][2], mvstr
[0][3],
1135 (mvstr
[0][4] == '+') ? "+" : "",
1139 fprintf(fd
, "%s%s%c%s",
1143 : (is_promoted
[g
->fpiece
] ? "+" : "")),
1157 ShowMessage(CP
[70]);
1161 /* ShowMessage("Could not open file"); */
1162 ShowMessage(CP
[48]);
1174 char fname
[256], dbuf
[256];
1178 strcpy(fname
, listfile
);
1183 strncpy(dbuf
, ctime(&when
), 20);
1190 /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
1193 program played white */
1195 sprintf(fname
, "CLp%s.%s%s-%s%s%s%c",
1196 patchlevel
, dbuf
+ 4, dbuf
+ 8, dbuf
+ 11, dbuf
+ 14,
1197 dbuf
+ 17, ColorStr
[computer
][0]);
1199 /* replace space padding with 0 */
1200 for (i
= 0; fname
[i
] != '\0'; i
++)
1202 if (fname
[i
] == ' ')
1207 fd
= fopen(fname
, "w");
1211 printf(CP
[219], fname
);
1215 /* fprintf(fd, "gnushogi game %d\n", u); */
1216 fprintf(fd
, CP
[161], version
, patchlevel
);
1220 for (i
= 1; i
<= GameCnt
; i
++)
1222 f
= GameList
[i
].gmove
>> 8;
1223 t
= (GameList
[i
].gmove
& 0xFF);
1224 algbr(f
, t
, GameList
[i
].flags
);
1226 if (GameList
[i
].flags
& book
)
1228 fprintf(fd
, "%c%c%-5s %5d Book%7ld %5ld",
1231 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1232 pxx
[GameList
[i
].fpiece
],
1234 ? &mvstr
[0][1] : mvstr
[0]),
1241 fprintf(fd
, "%c%c%-5s %5d %2d %7ld %5ld",
1244 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1245 pxx
[GameList
[i
].fpiece
],
1246 (f
> NO_SQUARES
? &mvstr
[0][1] : mvstr
[0]),
1247 GameList
[i
].score
, GameList
[i
].depth
,
1248 GameList
[i
].nodes
, GameList
[i
].time
);
1261 fprintf(fd
, "\n\n");
1263 if (GameList
[GameCnt
].flags
& draw
)
1265 fprintf(fd
, CP
[54], DRAW
);
1267 if (DRAW
== CP
[101])
1271 fprintf(fd
, "repetition by positions ");
1273 for (j
= GameCnt
- 1; j
>= Game50
; j
-= 2)
1275 if (GameList
[j
].hashkey
== hashkey
&&
1276 GameList
[j
].hashbd
== hashbd
)
1277 fprintf(fd
, "%d ", j
);
1283 else if (GameList
[GameCnt
].score
== -(SCORE_LIMIT
+ 999))
1285 fprintf(fd
, "%s\n", ColorStr
[player
]);
1287 else if (GameList
[GameCnt
].score
== (SCORE_LIMIT
+ 998))
1289 fprintf(fd
, "%s\n", ColorStr
[player
^ 1]);
1303 GameList
[GameCnt
].flags
|= badmove
;
1307 GameList
[GameCnt
].flags
|= goodmove
;
1310 #ifdef EASY_OPENINGS
1312 GameList
[GameCnt
].flags
|= difficult
;
1322 * Undo the most recent half-move.
1330 f
= GameList
[GameCnt
].gmove
>> 8;
1331 t
= GameList
[GameCnt
].gmove
& 0x7F;
1335 /* the move was a drop */
1336 Captured
[color
[t
]][board
[t
]]++;
1337 board
[t
] = no_piece
;
1343 if (GameList
[GameCnt
].flags
& promote
)
1344 board
[f
] = unpromoted
[board
[t
]];
1346 board
[f
] = board
[t
];
1348 color
[f
] = color
[t
];
1349 board
[t
] = GameList
[GameCnt
].piece
;
1350 color
[t
] = GameList
[GameCnt
].color
;
1352 if (board
[t
] != no_piece
)
1353 Captured
[color
[f
]][unpromoted
[board
[t
]]]--;
1355 if (color
[t
] != neutral
)
1363 if (TCflag
&& (TCmoves
> 1))
1364 ++TimeControl
.moves
[color
[f
]];
1366 hashkey
= GameList
[GameCnt
].hashkey
;
1367 hashbd
= GameList
[GameCnt
].hashbd
;
1369 computer
= computer
^ 1;
1370 opponent
= opponent
^ 1;
1373 player
= player
^ 1;
1375 UpdateDisplay(0, 0, 1, 0);
1377 if (flag
.regularstart
)
1384 FlagString(unsigned short flags
, char *s
)
1389 if (flags
& promote
)
1390 strcat(s
, " promote");
1392 if (flags
& dropmask
)
1393 strcat(s
, " drop:");
1395 if ((piece
= (flags
& pmask
)))
1399 if (is_promoted
[piece
])
1402 s
[l
++] = pxx
[piece
];
1406 if (flags
& capture
)
1407 strcat(s
, " capture");
1410 strcat(s
, " exact");
1413 strcat(s
, " tesuji");
1416 strcat(s
, " check");
1422 strcat(s
, " stupid");
1424 if (flags
& questionable
)
1425 strcat(s
, " questionable");
1427 if (flags
& kingattack
)
1428 strcat(s
, " kingattack");
1437 TestSpeed(void(*f
)(short side
, short ply
,
1438 short in_check
, short blockable
),
1446 long cnt
, rate
, t1
, t2
;
1448 #ifdef HAVE_GETTIMEOFDAY
1452 #ifdef HAVE_GETTIMEOFDAY
1453 gettimeofday(&tv
, NULL
);
1454 t1
= (tv
.tv_sec
*100 + (tv
.tv_usec
/10000));
1459 for (i
= 0; i
< j
; i
++)
1461 f(opponent
, 2, -1, true);
1464 for (jj
= TrPnt
[2]; i
< TrPnt
[3]; jj
++)
1466 if (!pick(jj
, TrPnt
[3] - 1))
1472 #ifdef HAVE_GETTIMEOFDAY
1473 gettimeofday(&tv
, NULL
);
1474 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1479 cnt
= j
* (TrPnt
[3] - TrPnt
[2]);
1486 rate
= (((et
) ? ((cnt
* 100) / et
) : 0));
1488 #ifdef DYNAMIC_ZNODES
1494 printf(CP
[91], cnt
, rate
);
1502 TestPSpeed(short(*f
) (short side
), unsigned j
)
1505 long cnt
, rate
, t1
, t2
;
1506 #ifdef HAVE_GETTIMEOFDAY
1510 #ifdef HAVE_GETTIMEOFDAY
1511 gettimeofday(&tv
, NULL
);
1512 t1
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1517 for (i
= 0; i
< j
; i
++)
1520 #ifdef HAVE_GETTIMEOFDAY
1521 gettimeofday(&tv
, NULL
);
1522 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1534 rate
= (et
) ? ((cnt
* 100) / et
) : 0;
1536 /* printf("Nodes= %ld Nodes/sec= %ld\n", cnt, rate); */
1539 printf(CP
[91], cnt
, rate
);
1553 time
= &s
[strlen(CP
[228])];
1554 t
= (int)strtol(time
, &time
, 10);
1559 sec
= (int)strtol(time
, &time
, 10);
1562 m
= (int)strtol(time
, &time
, 10);
1565 TimeControl
.clock
[opponent
] = t
;
1568 TimeControl
.moves
[opponent
] = m
;
1570 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1574 /* just to inform xshogi about availability of otime command */
1575 printf("otime %d %d\n", t
, m
);
1582 SetMachineTime(char *s
)
1587 time
= &s
[strlen(CP
[197])];
1589 t
= (int)strtol(time
, &time
, 10);
1594 sec
= (int)strtol(time
, &time
, 10);
1597 m
= (int)strtol(time
, &time
, 10);
1600 TimeControl
.clock
[computer
] = t
;
1603 TimeControl
.moves
[computer
] = m
;
1605 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1609 /* just to inform xshogi about availability of time command */
1610 printf("time %d %d\n", t
, m
);
1618 /* FIXME! This is truly the function from hell! */
1621 * Process the user's command. If easy mode is OFF (the computer is thinking
1622 * on opponents time) and the program is out of book, then make the 'hint'
1623 * move on the board and call SelectMove() to find a response. The user
1624 * terminates the search by entering ^C (quit siqnal) before entering a
1625 * command. If the opponent does not make the hint move, then set Sdepth to
1630 InputCommand(char *command
)
1633 short have_shown_prompt
= false;
1634 short ok
, done
, is_move
= false;
1638 ok
= flag
.quit
= done
= false;
1642 if (TTadd
> ttbllimit
)
1646 if ((hint
> 0) && !flag
.easy
&& !flag
.force
)
1649 * A hint move for the player is available. Compute a move for the
1650 * opponent in background mode assuming that the hint move will be
1651 * selected by the player.
1654 ft
= time0
; /* Save reference time for the player. */
1656 algbr((short) hint
>> 8, (short) hint
& 0xff, false);
1657 strcpy(s
, mvstr
[0]);
1664 /* do the hint move */
1665 if (VerifyMove(s
, VERIFY_AND_TRY_MODE
, &mv
))
1669 #ifdef QUIETBACKGROUND
1680 have_shown_prompt
= true;
1681 #endif /* QUIETBACKGROUND */
1683 /* Start computing a move until the search is interrupted. */
1685 #ifdef INTERRUPT_TEST
1689 /* would love to put null move in here */
1690 /* after we make the hint move make a 2 ply search
1691 * with both plys our moves */
1692 /* think on opponents time */
1693 SelectMove(computer
, BACKGROUND_MODE
);
1695 #ifdef INTERRUPT_TEST
1696 ElapsedTime(COMPUTE_INTERRUPT_MODE
);
1700 printf("searching not terminated by interrupt!\n");
1704 printf("elapsed time from interrupt to "
1705 "terminating search: %ld\n", it
);
1709 /* undo the hint and carry on */
1710 VerifyMove(s
, UNMAKE_MODE
, &mv
);
1714 time0
= ft
; /* Restore reference time for the player. */
1717 while(!(ok
|| flag
.quit
|| done
))
1721 #ifdef QUIETBACKGROUND
1722 if (!have_shown_prompt
)
1724 #endif /* QUIETBACKGROUND */
1736 #ifdef QUIETBACKGROUND
1739 have_shown_prompt
= false;
1740 #endif /* QUIETBACKGROUND */
1742 if (command
== NULL
)
1746 s
[0] = sx
[0] = '\0';
1749 (void)fgets(sx
, 80, stdin
);
1754 eof
= (getstr(sx
) == ERR
);
1759 strcpy(sx
, command
);
1763 sscanf(sx
, "%s", s
);
1771 if (strcmp(s
, CP
[131]) == 0) /* bd -- display board */
1773 /* FIXME: Hack alert! */
1774 short old_xshogi
= XSHOGI
;
1777 display_type
= DISPLAY_RAW
;
1780 UpdateDisplay(0, 0, 1, 0);
1783 display_type
= DISPLAY_X
;
1785 else if (strcmp(s
, "post") == 0)
1787 flag
.post
= !flag
.post
;
1789 else if (strcmp(s
, CP
[129]) == 0)
1791 /* noop */ ; /* alg */
1793 else if ((strcmp(s
, CP
[180]) == 0)
1794 || (strcmp(s
, CP
[216]) == 0)) /* quit exit */
1799 else if (strcmp(s
, CP
[178]) == 0) /* post */
1801 flag
.post
= !flag
.post
;
1804 else if ((strcmp(s
, CP
[191]) == 0)
1805 || (strcmp(s
, CP
[154]) == 0)) /* set edit */
1809 else if (NOT_CURSES
&& (strcmp(s
, CP
[190]) == 0)) /* setup */
1813 else if (strcmp(s
, CP
[156]) == 0) /* first */
1817 else if (strcmp(s
, CP
[162]) == 0) /* go */
1822 if (computer
== black
)
1833 else if (strcmp(s
, CP
[166]) == 0) /* help */
1837 else if (strcmp(s
, CP
[221]) == 0) /* material */
1839 flag
.material
= !flag
.material
;
1841 else if (strcmp(s
, CP
[157]) == 0) /* force */
1846 flag
.bothsides
= false;
1850 flag
.force
= !flag
.force
;
1851 flag
.bothsides
= false;
1854 else if (strcmp(s
, CP
[134]) == 0) /* book */
1856 Book
= Book
? 0 : BOOKFAIL
;
1858 else if (strcmp(s
, CP
[172]) == 0) /* new */
1861 UpdateDisplay(0, 0, 1, 0);
1863 else if (strcmp(s
, CP
[171]) == 0) /* list */
1867 else if ((strcmp(s
, CP
[169]) == 0)
1868 || (strcmp(s
, CP
[217]) == 0)) /* level clock */
1872 else if (strcmp(s
, CP
[165]) == 0) /* hash */
1874 flag
.hash
= !flag
.hash
;
1876 else if (strcmp(s
, CP
[227]) == 0) /* gamein */
1878 flag
.gamein
= !flag
.gamein
;
1880 else if (strcmp(s
, CP
[226]) == 0) /* beep */
1882 flag
.beep
= !flag
.beep
;
1884 else if (strcmp(s
, CP
[197]) == 0) /* time */
1888 else if (strcmp(s
, CP
[228]) == 0) /* otime */
1892 else if (strcmp(s
, CP
[33]) == 0) /* Awindow */
1894 ChangeAlphaWindow();
1896 else if (strcmp(s
, CP
[39]) == 0) /* Bwindow */
1900 else if (strcmp(s
, CP
[183]) == 0) /* rcptr */
1902 flag
.rcptr
= !flag
.rcptr
;
1904 else if (strcmp(s
, CP
[168]) == 0) /* hint */
1908 else if (strcmp(s
, CP
[135]) == 0) /* both */
1910 flag
.bothsides
= !flag
.bothsides
;
1913 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1914 SelectMove(opponent
, FOREGROUND_MODE
);
1917 else if (strcmp(s
, CP
[185]) == 0) /* reverse */
1919 flag
.reverse
= !flag
.reverse
;
1921 UpdateDisplay(0, 0, 1, 0);
1923 else if (strcmp(s
, CP
[195]) == 0) /* switch */
1925 computer
= computer
^ 1;
1926 opponent
= opponent
^ 1;
1927 xwndw
= (computer
== black
) ? WXWNDW
: BXWNDW
;
1932 else if (strcmp(s
, CP
[203]) == 0) /* black */
1941 * ok = true; don't automatically start with black command
1944 else if (strcmp(s
, CP
[133]) == 0) /* white */
1953 * ok = true; don't automatically start with white command
1956 else if (strcmp(s
, CP
[201]) == 0 && GameCnt
> 0) /* undo */
1960 else if (strcmp(s
, CP
[184]) == 0 && GameCnt
> 1) /* remove */
1965 /* CHECKME: are these next three correct? */
1966 else if (!XSHOGI
&& strcmp(s
, CP
[207]) == 0) /* xget */
1970 else if (!XSHOGI
&& strcmp(s
, "xsave") == 0) /* xsave */
1974 else if (!XSHOGI
&& strcmp(s
, "bsave") == 0) /* bsave */
1978 #ifdef EASY_OPENINGS
1979 else if ((strcmp(s
, "?") == 0)
1980 || (strcmp(s
, "!") == 0)
1981 || (strcmp(s
, "~") == 0))
1983 else if ((strcmp(s
, "?") == 0)
1984 || (strcmp(s
, "!") == 0))
1989 else if (strcmp(s
, CP
[160]) == 0) /* get */
1993 else if (strcmp(s
, CP
[189]) == 0) /* save */
1997 else if (strcmp(s
, CP
[151]) == 0) /* depth */
1999 ChangeSearchDepth();
2001 else if (strcmp(s
, CP
[164]) == 0) /* hashdepth */
2005 else if (strcmp(s
, CP
[182]) == 0) /* random */
2009 else if (strcmp(s
, CP
[229]) == 0) /* hard */
2013 else if (strcmp(s
, CP
[152]) == 0) /* easy */
2015 flag
.easy
= !flag
.easy
;
2017 else if (strcmp(s
, CP
[230]) == 0) /* tsume */
2019 flag
.tsume
= !flag
.tsume
;
2021 else if (strcmp(s
, CP
[143]) == 0) /* contempt */
2025 else if (strcmp(s
, CP
[209]) == 0) /* xwndw */
2029 else if (strcmp(s
, CP
[186]) == 0) /* rv */
2032 UpdateDisplay(0, 0, 1, 0);
2034 else if (strcmp(s
, CP
[145]) == 0) /* coords */
2036 flag
.coords
= !flag
.coords
;
2037 UpdateDisplay(0, 0, 1, 0);
2039 else if (strcmp(s
, CP
[193]) == 0) /* stars */
2041 flag
.stars
= !flag
.stars
;
2042 UpdateDisplay(0, 0, 1, 0);
2044 else if (!XSHOGI
&& strcmp(s
, CP
[5]) == 0) /* moves */
2051 extern unsigned short PrVar
[MAXDEPTH
];
2053 SwagHt
= (GameList
[GameCnt
].gmove
== PrVar
[1])
2060 ShowMessage(CP
[108]); /* test movelist */
2061 temp
= generate_move_flags
;
2062 generate_move_flags
= true;
2063 TestSpeed(MoveList
, 1);
2064 generate_move_flags
= temp
;
2065 ShowMessage(CP
[107]); /* test capturelist */
2066 TestSpeed(CaptureList
, 1);
2067 ShowMessage(CP
[85]); /* test score position */
2068 ExaminePosition(opponent
);
2069 TestPSpeed(ScorePosition
, 1);
2071 else if (!XSHOGI
&& strcmp(s
, CP
[196]) == 0) /* test */
2074 ShowMessage(CP
[108]); /* test movelist */
2075 TestSpeed(MoveList
, 2000);
2076 ShowMessage(CP
[107]); /* test capturelist */
2077 TestSpeed(CaptureList
, 3000);
2078 ShowMessage(CP
[85]); /* test score position */
2079 ExaminePosition(opponent
);
2080 TestPSpeed(ScorePosition
, 1500);
2082 ShowMessage(CP
[108]); /* test movelist */
2083 TestSpeed(MoveList
, 20000);
2084 ShowMessage(CP
[107]); /* test capturelist */
2085 TestSpeed(CaptureList
, 30000);
2086 ShowMessage(CP
[85]); /* test score position */
2087 ExaminePosition(opponent
);
2088 TestPSpeed(ScorePosition
, 15000);
2091 else if (!XSHOGI
&& strcmp(s
, CP
[179]) == 0) /* p */
2095 else if (!XSHOGI
&& strcmp(s
, CP
[148]) == 0) /* debug */
2105 else if ((ok
= VerifyMove(s
, VERIFY_AND_MAKE_MODE
, &mv
)))
2107 /* check for repetition */
2108 short rpt
= repetition();
2114 GameList
[GameCnt
].flags
|= draw
;
2128 ElapsedTime(COMPUTE_AND_INIT_MODE
);
2132 computer
= opponent
;
2133 opponent
= computer
^ 1;
2138 /* add remaining time in milliseconds for xshogi */
2141 printf("%d. %s %ld\n",
2142 ++mycnt2
, s
, TimeControl
.clock
[player
] * 10);
2145 #ifdef notdef /* optional pass best line to frontend with move */
2146 # if !defined NOPOST
2148 if (flag
.post
&& !flag
.mate
)
2152 printf(" %6d ", MSCORE
);
2154 for (i
= 1; MV
[i
] > 0; i
++)
2156 algbr((short) (MV
[i
] >> 8), (short) (MV
[i
] & 0xFF), false);
2157 printf("%5s ", mvstr
[0]);
2165 signal(SIGUSR1
, TerminateSearch
);
2172 SetTimeControl(void)
2176 TimeControl
.moves
[black
] = TimeControl
.moves
[white
] = TCmoves
;
2177 TimeControl
.clock
[black
] += 6000L * TCminutes
+ TCseconds
* 100;
2178 TimeControl
.clock
[white
] += 6000L * TCminutes
+ TCseconds
* 100;
2182 TimeControl
.moves
[black
] = TimeControl
.moves
[white
] = 0;
2183 TimeControl
.clock
[black
] = TimeControl
.clock
[white
] = 0;
2186 flag
.onemove
= (TCmoves
== 1);
2188 ElapsedTime(COMPUTE_AND_INIT_MODE
);