- int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
-
- int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
- int r = int(stm) + 2 * int(bksq) + 128 * int(wksq) + 8192 * p;
-
- assert(r >= 0 && r < IndexMax);
-
- return r;
- }
-
- void KPKPosition::from_index(int index) {
-
- int s = (index / 8192) % 24;
-
- sideToMove = Color(index % 2);
- blackKingSquare = Square((index / 2) % 64);
- whiteKingSquare = Square((index / 128) % 64);
- pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
- }
-
- bool KPKPosition::is_legal() const {
-
- if ( whiteKingSquare == pawnSquare
- || whiteKingSquare == blackKingSquare
- || pawnSquare == blackKingSquare)
- return false;
-
- if (sideToMove == WHITE)
- {
- if ( bit_is_set(wk_attacks(), blackKingSquare)
- || bit_is_set(pawn_attacks(), blackKingSquare))
- return false;
- }
- else if (bit_is_set(bk_attacks(), whiteKingSquare))
- return false;
-
- return true;
- }