- // A KPK bitbase index is an integer in [0, IndexMax] range
- //
- // Information is mapped in this way
- //
- // bit 0: side to move (WHITE or BLACK)
- // bit 1- 6: black king square (from SQ_A1 to SQ_H8)
- // bit 7-12: white king square (from SQ_A1 to SQ_H8)
- // bit 13-14: white pawn file (from FILE_A to FILE_D)
- // bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1)
-
- int index(Square w, Square b, Square p, Color c) {
-
- assert(file_of(p) <= FILE_D);
-
- return c + (b << 1) + (w << 7) + (file_of(p) << 13) + ((rank_of(p) - 1) << 15);
- }
-
- void KPKPosition::decode_index(int idx) {
-
- stm = Color(idx & 1);
- bksq = Square((idx >> 1) & 63);
- wksq = Square((idx >> 7) & 63);
- psq = File((idx >> 13) & 3) | Rank((idx >> 15) + 1);
- }
-
- Result KPKPosition::classify_leaf(int idx) {
-
- decode_index(idx);
-
- // Check if two pieces are on the same square or if a king can be captured
- if ( wksq == psq || wksq == bksq || bksq == psq
- || (k_attacks<WHITE>() & bksq)
- || (stm == WHITE && (p_attacks() & bksq)))
- return INVALID;
-
- // The position is an immediate win if it is white to move and the white
- // pawn can be promoted without getting captured.
- if ( rank_of(psq) == RANK_7
- && stm == WHITE
- && wksq != psq + DELTA_N
- && ( square_distance(bksq, psq + DELTA_N) > 1
- ||(k_attacks<WHITE>() & (psq + DELTA_N))))
- return WIN;
-
- // Check for known draw positions
- //
- // Case 1: Stalemate
- if ( stm == BLACK
- && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
- return DRAW;
-
- // Case 2: King can capture undefended pawn
- if ( stm == BLACK
- && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
- return DRAW;
-
- // Case 3: Black king in front of white pawn
- if ( bksq == psq + DELTA_N
- && rank_of(psq) < RANK_7)
- return DRAW;
-
- // Case 4: White king in front of pawn and black has opposition
- if ( stm == WHITE
- && wksq == psq + DELTA_N
- && bksq == wksq + DELTA_N + DELTA_N
- && rank_of(psq) < RANK_5)
- return DRAW;
-
- // Case 5: Stalemate with rook pawn
- if ( bksq == SQ_A8
- && file_of(psq) == FILE_A)
- return DRAW;
-
- // Case 6: White king trapped on the rook file
- if ( file_of(wksq) == FILE_A
- && file_of(psq) == FILE_A
- && rank_of(wksq) > rank_of(psq)
- && bksq == wksq + 2)
- return DRAW;
-
- return UNKNOWN;
+ KPKPosition::KPKPosition(unsigned idx) {
+
+ ksq[WHITE] = Square((idx >> 0) & 0x3F);
+ ksq[BLACK] = Square((idx >> 6) & 0x3F);
+ stm = Color ((idx >> 12) & 0x01);
+ psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
+
+ // Invalid if two pieces are on the same square or if a king can be captured
+ if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
+ || ksq[WHITE] == psq
+ || ksq[BLACK] == psq
+ || (stm == WHITE && (pawn_attacks_bb(WHITE, psq) & ksq[BLACK])))
+ result = INVALID;
+
+ // Win if the pawn can be promoted without getting captured
+ else if ( stm == WHITE
+ && rank_of(psq) == RANK_7
+ && ksq[WHITE] != psq + NORTH
+ && ( distance(ksq[BLACK], psq + NORTH) > 1
+ || (distance(ksq[WHITE], psq + NORTH) == 1)))
+ result = WIN;
+
+ // Draw if it is stalemate or the black king can capture the pawn
+ else if ( stm == BLACK
+ && ( !(attacks_bb<KING>(ksq[BLACK]) & ~(attacks_bb<KING>(ksq[WHITE]) | pawn_attacks_bb(WHITE, psq)))
+ || (attacks_bb<KING>(ksq[BLACK]) & ~attacks_bb<KING>(ksq[WHITE]) & psq)))
+ result = DRAW;
+
+ // Position will be classified later
+ else
+ result = UNKNOWN;