12 #include "move_do.h"
\r
13 #include "move_legal.h"
\r
20 static const bool UseSlowDebug = FALSE;
\r
28 static void game_update (game_t * game);
\r
29 static int game_comp_status (const game_t * game);
\r
35 bool game_is_ok(const game_t * game) {
\r
40 if (game == NULL) return FALSE;
\r
42 if (game->size < 0 || game->size > GameSize) return FALSE;
\r
43 if (game->pos < 0 || game->pos > game->size) return FALSE;
\r
45 // optional heavy DEBUG mode
\r
47 if (!UseSlowDebug) return TRUE;
\r
49 if (!board_is_ok(game->start_board)) return FALSE;
\r
51 board_copy(board,game->start_board);
\r
53 for (pos = 0; pos <= game->size; pos++) {
\r
55 if (pos == game->pos) {
\r
56 if (!board_equal(game->board,board)) return FALSE;
\r
59 if (pos >= game->size) break;
\r
61 if (game->key[pos] != board->key) return FALSE;
\r
63 move = game->move[pos];
\r
64 //if (!move_is_legal(move,board)); //huh??
\r
65 if (!move_is_legal(move,board)) return FALSE;
\r
67 move_do(board,move);
\r
70 if (game->status != game_comp_status(game)) return FALSE;
\r
77 void game_clear(game_t * game) {
\r
81 game_init(game,StartFen);
\r
86 bool game_init(game_t * game, const char fen[]) {
\r
91 if (!board_from_fen(game->start_board,fen)) return FALSE;
\r
95 board_copy(game->board,game->start_board);
\r
105 int game_status(const game_t * game) {
\r
107 ASSERT(game!=NULL);
\r
109 return game->status;
\r
114 int game_size(const game_t * game) {
\r
116 ASSERT(game!=NULL);
\r
123 int game_pos(const game_t * game) {
\r
125 ASSERT(game!=NULL);
\r
132 int game_move(const game_t * game, int pos) {
\r
134 ASSERT(game!=NULL);
\r
135 ASSERT(pos>=0&&pos<game->pos);
\r
137 return game->move[pos];
\r
140 // game_get_board()
\r
142 void game_get_board(const game_t * game, board_t * board) {
\r
143 game_get_board_ex(game, board, -1);
\r
146 // game_get_board_ex()
\r
148 void game_get_board_ex(const game_t * game, board_t * board, int pos) {
\r
153 ASSERT(game!=NULL);
\r
154 ASSERT(board!=NULL);
\r
155 ASSERT(pos==-1||(pos>=0&&pos<=game->size)); // HACK
\r
157 if (pos < 0) pos = game->pos;
\r
159 if (pos >= game->pos) { // forward from current position
\r
161 board_copy(board,game->board);
\r
162 } else { // backward => replay the whole game
\r
164 board_copy(board,game->start_board);
\r
167 for (i = start; i < pos; i++) move_do(board,game->move[i]);
\r
172 int game_turn(const game_t * game) {
\r
174 ASSERT(game!=NULL);
\r
176 return game->board->turn;
\r
181 int game_move_nb(const game_t * game) {
\r
183 ASSERT(game!=NULL);
\r
185 return game->board->move_nb;
\r
190 void game_add_move(game_t * game, int move) {
\r
192 ASSERT(game!=NULL);
\r
193 ASSERT(move_is_ok(move));
\r
195 ASSERT(move_is_legal(move,game->board));
\r
197 if (game->pos >= GameSize) my_fatal("game_add_move(): game overflow\n");
\r
199 game->move[game->pos] = move;
\r
200 game->key[game->pos] = game->board->key;
\r
202 move_do(game->board,move);
\r
205 game->size = game->pos; // truncate game, HACK: before calling game_is_ok() in game_update()
\r
212 void game_rem_move(game_t * game) {
\r
214 ASSERT(game!=NULL);
\r
216 game_goto(game,game->pos-1);
\r
218 game->size = game->pos; // truncate game
\r
223 void game_goto(game_t * game, int pos) {
\r
227 ASSERT(game!=NULL);
\r
228 ASSERT(pos>=0&&pos<=game->size);
\r
230 if (pos < game->pos) { // going backward => replay the whole game
\r
231 board_copy(game->board,game->start_board);
\r
235 for (i = game->pos; i < pos; i++) move_do(game->board,game->move[i]);
\r
245 void game_disp(const game_t * game) {
\r
250 ASSERT(game_is_ok(game));
\r
252 board_copy(board,game->start_board);
\r
256 for (i = 0; i < game->pos; i++) {
\r
258 move = game->move[i];
\r
259 move_disp(move,board);
\r
261 move_do(board,move);
\r
264 my_log("POLYGLOT\n");
\r
271 static void game_update(game_t * game) {
\r
273 ASSERT(game!=NULL);
\r
275 game->status = game_comp_status(game);
\r
277 ASSERT(game_is_ok(game));
\r
280 // game_comp_status()
\r
282 static int game_comp_status(const game_t * game) {
\r
286 const board_t * board;
\r
290 ASSERT(game!=NULL);
\r
294 board = game->board;
\r
296 // mate and stalemate
\r
298 if (!board_can_play(board)) {
\r
300 } else if (is_in_check(board,Black)) { // HACK
\r
301 return WHITE_MATES;
\r
302 } else if (is_in_check(board,White)) { // HACK
\r
303 return BLACK_MATES;
\r
309 // insufficient material
\r
311 if (board->number[WhitePawn12] == 0
\r
312 && board->number[BlackPawn12] == 0
\r
313 && board->number[WhiteQueen12] == 0
\r
314 && board->number[BlackQueen12] == 0
\r
315 && board->number[WhiteRook12] == 0
\r
316 && board->number[BlackRook12] == 0) {
\r
318 if (board->number[WhiteBishop12]
\r
319 + board->number[BlackBishop12]
\r
320 + board->number[WhiteKnight12]
\r
321 + board->number[BlackKnight12] <= 1) { // KK, KBK and KNK
\r
323 return DRAW_MATERIAL;
\r
325 } else if (board->number[WhiteBishop12] == 1
\r
326 && board->number[BlackBishop12] == 1
\r
327 && board->number[WhiteKnight12] == 0
\r
328 && board->number[BlackKnight12] == 0) {
\r
330 wb = board->list[White][1]; // HACK
\r
331 bb = board->list[Black][1]; // HACK
\r
333 if (square_colour(wb) == square_colour(bb)) { // KBKB
\r
334 return DRAW_MATERIAL;
\r
341 if (board->ply_nb >= 100) return DRAW_FIFTY;
\r
343 // position repetition
\r
348 start = game->pos - board->ply_nb;
\r
349 if (start < 0) start = 0;
\r
351 for (i = game->pos-4; i >= start; i -= 2) {
\r
352 if (game->key[i] == key) {
\r
353 if (++n == 2) return DRAW_REPETITION;
\r