2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 //// Local definitions
46 void from_index(int index);
47 bool is_legal() const;
48 bool is_immediate_draw() const;
49 bool is_immediate_win() const;
50 Bitboard wk_attacks() const { return NonSlidingAttacksBB[WK][whiteKingSquare]; }
51 Bitboard bk_attacks() const { return NonSlidingAttacksBB[BK][blackKingSquare]; }
52 Bitboard pawn_attacks() const { return NonSlidingAttacksBB[WP][pawnSquare]; }
54 Square whiteKingSquare, blackKingSquare, pawnSquare;
58 const int IndexMax = 2 * 24 * 64 * 64;
60 Result classify_wtm(const KPKPosition& pos, const Result bb[]);
61 Result classify_btm(const KPKPosition& pos, const Result bb[]);
62 int compute_index(Square wksq, Square bksq, Square psq, Color stm);
70 void generate_kpk_bitbase(uint8_t bitbase[]) {
78 for (i = 0; i < IndexMax; i++)
81 bb[i] = !pos.is_legal() ? RESULT_INVALID
82 : pos.is_immediate_draw() ? RESULT_DRAW
83 : pos.is_immediate_win() ? RESULT_WIN : RESULT_UNKNOWN;
86 // Iterate until all positions are classified (30 cycles needed)
90 for (i = 0; i < IndexMax; i++)
91 if (bb[i] == RESULT_UNKNOWN)
95 bb[i] = (pos.sideToMove == WHITE) ? classify_wtm(pos, bb)
96 : classify_btm(pos, bb);
97 if (bb[i] != RESULT_UNKNOWN)
103 // Compress result and map into supplied bitbase parameter
104 for (i = 0; i < 24576; i++)
107 for (j = 0; j < 8; j++)
108 if (bb[8*i+j] == RESULT_WIN || bb[8*i+j] == RESULT_LOSS)
111 bitbase[i] = (uint8_t)b;
118 int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
120 int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
121 int r = int(stm) + 2 * int(bksq) + 128 * int(wksq) + 8192 * p;
123 assert(r >= 0 && r < IndexMax);
128 void KPKPosition::from_index(int index) {
130 int s = (index / 8192) % 24;
132 sideToMove = Color(index % 2);
133 blackKingSquare = Square((index / 2) % 64);
134 whiteKingSquare = Square((index / 128) % 64);
135 pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
138 bool KPKPosition::is_legal() const {
140 if ( whiteKingSquare == pawnSquare
141 || whiteKingSquare == blackKingSquare
142 || pawnSquare == blackKingSquare)
145 if (sideToMove == WHITE)
147 if ( bit_is_set(wk_attacks(), blackKingSquare)
148 || bit_is_set(pawn_attacks(), blackKingSquare))
151 else if (bit_is_set(bk_attacks(), whiteKingSquare))
157 bool KPKPosition::is_immediate_draw() const {
159 if (sideToMove == BLACK)
161 Bitboard wka = wk_attacks();
162 Bitboard bka = bk_attacks();
165 if ((bka & ~(wka | pawn_attacks())) == EmptyBoardBB)
168 // Case 2: King can capture pawn
169 if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
175 if ( whiteKingSquare == SQ_A8
176 && pawnSquare == SQ_A7
177 && (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
183 bool KPKPosition::is_immediate_win() const {
185 // The position is an immediate win if it is white to move and the
186 // white pawn can be promoted without getting captured.
187 return sideToMove == WHITE
188 && square_rank(pawnSquare) == RANK_7
189 && ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
190 || bit_is_set(wk_attacks(), pawnSquare + DELTA_N));
193 Result classify_wtm(const KPKPosition& pos, const Result bb[]) {
195 // If one move leads to a position classified as RESULT_LOSS, the result
196 // of the current position is RESULT_WIN. If all moves lead to positions
197 // classified as RESULT_DRAW, the current position is classified RESULT_DRAW
198 // otherwise the current position is classified as RESULT_UNKNOWN.
200 bool unknownFound = false;
206 b = pos.wk_attacks();
210 idx = compute_index(s, pos.blackKingSquare, pos.pawnSquare, BLACK);
230 if (square_rank(pos.pawnSquare) < RANK_7)
232 s = pos.pawnSquare + DELTA_N;
233 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
252 if ( square_rank(s) == RANK_3
253 && s != pos.whiteKingSquare
254 && s != pos.blackKingSquare)
257 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
276 return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
280 Result classify_btm(const KPKPosition& pos, const Result bb[]) {
282 // If one move leads to a position classified as RESULT_DRAW, the result
283 // of the current position is RESULT_DRAW. If all moves lead to positions
284 // classified as RESULT_WIN, the current position is classified as
285 // RESULT_LOSS. Otherwise, the current position is classified as
288 bool unknownFound = false;
294 b = pos.bk_attacks();
298 idx = compute_index(pos.whiteKingSquare, s, pos.pawnSquare, WHITE);
316 return unknownFound ? RESULT_UNKNOWN : RESULT_LOSS;