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
9 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
11 * GNU SHOGI is based on GNU CHESS
13 * Copyright (c) 1988, 1989, 1990 John Stanback
14 * Copyright (c) 1992 Free Software Foundation
16 * This file is part of GNU SHOGI.
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.
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
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 * ----------------------------------------------------------------------
35 /* request *snprintf prototypes */
36 #define _POSIX_C_SOURCE 200112L
42 #if defined HAVE_GETTIMEOFDAY
49 #include <sys/param.h>
50 #include <sys/types.h>
56 struct display
*dsp
= &raw_display
;
63 movealgbr(short m
, char *s
)
66 short piece
= 0, flag
= 0;
79 piece
= f
- NO_SQUARES
;
81 if (piece
> NO_PIECES
)
84 flag
= (dropmask
| piece
);
99 *s
= COL_NAME(column(t
));
101 *s
= ROW_NAME(row(t
));
106 *s
= COL_NAME(column(f
));
108 *s
= ROW_NAME(row(f
));
110 *s
= COL_NAME(column(t
));
112 *s
= ROW_NAME(row(t
));
131 #endif /* BOOKTEST */
135 * Generate move strings in different formats.
138 * - f piece to be moved
139 * - 0 < f < NO_SQUARES source square
140 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
141 * - t & 0x7f target square
142 * - t & 0x80 promotion flag
144 * - if flag & dropmask, piece type encoded in flag & pmask
146 * FIXME: that makes 2 ways to specify drops and promotions, why ?
153 algbr(short f
, short t
, short flags
)
159 piece
= f
- NO_SQUARES
;
161 if (f
> (NO_SQUARES
+ NO_PIECES
))
164 flags
= (dropmask
| piece
);
173 if ((f
== t
) && ((f
!= 0) || (t
!= 0)))
176 dsp
->Printf("error in algbr: FROM=TO=%d, flags=0x%4x\n", t
, flags
);
179 mvstr
[0][0] = mvstr
[1][0] = mvstr
[2][0] = mvstr
[3][0] = '\0';
181 else if ((flags
& dropmask
) != 0)
183 short piece
= flags
& pmask
;
185 mvstr
[0][0] = pxx
[piece
];
186 mvstr
[0][1] = xboard
? '@' : '*';
187 mvstr
[0][2] = COL_NAME(column(t
));
188 mvstr
[0][3] = ROW_NAME(row(t
));
190 strcpy(mvstr
[1], mvstr
[0]);
191 strcpy(mvstr
[2], mvstr
[0]);
192 strcpy(mvstr
[3], mvstr
[0]);
194 else if ((f
!= 0) || (t
!= 0))
196 /* pure coordinates notation */
197 mvstr
[0][0] = COL_NAME(column(f
));
198 mvstr
[0][1] = ROW_NAME(row(f
));
199 mvstr
[0][2] = COL_NAME(column(t
));
200 mvstr
[0][3] = ROW_NAME(row(t
));
203 /* algebraic notation without disambiguation */
204 mvstr
[1][0] = pxx
[board
[f
]];
205 mvstr
[1][1] = mvstr
[0][2]; /* to column */
206 mvstr
[1][2] = mvstr
[0][3]; /* to row */
209 /* algebraic notation with row disambiguation */
210 mvstr
[2][0] = mvstr
[1][0];
211 mvstr
[2][1] = mvstr
[0][1];
212 mvstr
[2][2] = mvstr
[0][2]; /* to column */
213 mvstr
[2][3] = mvstr
[0][3]; /* to row */
216 /* algebraic notation with column disambiguation */
217 strcpy(mvstr
[3], mvstr
[2]);
218 mvstr
[3][1] = mvstr
[0][0];
222 strcat(mvstr
[0], "+");
223 strcat(mvstr
[1], "+");
224 strcat(mvstr
[2], "+");
225 strcat(mvstr
[3], "+");
230 mvstr
[0][0] = mvstr
[1][0] = mvstr
[2][0] = mvstr
[3][0] = '\0';
236 * Compare the string 's' to the list of legal moves available for the
237 * opponent. If a match is found, make the move on the board.
241 VerifyMove(char *s
, VerifyMove_mode iop
, unsigned short *mv
)
243 static short pnt
, tempb
, tempc
, tempsf
, tempst
, cnt
;
244 static struct leaf xnode
;
246 short i
, l
, local_flags
;
249 /* check and remove quality flags */
250 for (i
= local_flags
= 0, l
= strlen(s
); i
< l
; i
++)
255 local_flags
|= badmove
;
260 local_flags
|= goodmove
;
266 local_flags
|= difficult
;
275 if (iop
== UNMAKE_MODE
)
277 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
283 if (iop
== VERIFY_AND_MAKE_MODE
)
284 generate_move_flags
= true;
286 MoveList(opponent
, 2, -1, true);
287 generate_move_flags
= false;
289 if(s
[4] == '=') s
[4] = '\0'; /* deferral is implied */
291 while (pnt
< TrPnt
[3])
294 algbr(node
->f
, node
->t
, (short) node
->flags
);
296 if ((strcmp(s
, mvstr
[0]) == 0)
297 || (strcmp(s
, mvstr
[1]) == 0)
298 || (strcmp(s
, mvstr
[2]) == 0)
299 || (strcmp(s
, mvstr
[3]) == 0))
306 if ((cnt
== 1) && (xnode
.score
> DONTUSE
))
310 MakeMove(opponent
, &xnode
, &tempb
, &tempc
,
311 &tempsf
, &tempst
, &INCscore
);
313 if (SqAttacked(PieceList
[opponent
][0], computer
, &blocked
))
315 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
316 dsp
->AlwaysShowMessage("Illegal move (in check): %s", s
);
321 if (iop
== VERIFY_AND_TRY_MODE
)
324 dsp
->UpdateDisplay(xnode
.f
, xnode
.t
, 0, (short) xnode
.flags
);
325 GameList
[GameCnt
].depth
= GameList
[GameCnt
].score
= 0;
326 GameList
[GameCnt
].nodes
= 0;
327 ElapsedTime(COMPUTE_AND_INIT_MODE
);
328 GameList
[GameCnt
].time
= (short) (et
+ 50)/100;
329 GameList
[GameCnt
].flags
|= local_flags
;
333 TimeControl
.clock
[opponent
] -= et
;
334 timeopp
[oppptr
] = et
;
335 --TimeControl
.moves
[opponent
];
338 *mv
= (xnode
.f
<< 8) | xnode
.t
;
339 algbr(xnode
.f
, xnode
.t
, 0);
341 /* in force mode, check for mate conditions */
344 if (IsCheckmate(opponent
^ 1, -1, -1))
348 sprintf(buf
, "%s mates!\n", ColorStr
[opponent
]);
349 dsp
->ShowMessage(buf
);
358 dsp
->AlwaysShowMessage("Illegal move (no match): %s", s
);
360 if (!XSHOGI
&& (cnt
> 1))
362 sprintf(buffer
, "Ambiguous Move %s!", s
);
363 dsp
->ShowMessage(buffer
);
371 parser(char *f
, short *fpiece
)
380 for (i
= 1, *fpiece
= no_piece
; i
< NO_PIECES
; i
++)
382 if (f
[0] == pxx
[i
] || f
[0] == qxx
[i
])
384 *fpiece
= (p
? promoted
[i
] : unpromoted
[i
]);
389 if (f
[1] == '*' || f
[1] == '\'')
394 return ((NO_SQUARES
+ *fpiece
) << 8) | locn(r2
, c2
);
402 p
= (f
[5] == '+') ? 0x80 : 0;
404 return (locn(r1
, c1
) << 8) | locn(r2
, c2
) | p
;
412 while (*InPtr
!= ' ')
415 while (*InPtr
== ' ')
423 while (*InPtr
== ' ')
428 void RequestInputString(char* buffer
, unsigned bufsize
)
431 int ret
= snprintf(fmt
, sizeof(fmt
), "%%%us", bufsize
);
433 perror("RequestInputString snprintf");
436 if (ret
>= sizeof(fmt
)) {
438 "Insufficient format-buffer size in %s for bufsize=%u\n",
439 __FUNCTION__
, bufsize
);
442 dsp
->doRequestInputString(fmt
, buffer
);
456 strcpy(fname
, savefile
);
458 dsp
->ShowMessage("Enter file name: ");
459 RequestInputString(fname
, sizeof(fname
)-1);
462 if (fname
[0] == '\0')
463 strcpy(fname
, "shogi.000");
465 if ((fd
= fopen(fname
, "r")) != NULL
)
468 fgets(fname
, 256, fd
);
469 computer
= opponent
= black
;
478 /* FIXME: write a skipn() function so that we can get
479 * 3 skips by doing skipn(3) */
483 Game50
= atoi(InPtr
);
485 flag
.force
= (*InPtr
== 'f');
486 fgets(fname
, 256, fd
); /* empty */
487 fgets(fname
, 256, fd
);
490 TCflag
= atoi(InPtr
);
494 OperatorTime
= atoi(InPtr
);
495 fgets(fname
, 256, fd
);
498 TimeControl
.clock
[black
] = atol(InPtr
);
501 TimeControl
.moves
[black
] = atoi(InPtr
);
502 fgets(fname
, 256, fd
);
505 TimeControl
.clock
[white
] = atol(InPtr
);
508 TimeControl
.moves
[white
] = atoi(InPtr
);
509 fgets(fname
, 256, fd
); /* empty */
511 for (i
= NO_ROWS
- 1; i
> -1; i
--)
513 fgets(fname
, 256, fd
);
517 for (j
= 0; j
< NO_COLS
; j
++)
519 sq
= i
* NO_COLS
+ j
;
525 board
[sq
] = no_piece
;
530 for (c
= 0; c
< NO_PIECES
; c
++)
535 board
[sq
] = promoted
[c
];
537 board
[sq
] = unpromoted
[c
];
543 for (c
= 0; c
< NO_PIECES
; c
++)
548 board
[sq
] = promoted
[c
];
550 board
[sq
] = unpromoted
[c
];
558 Mvboard
[sq
] = atoi(InPtr
);
563 fgets(fname
, 256, fd
); /* empty */
564 fgets(fname
, 256, fd
); /* 9 8 7 ... */
565 fgets(fname
, 256, fd
); /* empty */
566 fgets(fname
, 256, fd
); /* p l n ... */
569 for (side
= 0; side
<= 1; side
++)
571 fgets(fname
, 256, fd
);
575 Captured
[side
][pawn
] = atoi(InPtr
);
578 Captured
[side
][lance
] = atoi(InPtr
);
580 Captured
[side
][knight
] = atoi(InPtr
);
583 Captured
[side
][silver
] = atoi(InPtr
);
585 Captured
[side
][gold
] = atoi(InPtr
);
587 Captured
[side
][bishop
] = atoi(InPtr
);
589 Captured
[side
][rook
] = atoi(InPtr
);
591 Captured
[side
][king
] = atoi(InPtr
);
595 flag
.regularstart
= true;
597 fgets(fname
, 256, fd
); /* empty */
598 fgets(fname
, 256, fd
); /* move score ... */
600 while (fgets(fname
, 256, fd
))
609 g
= &GameList
[GameCnt
];
610 g
->gmove
= parser(InPtr
, &g
->fpiece
);
612 g
->score
= atoi(InPtr
);
614 g
->depth
= atoi(InPtr
);
616 g
->nodes
= atol(InPtr
);
618 g
->time
= atol(InPtr
);
620 g
->flags
= c
= atoi(InPtr
);
622 g
->hashkey
= strtol(InPtr
, (char **) NULL
, 16);
624 g
->hashbd
= strtol(InPtr
, (char **) NULL
, 16);
632 for (piece
= no_piece
, i
= 0; i
< NO_PIECES
; i
++)
634 if (pxx
[i
] == *InPtr
)
642 g
->color
= ((*InPtr
== 'W') ? white
: black
);
644 g
->piece
= (*InPtr
== '+'
646 : unpromoted
[piece
]);
655 if (TimeControl
.clock
[black
] > 0)
663 dsp
->UpdateDisplay(0, 0, 1, 0);
674 short sq
, i
, c
, f
, t
;
677 char empty
[2] = "\n";
680 strcpy(fname
, savefile
);
682 dsp
->ShowMessage("Enter file name: ");
683 RequestInputString(fname
, sizeof(fname
)-1);
686 if (fname
[0] == '\0')
687 strcpy(fname
, "shogi.000");
689 if ((fd
= fopen(fname
, "w")) != NULL
)
694 if (computer
== white
)
697 if (computer
== black
)
700 fprintf(fd
, "White %s Black %s %d %s\n", w
, b
, Game50
,
701 flag
.force
? "force" : "");
703 fprintf(fd
, "TimeControl %d Operator Time %d\n", TCflag
, OperatorTime
);
704 fprintf(fd
, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
705 TimeControl
.clock
[black
], TimeControl
.moves
[black
],
706 TimeControl
.clock
[white
], TimeControl
.moves
[white
]);
709 for (i
= NO_ROWS
- 1; i
> -1; i
--)
711 fprintf(fd
, "%c ", ROW_NAME(i
));
713 for (c
= 0; c
< NO_COLS
; c
++)
715 sq
= i
* NO_COLS
+ c
;
717 p
= is_promoted
[piece
] ? '+' : ' ';
718 fprintf(fd
, "%c", p
);
734 fprintf(fd
, "%c", p
);
739 for (f
= i
* NO_COLS
; f
< i
* NO_COLS
+ NO_ROWS
; f
++)
740 fprintf(fd
, " %d", Mvboard
[f
]);
747 fprintf(fd
, " 9 8 7 6 5 4 3 2 1\n");
749 fprintf(fd
, " p l n s g b r k\n");
751 fprintf(fd
, " 5 4 3 2 1\n");
753 fprintf(fd
, " p s g b r k\n");
756 for (side
= 0; side
<= 1; side
++)
758 fprintf(fd
, "%c", (side
== black
) ? 'B' : 'W');
759 fprintf(fd
, " %2d", Captured
[side
][pawn
]);
761 fprintf(fd
, " %2d", Captured
[side
][lance
]);
762 fprintf(fd
, " %2d", Captured
[side
][knight
]);
764 fprintf(fd
, " %2d", Captured
[side
][silver
]);
765 fprintf(fd
, " %2d", Captured
[side
][gold
]);
766 fprintf(fd
, " %2d", Captured
[side
][bishop
]);
767 fprintf(fd
, " %2d", Captured
[side
][rook
]);
768 fprintf(fd
, " %2d", Captured
[side
][king
]);
773 fputs(" move score depth nodes time flags capture\n", fd
);
775 for (i
= 1; i
<= GameCnt
; i
++)
777 struct GameRec
*g
= &GameList
[i
];
780 t
= (g
->gmove
& 0xFF);
781 algbr(f
, t
, g
->flags
);
783 fprintf(fd
, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
786 : (is_promoted
[g
->fpiece
] ? '+' : ' ')),
788 ((f
> NO_SQUARES
) ? &mvstr
[0][1] : mvstr
[0]),
790 g
->nodes
, g
->time
, g
->flags
,
791 g
->hashkey
, g
->hashbd
);
793 if (g
->piece
!= no_piece
)
795 fprintf(fd
, " %c %s %c\n",
796 pxx
[g
->piece
], ColorStr
[g
->color
],
797 (is_promoted
[g
->piece
] ? '+' : ' '));
807 dsp
->ShowMessage("Game saved");
811 dsp
->ShowMessage("Could not open file");
817 * GetXGame, SaveXGame and BookGame used to only be defined if
818 * xshogi wasn't defined -- wonder why?
830 dsp
->ShowMessage("Enter file name: ");
831 RequestInputString(fname
, sizeof(fname
)-1);
833 if (fname
[0] == '\0')
834 strcpy(fname
, "xshogi.position.read");
836 if ((fd
= fopen(fname
, "r")) != NULL
)
839 flag
.regularstart
= false;
842 /* xshogi position file ... */
843 fgets(fname
, 256, fd
);
848 if (strcmp(fname
, "xshogi"))
852 /* -- empty line -- */
853 fgets(fname
, 256, fd
);
854 /* -- empty line -- */
855 fgets(fname
, 256, fd
);
857 for (i
= NO_ROWS
- 1; i
> -1; i
--)
859 fgets(fname
, 256, fd
);
862 for (j
= 0; j
< NO_COLS
; j
++)
864 sq
= i
* NO_COLS
+ j
;
870 board
[sq
] = no_piece
;
875 for (c
= 0; c
< NO_PIECES
; c
++)
880 board
[sq
] = promoted
[c
];
882 board
[sq
] = unpromoted
[c
];
888 for (c
= 0; c
< NO_PIECES
; c
++)
893 board
[sq
] = promoted
[c
];
895 board
[sq
] = unpromoted
[c
];
908 for (side
= 0; side
<= 1; side
++)
910 fgets(fname
, 256, fd
);
912 Captured
[side
][pawn
] = atoi(InPtr
);
915 Captured
[side
][lance
] = atoi(InPtr
);
917 Captured
[side
][knight
] = atoi(InPtr
);
920 Captured
[side
][silver
] = atoi(InPtr
);
922 Captured
[side
][gold
] = atoi(InPtr
);
924 Captured
[side
][bishop
] = atoi(InPtr
);
926 Captured
[side
][rook
] = atoi(InPtr
);
928 Captured
[side
][king
] = atoi(InPtr
);
931 if (fgets(fname
, 256, fd
) != NULL
&& strncmp(fname
, "white", 5) == 0)
944 dsp
->UpdateDisplay(0, 0, 1, 0);
959 dsp
->ShowMessage("Enter file name: ");
960 RequestInputString(fname
, sizeof(fname
)-1);
962 if (fname
[0] == '\0')
963 strcpy(fname
, "xshogi.position.read");
965 if ((fd
= fopen(fname
, "w")) != NULL
)
967 fputs("# xshogi position file -- \n", fd
);
971 for (i
= NO_ROWS
- 1; i
> -1; i
--)
975 for (j
= 0; j
< NO_COLS
; j
++)
977 sq
= i
* NO_COLS
+ j
;
979 isp
= is_promoted
[piece
];
980 *p
= (isp
? '+' : ' ');
983 if (piece
== no_piece
)
985 else if (color
[sq
] == white
)
998 for (side
= 0; side
<= 1; side
++)
1002 "%d %d %d %d %d %d %d %d\n",
1004 "%d %d %d %d %d %d\n",
1006 Captured
[side
][pawn
],
1008 Captured
[side
][lance
],
1009 Captured
[side
][knight
],
1011 Captured
[side
][silver
],
1012 Captured
[side
][gold
],
1013 Captured
[side
][bishop
],
1014 Captured
[side
][rook
],
1015 Captured
[side
][king
]);
1020 if (computer
== black
)
1021 fputs("white to play\n", fd
);
1023 fputs("black to play\n", fd
);
1034 char fname
[256], sflags
[4];
1038 strcpy(fname
, savefile
);
1040 /* Enter file name */
1041 dsp
->ShowMessage("Enter file name: ");
1042 RequestInputString(fname
, sizeof(fname
)-1);
1045 if (fname
[0] == '\0') {
1046 dsp
->AlwaysShowMessage("aborting book save");
1050 if ((fd
= fopen(fname
, "a")) != NULL
)
1054 for (i
= 1; i
<= GameCnt
; i
++)
1056 struct GameRec
*g
= &GameList
[i
];
1057 char mvnr
[20], mvs
[20];
1060 sprintf(mvnr
, "%d.", (i
+ 1)/2);
1065 t
= (g
->gmove
& 0xFF);
1066 algbr(f
, t
, g
->flags
);
1069 /* determine move quality string */
1070 if (g
->flags
& goodmove
)
1073 if (g
->flags
& badmove
)
1076 #ifdef EASY_OPENINGS
1077 if (g
->flags
& difficult
)
1083 /* determine move string */
1086 sprintf(mvs
, "%s%s ", &mvstr
[0][1], sflags
);
1090 sprintf(mvs
, "%c%c%c%c%c%s%s ",
1091 mvstr
[0][0], mvstr
[0][1],
1092 (g
->flags
& capture
) ? 'x' : '-',
1093 mvstr
[0][2], mvstr
[0][3],
1094 (mvstr
[0][4] == '+') ? "+" : "",
1098 fprintf(fd
, "%s%s%c%s",
1102 : (is_promoted
[g
->fpiece
] ? "+" : "")),
1115 dsp
->ShowMessage("Game saved");
1119 dsp
->ShowMessage("Could not open file");
1130 char fname
[256], dbuf
[256];
1134 strcpy(fname
, listfile
);
1139 strncpy(dbuf
, ctime(&when
), 20);
1146 /* use format "CL.Jan01-020304B" when
1149 program played white */
1151 sprintf(fname
, "CL.%s%s-%s%s%s%c",
1152 dbuf
+ 4, dbuf
+ 8, dbuf
+ 11, dbuf
+ 14,
1153 dbuf
+ 17, ColorStr
[computer
][0]);
1155 /* replace space padding with 0 */
1156 for (i
= 0; fname
[i
] != '\0'; i
++)
1158 if (fname
[i
] == ' ')
1163 fd
= fopen(fname
, "w");
1167 printf("Open failure for file: %s", fname
);
1171 fprintf(fd
, "gnushogi %s game\n", PACKAGE_VERSION
);
1172 fputs(" score depth nodes time ", fd
);
1173 fputs(" score depth nodes time\n", fd
);
1175 for (i
= 1; i
<= GameCnt
; i
++)
1177 f
= GameList
[i
].gmove
>> 8;
1178 t
= (GameList
[i
].gmove
& 0xFF);
1179 algbr(f
, t
, GameList
[i
].flags
);
1181 if (GameList
[i
].flags
& book
)
1183 fprintf(fd
, "%c%c%-5s %5d Book%7ld %5ld",
1186 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1187 pxx
[GameList
[i
].fpiece
],
1189 ? &mvstr
[0][1] : mvstr
[0]),
1196 fprintf(fd
, "%c%c%-5s %5d %2d %7ld %5ld",
1199 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1200 pxx
[GameList
[i
].fpiece
],
1201 (f
> NO_SQUARES
? &mvstr
[0][1] : mvstr
[0]),
1202 GameList
[i
].score
, GameList
[i
].depth
,
1203 GameList
[i
].nodes
, GameList
[i
].time
);
1216 fprintf(fd
, "\n\n");
1218 if (GameList
[GameCnt
].flags
& draw
)
1220 fprintf(fd
, "Draw %s\n", DRAW
);
1222 if (DRAW
== DRAW_REPETITION
)
1226 fprintf(fd
, "repetition by positions ");
1228 for (j
= GameCnt
- 1; j
>= Game50
; j
-= 2)
1230 if (GameList
[j
].hashkey
== hashkey
&&
1231 GameList
[j
].hashbd
== hashbd
)
1232 fprintf(fd
, "%d ", j
);
1238 else if (GameList
[GameCnt
].score
== -(SCORE_LIMIT
+ 999))
1240 fprintf(fd
, "%s\n", ColorStr
[player
]);
1242 else if (GameList
[GameCnt
].score
== (SCORE_LIMIT
+ 998))
1244 fprintf(fd
, "%s\n", ColorStr
[player
^ 1]);
1257 GameList
[GameCnt
].flags
|= badmove
;
1261 GameList
[GameCnt
].flags
|= goodmove
;
1264 #ifdef EASY_OPENINGS
1266 GameList
[GameCnt
].flags
|= difficult
;
1274 * Undo the most recent half-move.
1282 f
= GameList
[GameCnt
].gmove
>> 8;
1283 t
= GameList
[GameCnt
].gmove
& 0x7F;
1287 /* the move was a drop */
1288 Captured
[color
[t
]][board
[t
]]++;
1289 board
[t
] = no_piece
;
1295 if (GameList
[GameCnt
].flags
& promote
)
1296 board
[f
] = unpromoted
[board
[t
]];
1298 board
[f
] = board
[t
];
1300 color
[f
] = color
[t
];
1301 board
[t
] = GameList
[GameCnt
].piece
;
1302 color
[t
] = GameList
[GameCnt
].color
;
1304 if (board
[t
] != no_piece
)
1305 Captured
[color
[f
]][unpromoted
[board
[t
]]]--;
1307 if (color
[t
] != neutral
)
1315 if (TCflag
&& (TCmoves
> 1))
1316 ++TimeControl
.moves
[color
[f
]];
1318 hashkey
= GameList
[GameCnt
].hashkey
;
1319 hashbd
= GameList
[GameCnt
].hashbd
;
1321 computer
= computer
^ 1;
1322 opponent
= opponent
^ 1;
1325 player
= player
^ 1;
1326 dsp
->ShowSidetoMove();
1327 dsp
->UpdateDisplay(0, 0, 1, 0);
1329 if (flag
.regularstart
)
1335 TestSpeed(void(*f
)(short side
, short ply
,
1336 short in_check
, bool blockable
),
1346 #ifdef HAVE_GETTIMEOFDAY
1350 #ifdef HAVE_GETTIMEOFDAY
1351 gettimeofday(&tv
, NULL
);
1352 t1
= (tv
.tv_sec
*100 + (tv
.tv_usec
/10000));
1357 for (i
= 0; i
< j
; i
++)
1359 f(opponent
, 2, -1, true);
1362 for (jj
= TrPnt
[2]; i
< TrPnt
[3]; jj
++)
1364 if (!pick(jj
, TrPnt
[3] - 1))
1370 #ifdef HAVE_GETTIMEOFDAY
1371 gettimeofday(&tv
, NULL
);
1372 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1377 cnt
= j
* (TrPnt
[3] - TrPnt
[2]);
1384 dsp
->ShowNodeCnt(cnt
);
1389 TestPSpeed(short(*f
) (short side
), unsigned j
)
1393 #ifdef HAVE_GETTIMEOFDAY
1397 #ifdef HAVE_GETTIMEOFDAY
1398 gettimeofday(&tv
, NULL
);
1399 t1
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1404 for (i
= 0; i
< j
; i
++)
1407 #ifdef HAVE_GETTIMEOFDAY
1408 gettimeofday(&tv
, NULL
);
1409 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1421 dsp
->ShowNodeCnt(cnt
);
1426 SetOppTime(char *time
)
1430 t
= (int)strtol(time
, &time
, 10);
1435 /* FIXME: sec is parsed but ignored */
1436 (void)strtol(time
, &time
, 10);
1439 m
= (int)strtol(time
, &time
, 10);
1442 TimeControl
.clock
[opponent
] = t
;
1445 TimeControl
.moves
[opponent
] = m
;
1447 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1451 /* just to inform xshogi about availability of otime command */
1452 printf("otime %d %d\n", t
, m
);
1458 SetMachineTime(char *time
)
1462 t
= (int)strtol(time
, &time
, 10);
1467 /* FIXME: sec is parsed but ignored */
1468 (void)strtol(time
, &time
, 10);
1471 m
= (int)strtol(time
, &time
, 10);
1474 TimeControl
.clock
[computer
] = t
;
1477 TimeControl
.moves
[computer
] = m
;
1479 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1483 /* just to inform xshogi about availability of time command */
1484 printf("time %d %d\n", t
, m
);
1489 /* FIXME! This is truly the function from hell! */
1492 * Process the user's command. If easy mode is OFF (the computer is thinking
1493 * on opponents time) and the program is out of book, then make the 'hint'
1494 * move on the board and call SelectMove() to find a response. The user
1495 * terminates the search by entering a command. If the opponent does not make
1496 * the hint move, then set Sdepth to zero.
1500 InputCommand(char *command
)
1502 #ifdef QUIETBACKGROUND
1503 bool have_shown_prompt
= false;
1505 bool ok
, done
, is_move
= false;
1509 ok
= flag
.quit
= done
= false;
1513 if (TTadd
> ttbllimit
)
1517 if ((hint
> 0) && !flag
.easy
&& !flag
.force
)
1520 * A hint move for the player is available. Compute a move for the
1521 * opponent in background mode assuming that the hint move will be
1522 * selected by the player.
1525 ft
= time0
; /* Save reference time for the player. */
1527 algbr((short) hint
>> 8, (short) hint
& 0xff, 0);
1528 strcpy(s
, mvstr
[0]);
1533 /* do the hint move */
1534 if (VerifyMove(s
, VERIFY_AND_TRY_MODE
, &mv
))
1538 #ifdef QUIETBACKGROUND
1540 have_shown_prompt
= true;
1541 #endif /* QUIETBACKGROUND */
1543 /* Start computing a move until the search is interrupted. */
1545 #ifdef INTERRUPT_TEST
1549 /* would love to put null move in here */
1550 /* after we make the hint move make a 2 ply search
1551 * with both plys our moves */
1552 /* think on opponents time */
1553 SelectMove(computer
, BACKGROUND_MODE
);
1555 #ifdef INTERRUPT_TEST
1556 ElapsedTime(COMPUTE_INTERRUPT_MODE
);
1560 printf("searching not terminated by interrupt!\n");
1564 printf("elapsed time from interrupt to "
1565 "terminating search: %ld\n", it
);
1569 /* undo the hint and carry on */
1570 VerifyMove(s
, UNMAKE_MODE
, &mv
);
1574 time0
= ft
; /* Restore reference time for the player. */
1577 while(!(ok
|| flag
.quit
|| done
))
1581 #ifdef QUIETBACKGROUND
1582 if (!have_shown_prompt
)
1584 #endif /* QUIETBACKGROUND */
1588 #ifdef QUIETBACKGROUND
1591 have_shown_prompt
= false;
1592 #endif /* QUIETBACKGROUND */
1594 if (command
== NULL
) {
1595 int eof
= dsp
->GetString(sx
);
1599 strcpy(sx
, command
);
1603 /* extract first word */
1604 if (sscanf(sx
, "%s", s
) < 1)
1607 if (strcmp(s
, "bd") == 0) /* bd -- display board */
1609 /* FIXME: Hack alert! */
1610 short old_xshogi
= XSHOGI
;
1613 display_type
= DISPLAY_RAW
;
1616 dsp
->UpdateDisplay(0, 0, 1, 0);
1619 display_type
= DISPLAY_X
;
1621 else if (strcmp(s
, "post") == 0)
1623 flag
.post
= (xboard
? 1 : !flag
.post
);
1625 else if (strcmp(s
, "nopost") == 0)
1630 else if (strcmp(s
, "variant") == 0)
1631 { /* only variant we play is minishogi */
1632 printf("setup (P.BR.S...G.+.++.+Kp.br.s...g.+.++.+k) 5x5+5_shogi rbsgk/4p/5/P4/KGSBR [-] w 0 1\n");
1635 else if (strcmp(s
, "alg") == 0 ||
1636 strcmp(s
, "accepted") == 0 || strcmp(s
, "rejected") == 0 ||
1637 strcmp(s
, "variant") == 0 || strcmp(s
, "computer") == 0)
1641 else if ((strcmp(s
, "quit") == 0) ||
1642 (strcmp(s
, "exit") == 0))
1646 else if (strcmp(s
, "xboard") == 0)
1649 /* xboard calls Sente "White" */
1650 const char *tmp
= ColorStr
[0];
1651 ColorStr
[0] = ColorStr
[1];
1656 else if (strcmp(s
, "protover") == 0)
1658 printf("feature option=\"tsume -check 0\"\n");
1659 printf("feature option=\"Use hash-file -check %d\"\n", flag
.hash
);
1660 printf("feature option=\"contempt -spin %d -1000 1000\"\n", contempt
);
1661 printf("feature option=\"Hash-file search depth -spin %d 0 100\"\n", HashDepth
);
1662 printf("feature option=\"Hash-file move number -spin %d 0 100\"\n", HashMoveLimit
);
1663 printf("feature myname=\"GNU %s %s\" ",
1671 printf("variants=\"%s\" ",
1673 "5x5+5_shogi,minishogi"
1678 printf("debug=1 setboard=0 sigint=0 usermove=1 done=1\n");
1680 else if ((strcmp(s
, "set") == 0) ||
1681 (strcmp(s
, "edit") == 0))
1685 else if (strcmp(s
, "setup") == 0)
1689 else if (strcmp(s
, "first") == 0)
1693 else if (strcmp(s
, "go") == 0)
1698 if (computer
== black
)
1709 else if (strcmp(s
, "help") == 0)
1713 else if (strcmp(s
, "force") == 0)
1718 flag
.bothsides
= false;
1722 flag
.force
= !flag
.force
;
1723 flag
.bothsides
= false;
1726 else if (strcmp(s
, "book") == 0)
1728 Book
= Book
? 0 : BOOKFAIL
;
1730 else if (strcmp(s
, "new") == 0)
1733 dsp
->UpdateDisplay(0, 0, 1, 0);
1735 else if (strcmp(s
, "list") == 0)
1739 else if (strcmp(s
, "level") == 0)
1741 dsp
->SelectLevel(sx
+ strlen("level"));
1743 else if (strcmp(s
, "clock") == 0)
1745 dsp
->SelectLevel(sx
+ strlen("clock"));
1747 else if (strcmp(s
, "hash") == 0)
1749 flag
.hash
= !flag
.hash
;
1751 else if (strcmp(s
, "gamein") == 0)
1753 flag
.gamein
= !flag
.gamein
;
1755 else if (strcmp(s
, "beep") == 0)
1757 flag
.beep
= !flag
.beep
;
1759 else if (strcmp(s
, "time") == 0)
1761 SetMachineTime(sx
+ strlen("time"));
1763 else if ((strcmp(s
, "otime") == 0) ||
1764 (xboard
&& (strcmp(s
, "otim")) == 0))
1766 SetOppTime(sx
+ strlen("otime"));
1768 else if (strcmp(s
, "Awindow") == 0)
1770 dsp
->ChangeAlphaWindow();
1772 else if (strcmp(s
, "Bwindow") == 0)
1774 dsp
->ChangeBetaWindow();
1776 else if (strcmp(s
, "rcptr") == 0)
1778 flag
.rcptr
= !flag
.rcptr
;
1780 else if (strcmp(s
, "hint") == 0)
1784 else if (strcmp(s
, "both") == 0)
1786 flag
.bothsides
= !flag
.bothsides
;
1789 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1790 SelectMove(opponent
, FOREGROUND_MODE
);
1793 else if (strcmp(s
, "reverse") == 0)
1795 flag
.reverse
= !flag
.reverse
;
1797 dsp
->UpdateDisplay(0, 0, 1, 0);
1799 else if (strcmp(s
, "switch") == 0)
1801 computer
= computer
^ 1;
1802 opponent
= opponent
^ 1;
1803 xwndw
= (computer
== black
) ? WXWNDW
: BXWNDW
;
1807 dsp
->UpdateDisplay(0, 0, 1, 0);
1809 else if (xboard
? strcmp(s
, "white") == 0 : strcmp(s
, "black") == 0)
1818 * ok = true; don't automatically start with black command
1821 else if (xboard
? strcmp(s
, "black") == 0 : strcmp(s
, "white") == 0)
1830 * ok = true; don't automatically start with white command
1833 else if (strcmp(s
, "undo") == 0 && GameCnt
> 0)
1837 else if (strcmp(s
, "remove") == 0 && GameCnt
> 1)
1842 /* CHECKME: are these next three correct? */
1843 else if (!XSHOGI
&& strcmp(s
, "xget") == 0)
1847 else if (!XSHOGI
&& strcmp(s
, "xsave") == 0)
1851 else if (!XSHOGI
&& strcmp(s
, "bsave") == 0)
1855 #ifdef EASY_OPENINGS
1856 else if ((strcmp(s
, "?") == 0) ||
1857 (strcmp(s
, "!") == 0) ||
1858 (strcmp(s
, "~") == 0))
1860 else if ((strcmp(s
, "?") == 0) ||
1861 (strcmp(s
, "!") == 0))
1866 else if (strcmp(s
, "get") == 0)
1870 else if (strcmp(s
, "save") == 0)
1874 else if (strcmp(s
, "depth") == 0)
1876 dsp
->ChangeSearchDepth(sx
+ strlen("depth"));
1878 else if (strcmp(s
, "sd") == 0)
1880 dsp
->ChangeSearchDepth(sx
+ strlen("sd"));
1882 else if (strcmp(s
, "hashdepth") == 0)
1884 dsp
->ChangeHashDepth();
1886 else if (strcmp(s
, "random") == 0)
1890 else if (strcmp(s
, "hard") == 0)
1894 else if (strcmp(s
, "easy") == 0)
1896 flag
.easy
= !flag
.easy
;
1898 else if (strcmp(s
, "option") == 0)
1902 if (sscanf(sx
, "option tsume=%d", &value
) == 1) {
1903 flag
.tsume
= (value
!= 0);
1906 if (sscanf(sx
, "option Use hash-file=%d", &value
) == 1) {
1907 flag
.hash
= (value
!= 0);
1910 if (sscanf(sx
, "option Hash-file search depth=%hd", &HashDepth
) == 1)
1912 if (sscanf(sx
, "option Hash-file move number=%hd", &HashMoveLimit
) == 1)
1914 if (sscanf(sx
, "option contempt=%hd", &contempt
) == 1)
1916 dsp
->AlwaysShowMessage("Unknown '%s'", sx
);
1919 else if (strcmp(s
, "tsume") == 0)
1921 flag
.tsume
= !flag
.tsume
;
1923 else if (strcmp(s
, "contempt") == 0)
1927 else if (strcmp(s
, "xwndw") == 0)
1929 dsp
->ChangeXwindow();
1931 else if (!XSHOGI
&& strcmp(s
, "moves") == 0)
1938 extern unsigned short PrVar
[MAXDEPTH
];
1940 SwagHt
= (GameList
[GameCnt
].gmove
== PrVar
[1])
1947 dsp
->ShowMessage("Testing MoveList Speed");
1948 temp
= generate_move_flags
;
1949 generate_move_flags
= true;
1950 TestSpeed(MoveList
, 1);
1951 generate_move_flags
= temp
;
1952 dsp
->ShowMessage("Testing CaptureList Speed");
1953 TestSpeed(CaptureList
, 1);
1954 dsp
->ShowMessage("Testing Eval Speed");
1955 ExaminePosition(opponent
);
1956 TestPSpeed(ScorePosition
, 1);
1958 else if (!XSHOGI
&& strcmp(s
, "test") == 0)
1961 dsp
->ShowMessage("Testing MoveList Speed");
1962 TestSpeed(MoveList
, 2000);
1963 dsp
->ShowMessage("Testing CaptureList Speed");
1964 TestSpeed(CaptureList
, 3000);
1965 dsp
->ShowMessage("Testing Eval Speed");
1966 ExaminePosition(opponent
);
1967 TestPSpeed(ScorePosition
, 1500);
1969 dsp
->ShowMessage("Testing MoveList Speed");
1970 TestSpeed(MoveList
, 20000);
1971 dsp
->ShowMessage("Testing CaptureList Speed");
1972 TestSpeed(CaptureList
, 30000);
1973 dsp
->ShowMessage("Testing Eval Speed");
1974 ExaminePosition(opponent
);
1975 TestPSpeed(ScorePosition
, 15000);
1978 else if (!XSHOGI
&& strcmp(s
, "p") == 0)
1980 dsp
->ShowPostnValues();
1982 else if (!XSHOGI
&& strcmp(s
, "debug") == 0)
1988 if (strcmp(s
, "usermove") == 0 && sx
[8] == ' ')
1989 sscanf(sx
+ 9, "%s", s
);
1995 else if ((ok
= VerifyMove(s
, VERIFY_AND_MAKE_MODE
, &mv
)))
1997 /* check for repetition */
1998 short rpt
= repetition();
2002 DRAW
= DRAW_REPETITION
;
2003 dsp
->ShowMessage(DRAW
);
2004 GameList
[GameCnt
].flags
|= draw
;
2018 ElapsedTime(COMPUTE_AND_INIT_MODE
);
2022 computer
= opponent
;
2023 opponent
= computer
^ 1;
2028 /* add remaining time in milliseconds for xshogi */
2031 printf("%d. %s %ld\n",
2032 ++mycnt2
, s
, TimeControl
.clock
[player
] * 10);