U64 hash_key(const board_t *board) { ASSERT(board != nullptr); // init U64 key = 0; // pieces for (S8 colour = 0; colour < ColourNb; ++colour) { for (auto sq = board->piece[colour].begin(); sq != board->piece[colour].end(); ++sq) { const S32 piece = board->square[*sq]; key ^= hash_piece_key(piece, *sq); } for (auto sq = board->pawn[colour].begin(); sq != board->pawn[colour].end(); ++sq) { const S32 piece = board->square[*sq]; key ^= hash_piece_key(piece, *sq); } } // castle flags key ^= hash_castle_key(board->flags); // en-passant square const S32 sq = board->ep_square; if (sq != SquareNone) key ^= hash_ep_key(sq); // turn key ^= hash_turn_key(board->turn); return key; }
uint64 hash_key(const board_t * board) { uint64 key; int colour; const sq_t * ptr; int sq, piece; ASSERT(board!=NULL); // init key = 0; // pieces for (colour = 0; colour < ColourNb; colour++) { for (ptr = &board->piece[colour][0]; (sq=*ptr) != SquareNone; ptr++) { piece = board->square[sq]; key ^= hash_piece_key(piece,sq); } for (ptr = &board->pawn[colour][0]; (sq=*ptr) != SquareNone; ptr++) { piece = board->square[sq]; key ^= hash_piece_key(piece,sq); } } // castle flags key ^= hash_castle_key(board->flags); // en-passant square sq = board->ep_square; if (sq != SquareNone) key ^= hash_ep_key(sq); // turn key ^= hash_turn_key(board->turn); return key; }
void hash_init() { for (U8 i = 0; i < 16; ++i) Castle64[i] = hash_castle_key(i); }
void move_do(board_t * board, int move) { int me, opp; int from, to; int piece, pos, capture; int old_flags, new_flags; int sq, ep_square; int pawn; ASSERT(board_is_ok(board)); ASSERT(move_is_ok(move)); ASSERT(move_is_pseudo(move,board)); // init me = board->turn; opp = colour_opp(me); from = move_from(move); to = move_to(move); piece = board->square[from]; ASSERT(colour_equal(piece,me)); pos = board->pos[from]; ASSERT(pos>=0); // update turn board->turn = opp; board->key ^= random_64(RandomTurn); // update castling rights old_flags = board_flags(board); if (piece_is_king(piece)) { board->castle[me][SideH] = SquareNone; board->castle[me][SideA] = SquareNone; } if (board->castle[me][SideH] == from) board->castle[me][SideH] = SquareNone; if (board->castle[me][SideA] == from) board->castle[me][SideA] = SquareNone; if (board->castle[opp][SideH] == to) board->castle[opp][SideH] = SquareNone; if (board->castle[opp][SideA] == to) board->castle[opp][SideA] = SquareNone; new_flags = board_flags(board); board->key ^= hash_castle_key(new_flags^old_flags); // HACK // update en-passant square ep_square = sq = board->ep_square; if (sq != SquareNone) { board->key ^= random_64(RandomEnPassant+square_file(sq)); board->ep_square = SquareNone; } if (piece_is_pawn(piece) && abs(to-from) == 32) { pawn = piece_make_pawn(opp); if (board->square[to-1] == pawn || board->square[to+1] == pawn) { board->ep_square = sq = (from + to) / 2; board->key ^= random_64(RandomEnPassant+square_file(sq)); } } // update ply number (captures are handled later) board->ply_nb++; if (piece_is_pawn(piece)) board->ply_nb = 0; // conversion // update move number if (me == Black) board->move_nb++; // castle if (colour_equal(board->square[to],me)) { int rank; int king_from, king_to; int rook_from, rook_to; int rook; rank = colour_is_white(me) ? Rank1 : Rank8; king_from = from; rook_from = to; if (to > from) { // h side king_to = square_make(FileG,rank); rook_to = square_make(FileF,rank); } else { // a side king_to = square_make(FileC,rank); rook_to = square_make(FileD,rank); } // remove the rook pos = board->pos[rook_from]; ASSERT(pos>=0); rook = Rook64 | me; // HACK square_clear(board,rook_from,rook); // move the king square_move(board,king_from,king_to,piece); // put the rook back square_set(board,rook_to,rook,pos); ASSERT(board->key==hash_key(board)); return; } // remove the captured piece if (piece_is_pawn(piece) && to == ep_square) { // en-passant capture sq = square_ep_dual(to); capture = board->square[sq]; ASSERT(capture==piece_make_pawn(opp)); square_clear(board,sq,capture); board->ply_nb = 0; // conversion } else { capture = board->square[to]; if (capture != Empty) { // normal capture ASSERT(colour_equal(capture,opp)); ASSERT(!piece_is_king(capture)); square_clear(board,to,capture); board->ply_nb = 0; // conversion } } // move the piece if (move_is_promote(move)) { // promote square_clear(board,from,piece); piece = move_promote_hack(move) | me; // HACK square_set(board,to,piece,pos); } else { // normal move square_move(board,from,to,piece); } ASSERT(board->key==hash_key(board)); }
void hash_init() { int i; for (i = 0; i < 16; i++) Castle64[i] = hash_castle_key(i); }