- Bitboard KPKPosition::pawn_attacks() const {
- return StepAttackBB[WP][pawnSquare];
- }
-
-
- void initialize() {
- KPKPosition p;
- for(int i = 0; i < IndexMax; i++) {
- p.from_index(i);
- if(!p.is_legal())
- Bitbase[i] = RESULT_INVALID;
- else if(p.is_immediate_draw())
- Bitbase[i] = RESULT_DRAW;
- else if(p.is_immediate_win())
- Bitbase[i] = RESULT_WIN;
- else {
- Bitbase[i] = RESULT_UNKNOWN;
- UnknownCount++;
- }
- }
- }
-
-
- bool next_iteration() {
- KPKPosition p;
- int previousUnknownCount = UnknownCount;
-
- for(int i = 0; i < IndexMax; i++)
- if(Bitbase[i] == RESULT_UNKNOWN) {
- p.from_index(i);
-
- Bitbase[i] = (p.sideToMove == WHITE)? classify_wtm(p) : classify_btm(p);
-
- if(Bitbase[i] == RESULT_WIN || Bitbase[i] == RESULT_LOSS ||
- Bitbase[i] == RESULT_DRAW)
- UnknownCount--;
- }
-
- return UnknownCount != previousUnknownCount;
- }
-
-
- Result classify_wtm(const KPKPosition &p) {
-
- // If one move leads to a position classified as RESULT_LOSS, the result
- // of the current position is RESULT_WIN. If all moves lead to positions
- // classified as RESULT_DRAW, the current position is classified as
- // RESULT_DRAW. Otherwise, the current position is classified as
- // RESULT_UNKNOWN.
-
- bool unknownFound = false;
- Bitboard b;
- Square s;
-
- // King moves
- b = p.wk_attacks();
- while(b) {
- s = pop_1st_bit(&b);
- switch(Bitbase[compute_index(s, p.blackKingSquare, p.pawnSquare,
- BLACK)]) {
- case RESULT_LOSS:
- return RESULT_WIN;
-
- case RESULT_UNKNOWN:
- unknownFound = true;
- break;
-
- case RESULT_DRAW: case RESULT_INVALID:
- break;
-
- default:
- assert(false);
- }
- }
-
- // Pawn moves
- if(square_rank(p.pawnSquare) < RANK_7) {
- s = p.pawnSquare + DELTA_N;
- switch(Bitbase[compute_index(p.whiteKingSquare, p.blackKingSquare, s,
- BLACK)]) {
- case RESULT_LOSS:
- return RESULT_WIN;
-
- case RESULT_UNKNOWN:
- unknownFound = true;
- break;
-
- case RESULT_DRAW: case RESULT_INVALID:
- break;
-
- default:
- assert(false);
- }
-
- if(square_rank(s) == RANK_3 &&
- s != p.whiteKingSquare && s != p.blackKingSquare) {
- s += DELTA_N;
- switch(Bitbase[compute_index(p.whiteKingSquare, p.blackKingSquare, s,
- BLACK)]) {
- case RESULT_LOSS:
- return RESULT_WIN;
-
- case RESULT_UNKNOWN:
- unknownFound = true;
- break;
-
- case RESULT_DRAW: case RESULT_INVALID:
- break;
-
- default:
- assert(false);
- }
- }
- }
-
- return unknownFound? RESULT_UNKNOWN : RESULT_DRAW;
- }
-
-
- Result classify_btm(const KPKPosition &p) {
-
- // If one move leads to a position classified as RESULT_DRAW, the result
- // of the current position is RESULT_DRAW. If all moves lead to positions
- // classified as RESULT_WIN, the current position is classified as
- // RESULT_LOSS. Otherwise, the current position is classified as
- // RESULT_UNKNOWN.
-
- bool unknownFound = false;
- Bitboard b;
- Square s;
-
- // King moves
- b = p.bk_attacks();
- while(b) {
- s = pop_1st_bit(&b);
- switch(Bitbase[compute_index(p.whiteKingSquare, s, p.pawnSquare,
- WHITE)]) {
- case RESULT_DRAW:
- return RESULT_DRAW;
-
- case RESULT_UNKNOWN:
- unknownFound = true;
- break;
-
- case RESULT_WIN: case RESULT_INVALID:
- break;
-
- default:
- assert(false);
- }
- }
-
- return unknownFound? RESULT_UNKNOWN : RESULT_LOSS;
- }
-
-
- int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
- int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
- int result = int(stm) + 2*int(bksq) + 128*int(wksq) + 8192*p;
- assert(result >= 0 && result < IndexMax);
- return result;
- }
-
-
- int compress_result(Result r) {
- return (r == RESULT_WIN || r == RESULT_LOSS)? 1 : 0;
- }
-
-}