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/>.
36 void from_index(int index);
37 bool is_legal() const;
38 bool is_immediate_draw() const;
39 bool is_immediate_win() const;
40 Bitboard wk_attacks() const { return StepAttacksBB[WK][whiteKingSquare]; }
41 Bitboard bk_attacks() const { return StepAttacksBB[BK][blackKingSquare]; }
42 Bitboard pawn_attacks() const { return StepAttacksBB[WP][pawnSquare]; }
44 Square whiteKingSquare, blackKingSquare, pawnSquare;
48 const int IndexMax = 2 * 24 * 64 * 64;
50 uint8_t KPKBitbase[IndexMax / 8];
52 Result classify_wtm(const KPKPosition& pos, const Result bb[]);
53 Result classify_btm(const KPKPosition& pos, const Result bb[]);
54 int compute_index(Square wksq, Square bksq, Square wpsq, Color stm);
58 int probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm) {
60 int index = compute_index(wksq, bksq, wpsq, stm);
62 return KPKBitbase[index / 8] & (1 << (index & 7));
66 void init_kpk_bitbase() {
74 for (i = 0; i < IndexMax; i++)
77 bb[i] = !pos.is_legal() ? RESULT_INVALID
78 : pos.is_immediate_draw() ? RESULT_DRAW
79 : pos.is_immediate_win() ? RESULT_WIN : RESULT_UNKNOWN;
82 // Iterate until all positions are classified (30 cycles needed)
86 for (i = 0; i < IndexMax; i++)
87 if (bb[i] == RESULT_UNKNOWN)
91 bb[i] = (pos.sideToMove == WHITE) ? classify_wtm(pos, bb)
92 : classify_btm(pos, bb);
93 if (bb[i] != RESULT_UNKNOWN)
99 // Compress result and map into KPKBitbase[]
100 for (i = 0; i < 24576; i++)
103 for (j = 0; j < 8; j++)
104 if (bb[8*i+j] == RESULT_WIN || bb[8*i+j] == RESULT_LOSS)
107 KPKBitbase[i] = (uint8_t)b;
114 int compute_index(Square wksq, Square bksq, Square wpsq, Color stm) {
116 int p = int(square_file(wpsq)) + (int(square_rank(wpsq)) - 1) * 4;
117 int r = int(stm) + 2 * int(bksq) + 128 * int(wksq) + 8192 * p;
119 assert(r >= 0 && r < IndexMax);
124 void KPKPosition::from_index(int index) {
126 int s = (index / 8192) % 24;
128 sideToMove = Color(index % 2);
129 blackKingSquare = Square((index / 2) % 64);
130 whiteKingSquare = Square((index / 128) % 64);
131 pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
134 bool KPKPosition::is_legal() const {
136 if ( whiteKingSquare == pawnSquare
137 || whiteKingSquare == blackKingSquare
138 || pawnSquare == blackKingSquare)
141 if (sideToMove == WHITE)
143 if ( bit_is_set(wk_attacks(), blackKingSquare)
144 || bit_is_set(pawn_attacks(), blackKingSquare))
147 else if (bit_is_set(bk_attacks(), whiteKingSquare))
153 bool KPKPosition::is_immediate_draw() const {
155 if (sideToMove == BLACK)
157 Bitboard wka = wk_attacks();
158 Bitboard bka = bk_attacks();
161 if ((bka & ~(wka | pawn_attacks())) == EmptyBoardBB)
164 // Case 2: King can capture pawn
165 if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
171 if ( whiteKingSquare == SQ_A8
172 && pawnSquare == SQ_A7
173 && (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
176 if ( whiteKingSquare == SQ_H8
177 && pawnSquare == SQ_H7
178 && (blackKingSquare == SQ_F7 || blackKingSquare == SQ_F8))
184 bool KPKPosition::is_immediate_win() const {
186 // The position is an immediate win if it is white to move and the
187 // white pawn can be promoted without getting captured.
188 return sideToMove == WHITE
189 && square_rank(pawnSquare) == RANK_7
190 && whiteKingSquare != pawnSquare + DELTA_N
191 && ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
192 || bit_is_set(wk_attacks(), pawnSquare + DELTA_N));
195 Result classify_wtm(const KPKPosition& pos, const Result bb[]) {
197 // If one move leads to a position classified as RESULT_LOSS, the result
198 // of the current position is RESULT_WIN. If all moves lead to positions
199 // classified as RESULT_DRAW, the current position is classified RESULT_DRAW
200 // otherwise the current position is classified as RESULT_UNKNOWN.
202 bool unknownFound = false;
208 b = pos.wk_attacks();
212 idx = compute_index(s, pos.blackKingSquare, pos.pawnSquare, BLACK);
232 if (square_rank(pos.pawnSquare) < RANK_7)
234 s = pos.pawnSquare + DELTA_N;
235 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
254 if ( square_rank(s) == RANK_3
255 && s != pos.whiteKingSquare
256 && s != pos.blackKingSquare)
259 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
278 return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
282 Result classify_btm(const KPKPosition& pos, const Result bb[]) {
284 // If one move leads to a position classified as RESULT_DRAW, the result
285 // of the current position is RESULT_DRAW. If all moves lead to positions
286 // classified as RESULT_WIN, the current position is classified as
287 // RESULT_LOSS. Otherwise, the current position is classified as
290 bool unknownFound = false;
296 b = pos.bk_attacks();
300 idx = compute_index(pos.whiteKingSquare, s, pos.pawnSquare, WHITE);
318 return unknownFound ? RESULT_UNKNOWN : RESULT_LOSS;