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 Result classify_wtm(const KPKPosition& pos, const Result bb[]);
51 Result classify_btm(const KPKPosition& pos, const Result bb[]);
52 int compute_index(Square wksq, Square bksq, Square psq, Color stm);
56 void generate_kpk_bitbase(uint8_t bitbase[]) {
64 for (i = 0; i < IndexMax; i++)
67 bb[i] = !pos.is_legal() ? RESULT_INVALID
68 : pos.is_immediate_draw() ? RESULT_DRAW
69 : pos.is_immediate_win() ? RESULT_WIN : RESULT_UNKNOWN;
72 // Iterate until all positions are classified (30 cycles needed)
76 for (i = 0; i < IndexMax; i++)
77 if (bb[i] == RESULT_UNKNOWN)
81 bb[i] = (pos.sideToMove == WHITE) ? classify_wtm(pos, bb)
82 : classify_btm(pos, bb);
83 if (bb[i] != RESULT_UNKNOWN)
89 // Compress result and map into supplied bitbase parameter
90 for (i = 0; i < 24576; i++)
93 for (j = 0; j < 8; j++)
94 if (bb[8*i+j] == RESULT_WIN || bb[8*i+j] == RESULT_LOSS)
97 bitbase[i] = (uint8_t)b;
104 int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
106 int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
107 int r = int(stm) + 2 * int(bksq) + 128 * int(wksq) + 8192 * p;
109 assert(r >= 0 && r < IndexMax);
114 void KPKPosition::from_index(int index) {
116 int s = (index / 8192) % 24;
118 sideToMove = Color(index % 2);
119 blackKingSquare = Square((index / 2) % 64);
120 whiteKingSquare = Square((index / 128) % 64);
121 pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
124 bool KPKPosition::is_legal() const {
126 if ( whiteKingSquare == pawnSquare
127 || whiteKingSquare == blackKingSquare
128 || pawnSquare == blackKingSquare)
131 if (sideToMove == WHITE)
133 if ( bit_is_set(wk_attacks(), blackKingSquare)
134 || bit_is_set(pawn_attacks(), blackKingSquare))
137 else if (bit_is_set(bk_attacks(), whiteKingSquare))
143 bool KPKPosition::is_immediate_draw() const {
145 if (sideToMove == BLACK)
147 Bitboard wka = wk_attacks();
148 Bitboard bka = bk_attacks();
151 if ((bka & ~(wka | pawn_attacks())) == EmptyBoardBB)
154 // Case 2: King can capture pawn
155 if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
161 if ( whiteKingSquare == SQ_A8
162 && pawnSquare == SQ_A7
163 && (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
166 if ( whiteKingSquare == SQ_H8
167 && pawnSquare == SQ_H7
168 && (blackKingSquare == SQ_F7 || blackKingSquare == SQ_F8))
174 bool KPKPosition::is_immediate_win() const {
176 // The position is an immediate win if it is white to move and the
177 // white pawn can be promoted without getting captured.
178 return sideToMove == WHITE
179 && square_rank(pawnSquare) == RANK_7
180 && whiteKingSquare != pawnSquare + DELTA_N
181 && ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
182 || bit_is_set(wk_attacks(), pawnSquare + DELTA_N));
185 Result classify_wtm(const KPKPosition& pos, const Result bb[]) {
187 // If one move leads to a position classified as RESULT_LOSS, the result
188 // of the current position is RESULT_WIN. If all moves lead to positions
189 // classified as RESULT_DRAW, the current position is classified RESULT_DRAW
190 // otherwise the current position is classified as RESULT_UNKNOWN.
192 bool unknownFound = false;
198 b = pos.wk_attacks();
202 idx = compute_index(s, pos.blackKingSquare, pos.pawnSquare, BLACK);
222 if (square_rank(pos.pawnSquare) < RANK_7)
224 s = pos.pawnSquare + DELTA_N;
225 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
244 if ( square_rank(s) == RANK_3
245 && s != pos.whiteKingSquare
246 && s != pos.blackKingSquare)
249 idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
268 return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
272 Result classify_btm(const KPKPosition& pos, const Result bb[]) {
274 // If one move leads to a position classified as RESULT_DRAW, the result
275 // of the current position is RESULT_DRAW. If all moves lead to positions
276 // classified as RESULT_WIN, the current position is classified as
277 // RESULT_LOSS. Otherwise, the current position is classified as
280 bool unknownFound = false;
286 b = pos.bk_attacks();
290 idx = compute_index(pos.whiteKingSquare, s, pos.pawnSquare, WHITE);
308 return unknownFound ? RESULT_UNKNOWN : RESULT_LOSS;