+/// Position::set_state() computes the hash keys of the position, and other
+/// data that once computed is updated incrementally as moves are made.
+/// The function is only used when a new position is set up, and to verify
+/// the correctness of the StateInfo data when running in debug mode.
+
+void Position::set_state(StateInfo* si) const {
+
+ si->key = si->pawnKey = si->materialKey = 0;
+ si->npMaterial[WHITE] = si->npMaterial[BLACK] = VALUE_ZERO;
+ si->psq = SCORE_ZERO;
+
+ si->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
+
+ for (Bitboard b = pieces(); b; )
+ {
+ Square s = pop_lsb(&b);
+ Piece pc = piece_on(s);
+ si->key ^= Zobrist::psq[color_of(pc)][type_of(pc)][s];
+ si->psq += psq[color_of(pc)][type_of(pc)][s];
+ }
+
+ if (ep_square() != SQ_NONE)
+ si->key ^= Zobrist::enpassant[file_of(ep_square())];
+
+ if (sideToMove == BLACK)
+ si->key ^= Zobrist::side;
+
+ si->key ^= Zobrist::castling[st->castlingRights];
+
+ for (Bitboard b = pieces(PAWN); b; )
+ {
+ Square s = pop_lsb(&b);
+ si->pawnKey ^= Zobrist::psq[color_of(piece_on(s))][PAWN][s];
+ }
+
+ for (Color c = WHITE; c <= BLACK; ++c)
+ for (PieceType pt = PAWN; pt <= KING; ++pt)
+ for (int cnt = 0; cnt < pieceCount[c][pt]; ++cnt)
+ si->materialKey ^= Zobrist::psq[c][pt][cnt];
+
+ for (Color c = WHITE; c <= BLACK; ++c)
+ for (PieceType pt = KNIGHT; pt <= QUEEN; ++pt)
+ si->npMaterial[c] += pieceCount[c][pt] * PieceValue[MG][pt];
+}
+
+
+/// Position::fen() returns a FEN representation of the position. In case of
+/// Chess960 the Shredder-FEN notation is used. This is mainly a debugging function.