1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
// game.c
// includes
#include "attack.h"
#include "board.h"
#include "fen.h"
#include "game.h"
#include "list.h"
#include "move.h"
#include "move_do.h"
#include "move_legal.h"
#include "piece.h"
#include "square.h"
#include "util.h"
// constants
static const bool UseSlowDebug = FALSE;
// variables
game_t Game[1];
// prototypes
static void game_update (game_t * game);
static int game_comp_status (const game_t * game);
// functions
// game_is_ok()
bool game_is_ok(const game_t * game) {
board_t board[1];
int pos, move;
if (game == NULL) return FALSE;
if (game->size < 0 || game->size > GameSize) return FALSE;
if (game->pos < 0 || game->pos > game->size) return FALSE;
// optional heavy DEBUG mode
if (!UseSlowDebug) return TRUE;
if (!board_is_ok(game->start_board)) return FALSE;
board_copy(board,game->start_board);
for (pos = 0; pos <= game->size; pos++) {
if (pos == game->pos) {
if (!board_equal(game->board,board)) return FALSE;
}
if (pos >= game->size) break;
if (game->key[pos] != board->key) return FALSE;
move = game->move[pos];
//if (!move_is_legal(move,board)); //huh??
if (!move_is_legal(move,board)) return FALSE;
move_do(board,move);
}
if (game->status != game_comp_status(game)) return FALSE;
return TRUE;
}
// game_clear()
void game_clear(game_t * game) {
ASSERT(game!=NULL);
game_init(game,StartFen);
}
// game_init()
bool game_init(game_t * game, const char fen[]) {
ASSERT(game!=NULL);
ASSERT(fen!=NULL);
if (!board_from_fen(game->start_board,fen)) return FALSE;
game->size = 0;
board_copy(game->board,game->start_board);
game->pos = 0;
game_update(game);
return TRUE;
}
// game_status()
int game_status(const game_t * game) {
ASSERT(game!=NULL);
return game->status;
}
// game_size()
int game_size(const game_t * game) {
ASSERT(game!=NULL);
return game->size;
}
// game_pos()
int game_pos(const game_t * game) {
ASSERT(game!=NULL);
return game->pos;
}
// game_move()
int game_move(const game_t * game, int pos) {
ASSERT(game!=NULL);
ASSERT(pos>=0&&pos<game->pos);
return game->move[pos];
}
// game_get_board()
void game_get_board(const game_t * game, board_t * board) {
game_get_board_ex(game, board, -1);
}
// game_get_board_ex()
void game_get_board_ex(const game_t * game, board_t * board, int pos) {
int start;
int i;
ASSERT(game!=NULL);
ASSERT(board!=NULL);
ASSERT(pos==-1||(pos>=0&&pos<=game->size)); // HACK
if (pos < 0) pos = game->pos;
if (pos >= game->pos) { // forward from current position
start = game->pos;
board_copy(board,game->board);
} else { // backward => replay the whole game
start = 0;
board_copy(board,game->start_board);
}
for (i = start; i < pos; i++) move_do(board,game->move[i]);
}
// game_turn()
int game_turn(const game_t * game) {
ASSERT(game!=NULL);
return game->board->turn;
}
// game_move_nb()
int game_move_nb(const game_t * game) {
ASSERT(game!=NULL);
return game->board->move_nb;
}
// game_add_move()
void game_add_move(game_t * game, int move) {
ASSERT(game!=NULL);
ASSERT(move_is_ok(move));
ASSERT(move_is_legal(move,game->board));
if (game->pos >= GameSize) my_fatal("game_add_move(): game overflow\n");
game->move[game->pos] = move;
game->key[game->pos] = game->board->key;
move_do(game->board,move);
game->pos++;
game->size = game->pos; // truncate game, HACK: before calling game_is_ok() in game_update()
game_update(game);
}
// game_rem_move()
void game_rem_move(game_t * game) {
ASSERT(game!=NULL);
game_goto(game,game->pos-1);
game->size = game->pos; // truncate game
}
// game_goto()
void game_goto(game_t * game, int pos) {
int i;
ASSERT(game!=NULL);
ASSERT(pos>=0&&pos<=game->size);
if (pos < game->pos) { // going backward => replay the whole game
board_copy(game->board,game->start_board);
game->pos = 0;
}
for (i = game->pos; i < pos; i++) move_do(game->board,game->move[i]);
ASSERT(i==pos);
game->pos = pos;
game_update(game);
}
// game_disp()
void game_disp(const game_t * game) {
board_t board[1];
int i, move;
ASSERT(game_is_ok(game));
board_copy(board,game->start_board);
board_disp(board);
for (i = 0; i < game->pos; i++) {
move = game->move[i];
move_disp(move,board);
move_do(board,move);
}
my_log("POLYGLOT\n");
board_disp(board);
}
// game_update()
static void game_update(game_t * game) {
ASSERT(game!=NULL);
game->status = game_comp_status(game);
ASSERT(game_is_ok(game));
}
// game_comp_status()
static int game_comp_status(const game_t * game) {
int i, n;
int wb, bb;
const board_t * board;
uint64 key;
int start;
ASSERT(game!=NULL);
// init
board = game->board;
// mate and stalemate
if (!board_can_play(board)) {
if (FALSE) {
} else if (is_in_check(board,Black)) { // HACK
return WHITE_MATES;
} else if (is_in_check(board,White)) { // HACK
return BLACK_MATES;
} else {
return STALEMATE;
}
}
// insufficient material
if (board->number[WhitePawn12] == 0
&& board->number[BlackPawn12] == 0
&& board->number[WhiteQueen12] == 0
&& board->number[BlackQueen12] == 0
&& board->number[WhiteRook12] == 0
&& board->number[BlackRook12] == 0) {
if (board->number[WhiteBishop12]
+ board->number[BlackBishop12]
+ board->number[WhiteKnight12]
+ board->number[BlackKnight12] <= 1) { // KK, KBK and KNK
return DRAW_MATERIAL;
} else if (board->number[WhiteBishop12] == 1
&& board->number[BlackBishop12] == 1
&& board->number[WhiteKnight12] == 0
&& board->number[BlackKnight12] == 0) {
wb = board->list[White][1]; // HACK
bb = board->list[Black][1]; // HACK
if (square_colour(wb) == square_colour(bb)) { // KBKB
return DRAW_MATERIAL;
}
}
}
// 50-move rule
if (board->ply_nb >= 100) return DRAW_FIFTY;
// position repetition
key = board->key;
n = 0;
start = game->pos - board->ply_nb;
if (start < 0) start = 0;
for (i = game->pos-4; i >= start; i -= 2) {
if (game->key[i] == key) {
if (++n == 2) return DRAW_REPETITION;
}
}
return PLAYING;
}
// end of game.cpp
|