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/>.
33 //// Local definitions
47 void from_index(int index);
48 bool is_legal() const;
49 bool is_immediate_draw() const;
50 bool is_immediate_win() const;
51 Bitboard wk_attacks() const { return StepAttackBB[WK][whiteKingSquare]; }
52 Bitboard bk_attacks() const { return StepAttackBB[BK][blackKingSquare]; }
53 Bitboard pawn_attacks() const { return StepAttackBB[WP][pawnSquare]; }
55 Square whiteKingSquare, blackKingSquare, pawnSquare;
59 const int IndexMax = 2 * 24 * 64 * 64;
61 Result classify_wtm(const KPKPosition& pos, const Result bb[]);
62 Result classify_btm(const KPKPosition& pos, const Result bb[]);
63 int compute_index(Square wksq, Square bksq, Square psq, Color stm);
71 void generate_kpk_bitbase(uint8_t bitbase[]) {
79 for (i = 0; i < IndexMax; i++)
82 bb[i] = !pos.is_legal() ? RESULT_INVALID
83 : pos.is_immediate_draw() ? RESULT_DRAW
84 : pos.is_immediate_win() ? RESULT_WIN : RESULT_UNKNOWN;
87 // Iterate until all positions are classified (30 cycles needed)
91 for (i = 0; i < IndexMax; i++)
92 if (bb[i] == RESULT_UNKNOWN)
96 bb[i] = (pos.sideToMove == WHITE) ? classify_wtm(pos, bb)
97 : classify_btm(pos, bb);
98 if (bb[i] != RESULT_UNKNOWN)
104 // Compress result and map into supplied bitbase parameter
105 for (i = 0; i < 24576; i++)
108 for (j = 0; j < 8; j++)
109 if (bb[8*i+j] == RESULT_WIN || bb[8*i+j] == RESULT_LOSS)
112 bitbase[i] = (uint8_t)b;
119 int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
121 int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
122 int r = int(stm) + 2 * int(bksq) + 128 * int(wksq) + 8192 * p;
124 assert(r >= 0 && r < IndexMax);
129 void KPKPosition::from_index(int index) {
131 int s = (index / 8192) % 24;
133 sideToMove = Color(index % 2);
134 blackKingSquare = Square((index / 2) % 64);
135 whiteKingSquare = Square((index / 128) % 64);
136 pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
139 bool KPKPosition::is_legal() const {
141 if ( whiteKingSquare == pawnSquare
142 || whiteKingSquare == blackKingSquare
143 || pawnSquare == blackKingSquare)
146 if (sideToMove == WHITE)
148 if ( bit_is_set(wk_attacks(), blackKingSquare)
149 || bit_is_set(pawn_attacks(), blackKingSquare))
152 else if (bit_is_set(bk_attacks(), whiteKingSquare))
158 bool KPKPosition::is_immediate_draw() const {
160 if (sideToMove == BLACK)
162 Bitboard wka = wk_attacks();
163 Bitboard bka = bk_attacks();
166 if ((bka & ~(wka | pawn_attacks())) == EmptyBoardBB)
169 // Case 2: King can capture pawn
170 if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
176 if ( whiteKingSquare == SQ_A8
177 && pawnSquare == SQ_A7
178 && (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
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 && ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
191 || bit_is_set(wk_attacks(), pawnSquare + DELTA_N));
194 Result classify_wtm(const KPKPosition& pos, const Result bb[]) {
196 // If one move leads to a position classified as RESULT_LOSS, the result
197 // of the current position is RESULT_WIN. If all moves lead to positions
198 // classified as RESULT_DRAW, the current position is classified RESULT_DRAW
199 // otherwise the current position is classified as RESULT_UNKNOWN.
201 bool unknownFound = false;
207 b = pos.wk_attacks();
211 idx = compute_index(s, pos.blackKingSquare, pos.pawnSquare, BLACK);
231 if (square_rank(pos.pawnSquare) < RANK_7)
233 s = pos.pawnSquare + DELTA_N;
234 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
253 if ( square_rank(s) == RANK_3
254 && s != pos.whiteKingSquare
255 && s != pos.blackKingSquare)
258 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
277 return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
281 Result classify_btm(const KPKPosition& pos, const Result bb[]) {
283 // If one move leads to a position classified as RESULT_DRAW, the result
284 // of the current position is RESULT_DRAW. If all moves lead to positions
285 // classified as RESULT_WIN, the current position is classified as
286 // RESULT_LOSS. Otherwise, the current position is classified as
289 bool unknownFound = false;
295 b = pos.bk_attacks();
299 idx = compute_index(pos.whiteKingSquare, s, pos.pawnSquare, WHITE);
317 return unknownFound ? RESULT_UNKNOWN : RESULT_LOSS;