-/// Position::clear() erases the position object to a pristine state, with an
-/// empty board, white to move, and no castling rights.
-
-void Position::clear() {
-
- st = &startState;
- memset(st, 0, sizeof(StateInfo));
- st->epSquare = SQ_NONE;
- startPosPlyCounter = 0;
- nodes = 0;
-
- memset(byColorBB, 0, sizeof(Bitboard) * 2);
- memset(byTypeBB, 0, sizeof(Bitboard) * 8);
- memset(pieceCount, 0, sizeof(int) * 2 * 8);
- memset(index, 0, sizeof(int) * 64);
-
- for (int i = 0; i < 64; i++)
- board[i] = PIECE_NONE;
-
- for (int i = 0; i < 8; i++)
- for (int j = 0; j < 16; j++)
- pieceList[0][i][j] = pieceList[1][i][j] = SQ_NONE;
-
- for (Square sq = SQ_A1; sq <= SQ_H8; sq++)
- castleRightsMask[sq] = ALL_CASTLES;
-
- sideToMove = WHITE;
- initialKFile = FILE_E;
- initialKRFile = FILE_H;
- initialQRFile = FILE_A;
-}
-
-
-/// Position::put_piece() puts a piece on the given square of the board,
-/// updating the board array, pieces list, bitboards, and piece counts.
-
-void Position::put_piece(Piece p, Square s) {
-
- Color c = color_of_piece(p);
- PieceType pt = type_of_piece(p);
-
- board[s] = p;
- index[s] = pieceCount[c][pt]++;
- pieceList[c][pt][index[s]] = s;
-
- set_bit(&(byTypeBB[pt]), s);
- set_bit(&(byColorBB[c]), s);
- set_bit(&(byTypeBB[0]), s); // HACK: byTypeBB[0] contains all occupied squares.
-}
-
-
-/// Position::compute_key() computes the hash key of the position. The hash
-/// key is usually updated incrementally as moves are made and unmade, the
-/// compute_key() function is only used when a new position is set up, and
-/// to verify the correctness of the hash key when running in debug mode.
-
-Key Position::compute_key() const {
-
- Key result = zobCastle[st->castleRights];
-
- for (Square s = SQ_A1; s <= SQ_H8; s++)
- if (square_is_occupied(s))
- result ^= zobrist[color_of_piece_on(s)][type_of_piece_on(s)][s];
-
- if (ep_square() != SQ_NONE)
- result ^= zobEp[ep_square()];
-
- if (side_to_move() == BLACK)
- result ^= zobSideToMove;
-
- return result;
-}
-
-
-/// Position::compute_pawn_key() computes the hash key of the position. The
-/// hash key is usually updated incrementally as moves are made and unmade,
-/// the compute_pawn_key() function is only used when a new position is set
-/// up, and to verify the correctness of the pawn hash key when running in
-/// debug mode.
-
-Key Position::compute_pawn_key() const {
-
- Bitboard b;
- Key result = 0;
-
- for (Color c = WHITE; c <= BLACK; c++)
- {
- b = pieces(PAWN, c);
- while (b)
- result ^= zobrist[c][PAWN][pop_1st_bit(&b)];
- }
- return result;
-}
-
-
-/// Position::compute_material_key() computes the hash key of the position.
-/// The hash key is usually updated incrementally as moves are made and unmade,
-/// the compute_material_key() function is only used when a new position is set
-/// up, and to verify the correctness of the material hash key when running in
-/// debug mode.
-
-Key Position::compute_material_key() const {
-
- int count;
- Key result = 0;
-
- for (Color c = WHITE; c <= BLACK; c++)
- for (PieceType pt = PAWN; pt <= QUEEN; pt++)
- {
- count = piece_count(c, pt);
- for (int i = 0; i < count; i++)
- result ^= zobrist[c][pt][i];
- }
- return result;
-}
-
-
-/// Position::compute_value() compute the incremental scores for the middle
-/// game and the endgame. These functions are used to initialize the incremental
-/// scores when a new position is set up, and to verify that the scores are correctly
-/// updated by do_move and undo_move when the program is running in debug mode.
-Score Position::compute_value() const {
-
- Bitboard b;
- Score result = SCORE_ZERO;