+/// Position::init() initializes at startup the various arrays used to compute
+/// hash keys and the piece square tables. The latter is a two-step operation:
+/// First, the white halves of the tables are copied from PSQT[] tables. Second,
+/// the black halves of the tables are initialized by flipping and changing the
+/// sign of the white scores.
+
+void Position::init() {
+
+ RKISS rk;
+
+ for (Color c = WHITE; c <= BLACK; ++c)
+ for (PieceType pt = PAWN; pt <= KING; ++pt)
+ for (Square s = SQ_A1; s <= SQ_H8; ++s)
+ Zobrist::psq[c][pt][s] = rk.rand<Key>();
+
+ for (File f = FILE_A; f <= FILE_H; ++f)
+ Zobrist::enpassant[f] = rk.rand<Key>();
+
+ for (int cr = CASTLES_NONE; cr <= ALL_CASTLES; ++cr)
+ {
+ Bitboard b = cr;
+ while (b)
+ {
+ Key k = Zobrist::castle[1ULL << pop_lsb(&b)];
+ Zobrist::castle[cr] ^= k ? k : rk.rand<Key>();
+ }
+ }
+
+ Zobrist::side = rk.rand<Key>();
+ Zobrist::exclusion = rk.rand<Key>();
+
+ for (PieceType pt = PAWN; pt <= KING; ++pt)
+ {
+ PieceValue[MG][make_piece(BLACK, pt)] = PieceValue[MG][pt];
+ PieceValue[EG][make_piece(BLACK, pt)] = PieceValue[EG][pt];
+
+ Score v = make_score(PieceValue[MG][pt], PieceValue[EG][pt]);
+
+ for (Square s = SQ_A1; s <= SQ_H8; ++s)
+ {
+ psq[WHITE][pt][ s] = (v + PSQT[pt][s]);
+ psq[BLACK][pt][~s] = -(v + PSQT[pt][s]);
+ }
+ }
+}
+
+