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
39 #if defined HAVE_GETTIMEOFDAY
46 #include <sys/param.h>
47 #include <sys/types.h>
55 struct display
*dsp
= &raw_display
;
61 movealgbr(short m
, char *s
)
64 short piece
= 0, flag
= 0;
77 piece
= f
- NO_SQUARES
;
79 if (piece
> NO_PIECES
)
82 flag
= (dropmask
| piece
);
97 *s
= COL_NAME(column(t
));
99 *s
= ROW_NAME(row(t
));
104 *s
= COL_NAME(column(f
));
106 *s
= ROW_NAME(row(f
));
108 *s
= COL_NAME(column(t
));
110 *s
= ROW_NAME(row(t
));
129 #endif /* BOOKTEST */
133 * Generate move strings in different formats.
136 * - f piece to be moved
137 * - 0 < f < NO_SQUARES source square
138 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
139 * - t & 0x7f target square
140 * - t & 0x80 promotion flag
142 * - if flag & dropmask, piece type encoded in flag & pmask
144 * FIXME: that makes 2 ways to specify drops and promotions, why ?
151 algbr(short f
, short t
, short flag
)
157 piece
= f
- NO_SQUARES
;
159 if (f
> (NO_SQUARES
+ NO_PIECES
))
162 flag
= (dropmask
| piece
);
171 if ((f
== t
) && ((f
!= 0) || (t
!= 0)))
174 dsp
->Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t
, flag
);
177 mvstr
[0][0] = mvstr
[1][0] = mvstr
[2][0] = mvstr
[3][0] = '\0';
179 else if ((flag
& dropmask
) != 0)
181 short piece
= flag
& pmask
;
183 mvstr
[0][0] = pxx
[piece
];
185 mvstr
[0][2] = COL_NAME(column(t
));
186 mvstr
[0][3] = ROW_NAME(row(t
));
188 strcpy(mvstr
[1], mvstr
[0]);
189 strcpy(mvstr
[2], mvstr
[0]);
190 strcpy(mvstr
[3], mvstr
[0]);
192 else if ((f
!= 0) || (t
!= 0))
194 /* pure coordinates notation */
195 mvstr
[0][0] = COL_NAME(column(f
));
196 mvstr
[0][1] = ROW_NAME(row(f
));
197 mvstr
[0][2] = COL_NAME(column(t
));
198 mvstr
[0][3] = ROW_NAME(row(t
));
201 /* algebraic notation without disambiguation */
202 mvstr
[1][0] = pxx
[board
[f
]];
203 mvstr
[1][1] = mvstr
[0][2]; /* to column */
204 mvstr
[1][2] = mvstr
[0][3]; /* to row */
207 /* algebraic notation with row disambiguation */
208 mvstr
[2][0] = mvstr
[1][0];
209 mvstr
[2][1] = mvstr
[0][1];
210 mvstr
[2][2] = mvstr
[0][2]; /* to column */
211 mvstr
[2][3] = mvstr
[0][3]; /* to row */
214 /* algebraic notation with column disambiguation */
215 strcpy(mvstr
[3], mvstr
[2]);
216 mvstr
[3][1] = mvstr
[0][0];
220 strcat(mvstr
[0], "+");
221 strcat(mvstr
[1], "+");
222 strcat(mvstr
[2], "+");
223 strcat(mvstr
[3], "+");
228 mvstr
[0][0] = mvstr
[1][0] = mvstr
[2][0] = mvstr
[3][0] = '\0';
234 * Compare the string 's' to the list of legal moves available for the
235 * opponent. If a match is found, make the move on the board.
239 VerifyMove(char *s
, VerifyMove_mode iop
, unsigned short *mv
)
241 static short pnt
, tempb
, tempc
, tempsf
, tempst
, cnt
;
242 static struct leaf xnode
;
244 short i
, l
, local_flags
;
247 /* check and remove quality flags */
248 for (i
= local_flags
= 0, l
= strlen(s
); i
< l
; i
++)
253 local_flags
|= badmove
;
258 local_flags
|= goodmove
;
264 local_flags
|= difficult
;
273 if (iop
== UNMAKE_MODE
)
275 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
281 if (iop
== VERIFY_AND_MAKE_MODE
)
282 generate_move_flags
= true;
284 MoveList(opponent
, 2, -1, true);
285 generate_move_flags
= false;
288 while (pnt
< TrPnt
[3])
291 algbr(node
->f
, node
->t
, (short) node
->flags
);
293 if ((strcmp(s
, mvstr
[0]) == 0)
294 || (strcmp(s
, mvstr
[1]) == 0)
295 || (strcmp(s
, mvstr
[2]) == 0)
296 || (strcmp(s
, mvstr
[3]) == 0))
303 if ((cnt
== 1) && (xnode
.score
> DONTUSE
))
307 MakeMove(opponent
, &xnode
, &tempb
, &tempc
,
308 &tempsf
, &tempst
, &INCscore
);
310 if (SqAttacked(PieceList
[opponent
][0], computer
, &blocked
))
312 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
313 dsp
->AlwaysShowMessage("Illegal move (in check) %s", s
);
318 if (iop
== VERIFY_AND_TRY_MODE
)
321 dsp
->UpdateDisplay(xnode
.f
, xnode
.t
, 0, (short) xnode
.flags
);
322 GameList
[GameCnt
].depth
= GameList
[GameCnt
].score
= 0;
323 GameList
[GameCnt
].nodes
= 0;
324 ElapsedTime(COMPUTE_AND_INIT_MODE
);
325 GameList
[GameCnt
].time
= (short) (et
+ 50)/100;
326 GameList
[GameCnt
].flags
|= local_flags
;
330 TimeControl
.clock
[opponent
] -= et
;
331 timeopp
[oppptr
] = et
;
332 --TimeControl
.moves
[opponent
];
335 *mv
= (xnode
.f
<< 8) | xnode
.t
;
336 algbr(xnode
.f
, xnode
.t
, false);
338 /* in force mode, check for mate conditions */
341 if (IsCheckmate(opponent
^ 1, -1, -1))
345 sprintf(buf
, "%s mates!\n", ColorStr
[opponent
]);
346 dsp
->ShowMessage(buf
);
355 dsp
->AlwaysShowMessage("Illegal move (no match) %s", s
);
357 if (!XSHOGI
&& (cnt
> 1))
359 sprintf(buffer
, "Ambiguous Move %s!", s
);
360 dsp
->ShowMessage(buffer
);
368 parser(char *f
, short *fpiece
)
376 for (i
= 1, *fpiece
= no_piece
; i
< NO_PIECES
; i
++)
378 if (f
[0] == pxx
[i
] || f
[0] == qxx
[i
])
380 *fpiece
= (p
? promoted
[i
] : unpromoted
[i
]);
385 if (f
[1] == '*' || f
[1] == '\'')
390 return ((NO_SQUARES
+ *fpiece
) << 8) | locn(r2
, c2
);
398 p
= (f
[5] == '+') ? 0x80 : 0;
400 return (locn(r1
, c1
) << 8) | locn(r2
, c2
) | p
;
408 while (*InPtr
!= ' ')
411 while (*InPtr
== ' ')
419 while (*InPtr
== ' ')
424 void RequestInputString(char* buffer
, unsigned bufsize
)
427 int ret
= snprintf(fmt
, sizeof(fmt
), "%%%us", bufsize
);
429 perror("RequestInputString snprintf");
432 if (ret
>= sizeof(fmt
)) {
434 "Insufficient format-buffer size in %s for bufsize=%u\n",
435 __FUNCTION__
, bufsize
);
438 dsp
->doRequestInputString(fmt
, buffer
);
452 strcpy(fname
, savefile
);
454 dsp
->ShowMessage("Enter file name: ");
455 RequestInputString(fname
, sizeof(fname
)-1);
458 if (fname
[0] == '\0')
459 strcpy(fname
, "shogi.000");
461 if ((fd
= fopen(fname
, "r")) != NULL
)
464 fgets(fname
, 256, fd
);
465 computer
= opponent
= black
;
474 /* FIXME: write a skipn() function so that we can get
475 * 3 skips by doing skipn(3) */
479 Game50
= atoi(InPtr
);
481 flag
.force
= (*InPtr
== 'f');
482 fgets(fname
, 256, fd
); /* empty */
483 fgets(fname
, 256, fd
);
486 TCflag
= atoi(InPtr
);
490 OperatorTime
= atoi(InPtr
);
491 fgets(fname
, 256, fd
);
494 TimeControl
.clock
[black
] = atol(InPtr
);
497 TimeControl
.moves
[black
] = atoi(InPtr
);
498 fgets(fname
, 256, fd
);
501 TimeControl
.clock
[white
] = atol(InPtr
);
504 TimeControl
.moves
[white
] = atoi(InPtr
);
505 fgets(fname
, 256, fd
); /* empty */
507 for (i
= NO_ROWS
- 1; i
> -1; i
--)
509 fgets(fname
, 256, fd
);
513 for (j
= 0; j
< NO_COLS
; j
++)
515 sq
= i
* NO_COLS
+ j
;
521 board
[sq
] = no_piece
;
526 for (c
= 0; c
< NO_PIECES
; c
++)
531 board
[sq
] = promoted
[c
];
533 board
[sq
] = unpromoted
[c
];
539 for (c
= 0; c
< NO_PIECES
; c
++)
544 board
[sq
] = promoted
[c
];
546 board
[sq
] = unpromoted
[c
];
554 Mvboard
[sq
] = atoi(InPtr
);
559 fgets(fname
, 256, fd
); /* empty */
560 fgets(fname
, 256, fd
); /* 9 8 7 ... */
561 fgets(fname
, 256, fd
); /* empty */
562 fgets(fname
, 256, fd
); /* p l n ... */
565 for (side
= 0; side
<= 1; side
++)
567 fgets(fname
, 256, fd
);
571 Captured
[side
][pawn
] = atoi(InPtr
);
574 Captured
[side
][lance
] = atoi(InPtr
);
576 Captured
[side
][knight
] = atoi(InPtr
);
579 Captured
[side
][silver
] = atoi(InPtr
);
581 Captured
[side
][gold
] = atoi(InPtr
);
583 Captured
[side
][bishop
] = atoi(InPtr
);
585 Captured
[side
][rook
] = atoi(InPtr
);
587 Captured
[side
][king
] = atoi(InPtr
);
591 flag
.regularstart
= true;
593 fgets(fname
, 256, fd
); /* empty */
594 fgets(fname
, 256, fd
); /* move score ... */
596 while (fgets(fname
, 256, fd
))
605 g
= &GameList
[GameCnt
];
606 g
->gmove
= parser(InPtr
, &g
->fpiece
);
608 g
->score
= atoi(InPtr
);
610 g
->depth
= atoi(InPtr
);
612 g
->nodes
= atol(InPtr
);
614 g
->time
= atol(InPtr
);
616 g
->flags
= c
= atoi(InPtr
);
618 g
->hashkey
= strtol(InPtr
, (char **) NULL
, 16);
620 g
->hashbd
= strtol(InPtr
, (char **) NULL
, 16);
628 for (piece
= no_piece
, i
= 0; i
< NO_PIECES
; i
++)
630 if (pxx
[i
] == *InPtr
)
638 g
->color
= ((*InPtr
== 'W') ? white
: black
);
640 g
->piece
= (*InPtr
== '+'
642 : unpromoted
[piece
]);
651 if (TimeControl
.clock
[black
] > 0)
659 dsp
->UpdateDisplay(0, 0, 1, 0);
670 short sq
, i
, c
, f
, t
;
673 char empty
[2] = "\n";
676 strcpy(fname
, savefile
);
678 dsp
->ShowMessage("Enter file name: ");
679 RequestInputString(fname
, sizeof(fname
)-1);
682 if (fname
[0] == '\0')
683 strcpy(fname
, "shogi.000");
685 if ((fd
= fopen(fname
, "w")) != NULL
)
690 if (computer
== white
)
693 if (computer
== black
)
696 fprintf(fd
, "White %s Black %s %d %s\n", w
, b
, Game50
,
697 flag
.force
? "force" : "");
699 fprintf(fd
, "TimeControl %d Operator Time %d\n", TCflag
, OperatorTime
);
700 fprintf(fd
, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
701 TimeControl
.clock
[black
], TimeControl
.moves
[black
],
702 TimeControl
.clock
[white
], TimeControl
.moves
[white
]);
705 for (i
= NO_ROWS
- 1; i
> -1; i
--)
707 fprintf(fd
, "%c ", ROW_NAME(i
));
709 for (c
= 0; c
< NO_COLS
; c
++)
711 sq
= i
* NO_COLS
+ c
;
713 p
= is_promoted
[piece
] ? '+' : ' ';
714 fprintf(fd
, "%c", p
);
730 fprintf(fd
, "%c", p
);
735 for (f
= i
* NO_COLS
; f
< i
* NO_COLS
+ NO_ROWS
; f
++)
736 fprintf(fd
, " %d", Mvboard
[f
]);
743 fprintf(fd
, " 9 8 7 6 5 4 3 2 1\n");
745 fprintf(fd
, " p l n s g b r k\n");
747 fprintf(fd
, " 5 4 3 2 1\n");
749 fprintf(fd
, " p s g b r k\n");
752 for (side
= 0; side
<= 1; side
++)
754 fprintf(fd
, "%c", (side
== black
) ? 'B' : 'W');
755 fprintf(fd
, " %2d", Captured
[side
][pawn
]);
757 fprintf(fd
, " %2d", Captured
[side
][lance
]);
758 fprintf(fd
, " %2d", Captured
[side
][knight
]);
760 fprintf(fd
, " %2d", Captured
[side
][silver
]);
761 fprintf(fd
, " %2d", Captured
[side
][gold
]);
762 fprintf(fd
, " %2d", Captured
[side
][bishop
]);
763 fprintf(fd
, " %2d", Captured
[side
][rook
]);
764 fprintf(fd
, " %2d", Captured
[side
][king
]);
769 fputs(" move score depth nodes time flags capture\n", fd
);
771 for (i
= 1; i
<= GameCnt
; i
++)
773 struct GameRec
*g
= &GameList
[i
];
776 t
= (g
->gmove
& 0xFF);
777 algbr(f
, t
, g
->flags
);
779 fprintf(fd
, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
782 : (is_promoted
[g
->fpiece
] ? '+' : ' ')),
784 ((f
> NO_SQUARES
) ? &mvstr
[0][1] : mvstr
[0]),
786 g
->nodes
, g
->time
, g
->flags
,
787 g
->hashkey
, g
->hashbd
);
789 if (g
->piece
!= no_piece
)
791 fprintf(fd
, " %c %s %c\n",
792 pxx
[g
->piece
], ColorStr
[g
->color
],
793 (is_promoted
[g
->piece
] ? '+' : ' '));
803 dsp
->ShowMessage("Game saved");
807 dsp
->ShowMessage("Could not open file");
813 * GetXGame, SaveXGame and BookGame used to only be defined if
814 * xshogi wasn't defined -- wonder why?
826 dsp
->ShowMessage("Enter file name: ");
827 RequestInputString(fname
, sizeof(fname
)-1);
829 if (fname
[0] == '\0')
830 strcpy(fname
, "xshogi.position.read");
832 if ((fd
= fopen(fname
, "r")) != NULL
)
835 flag
.regularstart
= false;
838 /* xshogi position file ... */
839 fgets(fname
, 256, fd
);
844 if (strcmp(fname
, "xshogi"))
848 /* -- empty line -- */
849 fgets(fname
, 256, fd
);
850 /* -- empty line -- */
851 fgets(fname
, 256, fd
);
853 for (i
= NO_ROWS
- 1; i
> -1; i
--)
855 fgets(fname
, 256, fd
);
858 for (j
= 0; j
< NO_COLS
; j
++)
860 sq
= i
* NO_COLS
+ j
;
866 board
[sq
] = no_piece
;
871 for (c
= 0; c
< NO_PIECES
; c
++)
876 board
[sq
] = promoted
[c
];
878 board
[sq
] = unpromoted
[c
];
884 for (c
= 0; c
< NO_PIECES
; c
++)
889 board
[sq
] = promoted
[c
];
891 board
[sq
] = unpromoted
[c
];
904 for (side
= 0; side
<= 1; side
++)
906 fgets(fname
, 256, fd
);
908 Captured
[side
][pawn
] = atoi(InPtr
);
911 Captured
[side
][lance
] = atoi(InPtr
);
913 Captured
[side
][knight
] = atoi(InPtr
);
916 Captured
[side
][silver
] = atoi(InPtr
);
918 Captured
[side
][gold
] = atoi(InPtr
);
920 Captured
[side
][bishop
] = atoi(InPtr
);
922 Captured
[side
][rook
] = atoi(InPtr
);
924 Captured
[side
][king
] = atoi(InPtr
);
927 if (fgets(fname
, 256, fd
) != NULL
&& strncmp(fname
, "white", 5) == 0)
940 dsp
->UpdateDisplay(0, 0, 1, 0);
955 dsp
->ShowMessage("Enter file name: ");
956 RequestInputString(fname
, sizeof(fname
)-1);
958 if (fname
[0] == '\0')
959 strcpy(fname
, "xshogi.position.read");
961 if ((fd
= fopen(fname
, "w")) != NULL
)
963 fputs("# xshogi position file -- \n", fd
);
967 for (i
= NO_ROWS
- 1; i
> -1; i
--)
971 for (j
= 0; j
< NO_COLS
; j
++)
973 sq
= i
* NO_COLS
+ j
;
975 isp
= is_promoted
[piece
];
976 *p
= (isp
? '+' : ' ');
979 if (piece
== no_piece
)
981 else if (color
[sq
] == white
)
994 for (side
= 0; side
<= 1; side
++)
998 "%d %d %d %d %d %d %d %d\n",
1000 "%d %d %d %d %d %d\n",
1002 Captured
[side
][pawn
],
1004 Captured
[side
][lance
],
1005 Captured
[side
][knight
],
1007 Captured
[side
][silver
],
1008 Captured
[side
][gold
],
1009 Captured
[side
][bishop
],
1010 Captured
[side
][rook
],
1011 Captured
[side
][king
]);
1016 if (computer
== black
)
1017 fputs("white to play\n", fd
);
1019 fputs("black to play\n", fd
);
1030 char fname
[256], sflags
[4];
1034 strcpy(fname
, savefile
);
1036 /* Enter file name */
1037 dsp
->ShowMessage("Enter file name: ");
1038 RequestInputString(fname
, sizeof(fname
)-1);
1041 if (fname
[0] == '\0')
1044 if ((fd
= fopen(fname
, "a")) != NULL
)
1048 for (i
= 1; i
<= GameCnt
; i
++)
1050 struct GameRec
*g
= &GameList
[i
];
1051 char mvnr
[20], mvs
[20];
1054 sprintf(mvnr
, "%d.", (i
+ 1)/2);
1059 t
= (g
->gmove
& 0xFF);
1060 algbr(f
, t
, g
->flags
);
1063 /* determine move quality string */
1064 if (g
->flags
& goodmove
)
1067 if (g
->flags
& badmove
)
1070 #ifdef EASY_OPENINGS
1071 if (g
->flags
& difficult
)
1077 /* determine move string */
1080 sprintf(mvs
, "%s%s ", &mvstr
[0][1], sflags
);
1084 sprintf(mvs
, "%c%c%c%c%c%s%s ",
1085 mvstr
[0][0], mvstr
[0][1],
1086 (g
->flags
& capture
) ? 'x' : '-',
1087 mvstr
[0][2], mvstr
[0][3],
1088 (mvstr
[0][4] == '+') ? "+" : "",
1092 fprintf(fd
, "%s%s%c%s",
1096 : (is_promoted
[g
->fpiece
] ? "+" : "")),
1109 dsp
->ShowMessage("Game saved");
1113 dsp
->ShowMessage("Could not open file");
1124 char fname
[256], dbuf
[256];
1128 strcpy(fname
, listfile
);
1133 strncpy(dbuf
, ctime(&when
), 20);
1140 /* use format "CL.Jan01-020304B" when
1143 program played white */
1145 sprintf(fname
, "CL.%s%s-%s%s%s%c",
1146 dbuf
+ 4, dbuf
+ 8, dbuf
+ 11, dbuf
+ 14,
1147 dbuf
+ 17, ColorStr
[computer
][0]);
1149 /* replace space padding with 0 */
1150 for (i
= 0; fname
[i
] != '\0'; i
++)
1152 if (fname
[i
] == ' ')
1157 fd
= fopen(fname
, "w");
1161 printf("Open failure for file: %s", fname
);
1165 fprintf(fd
, "gnushogi %s game\n", PACKAGE_VERSION
);
1166 fputs(" score depth nodes time ", fd
);
1167 fputs(" score depth nodes time\n", fd
);
1169 for (i
= 1; i
<= GameCnt
; i
++)
1171 f
= GameList
[i
].gmove
>> 8;
1172 t
= (GameList
[i
].gmove
& 0xFF);
1173 algbr(f
, t
, GameList
[i
].flags
);
1175 if (GameList
[i
].flags
& book
)
1177 fprintf(fd
, "%c%c%-5s %5d Book%7ld %5ld",
1180 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1181 pxx
[GameList
[i
].fpiece
],
1183 ? &mvstr
[0][1] : mvstr
[0]),
1190 fprintf(fd
, "%c%c%-5s %5d %2d %7ld %5ld",
1193 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1194 pxx
[GameList
[i
].fpiece
],
1195 (f
> NO_SQUARES
? &mvstr
[0][1] : mvstr
[0]),
1196 GameList
[i
].score
, GameList
[i
].depth
,
1197 GameList
[i
].nodes
, GameList
[i
].time
);
1210 fprintf(fd
, "\n\n");
1212 if (GameList
[GameCnt
].flags
& draw
)
1214 fprintf(fd
, "Draw %s\n", DRAW
);
1216 if (DRAW
== DRAW_REPETITION
)
1220 fprintf(fd
, "repetition by positions ");
1222 for (j
= GameCnt
- 1; j
>= Game50
; j
-= 2)
1224 if (GameList
[j
].hashkey
== hashkey
&&
1225 GameList
[j
].hashbd
== hashbd
)
1226 fprintf(fd
, "%d ", j
);
1232 else if (GameList
[GameCnt
].score
== -(SCORE_LIMIT
+ 999))
1234 fprintf(fd
, "%s\n", ColorStr
[player
]);
1236 else if (GameList
[GameCnt
].score
== (SCORE_LIMIT
+ 998))
1238 fprintf(fd
, "%s\n", ColorStr
[player
^ 1]);
1251 GameList
[GameCnt
].flags
|= badmove
;
1255 GameList
[GameCnt
].flags
|= goodmove
;
1258 #ifdef EASY_OPENINGS
1260 GameList
[GameCnt
].flags
|= difficult
;
1268 * Undo the most recent half-move.
1276 f
= GameList
[GameCnt
].gmove
>> 8;
1277 t
= GameList
[GameCnt
].gmove
& 0x7F;
1281 /* the move was a drop */
1282 Captured
[color
[t
]][board
[t
]]++;
1283 board
[t
] = no_piece
;
1289 if (GameList
[GameCnt
].flags
& promote
)
1290 board
[f
] = unpromoted
[board
[t
]];
1292 board
[f
] = board
[t
];
1294 color
[f
] = color
[t
];
1295 board
[t
] = GameList
[GameCnt
].piece
;
1296 color
[t
] = GameList
[GameCnt
].color
;
1298 if (board
[t
] != no_piece
)
1299 Captured
[color
[f
]][unpromoted
[board
[t
]]]--;
1301 if (color
[t
] != neutral
)
1309 if (TCflag
&& (TCmoves
> 1))
1310 ++TimeControl
.moves
[color
[f
]];
1312 hashkey
= GameList
[GameCnt
].hashkey
;
1313 hashbd
= GameList
[GameCnt
].hashbd
;
1315 computer
= computer
^ 1;
1316 opponent
= opponent
^ 1;
1319 player
= player
^ 1;
1320 dsp
->ShowSidetoMove();
1321 dsp
->UpdateDisplay(0, 0, 1, 0);
1323 if (flag
.regularstart
)
1329 TestSpeed(void(*f
)(short side
, short ply
,
1330 short in_check
, short blockable
),
1340 #ifdef HAVE_GETTIMEOFDAY
1344 #ifdef HAVE_GETTIMEOFDAY
1345 gettimeofday(&tv
, NULL
);
1346 t1
= (tv
.tv_sec
*100 + (tv
.tv_usec
/10000));
1351 for (i
= 0; i
< j
; i
++)
1353 f(opponent
, 2, -1, true);
1356 for (jj
= TrPnt
[2]; i
< TrPnt
[3]; jj
++)
1358 if (!pick(jj
, TrPnt
[3] - 1))
1364 #ifdef HAVE_GETTIMEOFDAY
1365 gettimeofday(&tv
, NULL
);
1366 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1371 cnt
= j
* (TrPnt
[3] - TrPnt
[2]);
1378 dsp
->ShowNodeCnt(cnt
);
1383 TestPSpeed(short(*f
) (short side
), unsigned j
)
1387 #ifdef HAVE_GETTIMEOFDAY
1391 #ifdef HAVE_GETTIMEOFDAY
1392 gettimeofday(&tv
, NULL
);
1393 t1
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1398 for (i
= 0; i
< j
; i
++)
1401 #ifdef HAVE_GETTIMEOFDAY
1402 gettimeofday(&tv
, NULL
);
1403 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1415 dsp
->ShowNodeCnt(cnt
);
1420 SetOppTime(char *time
)
1425 t
= (int)strtol(time
, &time
, 10);
1430 /* FIXME: sec is parsed but ignored */
1431 sec
= (int)strtol(time
, &time
, 10);
1434 m
= (int)strtol(time
, &time
, 10);
1437 TimeControl
.clock
[opponent
] = t
;
1440 TimeControl
.moves
[opponent
] = m
;
1442 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1446 /* just to inform xshogi about availability of otime command */
1447 printf("otime %d %d\n", t
, m
);
1453 SetMachineTime(char *time
)
1458 t
= (int)strtol(time
, &time
, 10);
1463 /* FIXME: sec is parsed but ignored */
1464 sec
= (int)strtol(time
, &time
, 10);
1467 m
= (int)strtol(time
, &time
, 10);
1470 TimeControl
.clock
[computer
] = t
;
1473 TimeControl
.moves
[computer
] = m
;
1475 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1479 /* just to inform xshogi about availability of time command */
1480 printf("time %d %d\n", t
, m
);
1485 /* FIXME! This is truly the function from hell! */
1488 * Process the user's command. If easy mode is OFF (the computer is thinking
1489 * on opponents time) and the program is out of book, then make the 'hint'
1490 * move on the board and call SelectMove() to find a response. The user
1491 * terminates the search by entering a command. If the opponent does not make
1492 * the hint move, then set Sdepth to zero.
1496 InputCommand(char *command
)
1498 short have_shown_prompt
= false;
1499 short ok
, done
, is_move
= false;
1503 ok
= flag
.quit
= done
= false;
1507 if (TTadd
> ttbllimit
)
1511 if ((hint
> 0) && !flag
.easy
&& !flag
.force
)
1514 * A hint move for the player is available. Compute a move for the
1515 * opponent in background mode assuming that the hint move will be
1516 * selected by the player.
1519 ft
= time0
; /* Save reference time for the player. */
1521 algbr((short) hint
>> 8, (short) hint
& 0xff, false);
1522 strcpy(s
, mvstr
[0]);
1527 /* do the hint move */
1528 if (VerifyMove(s
, VERIFY_AND_TRY_MODE
, &mv
))
1532 #ifdef QUIETBACKGROUND
1534 have_shown_prompt
= true;
1535 #endif /* QUIETBACKGROUND */
1537 /* Start computing a move until the search is interrupted. */
1539 #ifdef INTERRUPT_TEST
1543 /* would love to put null move in here */
1544 /* after we make the hint move make a 2 ply search
1545 * with both plys our moves */
1546 /* think on opponents time */
1547 SelectMove(computer
, BACKGROUND_MODE
);
1549 #ifdef INTERRUPT_TEST
1550 ElapsedTime(COMPUTE_INTERRUPT_MODE
);
1554 printf("searching not terminated by interrupt!\n");
1558 printf("elapsed time from interrupt to "
1559 "terminating search: %ld\n", it
);
1563 /* undo the hint and carry on */
1564 VerifyMove(s
, UNMAKE_MODE
, &mv
);
1568 time0
= ft
; /* Restore reference time for the player. */
1571 while(!(ok
|| flag
.quit
|| done
))
1575 #ifdef QUIETBACKGROUND
1576 if (!have_shown_prompt
)
1578 #endif /* QUIETBACKGROUND */
1582 #ifdef QUIETBACKGROUND
1585 have_shown_prompt
= false;
1586 #endif /* QUIETBACKGROUND */
1588 if (command
== NULL
) {
1589 int eof
= dsp
->GetString(sx
);
1593 strcpy(sx
, command
);
1597 /* extract first word */
1598 if (sscanf(sx
, "%s", s
) < 1)
1601 if (strcmp(s
, "bd") == 0) /* bd -- display board */
1603 /* FIXME: Hack alert! */
1604 short old_xshogi
= XSHOGI
;
1607 display_type
= DISPLAY_RAW
;
1610 dsp
->UpdateDisplay(0, 0, 1, 0);
1613 display_type
= DISPLAY_X
;
1615 else if (strcmp(s
, "post") == 0)
1617 flag
.post
= !flag
.post
;
1619 else if (strcmp(s
, "alg") == 0)
1623 else if ((strcmp(s
, "quit") == 0)
1624 || (strcmp(s
, "exit") == 0))
1628 else if ((strcmp(s
, "set") == 0)
1629 || (strcmp(s
, "edit") == 0))
1633 else if (strcmp(s
, "setup") == 0)
1637 else if (strcmp(s
, "first") == 0)
1641 else if (strcmp(s
, "go") == 0)
1646 if (computer
== black
)
1657 else if (strcmp(s
, "help") == 0)
1661 else if (strcmp(s
, "material") == 0)
1663 flag
.material
= !flag
.material
;
1665 else if (strcmp(s
, "force") == 0)
1670 flag
.bothsides
= false;
1674 flag
.force
= !flag
.force
;
1675 flag
.bothsides
= false;
1678 else if (strcmp(s
, "book") == 0)
1680 Book
= Book
? 0 : BOOKFAIL
;
1682 else if (strcmp(s
, "new") == 0)
1685 dsp
->UpdateDisplay(0, 0, 1, 0);
1687 else if (strcmp(s
, "list") == 0)
1691 else if (strcmp(s
, "level") == 0)
1693 dsp
->SelectLevel(sx
+ strlen("level"));
1695 else if (strcmp(s
, "clock") == 0)
1697 dsp
->SelectLevel(sx
+ strlen("clock"));
1699 else if (strcmp(s
, "hash") == 0)
1701 flag
.hash
= !flag
.hash
;
1703 else if (strcmp(s
, "gamein") == 0)
1705 flag
.gamein
= !flag
.gamein
;
1707 else if (strcmp(s
, "beep") == 0)
1709 flag
.beep
= !flag
.beep
;
1711 else if (strcmp(s
, "time") == 0)
1713 SetMachineTime(sx
+ strlen("time"));
1715 else if (strcmp(s
, "otime") == 0)
1717 SetOppTime(sx
+ strlen("otime"));
1719 else if (strcmp(s
, "Awindow") == 0)
1721 dsp
->ChangeAlphaWindow();
1723 else if (strcmp(s
, "Bwindow") == 0)
1725 dsp
->ChangeBetaWindow();
1727 else if (strcmp(s
, "rcptr") == 0)
1729 flag
.rcptr
= !flag
.rcptr
;
1731 else if (strcmp(s
, "hint") == 0)
1735 else if (strcmp(s
, "both") == 0)
1737 flag
.bothsides
= !flag
.bothsides
;
1740 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1741 SelectMove(opponent
, FOREGROUND_MODE
);
1744 else if (strcmp(s
, "reverse") == 0)
1746 flag
.reverse
= !flag
.reverse
;
1748 dsp
->UpdateDisplay(0, 0, 1, 0);
1750 else if (strcmp(s
, "switch") == 0)
1752 computer
= computer
^ 1;
1753 opponent
= opponent
^ 1;
1754 xwndw
= (computer
== black
) ? WXWNDW
: BXWNDW
;
1759 else if (strcmp(s
, "black") == 0)
1768 * ok = true; don't automatically start with black command
1771 else if (strcmp(s
, "white") == 0)
1780 * ok = true; don't automatically start with white command
1783 else if (strcmp(s
, "undo") == 0 && GameCnt
> 0)
1787 else if (strcmp(s
, "remove") == 0 && GameCnt
> 1)
1792 /* CHECKME: are these next three correct? */
1793 else if (!XSHOGI
&& strcmp(s
, "xget") == 0)
1797 else if (!XSHOGI
&& strcmp(s
, "xsave") == 0)
1801 else if (!XSHOGI
&& strcmp(s
, "bsave") == 0)
1805 #ifdef EASY_OPENINGS
1806 else if ((strcmp(s
, "?") == 0)
1807 || (strcmp(s
, "!") == 0)
1808 || (strcmp(s
, "~") == 0))
1810 else if ((strcmp(s
, "?") == 0)
1811 || (strcmp(s
, "!") == 0))
1816 else if (strcmp(s
, "get") == 0)
1820 else if (strcmp(s
, "save") == 0)
1824 else if (strcmp(s
, "depth") == 0)
1826 dsp
->ChangeSearchDepth(sx
+ strlen("depth"));
1828 else if (strcmp(s
, "sd") == 0)
1830 dsp
->ChangeSearchDepth(sx
+ strlen("sd"));
1832 else if (strcmp(s
, "hashdepth") == 0)
1834 dsp
->ChangeHashDepth();
1836 else if (strcmp(s
, "random") == 0)
1840 else if (strcmp(s
, "hard") == 0)
1844 else if (strcmp(s
, "easy") == 0)
1846 flag
.easy
= !flag
.easy
;
1848 else if (strcmp(s
, "tsume") == 0)
1850 flag
.tsume
= !flag
.tsume
;
1852 else if (strcmp(s
, "contempt") == 0)
1856 else if (strcmp(s
, "xwndw") == 0)
1858 dsp
->ChangeXwindow();
1860 else if (strcmp(s
, "rv") == 0)
1863 dsp
->UpdateDisplay(0, 0, 1, 0);
1865 else if (strcmp(s
, "coords") == 0)
1867 flag
.coords
= !flag
.coords
;
1868 dsp
->UpdateDisplay(0, 0, 1, 0);
1870 else if (strcmp(s
, "stars") == 0)
1872 flag
.stars
= !flag
.stars
;
1873 dsp
->UpdateDisplay(0, 0, 1, 0);
1875 else if (!XSHOGI
&& strcmp(s
, "moves") == 0)
1882 extern unsigned short PrVar
[MAXDEPTH
];
1884 SwagHt
= (GameList
[GameCnt
].gmove
== PrVar
[1])
1891 dsp
->ShowMessage("Testing MoveList Speed");
1892 temp
= generate_move_flags
;
1893 generate_move_flags
= true;
1894 TestSpeed(MoveList
, 1);
1895 generate_move_flags
= temp
;
1896 dsp
->ShowMessage("Testing CaptureList Speed");
1897 TestSpeed(CaptureList
, 1);
1898 dsp
->ShowMessage("Testing Eval Speed");
1899 ExaminePosition(opponent
);
1900 TestPSpeed(ScorePosition
, 1);
1902 else if (!XSHOGI
&& strcmp(s
, "test") == 0)
1905 dsp
->ShowMessage("Testing MoveList Speed");
1906 TestSpeed(MoveList
, 2000);
1907 dsp
->ShowMessage("Testing CaptureList Speed");
1908 TestSpeed(CaptureList
, 3000);
1909 dsp
->ShowMessage("Testing Eval Speed");
1910 ExaminePosition(opponent
);
1911 TestPSpeed(ScorePosition
, 1500);
1913 dsp
->ShowMessage("Testing MoveList Speed");
1914 TestSpeed(MoveList
, 20000);
1915 dsp
->ShowMessage("Testing CaptureList Speed");
1916 TestSpeed(CaptureList
, 30000);
1917 dsp
->ShowMessage("Testing Eval Speed");
1918 ExaminePosition(opponent
);
1919 TestPSpeed(ScorePosition
, 15000);
1922 else if (!XSHOGI
&& strcmp(s
, "p") == 0)
1924 dsp
->ShowPostnValues();
1926 else if (!XSHOGI
&& strcmp(s
, "debug") == 0)
1936 else if ((ok
= VerifyMove(s
, VERIFY_AND_MAKE_MODE
, &mv
)))
1938 /* check for repetition */
1939 short rpt
= repetition();
1943 DRAW
= DRAW_REPETITION
;
1944 dsp
->ShowMessage(DRAW
);
1945 GameList
[GameCnt
].flags
|= draw
;
1959 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1963 computer
= opponent
;
1964 opponent
= computer
^ 1;
1969 /* add remaining time in milliseconds for xshogi */
1972 printf("%d. %s %ld\n",
1973 ++mycnt2
, s
, TimeControl
.clock
[player
] * 10);