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 Marco Costalba
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/>.
34 //// Local definitions
48 void from_index(int index);
50 bool is_legal() const;
51 bool is_immediate_draw() const;
52 bool is_immediate_win() const;
53 Bitboard wk_attacks() const;
54 Bitboard bk_attacks() const;
55 Bitboard pawn_attacks() const;
57 Square whiteKingSquare, blackKingSquare, pawnSquare;
63 const int IndexMax = 2*24*64*64;
67 bool next_iteration();
68 Result classify_wtm(const KPKPosition &p);
69 Result classify_btm(const KPKPosition &p);
70 int compute_index(Square wksq, Square bksq, Square psq, Color stm);
71 int compress_result(Result r);
80 void generate_kpk_bitbase(uint8_t bitbase[]) {
81 // Allocate array and initialize:
82 Bitbase = new Result[IndexMax];
85 // Iterate until all positions are classified:
86 while(next_iteration());
88 // Compress bitbase into the supplied parameter:
90 for(i = 0; i < 24576; i++) {
91 for(b = 0, j = 0; j < 8; b |= (compress_result(Bitbase[8*i+j]) << j), j++);
95 // Release allocated memory:
102 void KPKPosition::from_index(int index) {
104 sideToMove = Color(index % 2);
105 blackKingSquare = Square((index / 2) % 64);
106 whiteKingSquare = Square((index / 128) % 64);
107 s = (index / 8192) % 24;
108 pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
112 int KPKPosition::to_index() const {
113 return compute_index(whiteKingSquare, blackKingSquare, pawnSquare,
118 bool KPKPosition::is_legal() const {
119 if(whiteKingSquare == pawnSquare || whiteKingSquare == blackKingSquare ||
120 pawnSquare == blackKingSquare)
122 if(sideToMove == WHITE) {
123 if(bit_is_set(this->wk_attacks(), blackKingSquare))
125 if(bit_is_set(this->pawn_attacks(), blackKingSquare))
129 if(bit_is_set(this->bk_attacks(), whiteKingSquare))
136 bool KPKPosition::is_immediate_draw() const {
137 if(sideToMove == BLACK) {
138 Bitboard wka = this->wk_attacks();
139 Bitboard bka = this->bk_attacks();
142 if((bka & ~(wka | this->pawn_attacks())) == EmptyBoardBB)
145 // Case 2: King can capture pawn
146 if(bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
151 if(whiteKingSquare == SQ_A8 && pawnSquare == SQ_A7 &&
152 (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
160 bool KPKPosition::is_immediate_win() const {
161 // The position is an immediate win if it is white to move and the white
162 // pawn can be promoted without getting captured:
164 sideToMove == WHITE &&
165 square_rank(pawnSquare) == RANK_7 &&
166 (square_distance(blackKingSquare, pawnSquare+DELTA_N) > 1 ||
167 bit_is_set(this->wk_attacks(), pawnSquare+DELTA_N));
171 Bitboard KPKPosition::wk_attacks() const {
172 return StepAttackBB[WK][whiteKingSquare];
176 Bitboard KPKPosition::bk_attacks() const {
177 return StepAttackBB[BK][blackKingSquare];
181 Bitboard KPKPosition::pawn_attacks() const {
182 return StepAttackBB[WP][pawnSquare];
188 for(int i = 0; i < IndexMax; i++) {
191 Bitbase[i] = RESULT_INVALID;
192 else if(p.is_immediate_draw())
193 Bitbase[i] = RESULT_DRAW;
194 else if(p.is_immediate_win())
195 Bitbase[i] = RESULT_WIN;
197 Bitbase[i] = RESULT_UNKNOWN;
204 bool next_iteration() {
206 int previousUnknownCount = UnknownCount;
208 for(int i = 0; i < IndexMax; i++)
209 if(Bitbase[i] == RESULT_UNKNOWN) {
212 Bitbase[i] = (p.sideToMove == WHITE)? classify_wtm(p) : classify_btm(p);
214 if(Bitbase[i] == RESULT_WIN || Bitbase[i] == RESULT_LOSS ||
215 Bitbase[i] == RESULT_DRAW)
219 return UnknownCount != previousUnknownCount;
223 Result classify_wtm(const KPKPosition &p) {
225 // If one move leads to a position classified as RESULT_LOSS, the result
226 // of the current position is RESULT_WIN. If all moves lead to positions
227 // classified as RESULT_DRAW, the current position is classified as
228 // RESULT_DRAW. Otherwise, the current position is classified as
231 bool unknownFound = false;
239 switch(Bitbase[compute_index(s, p.blackKingSquare, p.pawnSquare,
248 case RESULT_DRAW: case RESULT_INVALID:
257 if(square_rank(p.pawnSquare) < RANK_7) {
258 s = p.pawnSquare + DELTA_N;
259 switch(Bitbase[compute_index(p.whiteKingSquare, p.blackKingSquare, s,
268 case RESULT_DRAW: case RESULT_INVALID:
275 if(square_rank(s) == RANK_3 &&
276 s != p.whiteKingSquare && s != p.blackKingSquare) {
278 switch(Bitbase[compute_index(p.whiteKingSquare, p.blackKingSquare, s,
287 case RESULT_DRAW: case RESULT_INVALID:
296 return unknownFound? RESULT_UNKNOWN : RESULT_DRAW;
300 Result classify_btm(const KPKPosition &p) {
302 // If one move leads to a position classified as RESULT_DRAW, the result
303 // of the current position is RESULT_DRAW. If all moves lead to positions
304 // classified as RESULT_WIN, the current position is classified as
305 // RESULT_LOSS. Otherwise, the current position is classified as
308 bool unknownFound = false;
316 switch(Bitbase[compute_index(p.whiteKingSquare, s, p.pawnSquare,
325 case RESULT_WIN: case RESULT_INVALID:
333 return unknownFound? RESULT_UNKNOWN : RESULT_LOSS;
337 int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
338 int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
339 int result = int(stm) + 2*int(bksq) + 128*int(wksq) + 8192*p;
340 assert(result >= 0 && result < IndexMax);
345 int compress_result(Result r) {
346 return (r == RESULT_WIN || r == RESULT_LOSS)? 1 : 0;