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>
54 int InBackground
= false;
60 movealgbr(short m
, char *s
)
63 short piece
= 0, flag
= 0;
76 piece
= f
- NO_SQUARES
;
78 if (piece
> NO_PIECES
)
81 flag
= (dropmask
| piece
);
128 #endif /* BOOKTEST */
134 * Generate move strings in different formats.
137 * - f piece to be moved
138 * - 0 < f < NO_SQUARES source square
139 * - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES dropped piece modulo NO_PIECES
140 * - t & 0x7f target square
141 * - t & 0x80 promotion flag
142 * - flag FIXME: must be zero ?
149 algbr(short f
, short t
, short flag
)
155 piece
= f
- NO_SQUARES
;
157 if (f
> (NO_SQUARES
+ NO_PIECES
))
160 flag
= (dropmask
| piece
);
169 if ((f
== t
) && ((f
!= 0) || (t
!= 0)))
172 Printf("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
);
305 AlwaysShowMessage("Illegal move (in check) %s", s
);
310 if (iop
== VERIFY_AND_TRY_MODE
)
313 UpdateDisplay(xnode
.f
, xnode
.t
, 0, (short) xnode
.flags
);
314 GameList
[GameCnt
].depth
= GameList
[GameCnt
].score
= 0;
315 GameList
[GameCnt
].nodes
= 0;
316 ElapsedTime(COMPUTE_AND_INIT_MODE
);
317 GameList
[GameCnt
].time
= (short) (et
+ 50)/100;
318 GameList
[GameCnt
].flags
|= local_flags
;
322 TimeControl
.clock
[opponent
] -= et
;
323 timeopp
[oppptr
] = et
;
324 --TimeControl
.moves
[opponent
];
327 *mv
= (xnode
.f
<< 8) | xnode
.t
;
328 algbr(xnode
.f
, xnode
.t
, false);
330 /* in force mode, check for mate conditions */
333 if (IsCheckmate(opponent
^ 1, -1, -1))
337 sprintf(buf
, "%s mates!\n", ColorStr
[opponent
]);
347 AlwaysShowMessage("Illegal move (no match) %s", s
);
349 if (!barebones
&& (cnt
> 1))
351 sprintf(buffer
, "Ambiguous Move %s!", s
);
361 parser(char *f
, int side
, short *fpiece
)
369 for (i
= 1, *fpiece
= no_piece
; i
< NO_PIECES
; i
++)
371 if (f
[0] == pxx
[i
] || f
[0] == qxx
[i
])
373 *fpiece
= (p
? promoted
[i
] : unpromoted
[i
]);
378 if (f
[1] == '*' || f
[1] == '\'')
383 return ((NO_SQUARES
+ *fpiece
) << 8) | locn(r2
, c2
);
391 p
= (f
[5] == '+') ? 0x80 : 0;
393 return (locn(r1
, c1
) << 8) | locn(r2
, c2
) | p
;
401 while (*InPtr
!= ' ')
404 while (*InPtr
== ' ')
413 while (*InPtr
== ' ')
429 strcpy(fname
, savefile
);
431 ShowMessage("Enter file name: ");
432 RequestInputString(fname
, sizeof(fname
)-1);
435 if (fname
[0] == '\0')
436 strcpy(fname
, "shogi.000");
438 if ((fd
= fopen(fname
, "r")) != NULL
)
441 fgets(fname
, 256, fd
);
442 computer
= opponent
= black
;
451 /* FIXME: write a skipn() function so that we can get
452 * 3 skips by doing skipn(3) */
456 Game50
= atoi(InPtr
);
458 flag
.force
= (*InPtr
== 'f');
459 fgets(fname
, 256, fd
); /* empty */
460 fgets(fname
, 256, fd
);
463 TCflag
= atoi(InPtr
);
467 OperatorTime
= atoi(InPtr
);
468 fgets(fname
, 256, fd
);
471 TimeControl
.clock
[black
] = atol(InPtr
);
474 TimeControl
.moves
[black
] = atoi(InPtr
);
475 fgets(fname
, 256, fd
);
478 TimeControl
.clock
[white
] = atol(InPtr
);
481 TimeControl
.moves
[white
] = atoi(InPtr
);
482 fgets(fname
, 256, fd
); /* empty */
484 for (i
= NO_ROWS
- 1; i
> -1; i
--)
486 fgets(fname
, 256, fd
);
490 for (j
= 0; j
< NO_COLS
; j
++)
492 sq
= i
* NO_COLS
+ j
;
498 board
[sq
] = no_piece
;
503 for (c
= 0; c
< NO_PIECES
; c
++)
508 board
[sq
] = promoted
[c
];
510 board
[sq
] = unpromoted
[c
];
516 for (c
= 0; c
< NO_PIECES
; c
++)
521 board
[sq
] = promoted
[c
];
523 board
[sq
] = unpromoted
[c
];
531 Mvboard
[sq
] = atoi(InPtr
);
536 fgets(fname
, 256, fd
); /* empty */
537 fgets(fname
, 256, fd
); /* 9 8 7 ... */
538 fgets(fname
, 256, fd
); /* empty */
539 fgets(fname
, 256, fd
); /* p l n ... */
542 for (side
= 0; side
<= 1; side
++)
544 fgets(fname
, 256, fd
);
548 Captured
[side
][pawn
] = atoi(InPtr
);
551 Captured
[side
][lance
] = atoi(InPtr
);
553 Captured
[side
][knight
] = atoi(InPtr
);
556 Captured
[side
][silver
] = atoi(InPtr
);
558 Captured
[side
][gold
] = atoi(InPtr
);
560 Captured
[side
][bishop
] = atoi(InPtr
);
562 Captured
[side
][rook
] = atoi(InPtr
);
564 Captured
[side
][king
] = atoi(InPtr
);
568 flag
.regularstart
= true;
570 fgets(fname
, 256, fd
); /* empty */
571 fgets(fname
, 256, fd
); /* move score ... */
573 while (fgets(fname
, 256, fd
))
582 g
= &GameList
[GameCnt
];
583 g
->gmove
= parser(InPtr
, side
, &g
->fpiece
);
585 g
->score
= atoi(InPtr
);
587 g
->depth
= atoi(InPtr
);
589 g
->nodes
= atol(InPtr
);
591 g
->time
= atol(InPtr
);
593 g
->flags
= c
= atoi(InPtr
);
595 g
->hashkey
= strtol(InPtr
, (char **) NULL
, 16);
597 g
->hashbd
= strtol(InPtr
, (char **) NULL
, 16);
605 for (piece
= no_piece
, i
= 0; i
< NO_PIECES
; i
++)
607 if (pxx
[i
] == *InPtr
)
615 g
->color
= ((*InPtr
== 'W') ? white
: black
);
617 g
->piece
= (*InPtr
== '+'
619 : unpromoted
[piece
]);
628 if (TimeControl
.clock
[black
] > 0)
636 UpdateDisplay(0, 0, 1, 0);
648 short sq
, i
, c
, f
, t
;
651 char empty
[2] = "\n";
654 strcpy(fname
, savefile
);
656 ShowMessage("Enter file name: ");
657 RequestInputString(fname
, sizeof(fname
)-1);
660 if (fname
[0] == '\0')
661 strcpy(fname
, "shogi.000");
663 if ((fd
= fopen(fname
, "w")) != NULL
)
668 if (computer
== white
)
671 if (computer
== black
)
674 fprintf(fd
, "White %s Black %s %d %s\n", w
, b
, Game50
,
675 flag
.force
? "force" : "");
677 fprintf(fd
, "TimeControl %d Operator Time %d\n", TCflag
, OperatorTime
);
678 fprintf(fd
, "Black Clock %ld Moves %d\nWhite Clock %ld Moves %d\n",
679 TimeControl
.clock
[black
], TimeControl
.moves
[black
],
680 TimeControl
.clock
[white
], TimeControl
.moves
[white
]);
683 for (i
= NO_ROWS
- 1; i
> -1; i
--)
685 fprintf(fd
, "%c ", ROW_NAME(i
));
687 for (c
= 0; c
< NO_COLS
; c
++)
689 sq
= i
* NO_COLS
+ c
;
691 p
= is_promoted
[piece
] ? '+' : ' ';
692 fprintf(fd
, "%c", p
);
708 fprintf(fd
, "%c", p
);
713 for (f
= i
* NO_COLS
; f
< i
* NO_COLS
+ NO_ROWS
; f
++)
714 fprintf(fd
, " %d", Mvboard
[f
]);
721 fprintf(fd
, " 9 8 7 6 5 4 3 2 1\n");
723 fprintf(fd
, " p l n s g b r k\n");
725 fprintf(fd
, " 5 4 3 2 1\n");
727 fprintf(fd
, " p s g b r k\n");
730 for (side
= 0; side
<= 1; side
++)
732 fprintf(fd
, "%c", (side
== black
) ? 'B' : 'W');
733 fprintf(fd
, " %2d", Captured
[side
][pawn
]);
735 fprintf(fd
, " %2d", Captured
[side
][lance
]);
736 fprintf(fd
, " %2d", Captured
[side
][knight
]);
738 fprintf(fd
, " %2d", Captured
[side
][silver
]);
739 fprintf(fd
, " %2d", Captured
[side
][gold
]);
740 fprintf(fd
, " %2d", Captured
[side
][bishop
]);
741 fprintf(fd
, " %2d", Captured
[side
][rook
]);
742 fprintf(fd
, " %2d", Captured
[side
][king
]);
747 fputs(" move score depth nodes time flags capture\n", fd
);
749 for (i
= 1; i
<= GameCnt
; i
++)
751 struct GameRec
*g
= &GameList
[i
];
754 t
= (g
->gmove
& 0xFF);
755 algbr(f
, t
, g
->flags
);
757 fprintf(fd
, "%c%c%-5s %6d %5d %7ld %6ld %5d 0x%08lx 0x%08lx",
760 : (is_promoted
[g
->fpiece
] ? '+' : ' ')),
762 ((f
> NO_SQUARES
) ? &mvstr
[0][1] : mvstr
[0]),
764 g
->nodes
, g
->time
, g
->flags
,
765 g
->hashkey
, g
->hashbd
);
767 if (g
->piece
!= no_piece
)
769 fprintf(fd
, " %c %s %c\n",
770 pxx
[g
->piece
], ColorStr
[g
->color
],
771 (is_promoted
[g
->piece
] ? '+' : ' '));
781 ShowMessage("Game saved");
785 ShowMessage("Could not open file");
792 * GetXGame, SaveXGame and BookGame used to only be defined if
793 * xshogi wasn't defined -- wonder why?
805 ShowMessage("Enter file name: ");
806 RequestInputString(fname
, sizeof(fname
)-1);
808 if (fname
[0] == '\0')
809 strcpy(fname
, "xshogi.position.read");
811 if ((fd
= fopen(fname
, "r")) != NULL
)
814 flag
.regularstart
= false;
817 /* xshogi position file ... */
818 fgets(fname
, 256, fd
);
823 if (strcmp(fname
, "xshogi"))
827 /* -- empty line -- */
828 fgets(fname
, 256, fd
);
829 /* -- empty line -- */
830 fgets(fname
, 256, fd
);
832 for (i
= NO_ROWS
- 1; i
> -1; i
--)
834 fgets(fname
, 256, fd
);
837 for (j
= 0; j
< NO_COLS
; j
++)
839 sq
= i
* NO_COLS
+ j
;
845 board
[sq
] = no_piece
;
850 for (c
= 0; c
< NO_PIECES
; c
++)
855 board
[sq
] = promoted
[c
];
857 board
[sq
] = unpromoted
[c
];
863 for (c
= 0; c
< NO_PIECES
; c
++)
868 board
[sq
] = promoted
[c
];
870 board
[sq
] = unpromoted
[c
];
883 for (side
= 0; side
<= 1; side
++)
885 fgets(fname
, 256, fd
);
887 Captured
[side
][pawn
] = atoi(InPtr
);
890 Captured
[side
][lance
] = atoi(InPtr
);
892 Captured
[side
][knight
] = atoi(InPtr
);
895 Captured
[side
][silver
] = atoi(InPtr
);
897 Captured
[side
][gold
] = atoi(InPtr
);
899 Captured
[side
][bishop
] = atoi(InPtr
);
901 Captured
[side
][rook
] = atoi(InPtr
);
903 Captured
[side
][king
] = atoi(InPtr
);
906 if (fgets(fname
, 256, fd
) != NULL
&& strncmp(fname
, "white", 5) == 0)
919 UpdateDisplay(0, 0, 1, 0);
934 ShowMessage("Enter file name: ");
935 RequestInputString(fname
, sizeof(fname
)-1);
937 if (fname
[0] == '\0')
938 strcpy(fname
, "xshogi.position.read");
940 if ((fd
= fopen(fname
, "w")) != NULL
)
942 fputs("# xshogi position file -- \n", fd
);
946 for (i
= NO_ROWS
- 1; i
> -1; i
--)
950 for (j
= 0; j
< NO_COLS
; j
++)
952 sq
= i
* NO_COLS
+ j
;
954 isp
= is_promoted
[piece
];
955 *p
= (isp
? '+' : ' ');
958 if (piece
== no_piece
)
960 else if (color
[sq
] == white
)
973 for (side
= 0; side
<= 1; side
++)
977 "%d %d %d %d %d %d %d %d\n",
979 "%d %d %d %d %d %d\n",
981 Captured
[side
][pawn
],
983 Captured
[side
][lance
],
984 Captured
[side
][knight
],
986 Captured
[side
][silver
],
987 Captured
[side
][gold
],
988 Captured
[side
][bishop
],
989 Captured
[side
][rook
],
990 Captured
[side
][king
]);
995 if (computer
== black
)
996 fputs("white to play\n", fd
);
998 fputs("black to play\n", fd
);
1009 char fname
[256], sflags
[4];
1013 strcpy(fname
, savefile
);
1015 /* Enter file name */
1016 ShowMessage("Enter file name: ");
1017 RequestInputString(fname
, sizeof(fname
)-1);
1020 if (fname
[0] == '\0')
1023 if ((fd
= fopen(fname
, "a")) != NULL
)
1027 for (i
= 1; i
<= GameCnt
; i
++)
1029 struct GameRec
*g
= &GameList
[i
];
1030 char mvnr
[20], mvs
[20];
1033 sprintf(mvnr
, "%d.", (i
+ 1)/2);
1038 t
= (g
->gmove
& 0xFF);
1039 algbr(f
, t
, g
->flags
);
1042 /* determine move quality string */
1043 if (g
->flags
& goodmove
)
1046 if (g
->flags
& badmove
)
1049 #ifdef EASY_OPENINGS
1050 if (g
->flags
& difficult
)
1056 /* determine move string */
1059 sprintf(mvs
, "%s%s ", &mvstr
[0][1], sflags
);
1063 sprintf(mvs
, "%c%c%c%c%c%s%s ",
1064 mvstr
[0][0], mvstr
[0][1],
1065 (g
->flags
& capture
) ? 'x' : '-',
1066 mvstr
[0][2], mvstr
[0][3],
1067 (mvstr
[0][4] == '+') ? "+" : "",
1071 fprintf(fd
, "%s%s%c%s",
1075 : (is_promoted
[g
->fpiece
] ? "+" : "")),
1088 ShowMessage("Game saved");
1092 ShowMessage("Could not open file");
1104 char fname
[256], dbuf
[256];
1108 strcpy(fname
, listfile
);
1113 strncpy(dbuf
, ctime(&when
), 20);
1120 /* use format "CL.Jan01-020304B" when
1123 program played white */
1125 sprintf(fname
, "CL.%s%s-%s%s%s%c",
1126 dbuf
+ 4, dbuf
+ 8, dbuf
+ 11, dbuf
+ 14,
1127 dbuf
+ 17, ColorStr
[computer
][0]);
1129 /* replace space padding with 0 */
1130 for (i
= 0; fname
[i
] != '\0'; i
++)
1132 if (fname
[i
] == ' ')
1137 fd
= fopen(fname
, "w");
1141 printf("Open failure for file: %s", fname
);
1145 fprintf(fd
, "gnushogi %s game\n", PACKAGE_VERSION
);
1146 fputs(" score depth nodes time ", fd
);
1147 fputs(" score depth nodes time\n", fd
);
1149 for (i
= 1; i
<= GameCnt
; i
++)
1151 f
= GameList
[i
].gmove
>> 8;
1152 t
= (GameList
[i
].gmove
& 0xFF);
1153 algbr(f
, t
, GameList
[i
].flags
);
1155 if (GameList
[i
].flags
& book
)
1157 fprintf(fd
, "%c%c%-5s %5d Book%7ld %5ld",
1160 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1161 pxx
[GameList
[i
].fpiece
],
1163 ? &mvstr
[0][1] : mvstr
[0]),
1170 fprintf(fd
, "%c%c%-5s %5d %2d %7ld %5ld",
1173 : (is_promoted
[GameList
[i
].fpiece
] ? '+' : ' ')),
1174 pxx
[GameList
[i
].fpiece
],
1175 (f
> NO_SQUARES
? &mvstr
[0][1] : mvstr
[0]),
1176 GameList
[i
].score
, GameList
[i
].depth
,
1177 GameList
[i
].nodes
, GameList
[i
].time
);
1190 fprintf(fd
, "\n\n");
1192 if (GameList
[GameCnt
].flags
& draw
)
1194 fprintf(fd
, "Draw %s\n", DRAW
);
1196 if (DRAW
== DRAW_REPETITION
)
1200 fprintf(fd
, "repetition by positions ");
1202 for (j
= GameCnt
- 1; j
>= Game50
; j
-= 2)
1204 if (GameList
[j
].hashkey
== hashkey
&&
1205 GameList
[j
].hashbd
== hashbd
)
1206 fprintf(fd
, "%d ", j
);
1212 else if (GameList
[GameCnt
].score
== -(SCORE_LIMIT
+ 999))
1214 fprintf(fd
, "%s\n", ColorStr
[player
]);
1216 else if (GameList
[GameCnt
].score
== (SCORE_LIMIT
+ 998))
1218 fprintf(fd
, "%s\n", ColorStr
[player
^ 1]);
1232 GameList
[GameCnt
].flags
|= badmove
;
1236 GameList
[GameCnt
].flags
|= goodmove
;
1239 #ifdef EASY_OPENINGS
1241 GameList
[GameCnt
].flags
|= difficult
;
1251 * Undo the most recent half-move.
1259 f
= GameList
[GameCnt
].gmove
>> 8;
1260 t
= GameList
[GameCnt
].gmove
& 0x7F;
1264 /* the move was a drop */
1265 Captured
[color
[t
]][board
[t
]]++;
1266 board
[t
] = no_piece
;
1272 if (GameList
[GameCnt
].flags
& promote
)
1273 board
[f
] = unpromoted
[board
[t
]];
1275 board
[f
] = board
[t
];
1277 color
[f
] = color
[t
];
1278 board
[t
] = GameList
[GameCnt
].piece
;
1279 color
[t
] = GameList
[GameCnt
].color
;
1281 if (board
[t
] != no_piece
)
1282 Captured
[color
[f
]][unpromoted
[board
[t
]]]--;
1284 if (color
[t
] != neutral
)
1292 if (TCflag
&& (TCmoves
> 1))
1293 ++TimeControl
.moves
[color
[f
]];
1295 hashkey
= GameList
[GameCnt
].hashkey
;
1296 hashbd
= GameList
[GameCnt
].hashbd
;
1298 computer
= computer
^ 1;
1299 opponent
= opponent
^ 1;
1302 player
= player
^ 1;
1304 UpdateDisplay(0, 0, 1, 0);
1306 if (flag
.regularstart
)
1313 FlagString(unsigned short flags
, char *s
)
1318 if (flags
& promote
)
1319 strcat(s
, " promote");
1321 if (flags
& dropmask
)
1322 strcat(s
, " drop:");
1324 if ((piece
= (flags
& pmask
)))
1328 if (is_promoted
[piece
])
1331 s
[l
++] = pxx
[piece
];
1335 if (flags
& capture
)
1336 strcat(s
, " capture");
1339 strcat(s
, " exact");
1342 strcat(s
, " tesuji");
1345 strcat(s
, " check");
1351 strcat(s
, " stupid");
1353 if (flags
& questionable
)
1354 strcat(s
, " questionable");
1356 if (flags
& kingattack
)
1357 strcat(s
, " kingattack");
1366 TestSpeed(void(*f
)(short side
, short ply
,
1367 short in_check
, short blockable
),
1377 #ifdef HAVE_GETTIMEOFDAY
1381 #ifdef HAVE_GETTIMEOFDAY
1382 gettimeofday(&tv
, NULL
);
1383 t1
= (tv
.tv_sec
*100 + (tv
.tv_usec
/10000));
1388 for (i
= 0; i
< j
; i
++)
1390 f(opponent
, 2, -1, true);
1393 for (jj
= TrPnt
[2]; i
< TrPnt
[3]; jj
++)
1395 if (!pick(jj
, TrPnt
[3] - 1))
1401 #ifdef HAVE_GETTIMEOFDAY
1402 gettimeofday(&tv
, NULL
);
1403 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1408 cnt
= j
* (TrPnt
[3] - TrPnt
[2]);
1421 TestPSpeed(short(*f
) (short side
), unsigned j
)
1425 #ifdef HAVE_GETTIMEOFDAY
1429 #ifdef HAVE_GETTIMEOFDAY
1430 gettimeofday(&tv
, NULL
);
1431 t1
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1436 for (i
= 0; i
< j
; i
++)
1439 #ifdef HAVE_GETTIMEOFDAY
1440 gettimeofday(&tv
, NULL
);
1441 t2
= (tv
.tv_sec
* 100 + (tv
.tv_usec
/ 10000));
1465 time
= &s
[strlen("otime")];
1466 t
= (int)strtol(time
, &time
, 10);
1471 sec
= (int)strtol(time
, &time
, 10);
1474 m
= (int)strtol(time
, &time
, 10);
1477 TimeControl
.clock
[opponent
] = t
;
1480 TimeControl
.moves
[opponent
] = m
;
1482 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1486 /* just to inform xshogi about availability of otime command */
1487 printf("otime %d %d\n", t
, m
);
1494 SetMachineTime(char *s
)
1499 time
= &s
[strlen("time")];
1501 t
= (int)strtol(time
, &time
, 10);
1506 sec
= (int)strtol(time
, &time
, 10);
1509 m
= (int)strtol(time
, &time
, 10);
1512 TimeControl
.clock
[computer
] = t
;
1515 TimeControl
.moves
[computer
] = m
;
1517 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1521 /* just to inform xshogi about availability of time command */
1522 printf("time %d %d\n", t
, m
);
1530 /* FIXME! This is truly the function from hell! */
1533 * Process the user's command. If easy mode is OFF (the computer is thinking
1534 * on opponents time) and the program is out of book, then make the 'hint'
1535 * move on the board and call SelectMove() to find a response. The user
1536 * terminates the search by entering ^C (quit siqnal) before entering a
1537 * command. If the opponent does not make the hint move, then set Sdepth to
1542 InputCommand(char *command
)
1545 short have_shown_prompt
= false;
1546 short ok
, done
, is_move
= false;
1550 ok
= flag
.quit
= done
= false;
1554 if (TTadd
> ttbllimit
)
1558 if ((hint
> 0) && !flag
.easy
&& !flag
.force
)
1561 * A hint move for the player is available. Compute a move for the
1562 * opponent in background mode assuming that the hint move will be
1563 * selected by the player.
1566 ft
= time0
; /* Save reference time for the player. */
1568 algbr((short) hint
>> 8, (short) hint
& 0xff, false);
1569 strcpy(s
, mvstr
[0]);
1576 /* do the hint move */
1577 if (VerifyMove(s
, VERIFY_AND_TRY_MODE
, &mv
))
1581 #ifdef QUIETBACKGROUND
1583 have_shown_prompt
= true;
1584 #endif /* QUIETBACKGROUND */
1586 /* Start computing a move until the search is interrupted. */
1588 #ifdef INTERRUPT_TEST
1592 /* would love to put null move in here */
1593 /* after we make the hint move make a 2 ply search
1594 * with both plys our moves */
1595 /* think on opponents time */
1596 SelectMove(computer
, BACKGROUND_MODE
);
1598 #ifdef INTERRUPT_TEST
1599 ElapsedTime(COMPUTE_INTERRUPT_MODE
);
1603 printf("searching not terminated by interrupt!\n");
1607 printf("elapsed time from interrupt to "
1608 "terminating search: %ld\n", it
);
1612 /* undo the hint and carry on */
1613 VerifyMove(s
, UNMAKE_MODE
, &mv
);
1617 time0
= ft
; /* Restore reference time for the player. */
1620 while(!(ok
|| flag
.quit
|| done
))
1624 #ifdef QUIETBACKGROUND
1625 if (!have_shown_prompt
)
1627 #endif /* QUIETBACKGROUND */
1631 #ifdef QUIETBACKGROUND
1634 have_shown_prompt
= false;
1635 #endif /* QUIETBACKGROUND */
1637 if (command
== NULL
) {
1641 eof
= GetString(sx
);
1643 strcpy(sx
, command
);
1647 sscanf(sx
, "%s", s
);
1655 if (strcmp(s
, "bd") == 0) /* bd -- display board */
1657 /* FIXME: Hack alert! */
1658 short old_xshogi
= XSHOGI
;
1661 display_type
= DISPLAY_RAW
;
1664 UpdateDisplay(0, 0, 1, 0);
1667 display_type
= DISPLAY_X
;
1669 else if (strcmp(s
, "post") == 0)
1671 flag
.post
= !flag
.post
;
1673 else if (strcmp(s
, "alg") == 0)
1675 /* noop */ ; /* alg */
1677 else if ((strcmp(s
, "quit") == 0)
1678 || (strcmp(s
, "exit") == 0))
1683 else if (strcmp(s
, "post") == 0)
1685 flag
.post
= !flag
.post
;
1688 else if ((strcmp(s
, "set") == 0)
1689 || (strcmp(s
, "edit") == 0))
1693 else if ((strcmp(s
, "setup") == 0))
1697 else if (strcmp(s
, "first") == 0)
1701 else if (strcmp(s
, "go") == 0)
1706 if (computer
== black
)
1717 else if (strcmp(s
, "help") == 0)
1721 else if (strcmp(s
, "material") == 0)
1723 flag
.material
= !flag
.material
;
1725 else if (strcmp(s
, "force") == 0)
1730 flag
.bothsides
= false;
1734 flag
.force
= !flag
.force
;
1735 flag
.bothsides
= false;
1738 else if (strcmp(s
, "book") == 0)
1740 Book
= Book
? 0 : BOOKFAIL
;
1742 else if (strcmp(s
, "new") == 0)
1745 UpdateDisplay(0, 0, 1, 0);
1747 else if (strcmp(s
, "list") == 0)
1751 else if ((strcmp(s
, "level") == 0)
1752 || (strcmp(s
, "clock") == 0))
1756 else if (strcmp(s
, "hash") == 0)
1758 flag
.hash
= !flag
.hash
;
1760 else if (strcmp(s
, "gamein") == 0)
1762 flag
.gamein
= !flag
.gamein
;
1764 else if (strcmp(s
, "beep") == 0)
1766 flag
.beep
= !flag
.beep
;
1768 else if (strcmp(s
, "time") == 0)
1772 else if (strcmp(s
, "otime") == 0)
1776 else if (strcmp(s
, "Awindow") == 0)
1778 ChangeAlphaWindow();
1780 else if (strcmp(s
, "Bwindow") == 0)
1784 else if (strcmp(s
, "rcptr") == 0)
1786 flag
.rcptr
= !flag
.rcptr
;
1788 else if (strcmp(s
, "hint") == 0)
1792 else if (strcmp(s
, "both") == 0)
1794 flag
.bothsides
= !flag
.bothsides
;
1797 ElapsedTime(COMPUTE_AND_INIT_MODE
);
1798 SelectMove(opponent
, FOREGROUND_MODE
);
1801 else if (strcmp(s
, "reverse") == 0)
1803 flag
.reverse
= !flag
.reverse
;
1805 UpdateDisplay(0, 0, 1, 0);
1807 else if (strcmp(s
, "switch") == 0)
1809 computer
= computer
^ 1;
1810 opponent
= opponent
^ 1;
1811 xwndw
= (computer
== black
) ? WXWNDW
: BXWNDW
;
1816 else if (strcmp(s
, "black") == 0)
1825 * ok = true; don't automatically start with black command
1828 else if (strcmp(s
, "white") == 0)
1837 * ok = true; don't automatically start with white command
1840 else if (strcmp(s
, "undo") == 0 && GameCnt
> 0)
1844 else if (strcmp(s
, "remove") == 0 && GameCnt
> 1)
1849 /* CHECKME: are these next three correct? */
1850 else if (!XSHOGI
&& strcmp(s
, "xget") == 0)
1854 else if (!XSHOGI
&& strcmp(s
, "xsave") == 0)
1858 else if (!XSHOGI
&& strcmp(s
, "bsave") == 0)
1862 #ifdef EASY_OPENINGS
1863 else if ((strcmp(s
, "?") == 0)
1864 || (strcmp(s
, "!") == 0)
1865 || (strcmp(s
, "~") == 0))
1867 else if ((strcmp(s
, "?") == 0)
1868 || (strcmp(s
, "!") == 0))
1873 else if (strcmp(s
, "get") == 0)
1877 else if (strcmp(s
, "save") == 0)
1881 else if (strcmp(s
, "depth") == 0)
1883 ChangeSearchDepth();
1885 else if (strcmp(s
, "hashdepth") == 0)
1889 else if (strcmp(s
, "random") == 0)
1893 else if (strcmp(s
, "hard") == 0)
1897 else if (strcmp(s
, "easy") == 0)
1899 flag
.easy
= !flag
.easy
;
1901 else if (strcmp(s
, "tsume") == 0)
1903 flag
.tsume
= !flag
.tsume
;
1905 else if (strcmp(s
, "contempt") == 0)
1909 else if (strcmp(s
, "xwndw") == 0)
1913 else if (strcmp(s
, "rv") == 0)
1916 UpdateDisplay(0, 0, 1, 0);
1918 else if (strcmp(s
, "coords") == 0)
1920 flag
.coords
= !flag
.coords
;
1921 UpdateDisplay(0, 0, 1, 0);
1923 else if (strcmp(s
, "stars") == 0)
1925 flag
.stars
= !flag
.stars
;
1926 UpdateDisplay(0, 0, 1, 0);
1928 else if (!XSHOGI
&& strcmp(s
, "moves") == 0)
1935 extern unsigned short PrVar
[MAXDEPTH
];
1937 SwagHt
= (GameList
[GameCnt
].gmove
== PrVar
[1])
1944 ShowMessage("Testing MoveList Speed");
1945 temp
= generate_move_flags
;
1946 generate_move_flags
= true;
1947 TestSpeed(MoveList
, 1);
1948 generate_move_flags
= temp
;
1949 ShowMessage("Testing CaptureList Speed");
1950 TestSpeed(CaptureList
, 1);
1951 ShowMessage("Testing Eval Speed");
1952 ExaminePosition(opponent
);
1953 TestPSpeed(ScorePosition
, 1);
1955 else if (!XSHOGI
&& strcmp(s
, "test") == 0)
1958 ShowMessage("Testing MoveList Speed");
1959 TestSpeed(MoveList
, 2000);
1960 ShowMessage("Testing CaptureList Speed");
1961 TestSpeed(CaptureList
, 3000);
1962 ShowMessage("Testing Eval Speed");
1963 ExaminePosition(opponent
);
1964 TestPSpeed(ScorePosition
, 1500);
1966 ShowMessage("Testing MoveList Speed");
1967 TestSpeed(MoveList
, 20000);
1968 ShowMessage("Testing CaptureList Speed");
1969 TestSpeed(CaptureList
, 30000);
1970 ShowMessage("Testing Eval Speed");
1971 ExaminePosition(opponent
);
1972 TestPSpeed(ScorePosition
, 15000);
1975 else if (!XSHOGI
&& strcmp(s
, "p") == 0)
1979 else if (!XSHOGI
&& strcmp(s
, "debug") == 0)
1989 else if ((ok
= VerifyMove(s
, VERIFY_AND_MAKE_MODE
, &mv
)))
1991 /* check for repetition */
1992 short rpt
= repetition();
1996 DRAW
= DRAW_REPETITION
;
1998 GameList
[GameCnt
].flags
|= draw
;
2012 ElapsedTime(COMPUTE_AND_INIT_MODE
);
2016 computer
= opponent
;
2017 opponent
= computer
^ 1;
2022 /* add remaining time in milliseconds for xshogi */
2025 printf("%d. %s %ld\n",
2026 ++mycnt2
, s
, TimeControl
.clock
[player
] * 10);
2029 #ifdef notdef /* optional pass best line to frontend with move */
2030 # if !defined NOPOST
2032 if (flag
.post
&& !flag
.mate
)
2036 printf(" %6d ", MSCORE
);
2038 for (i
= 1; MV
[i
] > 0; i
++)
2040 algbr((short) (MV
[i
] >> 8), (short) (MV
[i
] & 0xFF), false);
2041 printf("%5s ", mvstr
[0]);
2054 SetTimeControl(void)
2058 TimeControl
.moves
[black
] = TimeControl
.moves
[white
] = TCmoves
;
2059 TimeControl
.clock
[black
] += 6000L * TCminutes
+ TCseconds
* 100;
2060 TimeControl
.clock
[white
] += 6000L * TCminutes
+ TCseconds
* 100;
2064 TimeControl
.moves
[black
] = TimeControl
.moves
[white
] = 0;
2065 TimeControl
.clock
[black
] = TimeControl
.clock
[white
] = 0;
2068 flag
.onemove
= (TCmoves
== 1);
2070 ElapsedTime(COMPUTE_AND_INIT_MODE
);