mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-16 23:26:23 +08:00
Compare commits
191 Commits
sf_1.2_opt
...
sf_1.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e87931bbfc | ||
|
|
aa5c375ca9 | ||
|
|
cd1cc39b04 | ||
|
|
4e151f7e0d | ||
|
|
d13e4c16c2 | ||
|
|
1906df4494 | ||
|
|
39f2eda285 | ||
|
|
00a3380885 | ||
|
|
f1d982e2c0 | ||
|
|
fbca16da57 | ||
|
|
1e97cdd9f3 | ||
|
|
ab69f50c64 | ||
|
|
b35e593551 | ||
|
|
20d88dbf98 | ||
|
|
ef60043725 | ||
|
|
2f2e48fad2 | ||
|
|
2550c6383b | ||
|
|
fb560fa5d7 | ||
|
|
11491e71ee | ||
|
|
bd26374f21 | ||
|
|
d20e0cf048 | ||
|
|
342ceb1c91 | ||
|
|
f010b6db71 | ||
|
|
7c267587fc | ||
|
|
24485c96ec | ||
|
|
f98385e129 | ||
|
|
2acc89c6e8 | ||
|
|
a52ab2afbf | ||
|
|
de050db2b0 | ||
|
|
e68ebe618c | ||
|
|
4634be8ba6 | ||
|
|
e7f03913ea | ||
|
|
e30720b0bf | ||
|
|
7af1b40b4e | ||
|
|
ecec7dbf89 | ||
|
|
8da2153ee8 | ||
|
|
22f9f0cabe | ||
|
|
3e7e1a7c53 | ||
|
|
0ea716463b | ||
|
|
6c4e36aab6 | ||
|
|
6f28bcd483 | ||
|
|
acef5d6a59 | ||
|
|
44b497a972 | ||
|
|
b893583bb6 | ||
|
|
e81d0d08c3 | ||
|
|
72c7595f8a | ||
|
|
e38ad4d42b | ||
|
|
fad772f387 | ||
|
|
ebb0f31928 | ||
|
|
f0701e2b0f | ||
|
|
95aadb5e53 | ||
|
|
17ef886fc3 | ||
|
|
8590a6f3b7 | ||
|
|
7b05b83bf2 | ||
|
|
d7ef09727a | ||
|
|
683595fee1 | ||
|
|
659c54582d | ||
|
|
2c0cd95ecf | ||
|
|
6a8e46d53e | ||
|
|
c6c4713ab2 | ||
|
|
9e44a6dba9 | ||
|
|
941f4e1643 | ||
|
|
dcdac83187 | ||
|
|
5a0581498c | ||
|
|
1c087dd806 | ||
|
|
095a96b461 | ||
|
|
72af519e7f | ||
|
|
db46602b1f | ||
|
|
d3f99aea6b | ||
|
|
0e835bd334 | ||
|
|
43276cbec5 | ||
|
|
a9e55d4326 | ||
|
|
4d70e3aeac | ||
|
|
24b7ad54c7 | ||
|
|
0ff3bf34cd | ||
|
|
66d165921d | ||
|
|
b82c3021fa | ||
|
|
320630ca1a | ||
|
|
ef8acdc73b | ||
|
|
cc8e915ed5 | ||
|
|
16abc165d8 | ||
|
|
74160ac602 | ||
|
|
feb5342b39 | ||
|
|
6cddf9183c | ||
|
|
b870f5a091 | ||
|
|
fcecc5212e | ||
|
|
8de91be61e | ||
|
|
ca4e78db8d | ||
|
|
bfcfaf7101 | ||
|
|
c8773c720a | ||
|
|
3ed603cd64 | ||
|
|
f637ddc1e8 | ||
|
|
964bd86272 | ||
|
|
72b88e09e1 | ||
|
|
3e663d8c50 | ||
|
|
8c9c51c721 | ||
|
|
7fe1632a49 | ||
|
|
7c84b39a42 | ||
|
|
68e711aac6 | ||
|
|
02cd96e4c2 | ||
|
|
772a37cd54 | ||
|
|
c02613860a | ||
|
|
9b6b9e67fe | ||
|
|
6a8cfe79da | ||
|
|
1f97b48a31 | ||
|
|
a96cba0ec8 | ||
|
|
f9f30412e7 | ||
|
|
55f9afee2a | ||
|
|
962216440c | ||
|
|
243fa483d7 | ||
|
|
da7a62852a | ||
|
|
0bf45823da | ||
|
|
4324276419 | ||
|
|
43bc5479c2 | ||
|
|
8f59de48f5 | ||
|
|
2f6c5f00e6 | ||
|
|
2f21ec39ad | ||
|
|
9b257ba29d | ||
|
|
1b0fee9b17 | ||
|
|
b7cb6180cf | ||
|
|
f30aa83f8a | ||
|
|
1dc27f3232 | ||
|
|
95cfc0e306 | ||
|
|
9e1d142fdd | ||
|
|
683e6dc656 | ||
|
|
c6630abe0d | ||
|
|
734941672e | ||
|
|
0a0ea36e25 | ||
|
|
5f142ec209 | ||
|
|
7f4f18f959 | ||
|
|
c45818e9f8 | ||
|
|
7013efce4e | ||
|
|
9f5b709db7 | ||
|
|
2c955f25de | ||
|
|
f32992f88c | ||
|
|
e71d520758 | ||
|
|
52ed0e9563 | ||
|
|
30e8f0c9ad | ||
|
|
67375f4693 | ||
|
|
039badfda8 | ||
|
|
088ecc242f | ||
|
|
a188a047ab | ||
|
|
1d2247aea3 | ||
|
|
8365f8ac1e | ||
|
|
68d36b6f59 | ||
|
|
33c608e140 | ||
|
|
ff60dafe8d | ||
|
|
214f9dcc27 | ||
|
|
1dc1cecf01 | ||
|
|
67535711e8 | ||
|
|
56f607fe0f | ||
|
|
9d044cf4ee | ||
|
|
769f2fdecb | ||
|
|
9bffe811c4 | ||
|
|
3e20c6c07d | ||
|
|
4f5f97107e | ||
|
|
151d47dc85 | ||
|
|
33ddeec5e0 | ||
|
|
4573d618e4 | ||
|
|
1156eb865b | ||
|
|
76381cbd69 | ||
|
|
fa322b3768 | ||
|
|
2ea7449f2a | ||
|
|
ee6e8851be | ||
|
|
03211296f1 | ||
|
|
cc76951483 | ||
|
|
f0858cd229 | ||
|
|
225d89c51b | ||
|
|
8bef9e59a0 | ||
|
|
82bf08e4f2 | ||
|
|
c32904f0a0 | ||
|
|
6cfb661ca5 | ||
|
|
be43219136 | ||
|
|
d5b77ad45e | ||
|
|
c6d62b7da5 | ||
|
|
3e6e57231e | ||
|
|
14c1fd4d27 | ||
|
|
4b53bb02f6 | ||
|
|
ec2927286a | ||
|
|
bdbbc4e06b | ||
|
|
539051b1e0 | ||
|
|
c97104e854 | ||
|
|
5cacefe7c6 | ||
|
|
9b87d151bc | ||
|
|
cbfbf25d1b | ||
|
|
90585a8a36 | ||
|
|
e236a0c652 | ||
|
|
d0804341c5 | ||
|
|
dc4e2d8184 | ||
|
|
e828753a3a | ||
|
|
33bb8305a9 |
13
polyglot.ini
13
polyglot.ini
@@ -2,13 +2,13 @@
|
||||
[PolyGlot]
|
||||
|
||||
EngineDir = .
|
||||
EngineCommand = ./glaurung
|
||||
EngineCommand = ./stockfish
|
||||
|
||||
Book = false
|
||||
BookFile = book.bin
|
||||
|
||||
Log = true
|
||||
LogFile = glaurung.log
|
||||
LogFile = stockfish.log
|
||||
|
||||
Resign = true
|
||||
ResignScore = 600
|
||||
@@ -34,8 +34,7 @@ King Safety Coefficient = 40
|
||||
King Safety X Intercept = 0
|
||||
King Safety Max Slope = 30
|
||||
King Safety Max Value = 500
|
||||
Queen Contact Check Bonus = 4
|
||||
Rook Contact Check Bonus = 2
|
||||
Queen Contact Check Bonus = 3
|
||||
Queen Check Bonus = 2
|
||||
Rook Check Bonus = 1
|
||||
Bishop Check Bonus = 1
|
||||
@@ -57,14 +56,8 @@ Pawn Endgame Extension (non-PV nodes) = 2
|
||||
Full Depth Moves (PV nodes) = 14
|
||||
Full Depth Moves (non-PV nodes) = 3
|
||||
Threat Depth = 5
|
||||
Selective Plies = 7
|
||||
Futility Pruning (Main Search) = true
|
||||
Futility Pruning (Quiescence Search) = true
|
||||
Futility Margin 0 = 50
|
||||
Futility Margin 1 = 100
|
||||
Futility Margin 2 = 300
|
||||
Maximum Razoring Depth = 3
|
||||
Razoring Margin = 300
|
||||
Randomness = 0
|
||||
Minimum Split Depth = 4
|
||||
Maximum Number of Threads per Split Point = 5
|
||||
|
||||
@@ -24,9 +24,9 @@
|
||||
|
||||
EXE = stockfish
|
||||
|
||||
OBJS = bitboard.o color.o pawns.o material.o endgame.o evaluate.o main.o \
|
||||
OBJS = bitboard.o pawns.o material.o endgame.o evaluate.o main.o \
|
||||
misc.o move.o movegen.o history.o movepick.o search.o piece.o \
|
||||
position.o square.o direction.o tt.o value.o uci.o ucioption.o \
|
||||
position.o direction.o tt.o value.o uci.o ucioption.o \
|
||||
mersenne.o book.o bitbase.o san.o benchmark.o
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ OBJS = bitboard.o color.o pawns.o material.o endgame.o evaluate.o main.o \
|
||||
all: $(EXE) .depend
|
||||
|
||||
clean:
|
||||
$(RM) *.o .depend glaurung
|
||||
$(RM) *.o .depend stockfish
|
||||
|
||||
|
||||
###
|
||||
@@ -94,7 +94,7 @@ CXXFLAGS += -wd383,869,981
|
||||
# Profiler guided optimization with the Intel C++ compiler. To use it, first
|
||||
# create the directory ./profdata if it does not already exist, and delete its
|
||||
# contents if it does exist. Then compile with -prof_gen, and run the
|
||||
# resulting binary for a while (for instance, do ./glaurung bench 128 1, and
|
||||
# resulting binary for a while (for instance, do ./stockfish bench 128 1, and
|
||||
# wait 15 minutes for the benchmark to complete). Then do a 'make clean', and
|
||||
# recompile with -prof_use.
|
||||
|
||||
|
||||
@@ -89,7 +89,8 @@ void generate_kpk_bitbase(uint8_t bitbase[]) {
|
||||
int i, j, b;
|
||||
for(i = 0; i < 24576; i++) {
|
||||
for(b = 0, j = 0; j < 8; b |= (compress_result(Bitbase[8*i+j]) << j), j++);
|
||||
bitbase[i] = b;
|
||||
assert(b == int(uint8_t(b)));
|
||||
bitbase[i] = (uint8_t)b;
|
||||
}
|
||||
|
||||
// Release allocated memory:
|
||||
|
||||
@@ -38,64 +38,6 @@
|
||||
#include "direction.h"
|
||||
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
////
|
||||
|
||||
const Bitboard SquaresByColorBB[2] = {BlackSquaresBB, WhiteSquaresBB};
|
||||
|
||||
const Bitboard FileBB[8] = {
|
||||
FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
|
||||
};
|
||||
|
||||
const Bitboard NeighboringFilesBB[8] = {
|
||||
FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
|
||||
FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
|
||||
};
|
||||
|
||||
const Bitboard ThisAndNeighboringFilesBB[8] = {
|
||||
FileABB|FileBBB, FileABB|FileBBB|FileCBB,
|
||||
FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
|
||||
FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
|
||||
FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
|
||||
};
|
||||
|
||||
const Bitboard RankBB[8] = {
|
||||
Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
|
||||
};
|
||||
|
||||
const Bitboard RelativeRankBB[2][8] = {
|
||||
{
|
||||
Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
|
||||
},
|
||||
{
|
||||
Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB
|
||||
}
|
||||
};
|
||||
|
||||
const Bitboard InFrontBB[2][8] = {
|
||||
{
|
||||
Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank7BB | Rank8BB,
|
||||
Rank8BB,
|
||||
EmptyBoardBB
|
||||
},
|
||||
{
|
||||
EmptyBoardBB,
|
||||
Rank1BB,
|
||||
Rank2BB | Rank1BB,
|
||||
Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(USE_COMPACT_ROOK_ATTACKS)
|
||||
|
||||
Bitboard RankAttacks[8][64], FileAttacks[8][64];
|
||||
@@ -270,6 +212,7 @@ Bitboard QueenPseudoAttacks[64];
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
void init_masks();
|
||||
void init_ray_bitboards();
|
||||
void init_attacks();
|
||||
@@ -450,7 +393,7 @@ namespace {
|
||||
|
||||
void init_ray_bitboards() {
|
||||
int d[8] = {1, -1, 16, -16, 17, -17, 15, -15};
|
||||
for(int i = 0; i < 128; i = i + 9 & ~8) {
|
||||
for(int i = 0; i < 128; i = (i + 9) & ~8) {
|
||||
for(int j = 0; j < 8; j++) {
|
||||
RayBB[(i&7)|((i>>4)<<3)][j] = EmptyBoardBB;
|
||||
for(int k = i + d[j]; (k & 0x88) == 0; k += d[j])
|
||||
@@ -536,8 +479,15 @@ namespace {
|
||||
for(i = 0; i < 64; i++) {
|
||||
attackIndex[i] = index;
|
||||
mask[i] = sliding_attacks(i, 0ULL, 4, deltas, 1, 6, 1, 6);
|
||||
|
||||
#if defined(USE_32BIT_ATTACKS)
|
||||
j = (1 << (32 - shift[i]));
|
||||
#else
|
||||
j = (1 << (64 - shift[i]));
|
||||
#endif
|
||||
|
||||
for(k = 0; k < j; k++) {
|
||||
|
||||
#if defined(USE_32BIT_ATTACKS)
|
||||
b = index_to_bitboard(k, mask[i]);
|
||||
attacks[index +
|
||||
|
||||
@@ -84,8 +84,7 @@ const Bitboard EmptyBoardBB = 0ULL;
|
||||
|
||||
const Bitboard WhiteSquaresBB = 0x55AA55AA55AA55AAULL;
|
||||
const Bitboard BlackSquaresBB = 0xAA55AA55AA55AA55ULL;
|
||||
|
||||
extern const Bitboard SquaresByColorBB[2];
|
||||
const Bitboard SquaresByColorBB[2] = { BlackSquaresBB, WhiteSquaresBB };
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
const Bitboard FileBBB = 0x0202020202020202ULL;
|
||||
@@ -96,9 +95,21 @@ const Bitboard FileFBB = 0x2020202020202020ULL;
|
||||
const Bitboard FileGBB = 0x4040404040404040ULL;
|
||||
const Bitboard FileHBB = 0x8080808080808080ULL;
|
||||
|
||||
extern const Bitboard FileBB[8];
|
||||
extern const Bitboard NeighboringFilesBB[8];
|
||||
extern const Bitboard ThisAndNeighboringFilesBB[8];
|
||||
const Bitboard FileBB[8] = {
|
||||
FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
|
||||
};
|
||||
|
||||
const Bitboard NeighboringFilesBB[8] = {
|
||||
FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
|
||||
FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
|
||||
};
|
||||
|
||||
const Bitboard ThisAndNeighboringFilesBB[8] = {
|
||||
FileABB|FileBBB, FileABB|FileBBB|FileCBB,
|
||||
FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
|
||||
FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
|
||||
FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
|
||||
};
|
||||
|
||||
const Bitboard Rank1BB = 0xFFULL;
|
||||
const Bitboard Rank2BB = 0xFF00ULL;
|
||||
@@ -109,9 +120,35 @@ const Bitboard Rank6BB = 0xFF0000000000ULL;
|
||||
const Bitboard Rank7BB = 0xFF000000000000ULL;
|
||||
const Bitboard Rank8BB = 0xFF00000000000000ULL;
|
||||
|
||||
extern const Bitboard RankBB[8];
|
||||
extern const Bitboard RelativeRankBB[2][8];
|
||||
extern const Bitboard InFrontBB[2][8];
|
||||
const Bitboard RankBB[8] = {
|
||||
Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
|
||||
};
|
||||
|
||||
const Bitboard RelativeRankBB[2][8] = {
|
||||
{ Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB },
|
||||
{ Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB }
|
||||
};
|
||||
|
||||
const Bitboard InFrontBB[2][8] = {
|
||||
{ Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank7BB | Rank8BB,
|
||||
Rank8BB,
|
||||
EmptyBoardBB
|
||||
},
|
||||
{ EmptyBoardBB,
|
||||
Rank1BB,
|
||||
Rank2BB | Rank1BB,
|
||||
Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
|
||||
}
|
||||
};
|
||||
|
||||
extern Bitboard SetMaskBB[65];
|
||||
extern Bitboard ClearMaskBB[65];
|
||||
@@ -124,13 +161,17 @@ extern Bitboard PassedPawnMask[2][64];
|
||||
extern Bitboard OutpostMask[2][64];
|
||||
|
||||
#if defined(USE_COMPACT_ROOK_ATTACKS)
|
||||
|
||||
extern Bitboard RankAttacks[8][64], FileAttacks[8][64];
|
||||
|
||||
#else
|
||||
|
||||
extern const uint64_t RMult[64];
|
||||
extern const int RShift[64];
|
||||
extern Bitboard RMask[64];
|
||||
extern int RAttackIndex[64];
|
||||
extern Bitboard RAttacks[0x19000];
|
||||
|
||||
#endif // defined(USE_COMPACT_ROOK_ATTACKS)
|
||||
|
||||
extern const uint64_t BMult[64];
|
||||
@@ -212,7 +253,7 @@ inline Bitboard this_and_neighboring_files_bb(Square s) {
|
||||
|
||||
/// relative_rank_bb() takes a color and a rank as input, and returns a bitboard
|
||||
/// representing all squares on the given rank from the given color's point of
|
||||
/// view. For instance, relative_rank_bb(WHITE, 7) gives all squares on the
|
||||
/// view. For instance, relative_rank_bb(WHITE, 7) gives all squares on the
|
||||
/// 7th rank, while relative_rank_bb(BLACK, 7) gives all squares on the 2nd
|
||||
/// rank.
|
||||
|
||||
@@ -395,24 +436,25 @@ inline int count_1s_max_15(Bitboard b) {
|
||||
|
||||
inline int count_1s(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v = v - ((v >> 1) & 0x55555555);
|
||||
w = w - ((w >> 1) & 0x55555555);
|
||||
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
|
||||
w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
|
||||
v = (v + (v >> 4)) & 0x0F0F0F0F;
|
||||
w = (w + (w >> 4)) & 0x0F0F0F0F;
|
||||
v = ((v+w) * 0x01010101) >> 24; // mul is fast on amd procs
|
||||
return int(v);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
||||
v = ((v >> 4) + v) & 0x0F0F0F0F; // 0-8 in 8 bits
|
||||
v += (((w >> 4) + w) & 0x0F0F0F0F); // 0-16 in 8 bits
|
||||
v *= 0x01010101; // mul is fast on amd procs
|
||||
return int(v >> 24);
|
||||
}
|
||||
|
||||
inline int count_1s_max_15(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v = v - ((v >> 1) & 0x55555555);
|
||||
w = w - ((w >> 1) & 0x55555555);
|
||||
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
|
||||
w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
|
||||
v = ((v+w) * 0x11111111) >> 28;
|
||||
return int(v);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
||||
v += w; // 0-8 in 4 bits
|
||||
v *= 0x11111111;
|
||||
return int(v >> 28);
|
||||
}
|
||||
|
||||
#elif defined(BITCOUNT_SWAR_64)
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008 Marco Costalba
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "color.h"
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// color_is_ok(), for debugging:
|
||||
|
||||
bool color_is_ok(Color c) {
|
||||
return c == WHITE || c == BLACK;
|
||||
}
|
||||
18
src/color.h
18
src/color.h
@@ -21,12 +21,6 @@
|
||||
#if !defined(COLOR_H_INCLUDED)
|
||||
#define COLOR_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
@@ -44,18 +38,14 @@ enum Color {
|
||||
////
|
||||
|
||||
inline Color operator+ (Color c, int i) { return Color(int(c) + i); }
|
||||
inline void operator++ (Color &c, int i) { c = Color(int(c) + 1); }
|
||||
inline void operator++ (Color &c, int) { c = Color(int(c) + 1); }
|
||||
|
||||
inline Color opposite_color(Color c) {
|
||||
return Color(int(c) ^ 1);
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern bool color_is_ok(Color c);
|
||||
|
||||
inline bool color_is_ok(Color c) {
|
||||
return c == WHITE || c == BLACK;
|
||||
}
|
||||
|
||||
#endif // !defined(COLOR_H_INCLUDED)
|
||||
|
||||
@@ -38,8 +38,8 @@ enum Direction {
|
||||
};
|
||||
|
||||
enum SignedDirection {
|
||||
SIGNED_DIR_E = 0, SIGNED_DIR_W = 1,
|
||||
SIGNED_DIR_N = 2, SIGNED_DIR_S = 3,
|
||||
SIGNED_DIR_E = 0, SIGNED_DIR_W = 1,
|
||||
SIGNED_DIR_N = 2, SIGNED_DIR_S = 3,
|
||||
SIGNED_DIR_NE = 4, SIGNED_DIR_SW = 5,
|
||||
SIGNED_DIR_NW = 6, SIGNED_DIR_SE = 7,
|
||||
SIGNED_DIR_NONE = 8
|
||||
@@ -58,9 +58,11 @@ extern uint8_t SignedDirectionTable[64][64];
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline void operator++ (Direction &d, int) { d = Direction(int(d) + 1); }
|
||||
inline void operator++ (Direction& d, int) {
|
||||
d = Direction(int(d) + 1);
|
||||
}
|
||||
|
||||
inline void operator++ (SignedDirection &d, int) {
|
||||
inline void operator++ (SignedDirection& d, int) {
|
||||
d = SignedDirection(int(d) + 1);
|
||||
}
|
||||
|
||||
@@ -72,6 +74,13 @@ inline SignedDirection signed_direction_between_squares(Square s1, Square s2) {
|
||||
return SignedDirection(SignedDirectionTable[s1][s2]);
|
||||
}
|
||||
|
||||
inline int direction_is_diagonal(Square s1, Square s2) {
|
||||
return DirectionTable[s1][s2] & 2;
|
||||
}
|
||||
|
||||
inline bool direction_is_straight(Square s1, Square s2) {
|
||||
return DirectionTable[s1][s2] < 2;
|
||||
}
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
|
||||
871
src/endgame.cpp
871
src/endgame.cpp
File diff suppressed because it is too large
Load Diff
262
src/endgame.h
262
src/endgame.h
@@ -34,163 +34,61 @@
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// Abstract base class for all special endgame evaluation functions:
|
||||
enum EndgameType {
|
||||
|
||||
class EndgameEvaluationFunction {
|
||||
// Evaluation functions
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
KPK, // KP vs K
|
||||
KRKP, // KR vs KP
|
||||
KRKB, // KR vs KB
|
||||
KRKN, // KR vs KN
|
||||
KQKR, // KQ vs KR
|
||||
KBBKN, // KBB vs KN
|
||||
KmmKm, // K and two minors vs K and one or two minors
|
||||
|
||||
// Scaling functions
|
||||
KBPK, // KBP vs K
|
||||
KQKRP, // KQ vs KRP
|
||||
KRPKR, // KRP vs KR
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // King and pawns vs king
|
||||
KBPKB, // KBP vs KB
|
||||
KBPPKB, // KBPP vs KB
|
||||
KBPKN, // KBP vs KN
|
||||
KNPK, // KNP vs K
|
||||
KPKP // KP vs KP
|
||||
};
|
||||
|
||||
/// Template abstract base class for all special endgame functions
|
||||
|
||||
template<typename T>
|
||||
class EndgameFunctionBase {
|
||||
public:
|
||||
EndgameEvaluationFunction(Color c);
|
||||
virtual ~EndgameEvaluationFunction() { }
|
||||
|
||||
virtual Value apply(const Position &pos) =0;
|
||||
EndgameFunctionBase(Color c) : strongerSide(c) { weakerSide = opposite_color(strongerSide); }
|
||||
virtual ~EndgameFunctionBase() {}
|
||||
virtual T apply(const Position&) = 0;
|
||||
|
||||
protected:
|
||||
Color strongerSide, weakerSide;
|
||||
};
|
||||
|
||||
typedef EndgameFunctionBase<Value> EndgameEvaluationFunctionBase;
|
||||
typedef EndgameFunctionBase<ScaleFactor> EndgameScalingFunctionBase;
|
||||
|
||||
/// Subclasses for various concrete endgames:
|
||||
|
||||
// Generic "mate lone king" eval:
|
||||
class KXKEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KXKEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
/// Templates subclass for various concrete endgames
|
||||
|
||||
template<EndgameType>
|
||||
struct EvaluationFunction : public EndgameEvaluationFunctionBase {
|
||||
explicit EvaluationFunction(Color c): EndgameEvaluationFunctionBase(c) {}
|
||||
Value apply(const Position&);
|
||||
};
|
||||
|
||||
// KBN vs K:
|
||||
class KBNKEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KBNKEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KP vs K:
|
||||
class KPKEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KPKEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KR vs KP:
|
||||
class KRKPEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KRKPEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KR vs KB:
|
||||
class KRKBEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KRKBEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KR vs KN:
|
||||
class KRKNEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KRKNEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KQ vs KR:
|
||||
class KQKREvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KQKREvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KBB vs KN:
|
||||
class KBBKNEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KBBKNEvaluationFunction(Color C);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
// K and two minors vs K and one or two minors:
|
||||
class KmmKmEvaluationFunction : public EndgameEvaluationFunction {
|
||||
public:
|
||||
KmmKmEvaluationFunction(Color c);
|
||||
Value apply(const Position &pos);
|
||||
};
|
||||
|
||||
|
||||
/// Abstract base class for all evaluation scaling functions:
|
||||
|
||||
class ScalingFunction {
|
||||
public:
|
||||
ScalingFunction(Color c);
|
||||
virtual ~ScalingFunction() { }
|
||||
|
||||
virtual ScaleFactor apply(const Position &pos) =0;
|
||||
|
||||
protected:
|
||||
Color strongerSide, weakerSide;
|
||||
};
|
||||
|
||||
|
||||
/// Subclasses for various concrete endgames:
|
||||
|
||||
// KBP vs K:
|
||||
class KBPKScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KBPKScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KQ vs KRP:
|
||||
class KQKRPScalingFunction: public ScalingFunction {
|
||||
public:
|
||||
KQKRPScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KRP vs KR:
|
||||
class KRPKRScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KRPKRScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KRPP vs KRP:
|
||||
class KRPPKRPScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KRPPKRPScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// King and pawns vs king:
|
||||
class KPsKScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KPsKScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KBP vs KB:
|
||||
class KBPKBScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KBPKBScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KBP vs KN:
|
||||
class KBPKNScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KBPKNScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KNP vs K:
|
||||
class KNPKScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KNPKScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
};
|
||||
|
||||
// KP vs KP:
|
||||
class KPKPScalingFunction : public ScalingFunction {
|
||||
public:
|
||||
KPKPScalingFunction(Color c);
|
||||
ScaleFactor apply(const Position &pos);
|
||||
template<EndgameType>
|
||||
struct ScalingFunction : public EndgameScalingFunctionBase {
|
||||
explicit ScalingFunction(Color c) : EndgameScalingFunctionBase(c) {}
|
||||
ScaleFactor apply(const Position&);
|
||||
};
|
||||
|
||||
|
||||
@@ -198,60 +96,26 @@ public:
|
||||
//// Constants and variables
|
||||
////
|
||||
|
||||
// Generic "mate lone king" eval:
|
||||
extern KXKEvaluationFunction EvaluateKXK, EvaluateKKX;
|
||||
|
||||
// KBN vs K:
|
||||
extern KBNKEvaluationFunction EvaluateKBNK, EvaluateKKBN;
|
||||
|
||||
// KP vs K:
|
||||
extern KPKEvaluationFunction EvaluateKPK, EvaluateKKP;
|
||||
|
||||
// KR vs KP:
|
||||
extern KRKPEvaluationFunction EvaluateKRKP, EvaluateKPKR;
|
||||
|
||||
// KR vs KB:
|
||||
extern KRKBEvaluationFunction EvaluateKRKB, EvaluateKBKR;
|
||||
|
||||
// KR vs KN:
|
||||
extern KRKNEvaluationFunction EvaluateKRKN, EvaluateKNKR;
|
||||
|
||||
// KQ vs KR:
|
||||
extern KQKREvaluationFunction EvaluateKQKR, EvaluateKRKQ;
|
||||
|
||||
// KBB vs KN:
|
||||
extern KBBKNEvaluationFunction EvaluateKBBKN, EvaluateKNKBB;
|
||||
|
||||
// K and two minors vs K and one or two minors:
|
||||
extern KmmKmEvaluationFunction EvaluateKmmKm;
|
||||
|
||||
// KBP vs K:
|
||||
extern KBPKScalingFunction ScaleKBPK, ScaleKKBP;
|
||||
|
||||
// KQ vs KRP:
|
||||
extern KQKRPScalingFunction ScaleKQKRP, ScaleKRPKQ;
|
||||
|
||||
// KRP vs KR:
|
||||
extern KRPKRScalingFunction ScaleKRPKR, ScaleKRKRP;
|
||||
|
||||
// KRPP vs KRP:
|
||||
extern KRPPKRPScalingFunction ScaleKRPPKRP, ScaleKRPKRPP;
|
||||
|
||||
// King and pawns vs king:
|
||||
extern KPsKScalingFunction ScaleKPsK, ScaleKKPs;
|
||||
|
||||
// KBP vs KB:
|
||||
extern KBPKBScalingFunction ScaleKBPKB, ScaleKBKBP;
|
||||
|
||||
// KBP vs KN:
|
||||
extern KBPKNScalingFunction ScaleKBPKN, ScaleKNKBP;
|
||||
|
||||
// KNP vs K:
|
||||
extern KNPKScalingFunction ScaleKNPK, ScaleKKNP;
|
||||
|
||||
// KP vs KP:
|
||||
extern KPKPScalingFunction ScaleKPKPw, ScaleKPKPb;
|
||||
extern EvaluationFunction<KXK> EvaluateKXK, EvaluateKKX; // Generic "mate lone king" eval
|
||||
extern EvaluationFunction<KBNK> EvaluateKBNK, EvaluateKKBN; // KBN vs K
|
||||
extern EvaluationFunction<KPK> EvaluateKPK, EvaluateKKP; // KP vs K
|
||||
extern EvaluationFunction<KRKP> EvaluateKRKP, EvaluateKPKR; // KR vs KP
|
||||
extern EvaluationFunction<KRKB> EvaluateKRKB, EvaluateKBKR; // KR vs KB
|
||||
extern EvaluationFunction<KRKN> EvaluateKRKN, EvaluateKNKR; // KR vs KN
|
||||
extern EvaluationFunction<KQKR> EvaluateKQKR, EvaluateKRKQ; // KQ vs KR
|
||||
extern EvaluationFunction<KBBKN> EvaluateKBBKN, EvaluateKNKBB; // KBB vs KN
|
||||
extern EvaluationFunction<KmmKm> EvaluateKmmKm; // K and two minors vs K and one or two minors:
|
||||
|
||||
extern ScalingFunction<KBPK> ScaleKBPK, ScaleKKBP; // KBP vs K
|
||||
extern ScalingFunction<KQKRP> ScaleKQKRP, ScaleKRPKQ; // KQ vs KRP
|
||||
extern ScalingFunction<KRPKR> ScaleKRPKR, ScaleKRKRP; // KRP vs KR
|
||||
extern ScalingFunction<KRPPKRP> ScaleKRPPKRP, ScaleKRPKRPP; // KRPP vs KRP
|
||||
extern ScalingFunction<KPsK> ScaleKPsK, ScaleKKPs; // King and pawns vs king
|
||||
extern ScalingFunction<KBPKB> ScaleKBPKB, ScaleKBKBP; // KBP vs KB
|
||||
extern ScalingFunction<KBPPKB> ScaleKBPPKB, ScaleKBKBPP; // KBPP vs KB
|
||||
extern ScalingFunction<KBPKN> ScaleKBPKN, ScaleKNKBP; // KBP vs KN
|
||||
extern ScalingFunction<KNPK> ScaleKNPK, ScaleKKNP; // KNP vs K
|
||||
extern ScalingFunction<KPKP> ScaleKPKPw, ScaleKPKPb; // KP vs KP
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
|
||||
669
src/evaluate.cpp
669
src/evaluate.cpp
@@ -58,15 +58,17 @@ namespace {
|
||||
// weights read from UCI parameters. The purpose is to be able to change
|
||||
// the evaluation weights while keeping the default values of the UCI
|
||||
// parameters at 100, which looks prettier.
|
||||
const int WeightMobilityMidgameInternal = 0x100;
|
||||
const int WeightMobilityEndgameInternal = 0x100;
|
||||
const int WeightPawnStructureMidgameInternal = 0x100;
|
||||
const int WeightPawnStructureEndgameInternal = 0x100;
|
||||
const int WeightPassedPawnsMidgameInternal = 0x100;
|
||||
const int WeightPassedPawnsEndgameInternal = 0x100;
|
||||
const int WeightKingSafetyInternal = 0x110;
|
||||
const int WeightKingOppSafetyInternal = 0x110;
|
||||
const int WeightSpaceInternal = 0x30;
|
||||
//
|
||||
// Values modified by Joona Kiiski
|
||||
const int WeightMobilityMidgameInternal = 0x0FA;
|
||||
const int WeightMobilityEndgameInternal = 0x10A;
|
||||
const int WeightPawnStructureMidgameInternal = 0x0EC;
|
||||
const int WeightPawnStructureEndgameInternal = 0x0CD;
|
||||
const int WeightPassedPawnsMidgameInternal = 0x108;
|
||||
const int WeightPassedPawnsEndgameInternal = 0x109;
|
||||
const int WeightKingSafetyInternal = 0x0F7;
|
||||
const int WeightKingOppSafetyInternal = 0x101;
|
||||
const int WeightSpaceInternal = 0x02F;
|
||||
|
||||
// Visually better to define tables constants
|
||||
typedef Value V;
|
||||
@@ -167,18 +169,19 @@ namespace {
|
||||
V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) // 8
|
||||
};
|
||||
|
||||
// Bonus for unstoppable passed pawns:
|
||||
// Bonus for unstoppable passed pawns
|
||||
const Value UnstoppablePawnValue = Value(0x500);
|
||||
|
||||
// Rooks and queens on the 7th rank:
|
||||
const Value MidgameRookOn7thBonus = Value(50);
|
||||
const Value EndgameRookOn7thBonus = Value(100);
|
||||
const Value MidgameQueenOn7thBonus = Value(25);
|
||||
const Value EndgameQueenOn7thBonus = Value(50);
|
||||
// Rooks and queens on the 7th rank
|
||||
const Value MidgameRookOn7thBonus = Value(47);
|
||||
const Value EndgameRookOn7thBonus = Value(98);
|
||||
const Value MidgameQueenOn7thBonus = Value(27);
|
||||
const Value EndgameQueenOn7thBonus = Value(54);
|
||||
|
||||
// Rooks on open files:
|
||||
const Value RookOpenFileBonus = Value(40);
|
||||
const Value RookHalfOpenFileBonus = Value(20);
|
||||
|
||||
// Rooks on open files
|
||||
const Value RookOpenFileBonus = Value(43);
|
||||
const Value RookHalfOpenFileBonus = Value(19);
|
||||
|
||||
// Penalty for rooks trapped inside a friendly king which has lost the
|
||||
// right to castle:
|
||||
@@ -272,11 +275,11 @@ namespace {
|
||||
uint8_t BitCount8Bit[256];
|
||||
|
||||
// Function prototypes
|
||||
void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei);
|
||||
void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei);
|
||||
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei);
|
||||
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei);
|
||||
void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei);
|
||||
template<PieceType Piece>
|
||||
void evaluate_pieces(const Position& p, Color us, EvalInfo& ei);
|
||||
|
||||
template<>
|
||||
void evaluate_pieces<KING>(const Position& p, Color us, EvalInfo &ei);
|
||||
|
||||
void evaluate_passed_pawns(const Position &pos, EvalInfo &ei);
|
||||
void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us,
|
||||
@@ -352,52 +355,22 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
|
||||
// Evaluate pieces
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
{
|
||||
// Knights
|
||||
for (int i = 0; i < pos.piece_count(c, KNIGHT); i++)
|
||||
evaluate_knight(pos, pos.piece_list(c, KNIGHT, i), c, ei);
|
||||
evaluate_pieces<KNIGHT>(pos, c, ei);
|
||||
evaluate_pieces<BISHOP>(pos, c, ei);
|
||||
evaluate_pieces<ROOK>(pos, c, ei);
|
||||
evaluate_pieces<QUEEN>(pos, c, ei);
|
||||
|
||||
// Bishops
|
||||
for (int i = 0; i < pos.piece_count(c, BISHOP); i++)
|
||||
evaluate_bishop(pos, pos.piece_list(c, BISHOP, i), c, ei);
|
||||
|
||||
// Rooks
|
||||
for (int i = 0; i < pos.piece_count(c, ROOK); i++)
|
||||
evaluate_rook(pos, pos.piece_list(c, ROOK, i), c, ei);
|
||||
|
||||
// Queens
|
||||
for(int i = 0; i < pos.piece_count(c, QUEEN); i++)
|
||||
evaluate_queen(pos, pos.piece_list(c, QUEEN, i), c, ei);
|
||||
|
||||
// Special pattern: trapped bishops on a7/h7/a2/h2
|
||||
Bitboard b = pos.bishops(c) & MaskA7H7[c];
|
||||
while (b)
|
||||
{
|
||||
Square s = pop_1st_bit(&b);
|
||||
evaluate_trapped_bishop_a7h7(pos, s, c, ei);
|
||||
}
|
||||
|
||||
// Special pattern: trapped bishops on a1/h1/a8/h8 in Chess960:
|
||||
if (Chess960)
|
||||
{
|
||||
b = pos.bishops(c) & MaskA1H1[c];
|
||||
while (b)
|
||||
{
|
||||
Square s = pop_1st_bit(&b);
|
||||
evaluate_trapped_bishop_a1h1(pos, s, c, ei);
|
||||
}
|
||||
}
|
||||
|
||||
// Sum up all attacked squares
|
||||
ei.attackedBy[c][0] = ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT]
|
||||
| ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
|
||||
| ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING];
|
||||
// Sum up all attacked squares
|
||||
ei.attackedBy[c][0] = ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT]
|
||||
| ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
|
||||
| ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING];
|
||||
}
|
||||
|
||||
// Kings. Kings are evaluated after all other pieces for both sides,
|
||||
// because we need complete attack information for all pieces when computing
|
||||
// the king safety evaluation.
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
evaluate_king(pos, pos.king_square(c), c, ei);
|
||||
evaluate_pieces<KING>(pos, c, ei);
|
||||
|
||||
// Evaluate passed pawns. We evaluate passed pawns for both sides at once,
|
||||
// because we need to know which side promotes first in positions where
|
||||
@@ -517,7 +490,10 @@ void init_eval(int threads) {
|
||||
}
|
||||
|
||||
for (Bitboard b = 0ULL; b < 256ULL; b++)
|
||||
BitCount8Bit[b] = count_1s(b);
|
||||
{
|
||||
assert(count_1s(b) == int(uint8_t(count_1s(b))));
|
||||
BitCount8Bit[b] = (uint8_t)count_1s(b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -559,34 +535,47 @@ namespace {
|
||||
|
||||
// evaluate_common() computes terms common to all pieces attack
|
||||
|
||||
int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei,
|
||||
int AttackWeight, const Value* mgBonus, const Value* egBonus,
|
||||
Square s = SQ_NONE, const Value* OutpostBonus = NULL) {
|
||||
template<PieceType Piece>
|
||||
int evaluate_common(const Position& p, const Bitboard& b, Color us, EvalInfo& ei, Square s = SQ_NONE) {
|
||||
|
||||
static const int AttackWeight[] = { 0, 0, KnightAttackWeight, BishopAttackWeight, RookAttackWeight, QueenAttackWeight };
|
||||
static const Value* MgBonus[] = { 0, 0, MidgameKnightMobilityBonus, MidgameBishopMobilityBonus, MidgameRookMobilityBonus, MidgameQueenMobilityBonus };
|
||||
static const Value* EgBonus[] = { 0, 0, EndgameKnightMobilityBonus, EndgameBishopMobilityBonus, EndgameRookMobilityBonus, EndgameQueenMobilityBonus };
|
||||
static const Value* OutpostBonus[] = { 0, 0, KnightOutpostBonus, BishopOutpostBonus, 0, 0 };
|
||||
|
||||
Color them = opposite_color(us);
|
||||
|
||||
// Update attack info
|
||||
ei.attackedBy[us][Piece] |= b;
|
||||
|
||||
// King attack
|
||||
if (b & ei.kingZone[us])
|
||||
{
|
||||
ei.kingAttackersCount[us]++;
|
||||
ei.kingAttackersWeight[us] += AttackWeight;
|
||||
ei.kingAttackersWeight[us] += AttackWeight[Piece];
|
||||
Bitboard bb = (b & ei.attackedBy[them][KING]);
|
||||
if (bb)
|
||||
ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15(bb);
|
||||
}
|
||||
|
||||
// Remove squares protected by enemy pawns
|
||||
Bitboard bb = (b & ~ei.attackedBy[them][PAWN]);
|
||||
|
||||
// Mobility
|
||||
int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
|
||||
ei.mgMobility += Sign[us] * mgBonus[mob];
|
||||
ei.egMobility += Sign[us] * egBonus[mob];
|
||||
int mob = (Piece != QUEEN ? count_1s_max_15(bb & ~p.pieces_of_color(us))
|
||||
: count_1s(bb & ~p.pieces_of_color(us)));
|
||||
|
||||
ei.mgMobility += Sign[us] * MgBonus[Piece][mob];
|
||||
ei.egMobility += Sign[us] * EgBonus[Piece][mob];
|
||||
|
||||
// Bishop and Knight outposts
|
||||
if (!OutpostBonus || !p.square_is_weak(s, them))
|
||||
if ( (Piece != BISHOP && Piece != KNIGHT) // compile time condition
|
||||
|| !p.square_is_weak(s, them))
|
||||
return mob;
|
||||
|
||||
// Initial bonus based on square
|
||||
Value v, bonus;
|
||||
v = bonus = OutpostBonus[relative_square(us, s)];
|
||||
v = bonus = OutpostBonus[Piece][relative_square(us, s)];
|
||||
|
||||
// Increase bonus if supported by pawn, especially if the opponent has
|
||||
// no minor piece which can exchange the outpost piece
|
||||
@@ -603,120 +592,101 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
// evaluate_knight() assigns bonuses and penalties to a knight of a given
|
||||
// color on a given square.
|
||||
// evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given
|
||||
// color.
|
||||
|
||||
void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) {
|
||||
template<PieceType Piece>
|
||||
void evaluate_pieces(const Position& pos, Color us, EvalInfo& ei) {
|
||||
|
||||
Bitboard b = p.piece_attacks<KNIGHT>(s);
|
||||
ei.attackedBy[us][KNIGHT] |= b;
|
||||
Bitboard b;
|
||||
Square s, ksq;
|
||||
Color them;
|
||||
int mob;
|
||||
File f;
|
||||
|
||||
// King attack, mobility and outposts
|
||||
evaluate_common(p, b, us, ei, KnightAttackWeight, MidgameKnightMobilityBonus,
|
||||
EndgameKnightMobilityBonus, s, KnightOutpostBonus);
|
||||
}
|
||||
|
||||
|
||||
// evaluate_bishop() assigns bonuses and penalties to a bishop of a given
|
||||
// color on a given square.
|
||||
|
||||
void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei) {
|
||||
|
||||
Bitboard b = bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us));
|
||||
ei.attackedBy[us][BISHOP] |= b;
|
||||
|
||||
// King attack, mobility and outposts
|
||||
evaluate_common(p, b, us, ei, BishopAttackWeight, MidgameBishopMobilityBonus,
|
||||
EndgameBishopMobilityBonus, s, BishopOutpostBonus);
|
||||
}
|
||||
|
||||
|
||||
// evaluate_rook() assigns bonuses and penalties to a rook of a given
|
||||
// color on a given square.
|
||||
|
||||
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) {
|
||||
|
||||
Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
|
||||
ei.attackedBy[us][ROOK] |= b;
|
||||
|
||||
// King attack and mobility
|
||||
int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus,
|
||||
EndgameRookMobilityBonus);
|
||||
|
||||
// Rook on 7th rank
|
||||
Color them = opposite_color(us);
|
||||
|
||||
if ( relative_rank(us, s) == RANK_7
|
||||
&& relative_rank(us, p.king_square(them)) == RANK_8)
|
||||
for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
|
||||
{
|
||||
ei.mgValue += Sign[us] * MidgameRookOn7thBonus;
|
||||
ei.egValue += Sign[us] * EndgameRookOn7thBonus;
|
||||
}
|
||||
s = pos.piece_list(us, Piece, i);
|
||||
|
||||
// Open and half-open files
|
||||
File f = square_file(s);
|
||||
if (ei.pi->file_is_half_open(us, f))
|
||||
{
|
||||
if (ei.pi->file_is_half_open(them, f))
|
||||
if (Piece == KNIGHT || Piece == QUEEN)
|
||||
b = pos.piece_attacks<Piece>(s);
|
||||
else if (Piece == BISHOP)
|
||||
b = bishop_attacks_bb(s, pos.occupied_squares() & ~pos.queens(us));
|
||||
else if (Piece == ROOK)
|
||||
b = rook_attacks_bb(s, pos.occupied_squares() & ~pos.rooks_and_queens(us));
|
||||
|
||||
// Attacks, mobility and outposts
|
||||
mob = evaluate_common<Piece>(pos, b, us, ei, s);
|
||||
|
||||
// Special patterns: trapped bishops on a7/h7/a2/h2
|
||||
// and trapped bishops on a1/h1/a8/h8 in Chess960.
|
||||
if (Piece == BISHOP)
|
||||
{
|
||||
ei.mgValue += Sign[us] * RookOpenFileBonus;
|
||||
ei.egValue += Sign[us] * RookOpenFileBonus;
|
||||
if (bit_is_set(MaskA7H7[us], s))
|
||||
evaluate_trapped_bishop_a7h7(pos, s, us, ei);
|
||||
|
||||
if (Chess960 && bit_is_set(MaskA1H1[us], s))
|
||||
evaluate_trapped_bishop_a1h1(pos, s, us, ei);
|
||||
}
|
||||
else
|
||||
|
||||
if (Piece == ROOK || Piece == QUEEN)
|
||||
{
|
||||
ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
|
||||
ei.egValue += Sign[us] * RookHalfOpenFileBonus;
|
||||
// Queen or rook on 7th rank
|
||||
them = opposite_color(us);
|
||||
|
||||
if ( relative_rank(us, s) == RANK_7
|
||||
&& relative_rank(us, pos.king_square(them)) == RANK_8)
|
||||
{
|
||||
ei.mgValue += Sign[us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
|
||||
ei.egValue += Sign[us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Penalize rooks which are trapped inside a king. Penalize more if
|
||||
// king has lost right to castle
|
||||
if (mob > 6 || ei.pi->file_is_half_open(us, f))
|
||||
return;
|
||||
// Special extra evaluation for rooks
|
||||
if (Piece == ROOK)
|
||||
{
|
||||
// Open and half-open files
|
||||
f = square_file(s);
|
||||
if (ei.pi->file_is_half_open(us, f))
|
||||
{
|
||||
if (ei.pi->file_is_half_open(them, f))
|
||||
{
|
||||
ei.mgValue += Sign[us] * RookOpenFileBonus;
|
||||
ei.egValue += Sign[us] * RookOpenFileBonus;
|
||||
}
|
||||
else
|
||||
{
|
||||
ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
|
||||
ei.egValue += Sign[us] * RookHalfOpenFileBonus;
|
||||
}
|
||||
}
|
||||
|
||||
Square ksq = p.king_square(us);
|
||||
// Penalize rooks which are trapped inside a king. Penalize more if
|
||||
// king has lost right to castle.
|
||||
if (mob > 6 || ei.pi->file_is_half_open(us, f))
|
||||
continue;
|
||||
|
||||
if ( square_file(ksq) >= FILE_E
|
||||
&& square_file(s) > square_file(ksq)
|
||||
&& (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
|
||||
{
|
||||
// Is there a half-open file between the king and the edge of the board?
|
||||
if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
|
||||
ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
|
||||
: Sign[us] * (TrappedRookPenalty - mob * 16);
|
||||
}
|
||||
else if ( square_file(ksq) <= FILE_D
|
||||
&& square_file(s) < square_file(ksq)
|
||||
&& (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
|
||||
{
|
||||
// Is there a half-open file between the king and the edge of the board?
|
||||
if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
|
||||
ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
|
||||
: Sign[us] * (TrappedRookPenalty - mob * 16);
|
||||
}
|
||||
}
|
||||
ksq = pos.king_square(us);
|
||||
|
||||
|
||||
// evaluate_queen() assigns bonuses and penalties to a queen of a given
|
||||
// color on a given square.
|
||||
|
||||
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) {
|
||||
|
||||
Bitboard b = p.piece_attacks<QUEEN>(s);
|
||||
ei.attackedBy[us][QUEEN] |= b;
|
||||
|
||||
// King attack and mobility
|
||||
evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus,
|
||||
EndgameQueenMobilityBonus);
|
||||
|
||||
// Queen on 7th rank
|
||||
Color them = opposite_color(us);
|
||||
|
||||
if ( relative_rank(us, s) == RANK_7
|
||||
&& relative_rank(us, p.king_square(them)) == RANK_8)
|
||||
{
|
||||
ei.mgValue += Sign[us] * MidgameQueenOn7thBonus;
|
||||
ei.egValue += Sign[us] * EndgameQueenOn7thBonus;
|
||||
if ( square_file(ksq) >= FILE_E
|
||||
&& square_file(s) > square_file(ksq)
|
||||
&& (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
|
||||
{
|
||||
// Is there a half-open file between the king and the edge of the board?
|
||||
if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
|
||||
ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
|
||||
: Sign[us] * (TrappedRookPenalty - mob * 16);
|
||||
}
|
||||
else if ( square_file(ksq) <= FILE_D
|
||||
&& square_file(s) < square_file(ksq)
|
||||
&& (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
|
||||
{
|
||||
// Is there a half-open file between the king and the edge of the board?
|
||||
if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
|
||||
ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
|
||||
: Sign[us] * (TrappedRookPenalty - mob * 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -725,12 +695,14 @@ namespace {
|
||||
return b >> (num << 3);
|
||||
}
|
||||
|
||||
// evaluate_king() assigns bonuses and penalties to a king of a given
|
||||
// color on a given square.
|
||||
// evaluate_pieces<KING>() assigns bonuses and penalties to a king of a given
|
||||
// color.
|
||||
|
||||
void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei) {
|
||||
template<>
|
||||
void evaluate_pieces<KING>(const Position& p, Color us, EvalInfo& ei) {
|
||||
|
||||
int shelter = 0, sign = Sign[us];
|
||||
Square s = p.king_square(us);
|
||||
|
||||
// King shelter
|
||||
if (relative_rank(us, s) <= RANK_4)
|
||||
@@ -810,7 +782,7 @@ namespace {
|
||||
if ( bit_is_set(p.piece_attacks<QUEEN>(from), to)
|
||||
&& !bit_is_set(p.pinned_pieces(them), from)
|
||||
&& !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us))
|
||||
&& !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us)))
|
||||
&& !(bishop_attacks_bb(to, occ & ClearMaskBB[from]) & p.bishops_and_queens(us)))
|
||||
|
||||
ei.mateThreat[them] = make_move(from, to);
|
||||
}
|
||||
@@ -902,148 +874,161 @@ namespace {
|
||||
// evaluate_passed_pawns() evaluates the passed pawns for both sides.
|
||||
|
||||
void evaluate_passed_pawns(const Position &pos, EvalInfo &ei) {
|
||||
|
||||
bool hasUnstoppable[2] = {false, false};
|
||||
int movesToGo[2] = {100, 100};
|
||||
|
||||
for(Color us = WHITE; us <= BLACK; us++) {
|
||||
Color them = opposite_color(us);
|
||||
Square ourKingSq = pos.king_square(us);
|
||||
Square theirKingSq = pos.king_square(them);
|
||||
Bitboard b = ei.pi->passed_pawns() & pos.pawns(us), b2, b3, b4;
|
||||
for (Color us = WHITE; us <= BLACK; us++)
|
||||
{
|
||||
Color them = opposite_color(us);
|
||||
Square ourKingSq = pos.king_square(us);
|
||||
Square theirKingSq = pos.king_square(them);
|
||||
Bitboard b = ei.pi->passed_pawns() & pos.pawns(us), b2, b3, b4;
|
||||
|
||||
while(b) {
|
||||
Square s = pop_1st_bit(&b);
|
||||
assert(pos.piece_on(s) == pawn_of_color(us));
|
||||
assert(pos.pawn_is_passed(us, s));
|
||||
while (b)
|
||||
{
|
||||
Square s = pop_1st_bit(&b);
|
||||
|
||||
int r = int(relative_rank(us, s) - RANK_2);
|
||||
int tr = Max(0, r * (r-1));
|
||||
Square blockSq = s + pawn_push(us);
|
||||
assert(pos.piece_on(s) == piece_of_color_and_type(us, PAWN));
|
||||
assert(pos.pawn_is_passed(us, s));
|
||||
|
||||
// Base bonus based on rank:
|
||||
Value mbonus = Value(20 * tr);
|
||||
Value ebonus = Value(10 + r * r * 10);
|
||||
int r = int(relative_rank(us, s) - RANK_2);
|
||||
int tr = Max(0, r * (r - 1));
|
||||
Square blockSq = s + pawn_push(us);
|
||||
|
||||
// Adjust bonus based on king proximity:
|
||||
ebonus -= Value(square_distance(ourKingSq, blockSq) * 3 * tr);
|
||||
ebonus -=
|
||||
Value(square_distance(ourKingSq, blockSq + pawn_push(us)) * 1 * tr);
|
||||
ebonus += Value(square_distance(theirKingSq, blockSq) * 6 * tr);
|
||||
// Base bonus based on rank
|
||||
Value mbonus = Value(20 * tr);
|
||||
Value ebonus = Value(10 + r * r * 10);
|
||||
|
||||
// If the pawn is free to advance, increase bonus:
|
||||
if(pos.square_is_empty(blockSq)) {
|
||||
// Adjust bonus based on king proximity
|
||||
if (tr != 0)
|
||||
{
|
||||
ebonus -= Value(square_distance(ourKingSq, blockSq) * 3 * tr);
|
||||
ebonus -= Value(square_distance(ourKingSq, blockSq + pawn_push(us)) * 1 * tr);
|
||||
ebonus += Value(square_distance(theirKingSq, blockSq) * 6 * tr);
|
||||
|
||||
b2 = squares_in_front_of(us, s);
|
||||
b3 = b2 & ei.attacked_by(them);
|
||||
b4 = b2 & ei.attacked_by(us);
|
||||
// If the pawn is free to advance, increase bonus
|
||||
if (pos.square_is_empty(blockSq))
|
||||
{
|
||||
b2 = squares_in_front_of(us, s);
|
||||
b3 = b2 & ei.attacked_by(them);
|
||||
b4 = b2 & ei.attacked_by(us);
|
||||
|
||||
// If there is an enemy rook or queen attacking the pawn from behind,
|
||||
// add all X-ray attacks by the rook or queen.
|
||||
if(bit_is_set(ei.attacked_by(them,ROOK)|ei.attacked_by(them,QUEEN),s)
|
||||
&& squares_behind(us, s) & pos.rooks_and_queens(them))
|
||||
b3 = b2;
|
||||
// If there is an enemy rook or queen attacking the pawn from behind,
|
||||
// add all X-ray attacks by the rook or queen.
|
||||
if ( bit_is_set(ei.attacked_by(them,ROOK) | ei.attacked_by(them,QUEEN),s)
|
||||
&& (squares_behind(us, s) & pos.rooks_and_queens(them)))
|
||||
b3 = b2;
|
||||
|
||||
if((b2 & pos.pieces_of_color(them)) == EmptyBoardBB) {
|
||||
// There are no enemy pieces in the pawn's path! Are any of the
|
||||
// squares in the pawn's path attacked by the enemy?
|
||||
if(b3 == EmptyBoardBB)
|
||||
// No enemy attacks, huge bonus!
|
||||
ebonus += Value(tr * ((b2 == b4)? 17 : 15));
|
||||
else
|
||||
// OK, there are enemy attacks. Are those squares which are
|
||||
// attacked by the enemy also attacked by us? If yes, big bonus
|
||||
// (but smaller than when there are no enemy attacks), if no,
|
||||
// somewhat smaller bonus.
|
||||
ebonus += Value(tr * (((b3 & b4) == b3)? 13 : 8));
|
||||
}
|
||||
else {
|
||||
// There are some enemy pieces in the pawn's path. While this is
|
||||
// sad, we still assign a moderate bonus if all squares in the path
|
||||
// which are either occupied by or attacked by enemy pieces are
|
||||
// also attacked by us.
|
||||
if(((b3 | (b2 & pos.pieces_of_color(them))) & ~b4) == EmptyBoardBB)
|
||||
ebonus += Value(tr * 6);
|
||||
}
|
||||
// At last, add a small bonus when there are no *friendly* pieces
|
||||
// in the pawn's path:
|
||||
if((b2 & pos.pieces_of_color(us)) == EmptyBoardBB)
|
||||
ebonus += Value(tr);
|
||||
}
|
||||
|
||||
// If the pawn is supported by a friendly pawn, increase bonus.
|
||||
b2 = pos.pawns(us) & neighboring_files_bb(s);
|
||||
if(b2 & rank_bb(s))
|
||||
ebonus += Value(r * 20);
|
||||
else if(pos.pawn_attacks(them, s) & b2)
|
||||
ebonus += Value(r * 12);
|
||||
|
||||
// If the other side has only a king, check whether the pawn is
|
||||
// unstoppable:
|
||||
if(pos.non_pawn_material(them) == Value(0)) {
|
||||
Square qsq;
|
||||
int d;
|
||||
|
||||
qsq = relative_square(us, make_square(square_file(s), RANK_8));
|
||||
d = square_distance(s, qsq) - square_distance(theirKingSq, qsq)
|
||||
+ ((us == pos.side_to_move())? 0 : 1);
|
||||
|
||||
if(d < 0) {
|
||||
int mtg = RANK_8 - relative_rank(us, s);
|
||||
int blockerCount =
|
||||
count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares());
|
||||
mtg += blockerCount;
|
||||
d += blockerCount;
|
||||
if(d < 0) {
|
||||
hasUnstoppable[us] = true;
|
||||
movesToGo[us] = Min(movesToGo[us], mtg);
|
||||
if ((b2 & pos.pieces_of_color(them)) == EmptyBoardBB)
|
||||
{
|
||||
// There are no enemy pieces in the pawn's path! Are any of the
|
||||
// squares in the pawn's path attacked by the enemy?
|
||||
if (b3 == EmptyBoardBB)
|
||||
// No enemy attacks, huge bonus!
|
||||
ebonus += Value(tr * (b2 == b4 ? 17 : 15));
|
||||
else
|
||||
// OK, there are enemy attacks. Are those squares which are
|
||||
// attacked by the enemy also attacked by us? If yes, big bonus
|
||||
// (but smaller than when there are no enemy attacks), if no,
|
||||
// somewhat smaller bonus.
|
||||
ebonus += Value(tr * ((b3 & b4) == b3 ? 13 : 8));
|
||||
}
|
||||
else
|
||||
{
|
||||
// There are some enemy pieces in the pawn's path. While this is
|
||||
// sad, we still assign a moderate bonus if all squares in the path
|
||||
// which are either occupied by or attacked by enemy pieces are
|
||||
// also attacked by us.
|
||||
if (((b3 | (b2 & pos.pieces_of_color(them))) & ~b4) == EmptyBoardBB)
|
||||
ebonus += Value(tr * 6);
|
||||
}
|
||||
// At last, add a small bonus when there are no *friendly* pieces
|
||||
// in the pawn's path.
|
||||
if ((b2 & pos.pieces_of_color(us)) == EmptyBoardBB)
|
||||
ebonus += Value(tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Rook pawns are a special case: They are sometimes worse, and
|
||||
// sometimes better than other passed pawns. It is difficult to find
|
||||
// good rules for determining whether they are good or bad. For now,
|
||||
// we try the following: Increase the value for rook pawns if the
|
||||
// other side has no pieces apart from a knight, and decrease the
|
||||
// value if the other side has a rook or queen.
|
||||
if(square_file(s) == FILE_A || square_file(s) == FILE_H) {
|
||||
if(pos.non_pawn_material(them) == KnightValueMidgame
|
||||
&& pos.piece_count(them, KNIGHT) == 1)
|
||||
ebonus += ebonus / 4;
|
||||
else if(pos.rooks_and_queens(them))
|
||||
ebonus -= ebonus / 4;
|
||||
}
|
||||
|
||||
// Add the scores for this pawn to the middle game and endgame eval.
|
||||
ei.mgValue += apply_weight(Sign[us] * mbonus, WeightPassedPawnsMidgame);
|
||||
ei.egValue += apply_weight(Sign[us] * ebonus, WeightPassedPawnsEndgame);
|
||||
}
|
||||
// If the pawn is supported by a friendly pawn, increase bonus
|
||||
b2 = pos.pawns(us) & neighboring_files_bb(s);
|
||||
if (b2 & rank_bb(s))
|
||||
ebonus += Value(r * 20);
|
||||
else if (pos.pawn_attacks(them, s) & b2)
|
||||
ebonus += Value(r * 12);
|
||||
|
||||
// If the other side has only a king, check whether the pawn is
|
||||
// unstoppable
|
||||
if (pos.non_pawn_material(them) == Value(0))
|
||||
{
|
||||
Square qsq;
|
||||
int d;
|
||||
|
||||
qsq = relative_square(us, make_square(square_file(s), RANK_8));
|
||||
d = square_distance(s, qsq)
|
||||
- square_distance(theirKingSq, qsq)
|
||||
+ (us != pos.side_to_move());
|
||||
|
||||
if (d < 0)
|
||||
{
|
||||
int mtg = RANK_8 - relative_rank(us, s);
|
||||
int blockerCount = count_1s_max_15(squares_in_front_of(us,s) & pos.occupied_squares());
|
||||
mtg += blockerCount;
|
||||
d += blockerCount;
|
||||
if (d < 0)
|
||||
{
|
||||
hasUnstoppable[us] = true;
|
||||
movesToGo[us] = Min(movesToGo[us], mtg);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Rook pawns are a special case: They are sometimes worse, and
|
||||
// sometimes better than other passed pawns. It is difficult to find
|
||||
// good rules for determining whether they are good or bad. For now,
|
||||
// we try the following: Increase the value for rook pawns if the
|
||||
// other side has no pieces apart from a knight, and decrease the
|
||||
// value if the other side has a rook or queen.
|
||||
if (square_file(s) == FILE_A || square_file(s) == FILE_H)
|
||||
{
|
||||
if( pos.non_pawn_material(them) <= KnightValueMidgame
|
||||
&& pos.piece_count(them, KNIGHT) <= 1)
|
||||
ebonus += ebonus / 4;
|
||||
else if(pos.rooks_and_queens(them))
|
||||
ebonus -= ebonus / 4;
|
||||
}
|
||||
|
||||
// Add the scores for this pawn to the middle game and endgame eval.
|
||||
ei.mgValue += apply_weight(Sign[us] * mbonus, WeightPassedPawnsMidgame);
|
||||
ei.egValue += apply_weight(Sign[us] * ebonus, WeightPassedPawnsEndgame);
|
||||
}
|
||||
}
|
||||
|
||||
// Does either side have an unstoppable passed pawn?
|
||||
if(hasUnstoppable[WHITE] && !hasUnstoppable[BLACK])
|
||||
ei.egValue += UnstoppablePawnValue - Value(0x40 * movesToGo[WHITE]);
|
||||
else if(hasUnstoppable[BLACK] && !hasUnstoppable[WHITE])
|
||||
ei.egValue -= UnstoppablePawnValue - Value(0x40 * movesToGo[BLACK]);
|
||||
else if(hasUnstoppable[BLACK] && hasUnstoppable[WHITE]) {
|
||||
// Both sides have unstoppable pawns! Try to find out who queens
|
||||
// first. We begin by transforming 'movesToGo' to the number of
|
||||
// plies until the pawn queens for both sides:
|
||||
movesToGo[WHITE] *= 2;
|
||||
movesToGo[BLACK] *= 2;
|
||||
movesToGo[pos.side_to_move()]--;
|
||||
if (hasUnstoppable[WHITE] && !hasUnstoppable[BLACK])
|
||||
ei.egValue += UnstoppablePawnValue - Value(0x40 * movesToGo[WHITE]);
|
||||
else if (hasUnstoppable[BLACK] && !hasUnstoppable[WHITE])
|
||||
ei.egValue -= UnstoppablePawnValue - Value(0x40 * movesToGo[BLACK]);
|
||||
else if (hasUnstoppable[BLACK] && hasUnstoppable[WHITE])
|
||||
{
|
||||
// Both sides have unstoppable pawns! Try to find out who queens
|
||||
// first. We begin by transforming 'movesToGo' to the number of
|
||||
// plies until the pawn queens for both sides.
|
||||
movesToGo[WHITE] *= 2;
|
||||
movesToGo[BLACK] *= 2;
|
||||
movesToGo[pos.side_to_move()]--;
|
||||
|
||||
// If one side queens at least three plies before the other, that
|
||||
// side wins:
|
||||
if(movesToGo[WHITE] <= movesToGo[BLACK] - 3)
|
||||
ei.egValue += UnstoppablePawnValue - Value(0x40 * (movesToGo[WHITE]/2));
|
||||
else if(movesToGo[BLACK] <= movesToGo[WHITE] - 3)
|
||||
ei.egValue -= UnstoppablePawnValue - Value(0x40 * (movesToGo[BLACK]/2));
|
||||
// If one side queens at least three plies before the other, that
|
||||
// side wins.
|
||||
if (movesToGo[WHITE] <= movesToGo[BLACK] - 3)
|
||||
ei.egValue += UnstoppablePawnValue - Value(0x40 * (movesToGo[WHITE]/2));
|
||||
else if(movesToGo[BLACK] <= movesToGo[WHITE] - 3)
|
||||
ei.egValue -= UnstoppablePawnValue - Value(0x40 * (movesToGo[BLACK]/2));
|
||||
|
||||
// We could also add some rules about the situation when one side
|
||||
// queens exactly one ply before the other: Does the first queen
|
||||
// check the opponent's king, or attack the opponent's queening square?
|
||||
// This is slightly tricky to get right, because it is possible that
|
||||
// the opponent's king has moved somewhere before the first pawn queens.
|
||||
// We could also add some rules about the situation when one side
|
||||
// queens exactly one ply before the other: Does the first queen
|
||||
// check the opponent's king, or attack the opponent's queening square?
|
||||
// This is slightly tricky to get right, because it is possible that
|
||||
// the opponent's king has moved somewhere before the first pawn queens.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1054,14 +1039,13 @@ namespace {
|
||||
|
||||
void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us,
|
||||
EvalInfo &ei) {
|
||||
|
||||
assert(square_is_ok(s));
|
||||
assert(pos.piece_on(s) == bishop_of_color(us));
|
||||
assert(pos.piece_on(s) == piece_of_color_and_type(us, BISHOP));
|
||||
|
||||
Square b6 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B6 : SQ_G6);
|
||||
Square b8 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B8 : SQ_G8);
|
||||
|
||||
if ( pos.piece_on(b6) == pawn_of_color(opposite_color(us))
|
||||
if ( pos.piece_on(b6) == piece_of_color_and_type(opposite_color(us), PAWN)
|
||||
&& pos.see(s, b6) < 0
|
||||
&& pos.see(s, b8) < 0)
|
||||
{
|
||||
@@ -1078,38 +1062,40 @@ namespace {
|
||||
|
||||
void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us,
|
||||
EvalInfo &ei) {
|
||||
Piece pawn = pawn_of_color(us);
|
||||
Piece pawn = piece_of_color_and_type(us, PAWN);
|
||||
Square b2, b3, c3;
|
||||
|
||||
assert(Chess960);
|
||||
assert(square_is_ok(s));
|
||||
assert(pos.piece_on(s) == bishop_of_color(us));
|
||||
assert(pos.piece_on(s) == piece_of_color_and_type(us, BISHOP));
|
||||
|
||||
if(square_file(s) == FILE_A) {
|
||||
b2 = relative_square(us, SQ_B2);
|
||||
b3 = relative_square(us, SQ_B3);
|
||||
c3 = relative_square(us, SQ_C3);
|
||||
if (square_file(s) == FILE_A)
|
||||
{
|
||||
b2 = relative_square(us, SQ_B2);
|
||||
b3 = relative_square(us, SQ_B3);
|
||||
c3 = relative_square(us, SQ_C3);
|
||||
}
|
||||
else {
|
||||
b2 = relative_square(us, SQ_G2);
|
||||
b3 = relative_square(us, SQ_G3);
|
||||
c3 = relative_square(us, SQ_F3);
|
||||
else
|
||||
{
|
||||
b2 = relative_square(us, SQ_G2);
|
||||
b3 = relative_square(us, SQ_G3);
|
||||
c3 = relative_square(us, SQ_F3);
|
||||
}
|
||||
|
||||
if(pos.piece_on(b2) == pawn) {
|
||||
Value penalty;
|
||||
if (pos.piece_on(b2) == pawn)
|
||||
{
|
||||
Value penalty;
|
||||
|
||||
if(!pos.square_is_empty(b3))
|
||||
penalty = 2*TrappedBishopA1H1Penalty;
|
||||
else if(pos.piece_on(c3) == pawn)
|
||||
penalty = TrappedBishopA1H1Penalty;
|
||||
else
|
||||
penalty = TrappedBishopA1H1Penalty / 2;
|
||||
if (!pos.square_is_empty(b3))
|
||||
penalty = 2*TrappedBishopA1H1Penalty;
|
||||
else if (pos.piece_on(c3) == pawn)
|
||||
penalty = TrappedBishopA1H1Penalty;
|
||||
else
|
||||
penalty = TrappedBishopA1H1Penalty / 2;
|
||||
|
||||
ei.mgValue -= Sign[us] * penalty;
|
||||
ei.egValue -= Sign[us] * penalty;
|
||||
ei.mgValue -= Sign[us] * penalty;
|
||||
ei.egValue -= Sign[us] * penalty;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1125,31 +1111,32 @@ namespace {
|
||||
Color them = opposite_color(us);
|
||||
|
||||
// Find the safe squares for our pieces inside the area defined by
|
||||
// SpaceMask[us]. A square is unsafe it is attacked by an enemy
|
||||
// SpaceMask[us]. A square is unsafe it is attacked by an enemy
|
||||
// pawn, or if it is undefended and attacked by an enemy piece.
|
||||
|
||||
Bitboard safeSquares =
|
||||
SpaceMask[us] & ~pos.pawns(us) & ~ei.attacked_by(them, PAWN)
|
||||
& ~(~ei.attacked_by(us) & ei.attacked_by(them));
|
||||
Bitboard safeSquares = SpaceMask[us]
|
||||
& ~pos.pawns(us)
|
||||
& ~ei.attacked_by(them, PAWN)
|
||||
& ~(~ei.attacked_by(us) & ei.attacked_by(them));
|
||||
|
||||
// Find all squares which are at most three squares behind some friendly
|
||||
// pawn.
|
||||
Bitboard behindFriendlyPawns = pos.pawns(us);
|
||||
if(us == WHITE) {
|
||||
behindFriendlyPawns |= (behindFriendlyPawns >> 8);
|
||||
behindFriendlyPawns |= (behindFriendlyPawns >> 16);
|
||||
if (us == WHITE)
|
||||
{
|
||||
behindFriendlyPawns |= (behindFriendlyPawns >> 8);
|
||||
behindFriendlyPawns |= (behindFriendlyPawns >> 16);
|
||||
}
|
||||
else {
|
||||
behindFriendlyPawns |= (behindFriendlyPawns << 8);
|
||||
behindFriendlyPawns |= (behindFriendlyPawns << 16);
|
||||
else
|
||||
{
|
||||
behindFriendlyPawns |= (behindFriendlyPawns << 8);
|
||||
behindFriendlyPawns |= (behindFriendlyPawns << 16);
|
||||
}
|
||||
|
||||
int space =
|
||||
count_1s_max_15(safeSquares)
|
||||
+ count_1s_max_15(behindFriendlyPawns & safeSquares);
|
||||
int space = count_1s_max_15(safeSquares)
|
||||
+ count_1s_max_15(behindFriendlyPawns & safeSquares);
|
||||
|
||||
ei.mgValue += Sign[us] *
|
||||
apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
|
||||
ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
|
||||
}
|
||||
|
||||
|
||||
@@ -1190,6 +1177,7 @@ namespace {
|
||||
// an UCI-configurable weight with an internal weight.
|
||||
|
||||
int compute_weight(int uciWeight, int internalWeight) {
|
||||
|
||||
uciWeight = (uciWeight * 0x100) / 100;
|
||||
return (uciWeight * internalWeight) / 0x100;
|
||||
}
|
||||
@@ -1242,5 +1230,4 @@ namespace {
|
||||
SafetyTable[i] = Value(peak);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,29 +27,30 @@
|
||||
|
||||
#include "material.h"
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
|
||||
/// The EvalInfo struct contains various information computed and collected
|
||||
/// by the evaluation function. An EvalInfo object is passed as one of the
|
||||
/// by the evaluation function. An EvalInfo object is passed as one of the
|
||||
/// arguments to the evaluation function, and the search can make use of its
|
||||
/// contents to make intelligent search decisions.
|
||||
///
|
||||
/// At the moment, this is not utilized very much: The only part of the
|
||||
/// EvalInfo object which is used by the search is futilityMargin.
|
||||
class Position;
|
||||
|
||||
struct EvalInfo {
|
||||
|
||||
// Middle game and endgame evaluations:
|
||||
// Middle game and endgame evaluations
|
||||
Value mgValue, egValue;
|
||||
|
||||
// Pointers to material and pawn hash table entries:
|
||||
MaterialInfo *mi;
|
||||
PawnInfo *pi;
|
||||
// Pointers to material and pawn hash table entries
|
||||
MaterialInfo* mi;
|
||||
PawnInfo* pi;
|
||||
|
||||
// attackedBy[color][piece type] is a bitboard representing all squares
|
||||
// attacked by a given color and piece type. attackedBy[color][0] contains
|
||||
@@ -100,8 +101,8 @@ struct EvalInfo {
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern Value evaluate(const Position &pos, EvalInfo &ei, int threadID);
|
||||
extern Value quick_evaluate(const Position &pos);
|
||||
extern Value evaluate(const Position& pos, EvalInfo& ei, int threadID);
|
||||
extern Value quick_evaluate(const Position& pos);
|
||||
extern void init_eval(int threads);
|
||||
extern void quit_eval();
|
||||
extern void read_weights(Color sideToMove);
|
||||
|
||||
@@ -87,7 +87,7 @@ typedef pthread_mutex_t Lock;
|
||||
#else
|
||||
|
||||
|
||||
# include <windows.h>
|
||||
#include <windows.h>
|
||||
|
||||
typedef CRITICAL_SECTION Lock;
|
||||
# define lock_init(x, y) InitializeCriticalSection(x)
|
||||
|
||||
@@ -52,7 +52,6 @@ int main(int argc, char *argv[]) {
|
||||
std::cin.rdbuf()->pubsetbuf(NULL, 0);
|
||||
|
||||
// Initialization
|
||||
|
||||
init_mersenne();
|
||||
init_direction_table();
|
||||
init_bitboards();
|
||||
@@ -73,7 +72,7 @@ int main(int argc, char *argv[]) {
|
||||
{
|
||||
if (argc < 4 || argc > 7)
|
||||
{
|
||||
std::cout << "Usage: glaurung bench <hash size> <threads> "
|
||||
std::cout << "Usage: stockfish bench <hash size> <threads> "
|
||||
<< "[time = 60s] [fen positions file = default] "
|
||||
<< "[time, depth or node limited = time]"
|
||||
<< std::endl;
|
||||
|
||||
123
src/material.cpp
123
src/material.cpp
@@ -24,6 +24,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
|
||||
#include "material.h"
|
||||
@@ -35,8 +36,8 @@
|
||||
|
||||
namespace {
|
||||
|
||||
const Value BishopPairMidgameBonus = Value(100);
|
||||
const Value BishopPairEndgameBonus = Value(100);
|
||||
const Value BishopPairMidgameBonus = Value(109);
|
||||
const Value BishopPairEndgameBonus = Value(97);
|
||||
|
||||
Key KNNKMaterialKey, KKNNMaterialKey;
|
||||
|
||||
@@ -54,20 +55,21 @@ class EndgameFunctions {
|
||||
|
||||
public:
|
||||
EndgameFunctions();
|
||||
EndgameEvaluationFunction* getEEF(Key key) const;
|
||||
ScalingFunction* getESF(Key key, Color* c) const;
|
||||
EndgameEvaluationFunctionBase* getEEF(Key key) const;
|
||||
EndgameScalingFunctionBase* getESF(Key key, Color* c) const;
|
||||
|
||||
private:
|
||||
void add(Key k, EndgameEvaluationFunction* f);
|
||||
void add(Key k, Color c, ScalingFunction* f);
|
||||
void add(const std::string& keyCode, EndgameEvaluationFunctionBase* f);
|
||||
void add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f);
|
||||
Key buildKey(const std::string& keyCode);
|
||||
|
||||
struct ScalingInfo
|
||||
{
|
||||
Color col;
|
||||
ScalingFunction* fun;
|
||||
EndgameScalingFunctionBase* fun;
|
||||
};
|
||||
|
||||
std::map<Key, EndgameEvaluationFunction*> EEFmap;
|
||||
std::map<Key, EndgameEvaluationFunctionBase*> EEFmap;
|
||||
std::map<Key, ScalingInfo> ESFmap;
|
||||
};
|
||||
|
||||
@@ -143,7 +145,8 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
||||
}
|
||||
|
||||
// Let's look if we have a specialized evaluation function for this
|
||||
// particular material configuration.
|
||||
// particular material configuration. First we look for a fixed
|
||||
// configuration one, then a generic one if previous search failed.
|
||||
if ((mi->evaluationFunction = funcs->getEEF(key)) != NULL)
|
||||
return mi;
|
||||
|
||||
@@ -186,7 +189,7 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
||||
// are several conflicting applicable scaling functions and we need to
|
||||
// decide which one to use.
|
||||
Color c;
|
||||
ScalingFunction* sf;
|
||||
EndgameScalingFunctionBase* sf;
|
||||
|
||||
if ((sf = funcs->getESF(key, &c)) != NULL)
|
||||
{
|
||||
@@ -315,63 +318,79 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
||||
|
||||
EndgameFunctions::EndgameFunctions() {
|
||||
|
||||
typedef Key ZM[2][8][16];
|
||||
const ZM& z = Position::zobMaterial;
|
||||
KNNKMaterialKey = buildKey("KNNK");
|
||||
KKNNMaterialKey = buildKey("KKNN");
|
||||
|
||||
static const Color W = WHITE;
|
||||
static const Color B = BLACK;
|
||||
add("KPK", &EvaluateKPK);
|
||||
add("KKP", &EvaluateKKP);
|
||||
add("KBNK", &EvaluateKBNK);
|
||||
add("KKBN", &EvaluateKKBN);
|
||||
add("KRKP", &EvaluateKRKP);
|
||||
add("KPKR", &EvaluateKPKR);
|
||||
add("KRKB", &EvaluateKRKB);
|
||||
add("KBKR", &EvaluateKBKR);
|
||||
add("KRKN", &EvaluateKRKN);
|
||||
add("KNKR", &EvaluateKNKR);
|
||||
add("KQKR", &EvaluateKQKR);
|
||||
add("KRKQ", &EvaluateKRKQ);
|
||||
add("KBBKN", &EvaluateKBBKN);
|
||||
add("KNKBB", &EvaluateKNKBB);
|
||||
|
||||
KNNKMaterialKey = z[W][KNIGHT][1] ^ z[W][KNIGHT][2];
|
||||
KKNNMaterialKey = z[B][KNIGHT][1] ^ z[B][KNIGHT][2];
|
||||
|
||||
add(z[W][PAWN][1], &EvaluateKPK);
|
||||
add(z[B][PAWN][1], &EvaluateKKP);
|
||||
|
||||
add(z[W][BISHOP][1] ^ z[W][KNIGHT][1], &EvaluateKBNK);
|
||||
add(z[B][BISHOP][1] ^ z[B][KNIGHT][1], &EvaluateKKBN);
|
||||
add(z[W][ROOK][1] ^ z[B][PAWN][1], &EvaluateKRKP);
|
||||
add(z[W][PAWN][1] ^ z[B][ROOK][1], &EvaluateKPKR);
|
||||
add(z[W][ROOK][1] ^ z[B][BISHOP][1], &EvaluateKRKB);
|
||||
add(z[W][BISHOP][1] ^ z[B][ROOK][1], &EvaluateKBKR);
|
||||
add(z[W][ROOK][1] ^ z[B][KNIGHT][1], &EvaluateKRKN);
|
||||
add(z[W][KNIGHT][1] ^ z[B][ROOK][1], &EvaluateKNKR);
|
||||
add(z[W][QUEEN][1] ^ z[B][ROOK][1], &EvaluateKQKR);
|
||||
add(z[W][ROOK][1] ^ z[B][QUEEN][1], &EvaluateKRKQ);
|
||||
add(z[W][BISHOP][2] ^ z[B][KNIGHT][1], &EvaluateKBBKN);
|
||||
add(z[W][KNIGHT][1] ^ z[B][BISHOP][2], &EvaluateKNKBB);
|
||||
|
||||
add(z[W][KNIGHT][1] ^ z[W][PAWN][1], W, &ScaleKNPK);
|
||||
add(z[B][KNIGHT][1] ^ z[B][PAWN][1], B, &ScaleKKNP);
|
||||
|
||||
add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[B][ROOK][1] , W, &ScaleKRPKR);
|
||||
add(z[W][ROOK][1] ^ z[B][ROOK][1] ^ z[B][PAWN][1] , B, &ScaleKRKRP);
|
||||
add(z[W][BISHOP][1] ^ z[W][PAWN][1] ^ z[B][BISHOP][1], W, &ScaleKBPKB);
|
||||
add(z[W][BISHOP][1] ^ z[B][BISHOP][1] ^ z[B][PAWN][1] , B, &ScaleKBKBP);
|
||||
add(z[W][BISHOP][1] ^ z[W][PAWN][1] ^ z[B][KNIGHT][1], W, &ScaleKBPKN);
|
||||
add(z[W][KNIGHT][1] ^ z[B][BISHOP][1] ^ z[B][PAWN][1] , B, &ScaleKNKBP);
|
||||
|
||||
add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[W][PAWN][2] ^ z[B][ROOK][1] ^ z[B][PAWN][1], W, &ScaleKRPPKRP);
|
||||
add(z[W][ROOK][1] ^ z[W][PAWN][1] ^ z[B][ROOK][1] ^ z[B][PAWN][1] ^ z[B][PAWN][2], B, &ScaleKRPKRPP);
|
||||
add("KNPK", WHITE, &ScaleKNPK);
|
||||
add("KKNP", BLACK, &ScaleKKNP);
|
||||
add("KRPKR", WHITE, &ScaleKRPKR);
|
||||
add("KRKRP", BLACK, &ScaleKRKRP);
|
||||
add("KBPKB", WHITE, &ScaleKBPKB);
|
||||
add("KBKBP", BLACK, &ScaleKBKBP);
|
||||
add("KBPPKB", WHITE, &ScaleKBPPKB);
|
||||
add("KBKBPP", BLACK, &ScaleKBKBPP);
|
||||
add("KBPKN", WHITE, &ScaleKBPKN);
|
||||
add("KNKBP", BLACK, &ScaleKNKBP);
|
||||
add("KRPPKRP", WHITE, &ScaleKRPPKRP);
|
||||
add("KRPKRPP", BLACK, &ScaleKRPKRPP);
|
||||
add("KRPPKRP", WHITE, &ScaleKRPPKRP);
|
||||
add("KRPKRPP", BLACK, &ScaleKRPKRPP);
|
||||
}
|
||||
|
||||
void EndgameFunctions::add(Key k, EndgameEvaluationFunction* f) {
|
||||
Key EndgameFunctions::buildKey(const std::string& keyCode) {
|
||||
|
||||
EEFmap.insert(std::pair<Key, EndgameEvaluationFunction*>(k, f));
|
||||
assert(keyCode.length() > 0 && keyCode[0] == 'K');
|
||||
assert(keyCode.length() < 8);
|
||||
|
||||
std::stringstream s;
|
||||
bool upcase = false;
|
||||
|
||||
// Build up a fen substring with the given pieces, note
|
||||
// that the fen string could be of an illegal position.
|
||||
for (size_t i = 0; i < keyCode.length(); i++)
|
||||
{
|
||||
if (keyCode[i] == 'K')
|
||||
upcase = !upcase;
|
||||
|
||||
s << char(upcase? toupper(keyCode[i]) : tolower(keyCode[i]));
|
||||
}
|
||||
s << 8 - keyCode.length() << "/8/8/8/8/8/8/8 w -";
|
||||
return Position(s.str()).get_material_key();
|
||||
}
|
||||
|
||||
void EndgameFunctions::add(Key k, Color c, ScalingFunction* f) {
|
||||
void EndgameFunctions::add(const std::string& keyCode, EndgameEvaluationFunctionBase* f) {
|
||||
|
||||
EEFmap.insert(std::pair<Key, EndgameEvaluationFunctionBase*>(buildKey(keyCode), f));
|
||||
}
|
||||
|
||||
void EndgameFunctions::add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f) {
|
||||
|
||||
ScalingInfo s = {c, f};
|
||||
ESFmap.insert(std::pair<Key, ScalingInfo>(k, s));
|
||||
ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(keyCode), s));
|
||||
}
|
||||
|
||||
EndgameEvaluationFunction* EndgameFunctions::getEEF(Key key) const {
|
||||
EndgameEvaluationFunctionBase* EndgameFunctions::getEEF(Key key) const {
|
||||
|
||||
std::map<Key, EndgameEvaluationFunction*>::const_iterator it(EEFmap.find(key));
|
||||
std::map<Key, EndgameEvaluationFunctionBase*>::const_iterator it(EEFmap.find(key));
|
||||
return (it != EEFmap.end() ? it->second : NULL);
|
||||
}
|
||||
|
||||
ScalingFunction* EndgameFunctions::getESF(Key key, Color* c) const {
|
||||
EndgameScalingFunctionBase* EndgameFunctions::getESF(Key key, Color* c) const {
|
||||
|
||||
std::map<Key, ScalingInfo>::const_iterator it(ESFmap.find(key));
|
||||
if (it == ESFmap.end())
|
||||
|
||||
@@ -63,9 +63,9 @@ private:
|
||||
int16_t mgValue;
|
||||
int16_t egValue;
|
||||
uint8_t factor[2];
|
||||
EndgameEvaluationFunction* evaluationFunction;
|
||||
ScalingFunction* scalingFunction[2];
|
||||
uint8_t spaceWeight;
|
||||
EndgameEvaluationFunctionBase* evaluationFunction;
|
||||
EndgameScalingFunctionBase* scalingFunction[2];
|
||||
int spaceWeight;
|
||||
};
|
||||
|
||||
|
||||
|
||||
89
src/misc.cpp
89
src/misc.cpp
@@ -29,11 +29,32 @@
|
||||
# include <unistd.h>
|
||||
|
||||
#else
|
||||
/*
|
||||
(c) Copyright 1992 Eric Backus
|
||||
|
||||
This software may be used freely so long as this copyright notice is
|
||||
left intact. There is no warrantee on this software.
|
||||
*/
|
||||
# include <windows.h>
|
||||
# include <time.h>
|
||||
# include "dos.h"
|
||||
int gettimeofday(struct timeval * tp, struct timezone * tzp);
|
||||
static int gettimeofday(struct timeval* tp, struct timezone*)
|
||||
{
|
||||
SYSTEMTIME systime;
|
||||
|
||||
if (tp)
|
||||
{
|
||||
struct tm tmrec;
|
||||
time_t theTime = time(NULL);
|
||||
|
||||
tmrec = *localtime(&theTime);
|
||||
tp->tv_sec = mktime(&tmrec);
|
||||
GetLocalTime(&systime); /* system time */
|
||||
|
||||
tp->tv_usec = systime.wMilliseconds * 1000;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -45,14 +66,20 @@ int gettimeofday(struct timeval * tp, struct timezone * tzp);
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Version number. If this is left empty, the current date (in the format
|
||||
/// YYMMDD) is used as a version number.
|
||||
|
||||
static const string EngineVersion = "1.3";
|
||||
static const string AppName = "Stockfish";
|
||||
static const string AppTag = "";
|
||||
|
||||
|
||||
////
|
||||
//// Variables
|
||||
////
|
||||
|
||||
static const std::string AppName = "Stockfish";
|
||||
static const std::string AppTag = "";
|
||||
|
||||
long dbg_cnt0 = 0;
|
||||
long dbg_cnt1 = 0;
|
||||
|
||||
@@ -96,34 +123,33 @@ void dbg_after() {
|
||||
void dbg_mean_of(int v) {
|
||||
|
||||
assert(!dbg_show_hit_rate);
|
||||
dbg_show_mean = true;
|
||||
dbg_cnt0++;
|
||||
dbg_cnt1 += v;
|
||||
}
|
||||
|
||||
void dbg_print_hit_rate() {
|
||||
|
||||
std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
|
||||
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
|
||||
<< std::endl;
|
||||
cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
|
||||
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
|
||||
}
|
||||
|
||||
void dbg_print_mean() {
|
||||
|
||||
std::cout << "Total " << dbg_cnt0 << " Mean "
|
||||
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
|
||||
cout << "Total " << dbg_cnt0 << " Mean "
|
||||
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
|
||||
}
|
||||
|
||||
void dbg_print_hit_rate(std::ofstream& logFile) {
|
||||
void dbg_print_hit_rate(ofstream& logFile) {
|
||||
|
||||
logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
|
||||
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
|
||||
<< std::endl;
|
||||
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
|
||||
}
|
||||
|
||||
void dbg_print_mean(std::ofstream& logFile) {
|
||||
void dbg_print_mean(ofstream& logFile) {
|
||||
|
||||
logFile << "Total " << dbg_cnt0 << " Mean "
|
||||
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
|
||||
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
|
||||
}
|
||||
|
||||
/// engine_name() returns the full name of the current Stockfish version.
|
||||
@@ -131,26 +157,25 @@ void dbg_print_mean(std::ofstream& logFile) {
|
||||
/// program was compiled) or "Stockfish <version number>", depending on whether
|
||||
/// the constant EngineVersion (defined in misc.h) is empty.
|
||||
|
||||
const std::string engine_name() {
|
||||
const string engine_name() {
|
||||
|
||||
if (EngineVersion.empty())
|
||||
{
|
||||
std::string date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
std::string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
||||
|
||||
size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
|
||||
|
||||
std::stringstream s;
|
||||
std::string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
|
||||
|
||||
std::string name = AppName + " " + AppTag + " ";
|
||||
|
||||
s << name << date.substr(date.length() - 2) << std::setfill('0')
|
||||
<< std::setw(2) << mon << std::setw(2) << day;
|
||||
|
||||
return s.str();
|
||||
} else
|
||||
if (!EngineVersion.empty())
|
||||
return "Stockfish " + EngineVersion;
|
||||
|
||||
string date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
||||
|
||||
size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
|
||||
|
||||
stringstream s;
|
||||
string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
|
||||
|
||||
string name = AppName + " " + AppTag + " ";
|
||||
|
||||
s << name << date.substr(date.length() - 2) << setfill('0')
|
||||
<< setw(2) << mon << setw(2) << day;
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
11
src/misc.h
11
src/misc.h
@@ -30,17 +30,6 @@
|
||||
#include <string>
|
||||
|
||||
|
||||
////
|
||||
//// Constants
|
||||
////
|
||||
|
||||
|
||||
/// Version number. If this is left empty, the current date (in the format
|
||||
/// YYMMDD) is used as a version number.
|
||||
|
||||
const std::string EngineVersion = "1.2";
|
||||
|
||||
|
||||
////
|
||||
//// Macros
|
||||
////
|
||||
|
||||
10
src/move.cpp
10
src/move.cpp
@@ -72,13 +72,13 @@ Move move_from_string(const Position& pos, const std::string& str) {
|
||||
}
|
||||
}
|
||||
|
||||
if (piece == king_of_color(us))
|
||||
if (piece == piece_of_color_and_type(us, KING))
|
||||
{
|
||||
// Is this a castling move? A king move is assumed to be a castling
|
||||
// move if the destination square is occupied by a friendly rook, or
|
||||
// if the distance between the source and destination squares is more
|
||||
// than 1.
|
||||
if (pos.piece_on(to) == rook_of_color(us))
|
||||
if (pos.piece_on(to) == piece_of_color_and_type(us, ROOK))
|
||||
return make_castle_move(from, to);
|
||||
|
||||
else if (square_distance(from, to) > 1)
|
||||
@@ -87,13 +87,13 @@ Move move_from_string(const Position& pos, const std::string& str) {
|
||||
// internal "king captures rook" representation.
|
||||
SquareDelta delta = (to > from ? DELTA_E : DELTA_W);
|
||||
Square s = from + delta;
|
||||
while (relative_rank(us, s) == RANK_1 && pos.piece_on(s) != rook_of_color(us))
|
||||
while (relative_rank(us, s) == RANK_1 && pos.piece_on(s) != piece_of_color_and_type(us, ROOK))
|
||||
s += delta;
|
||||
|
||||
return (relative_rank(us, s) == RANK_1 ? make_castle_move(from, s) : MOVE_NONE);
|
||||
}
|
||||
}
|
||||
else if (piece == pawn_of_color(us))
|
||||
else if (piece == piece_of_color_and_type(us, PAWN))
|
||||
{
|
||||
// En passant move? We assume that a pawn move is an en passant move
|
||||
// without further testing if the destination square is epSquare.
|
||||
@@ -139,7 +139,7 @@ const std::string move_to_string(Move move) {
|
||||
|
||||
/// Overload the << operator, to make it easier to print moves.
|
||||
|
||||
std::ostream &operator << (std::ostream &os, Move m) {
|
||||
std::ostream &operator << (std::ostream& os, Move m) {
|
||||
|
||||
return os << move_to_string(m);
|
||||
}
|
||||
|
||||
23
src/move.h
23
src/move.h
@@ -38,10 +38,22 @@
|
||||
|
||||
class Position;
|
||||
|
||||
/// A move needs 17 bits to be stored
|
||||
///
|
||||
/// bit 0- 5: destination square (from 0 to 63)
|
||||
/// bit 6-11: origin square (from 0 to 63)
|
||||
/// bit 12-14: promotion piece type
|
||||
/// bit 15: en passant flag
|
||||
/// bit 16: castle flag
|
||||
///
|
||||
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in
|
||||
/// because in any normal move destination square is always different
|
||||
/// from origin square while MOVE_NONE and MOVE_NULL have the same
|
||||
/// origin and destination square, 0 and 1 respectively.
|
||||
|
||||
enum Move {
|
||||
MOVE_NONE = 0,
|
||||
MOVE_NULL = 65,
|
||||
MOVE_MAX = 0xFFFFFF
|
||||
MOVE_NULL = 65
|
||||
};
|
||||
|
||||
|
||||
@@ -50,17 +62,20 @@ struct MoveStack {
|
||||
int score;
|
||||
};
|
||||
|
||||
// Note that operator< is set up such that std::sort() will sort in descending order
|
||||
inline bool operator<(const MoveStack& f, const MoveStack& s) { return s.score < f.score; }
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline Square move_from(Move m) {
|
||||
return Square((int(m) >> 6) & 077);
|
||||
return Square((int(m) >> 6) & 0x3F);
|
||||
}
|
||||
|
||||
inline Square move_to(Move m) {
|
||||
return Square(m & 077);
|
||||
return Square(m & 0x3F);
|
||||
}
|
||||
|
||||
inline PieceType move_promotion(Move m) {
|
||||
|
||||
606
src/movegen.cpp
606
src/movegen.cpp
@@ -35,73 +35,93 @@
|
||||
|
||||
namespace {
|
||||
|
||||
// Function
|
||||
MoveStack* generate_castle_moves(const Position&, MoveStack*);
|
||||
enum CastlingSide {
|
||||
KING_SIDE,
|
||||
QUEEN_SIDE
|
||||
};
|
||||
|
||||
// Template generate_pawn_captures() with specializations
|
||||
template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
|
||||
MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist);
|
||||
enum MoveType {
|
||||
CAPTURE,
|
||||
NON_CAPTURE
|
||||
};
|
||||
|
||||
template<Color>
|
||||
inline MoveStack* generate_pawn_captures(const Position& p, MoveStack* m) {
|
||||
return do_generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
|
||||
}
|
||||
template<>
|
||||
inline MoveStack* generate_pawn_captures<BLACK>(const Position& p, MoveStack* m) {
|
||||
return do_generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
|
||||
}
|
||||
// Functions
|
||||
bool castling_is_check(const Position&, CastlingSide);
|
||||
|
||||
// Template generate_pawn_noncaptures() with specializations
|
||||
template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
|
||||
MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
|
||||
// Helper templates
|
||||
template<CastlingSide Side>
|
||||
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
|
||||
|
||||
template<Color>
|
||||
inline MoveStack* generate_pawn_noncaptures(const Position& p, MoveStack* m) {
|
||||
return do_generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m);
|
||||
}
|
||||
template<>
|
||||
inline MoveStack* generate_pawn_noncaptures<BLACK>(const Position& p, MoveStack* m) {
|
||||
return do_generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m);
|
||||
}
|
||||
|
||||
// Template generate_pawn_blocking_evasions() with specializations
|
||||
template<Color Us, Rank, Bitboard, SquareDelta>
|
||||
MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
|
||||
Bitboard blockSquares, MoveStack* mlist);
|
||||
template<Color>
|
||||
inline MoveStack* generate_pawn_blocking_evasions(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
|
||||
return do_generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, np, bs, m);
|
||||
}
|
||||
template<>
|
||||
inline MoveStack* generate_pawn_blocking_evasions<BLACK>(const Position& p, Bitboard np, Bitboard bs, MoveStack* m) {
|
||||
return do_generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, np, bs, m);
|
||||
}
|
||||
MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
|
||||
|
||||
template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
|
||||
MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
|
||||
|
||||
template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
|
||||
MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
|
||||
|
||||
// Template generate_pawn_checks() with specializations
|
||||
template<Color, Color, Bitboard, Bitboard, SquareDelta>
|
||||
MoveStack* do_generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
|
||||
MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
|
||||
|
||||
template<Color>
|
||||
inline MoveStack* generate_pawn_checks(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
|
||||
return do_generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
|
||||
template<Color Us, SquareDelta Direction>
|
||||
inline Bitboard move_pawns(Bitboard p) {
|
||||
|
||||
if (Direction == DELTA_N)
|
||||
return Us == WHITE ? p << 8 : p >> 8;
|
||||
else if (Direction == DELTA_NE)
|
||||
return Us == WHITE ? p << 9 : p >> 7;
|
||||
else if (Direction == DELTA_NW)
|
||||
return Us == WHITE ? p << 7 : p >> 9;
|
||||
else
|
||||
return p;
|
||||
}
|
||||
|
||||
// Template generate_piece_checks() with specializations
|
||||
template<PieceType>
|
||||
MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
|
||||
|
||||
template<>
|
||||
inline MoveStack* generate_pawn_checks<BLACK>(const Position& p, Bitboard dc, Square ksq, MoveStack* m) {
|
||||
return do_generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
|
||||
inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
|
||||
|
||||
if (us == WHITE)
|
||||
return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
|
||||
else
|
||||
return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
|
||||
|
||||
}
|
||||
|
||||
// non-pawn templates
|
||||
// Template generate_piece_moves() with specializations and overloads
|
||||
template<PieceType>
|
||||
MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
|
||||
|
||||
template<>
|
||||
MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target);
|
||||
MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
|
||||
|
||||
template<PieceType Piece, MoveType Type>
|
||||
inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
|
||||
|
||||
assert(Piece == PAWN);
|
||||
|
||||
if (Type == CAPTURE)
|
||||
return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
|
||||
: generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
|
||||
else
|
||||
return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
|
||||
: generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
|
||||
}
|
||||
|
||||
template<PieceType>
|
||||
MoveStack* generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*);
|
||||
MoveStack* generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
|
||||
MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
|
||||
|
||||
template<PieceType>
|
||||
MoveStack* generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
|
||||
template<>
|
||||
inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
|
||||
Color us, Bitboard t, Bitboard pnd) {
|
||||
if (us == WHITE)
|
||||
return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, t, m);
|
||||
else
|
||||
return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, t, m);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -126,19 +146,14 @@ int generate_captures(const Position& pos, MoveStack* mlist) {
|
||||
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
|
||||
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
|
||||
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
|
||||
|
||||
if (us == WHITE)
|
||||
mlist = generate_pawn_captures<WHITE>(pos, mlist);
|
||||
else
|
||||
mlist = generate_pawn_captures<BLACK>(pos, mlist);
|
||||
|
||||
mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
|
||||
mlist = generate_piece_moves<KING>(pos, mlist, us, target);
|
||||
return int(mlist - mlist_start);
|
||||
}
|
||||
|
||||
|
||||
/// generate_noncaptures() generates all pseudo-legal non-captures and
|
||||
/// underpromotions. The return value is the number of moves generated.
|
||||
/// underpromotions. The return value is the number of moves generated.
|
||||
|
||||
int generate_noncaptures(const Position& pos, MoveStack* mlist) {
|
||||
|
||||
@@ -149,26 +164,22 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) {
|
||||
Bitboard target = pos.empty_squares();
|
||||
MoveStack* mlist_start = mlist;
|
||||
|
||||
if (us == WHITE)
|
||||
mlist = generate_pawn_noncaptures<WHITE>(pos, mlist);
|
||||
else
|
||||
mlist = generate_pawn_noncaptures<BLACK>(pos, mlist);
|
||||
|
||||
mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
|
||||
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
|
||||
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
|
||||
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
|
||||
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
|
||||
mlist = generate_piece_moves<KING>(pos, mlist, us, target);
|
||||
mlist = generate_castle_moves(pos, mlist);
|
||||
mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
|
||||
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
|
||||
return int(mlist - mlist_start);
|
||||
}
|
||||
|
||||
|
||||
/// generate_checks() generates all pseudo-legal non-capturing, non-promoting
|
||||
/// checks, except castling moves (will add this later). It returns the
|
||||
/// number of generated moves.
|
||||
/// generate_non_capture_checks() generates all pseudo-legal non-capturing,
|
||||
/// non-promoting checks. It returns the number of generated moves.
|
||||
|
||||
int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
|
||||
int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
|
||||
|
||||
assert(pos.is_ok());
|
||||
assert(!pos.is_check());
|
||||
@@ -177,48 +188,36 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
|
||||
Square ksq = pos.king_square(opposite_color(us));
|
||||
MoveStack* mlist_start = mlist;
|
||||
|
||||
assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
|
||||
|
||||
dc = pos.discovered_check_candidates(us);
|
||||
|
||||
// Pawn moves
|
||||
if (us == WHITE)
|
||||
mlist = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
|
||||
else
|
||||
mlist = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
|
||||
assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
|
||||
|
||||
// Pieces moves
|
||||
Bitboard b = pos.knights(us);
|
||||
if (b)
|
||||
mlist = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist);
|
||||
mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
|
||||
mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
|
||||
mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
|
||||
mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
|
||||
mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
|
||||
mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
|
||||
|
||||
b = pos.bishops(us);
|
||||
if (b)
|
||||
mlist = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist);
|
||||
// Castling moves that give check. Very rare but nice to have!
|
||||
if ( pos.can_castle_queenside(us)
|
||||
&& (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
|
||||
&& castling_is_check(pos, QUEEN_SIDE))
|
||||
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
|
||||
|
||||
b = pos.rooks(us);
|
||||
if (b)
|
||||
mlist = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist);
|
||||
|
||||
b = pos.queens(us);
|
||||
if (b)
|
||||
mlist = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist);
|
||||
|
||||
// Hopefully we always have a king ;-)
|
||||
mlist = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist);
|
||||
|
||||
// TODO: Castling moves!
|
||||
if ( pos.can_castle_kingside(us)
|
||||
&& (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
|
||||
&& castling_is_check(pos, KING_SIDE))
|
||||
mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
|
||||
|
||||
return int(mlist - mlist_start);
|
||||
}
|
||||
|
||||
|
||||
/// generate_evasions() generates all check evasions when the side to move is
|
||||
/// in check. Unlike the other move generation functions, this one generates
|
||||
/// only legal moves. It returns the number of generated moves. This
|
||||
/// function is very ugly, and needs cleaning up some time later. FIXME
|
||||
/// in check. Unlike the other move generation functions, this one generates
|
||||
/// only legal moves. It returns the number of generated moves.
|
||||
|
||||
int generate_evasions(const Position& pos, MoveStack* mlist) {
|
||||
int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
|
||||
|
||||
assert(pos.is_ok());
|
||||
assert(pos.is_check());
|
||||
@@ -229,49 +228,56 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
|
||||
Square ksq = pos.king_square(us);
|
||||
MoveStack* mlist_start = mlist;
|
||||
|
||||
assert(pos.piece_on(ksq) == king_of_color(us));
|
||||
assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
|
||||
|
||||
// The bitboard of occupied pieces without our king
|
||||
Bitboard b_noKing = pos.occupied_squares();
|
||||
clear_bit(&b_noKing, ksq);
|
||||
|
||||
// Find squares attacked by slider checkers, we will
|
||||
// remove them from king evasions set so to avoid a couple
|
||||
// of cycles in the slow king evasions legality check loop
|
||||
// and to be able to use square_is_attacked().
|
||||
Bitboard checkers = pos.checkers();
|
||||
Bitboard checkersAttacks = EmptyBoardBB;
|
||||
Bitboard b = checkers & (pos.queens() | pos.bishops());
|
||||
while (b)
|
||||
{
|
||||
from = pop_1st_bit(&b);
|
||||
checkersAttacks |= bishop_attacks_bb(from, b_noKing);
|
||||
}
|
||||
|
||||
b = checkers & (pos.queens() | pos.rooks());
|
||||
while (b)
|
||||
{
|
||||
from = pop_1st_bit(&b);
|
||||
checkersAttacks |= rook_attacks_bb(from, b_noKing);
|
||||
}
|
||||
|
||||
// Generate evasions for king
|
||||
Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
|
||||
Bitboard b2 = pos.occupied_squares();
|
||||
clear_bit(&b2, ksq);
|
||||
|
||||
Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
|
||||
while (b1)
|
||||
{
|
||||
to = pop_1st_bit(&b1);
|
||||
|
||||
// Make sure 'to' is not attacked by the other side. This is a bit ugly,
|
||||
// because we can't use Position::square_is_attacked. Instead we use
|
||||
// the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
|
||||
// b2 (the occupied squares with the king removed) in order to test whether
|
||||
// the king will remain in check on the destination square.
|
||||
if (!( (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
|
||||
|| (pos.pawn_attacks(us, to) & pos.pawns(them))
|
||||
|| (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
|
||||
|| (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
|
||||
|| (pos.piece_attacks<KING>(to) & pos.kings(them))))
|
||||
|
||||
(*mlist++).move = make_move(ksq, to);
|
||||
to = pop_1st_bit(&b1);
|
||||
// Note that we can use square_is_attacked() only because we
|
||||
// have already removed slider checkers.
|
||||
if (!pos.square_is_attacked(to, them))
|
||||
(*mlist++).move = make_move(ksq, to);
|
||||
}
|
||||
|
||||
// Generate evasions for other pieces only if not double check. We use a
|
||||
// simple bit twiddling hack here rather than calling count_1s in order to
|
||||
// save some time (we know that pos.checkers() has at most two nonzero bits).
|
||||
Bitboard checkers = pos.checkers();
|
||||
|
||||
if (!(checkers & (checkers - 1))) // Only one bit set?
|
||||
{
|
||||
Square checksq = first_1(checkers);
|
||||
|
||||
assert(pos.color_of_piece_on(checksq) == them);
|
||||
|
||||
// Find pinned pieces
|
||||
Bitboard not_pinned = ~pos.pinned_pieces(us);
|
||||
|
||||
// Generate captures of the checking piece
|
||||
|
||||
// Pawn captures
|
||||
b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
|
||||
b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
|
||||
while (b1)
|
||||
{
|
||||
from = pop_1st_bit(&b1);
|
||||
@@ -288,7 +294,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
|
||||
// Pieces captures
|
||||
b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
|
||||
| (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
|
||||
| (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
|
||||
| (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & ~pinned;
|
||||
|
||||
while (b1)
|
||||
{
|
||||
@@ -297,77 +303,56 @@ int generate_evasions(const Position& pos, MoveStack* mlist) {
|
||||
}
|
||||
|
||||
// Blocking check evasions are possible only if the checking piece is
|
||||
// a slider
|
||||
// a slider.
|
||||
if (checkers & pos.sliders())
|
||||
{
|
||||
Bitboard blockSquares = squares_between(checksq, ksq);
|
||||
|
||||
assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
|
||||
|
||||
// Pawn moves. Because a blocking evasion can never be a capture, we
|
||||
// only generate pawn pushes.
|
||||
if (us == WHITE)
|
||||
mlist = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist);
|
||||
else
|
||||
mlist = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist);
|
||||
if (blockSquares != EmptyBoardBB)
|
||||
{
|
||||
mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
|
||||
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
|
||||
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
|
||||
mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
|
||||
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
|
||||
}
|
||||
}
|
||||
|
||||
// Pieces moves
|
||||
b1 = pos.knights(us) & not_pinned;
|
||||
if (b1)
|
||||
mlist = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist);
|
||||
// Finally, the special case of en passant captures. An en passant
|
||||
// capture can only be a check evasion if the check is not a discovered
|
||||
// check. If pos.ep_square() is set, the last move made must have been
|
||||
// a double pawn push. If, furthermore, the checking piece is a pawn,
|
||||
// an en passant check evasion may be possible.
|
||||
if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
|
||||
{
|
||||
to = pos.ep_square();
|
||||
b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
|
||||
|
||||
b1 = pos.bishops(us) & not_pinned;
|
||||
if (b1)
|
||||
mlist = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist);
|
||||
// The checking pawn cannot be a discovered (bishop) check candidate
|
||||
// otherwise we were in check also before last double push move.
|
||||
assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
|
||||
assert(count_1s(b1) == 1 || count_1s(b1) == 2);
|
||||
|
||||
b1 = pos.rooks(us) & not_pinned;
|
||||
if (b1)
|
||||
mlist = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist);
|
||||
|
||||
b1 = pos.queens(us) & not_pinned;
|
||||
if (b1)
|
||||
mlist = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist);
|
||||
}
|
||||
|
||||
// Finally, the ugly special case of en passant captures. An en passant
|
||||
// capture can only be a check evasion if the check is not a discovered
|
||||
// check. If pos.ep_square() is set, the last move made must have been
|
||||
// a double pawn push. If, furthermore, the checking piece is a pawn,
|
||||
// an en passant check evasion may be possible.
|
||||
if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
|
||||
{
|
||||
to = pos.ep_square();
|
||||
b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
|
||||
|
||||
assert(b1 != EmptyBoardBB);
|
||||
|
||||
b1 &= not_pinned;
|
||||
while (b1)
|
||||
{
|
||||
from = pop_1st_bit(&b1);
|
||||
|
||||
// Before generating the move, we have to make sure it is legal.
|
||||
// This is somewhat tricky, because the two disappearing pawns may
|
||||
// cause new "discovered checks". We test this by removing the
|
||||
// two relevant bits from the occupied squares bitboard, and using
|
||||
// the low-level bitboard functions for bishop and rook attacks.
|
||||
b2 = pos.occupied_squares();
|
||||
clear_bit(&b2, from);
|
||||
clear_bit(&b2, checksq);
|
||||
if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
|
||||
||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
|
||||
|
||||
(*mlist++).move = make_ep_move(from, to);
|
||||
}
|
||||
}
|
||||
b1 &= ~pinned;
|
||||
while (b1)
|
||||
{
|
||||
from = pop_1st_bit(&b1);
|
||||
// Move is always legal because checking pawn is not a discovered
|
||||
// check candidate and our capturing pawn has been already tested
|
||||
// against pinned pieces.
|
||||
(*mlist++).move = make_ep_move(from, to);
|
||||
}
|
||||
}
|
||||
}
|
||||
return int(mlist - mlist_start);
|
||||
}
|
||||
|
||||
|
||||
/// generate_legal_moves() computes a complete list of legal moves in the
|
||||
/// current position. This function is not very fast, and should be used
|
||||
/// only in situations where performance is unimportant. It wouldn't be
|
||||
/// current position. This function is not very fast, and should be used
|
||||
/// only in situations where performance is unimportant. It wouldn't be
|
||||
/// very hard to write an efficient legal move generator, but for the moment
|
||||
/// we don't need it.
|
||||
|
||||
@@ -375,15 +360,15 @@ int generate_legal_moves(const Position& pos, MoveStack* mlist) {
|
||||
|
||||
assert(pos.is_ok());
|
||||
|
||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
||||
|
||||
if (pos.is_check())
|
||||
return generate_evasions(pos, mlist);
|
||||
return generate_evasions(pos, mlist, pinned);
|
||||
|
||||
// Generate pseudo-legal moves
|
||||
int n = generate_captures(pos, mlist);
|
||||
n += generate_noncaptures(pos, mlist + n);
|
||||
|
||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
||||
|
||||
// Remove illegal moves from the list
|
||||
for (int i = 0; i < n; i++)
|
||||
if (!pos.pl_move_is_legal(mlist[i].move, pinned))
|
||||
@@ -428,7 +413,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
|
||||
return false;
|
||||
|
||||
assert(pos.square_is_empty(to));
|
||||
assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
|
||||
assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
|
||||
|
||||
// The move is pseudo-legal, check if it is also legal
|
||||
return pos.pl_move_is_legal(m, pinned);
|
||||
@@ -445,7 +430,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
|
||||
|
||||
assert(from == pos.king_square(us));
|
||||
assert(to == pos.initial_kr_square(us));
|
||||
assert(pos.piece_on(to) == rook_of_color(us));
|
||||
assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
|
||||
|
||||
Square g1 = relative_square(us, SQ_G1);
|
||||
Square f1 = relative_square(us, SQ_F1);
|
||||
@@ -478,7 +463,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
|
||||
|
||||
assert(from == pos.king_square(us));
|
||||
assert(to == pos.initial_qr_square(us));
|
||||
assert(pos.piece_on(to) == rook_of_color(us));
|
||||
assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
|
||||
|
||||
Square c1 = relative_square(us, SQ_C1);
|
||||
Square d1 = relative_square(us, SQ_D1);
|
||||
@@ -495,8 +480,8 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
|
||||
illegal = true;
|
||||
|
||||
if ( square_file(to) == FILE_B
|
||||
&& ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
|
||||
|| pos.piece_on(to + DELTA_W) == queen_of_color(them)))
|
||||
&& ( pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, ROOK)
|
||||
|| pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, QUEEN)))
|
||||
illegal = true;
|
||||
|
||||
return !illegal;
|
||||
@@ -567,7 +552,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
|
||||
}
|
||||
|
||||
// Luckly we can handle all the other pieces in one go
|
||||
return ( pos.piece_attacks_square(from, to)
|
||||
return ( pos.piece_attacks_square(pos.piece_on(from), from, to)
|
||||
&& pos.pl_move_is_legal(m, pinned)
|
||||
&& !move_promotion(m));
|
||||
}
|
||||
@@ -590,6 +575,24 @@ namespace {
|
||||
return mlist;
|
||||
}
|
||||
|
||||
template<PieceType Piece>
|
||||
MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
|
||||
Color us, Bitboard target, Bitboard pinned) {
|
||||
Square from;
|
||||
Bitboard b;
|
||||
|
||||
for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
|
||||
{
|
||||
from = pos.piece_list(us, Piece, i);
|
||||
if (pinned && bit_is_set(pinned, from))
|
||||
continue;
|
||||
|
||||
b = pos.piece_attacks<Piece>(from) & target;
|
||||
SERIALIZE_MOVES(b);
|
||||
}
|
||||
return mlist;
|
||||
}
|
||||
|
||||
template<>
|
||||
MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
|
||||
|
||||
@@ -601,30 +604,17 @@ namespace {
|
||||
return mlist;
|
||||
}
|
||||
|
||||
template<PieceType Piece>
|
||||
MoveStack* generate_piece_blocking_evasions(const Position& pos, Bitboard b,
|
||||
Bitboard blockSquares, MoveStack* mlist) {
|
||||
while (b)
|
||||
{
|
||||
Square from = pop_1st_bit(&b);
|
||||
Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
|
||||
SERIALIZE_MOVES(bb);
|
||||
}
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
|
||||
SquareDelta TDELTA_NW, SquareDelta TDELTA_N
|
||||
>
|
||||
MoveStack* do_generate_pawn_captures(const Position& pos, MoveStack* mlist) {
|
||||
MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
|
||||
|
||||
Square to;
|
||||
Bitboard pawns = pos.pawns(Us);
|
||||
Bitboard enemyPieces = pos.pieces_of_color(Them);
|
||||
|
||||
// Captures in the a1-h8 (a8-h1 for black) direction
|
||||
Bitboard b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
|
||||
Bitboard b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces;
|
||||
|
||||
// Capturing promotions
|
||||
Bitboard b2 = b1 & TRank8BB;
|
||||
@@ -643,7 +633,7 @@ namespace {
|
||||
}
|
||||
|
||||
// Captures in the h1-a8 (h8-a1 for black) direction
|
||||
b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
|
||||
b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces;
|
||||
|
||||
// Capturing promotions
|
||||
b2 = b1 & TRank8BB;
|
||||
@@ -662,7 +652,7 @@ namespace {
|
||||
}
|
||||
|
||||
// Non-capturing promotions
|
||||
b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & TRank8BB;
|
||||
b1 = move_pawns<Us, DELTA_N>(pawns) & pos.empty_squares() & TRank8BB;
|
||||
while (b1)
|
||||
{
|
||||
to = pop_1st_bit(&b1);
|
||||
@@ -690,7 +680,7 @@ namespace {
|
||||
template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
|
||||
SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
|
||||
>
|
||||
MoveStack* do_generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
|
||||
MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
|
||||
|
||||
Bitboard pawns = pos.pawns(Us);
|
||||
Bitboard enemyPieces = pos.pieces_of_color(Them);
|
||||
@@ -699,7 +689,7 @@ namespace {
|
||||
Square to;
|
||||
|
||||
// Underpromotion captures in the a1-h8 (a8-h1 for black) direction
|
||||
b1 = (Us == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & TRank8BB;
|
||||
b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces & TRank8BB;
|
||||
while (b1)
|
||||
{
|
||||
to = pop_1st_bit(&b1);
|
||||
@@ -709,7 +699,7 @@ namespace {
|
||||
}
|
||||
|
||||
// Underpromotion captures in the h1-a8 (h8-a1 for black) direction
|
||||
b1 = (Us == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & TRank8BB;
|
||||
b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces & TRank8BB;
|
||||
while (b1)
|
||||
{
|
||||
to = pop_1st_bit(&b1);
|
||||
@@ -719,14 +709,14 @@ namespace {
|
||||
}
|
||||
|
||||
// Single pawn pushes
|
||||
b1 = (Us == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
|
||||
b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares;
|
||||
b2 = b1 & TRank8BB;
|
||||
while (b2)
|
||||
{
|
||||
to = pop_1st_bit(&b2);
|
||||
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
|
||||
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
|
||||
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
|
||||
to = pop_1st_bit(&b2);
|
||||
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
|
||||
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
|
||||
(*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
|
||||
}
|
||||
b2 = b1 & ~TRank8BB;
|
||||
while (b2)
|
||||
@@ -736,50 +726,53 @@ namespace {
|
||||
}
|
||||
|
||||
// Double pawn pushes
|
||||
b2 = (Us == WHITE ? (b1 & TRank3BB) << 8 : (b1 & TRank3BB) >> 8) & emptySquares;
|
||||
b2 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
|
||||
while (b2)
|
||||
{
|
||||
to = pop_1st_bit(&b2);
|
||||
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
|
||||
to = pop_1st_bit(&b2);
|
||||
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
|
||||
}
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
|
||||
MoveStack* do_generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
|
||||
MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
|
||||
{
|
||||
// Pawn moves which give discovered check. This is possible only if the
|
||||
// pawn is not on the same file as the enemy king, because we don't
|
||||
// generate captures.
|
||||
// Find all friendly pawns not on the enemy king's file
|
||||
Bitboard b1, b2, b3;
|
||||
Bitboard empty = pos.empty_squares();
|
||||
|
||||
// Find all friendly pawns not on the enemy king's file
|
||||
Bitboard b1 = pos.pawns(Us) & ~file_bb(ksq), b2, b3;
|
||||
|
||||
// Discovered checks, single pawn pushes
|
||||
b2 = b3 = (Us == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~TRank8BB & empty;
|
||||
while (b3)
|
||||
if (dc != EmptyBoardBB)
|
||||
{
|
||||
Square to = pop_1st_bit(&b3);
|
||||
(*mlist++).move = make_move(to - TDELTA_N, to);
|
||||
}
|
||||
// Pawn moves which gives discovered check. This is possible only if the
|
||||
// pawn is not on the same file as the enemy king, because we don't
|
||||
// generate captures.
|
||||
b1 = pos.pawns(Us) & ~file_bb(ksq);
|
||||
|
||||
// Discovered checks, double pawn pushes
|
||||
b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8) & empty;
|
||||
while (b3)
|
||||
{
|
||||
Square to = pop_1st_bit(&b3);
|
||||
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
|
||||
// Discovered checks, single pawn pushes, no promotions
|
||||
b2 = b3 = move_pawns<Us, DELTA_N>(b1 & dc) & empty & ~TRank8BB;
|
||||
while (b3)
|
||||
{
|
||||
Square to = pop_1st_bit(&b3);
|
||||
(*mlist++).move = make_move(to - TDELTA_N, to);
|
||||
}
|
||||
|
||||
// Discovered checks, double pawn pushes
|
||||
b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty;
|
||||
while (b3)
|
||||
{
|
||||
Square to = pop_1st_bit(&b3);
|
||||
(*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
|
||||
}
|
||||
}
|
||||
|
||||
// Direct checks. These are possible only for pawns on neighboring files
|
||||
// of the enemy king
|
||||
|
||||
b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
|
||||
// of the enemy king.
|
||||
b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
|
||||
|
||||
// Direct checks, single pawn pushes
|
||||
b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & empty;
|
||||
b2 = move_pawns<Us, DELTA_N>(b1) & empty;
|
||||
b3 = b2 & pos.pawn_attacks(Them, ksq);
|
||||
while (b3)
|
||||
{
|
||||
@@ -788,10 +781,7 @@ namespace {
|
||||
}
|
||||
|
||||
// Direct checks, double pawn pushes
|
||||
b3 = (Us == WHITE ? (b2 & TRank3BB) << 8 : (b2 & TRank3BB) >> 8)
|
||||
& empty
|
||||
& pos.pawn_attacks(Them, ksq);
|
||||
|
||||
b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty & pos.pawn_attacks(Them, ksq);
|
||||
while (b3)
|
||||
{
|
||||
Square to = pop_1st_bit(&b3);
|
||||
@@ -801,52 +791,57 @@ namespace {
|
||||
}
|
||||
|
||||
template<PieceType Piece>
|
||||
MoveStack* generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
|
||||
Square ksq, MoveStack* mlist) {
|
||||
MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
|
||||
Bitboard dc, Square ksq) {
|
||||
|
||||
Bitboard target = pos.pieces_of_color_and_type(us, Piece);
|
||||
|
||||
// Discovered checks
|
||||
Bitboard b = target & dc;
|
||||
while (b)
|
||||
{
|
||||
Square from = pop_1st_bit(&b);
|
||||
Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
|
||||
if (Piece == KING)
|
||||
bb &= ~QueenPseudoAttacks[ksq];
|
||||
|
||||
SERIALIZE_MOVES(bb);
|
||||
}
|
||||
|
||||
// Direct checks
|
||||
b = target & ~dc;
|
||||
if (Piece == KING || !b)
|
||||
return mlist;
|
||||
|
||||
Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
|
||||
if (!checkSqs)
|
||||
return mlist;
|
||||
|
||||
while (b)
|
||||
{
|
||||
Square from = pop_1st_bit(&b);
|
||||
if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
|
||||
|| (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
|
||||
|| (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
|
||||
continue;
|
||||
|
||||
Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
|
||||
SERIALIZE_MOVES(bb);
|
||||
}
|
||||
return mlist;
|
||||
}
|
||||
|
||||
MoveStack* generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
|
||||
Square ksq, MoveStack* mlist) {
|
||||
if (bit_is_set(dc, from))
|
||||
{
|
||||
Bitboard b = pos.piece_attacks<KING>(from)
|
||||
& pos.empty_squares()
|
||||
& ~QueenPseudoAttacks[ksq];
|
||||
SERIALIZE_MOVES(b);
|
||||
}
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
|
||||
MoveStack* do_generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
|
||||
Bitboard blockSquares, MoveStack* mlist) {
|
||||
MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
|
||||
Bitboard blockSquares, MoveStack* mlist) {
|
||||
Square to;
|
||||
|
||||
// Find non-pinned pawns
|
||||
Bitboard b1 = pos.pawns(Us) & not_pinned;
|
||||
// Find non-pinned pawns and push them one square
|
||||
Bitboard b1 = move_pawns<Us, DELTA_N>(pos.pawns(Us) & ~pinned);
|
||||
|
||||
// Single pawn pushes. We don't have to AND with empty squares here,
|
||||
// We don't have to AND with empty squares here,
|
||||
// because the blocking squares will always be empty.
|
||||
Bitboard b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
|
||||
Bitboard b2 = b1 & blockSquares;
|
||||
while (b2)
|
||||
{
|
||||
to = pop_1st_bit(&b2);
|
||||
@@ -864,8 +859,8 @@ namespace {
|
||||
}
|
||||
|
||||
// Double pawn pushes
|
||||
b2 = (Us == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & TRank3BB;
|
||||
b2 = (Us == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
|
||||
b2 = b1 & pos.empty_squares() & TRank3BB;
|
||||
b2 = move_pawns<Us, DELTA_N>(b2) & blockSquares;
|
||||
while (b2)
|
||||
{
|
||||
to = pop_1st_bit(&b2);
|
||||
@@ -879,70 +874,59 @@ namespace {
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
template<CastlingSide Side>
|
||||
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
|
||||
if (pos.can_castle(us))
|
||||
if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
|
||||
||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
|
||||
{
|
||||
Color them = opposite_color(us);
|
||||
Square ksq = pos.king_square(us);
|
||||
|
||||
assert(pos.piece_on(ksq) == king_of_color(us));
|
||||
assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
|
||||
|
||||
if (pos.can_castle_kingside(us))
|
||||
{
|
||||
Square rsq = pos.initial_kr_square(us);
|
||||
Square g1 = relative_square(us, SQ_G1);
|
||||
Square f1 = relative_square(us, SQ_F1);
|
||||
Square s;
|
||||
bool illegal = false;
|
||||
Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
|
||||
Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
|
||||
Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
|
||||
Square s;
|
||||
bool illegal = false;
|
||||
|
||||
assert(pos.piece_on(rsq) == rook_of_color(us));
|
||||
assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
|
||||
|
||||
for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
|
||||
if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
|
||||
|| pos.square_is_attacked(s, them))
|
||||
illegal = true;
|
||||
// It is a bit complicated to correctly handle Chess960
|
||||
for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
|
||||
if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
|
||||
|| pos.square_is_attacked(s, them))
|
||||
illegal = true;
|
||||
|
||||
for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
|
||||
if (s != ksq && s != rsq && pos.square_is_occupied(s))
|
||||
illegal = true;
|
||||
for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
|
||||
if (s != ksq && s != rsq && pos.square_is_occupied(s))
|
||||
illegal = true;
|
||||
|
||||
if (!illegal)
|
||||
(*mlist++).move = make_castle_move(ksq, rsq);
|
||||
}
|
||||
|
||||
if (pos.can_castle_queenside(us))
|
||||
{
|
||||
Square rsq = pos.initial_qr_square(us);
|
||||
Square c1 = relative_square(us, SQ_C1);
|
||||
Square d1 = relative_square(us, SQ_D1);
|
||||
Square s;
|
||||
bool illegal = false;
|
||||
|
||||
assert(pos.piece_on(rsq) == rook_of_color(us));
|
||||
|
||||
for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
|
||||
if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
|
||||
|| pos.square_is_attacked(s, them))
|
||||
illegal = true;
|
||||
|
||||
for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
|
||||
if (s != ksq && s != rsq && pos.square_is_occupied(s))
|
||||
illegal = true;
|
||||
|
||||
if ( square_file(rsq) == FILE_B
|
||||
&& ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
|
||||
|| pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
|
||||
if ( Side == QUEEN_SIDE
|
||||
&& square_file(rsq) == FILE_B
|
||||
&& ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
|
||||
|| pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
|
||||
illegal = true;
|
||||
|
||||
if (!illegal)
|
||||
(*mlist++).move = make_castle_move(ksq, rsq);
|
||||
}
|
||||
}
|
||||
return mlist;
|
||||
}
|
||||
|
||||
bool castling_is_check(const Position& pos, CastlingSide side) {
|
||||
|
||||
// After castling opponent king is attacked by the castled rook?
|
||||
File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
|
||||
Color us = pos.side_to_move();
|
||||
Square ksq = pos.king_square(us);
|
||||
Bitboard occ = pos.occupied_squares();
|
||||
|
||||
clear_bit(&occ, ksq); // Remove our king from the board
|
||||
Square rsq = make_square(rookFile, square_rank(ksq));
|
||||
return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,12 +32,12 @@
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern int generate_captures(const Position &pos, MoveStack *mlist);
|
||||
extern int generate_noncaptures(const Position &pos, MoveStack *mlist);
|
||||
extern int generate_checks(const Position &pos, MoveStack *mlist, Bitboard dc);
|
||||
extern int generate_evasions(const Position &pos, MoveStack *mlist);
|
||||
extern int generate_legal_moves(const Position &pos, MoveStack *mlist);
|
||||
extern bool move_is_legal(const Position &pos, const Move m, Bitboard pinned);
|
||||
extern int generate_captures(const Position& pos, MoveStack* mlist);
|
||||
extern int generate_noncaptures(const Position& pos, MoveStack* mlist);
|
||||
extern int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc);
|
||||
extern int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned);
|
||||
extern int generate_legal_moves(const Position& pos, MoveStack* mlist);
|
||||
extern bool move_is_legal(const Position& pos, const Move m, Bitboard pinned);
|
||||
|
||||
|
||||
#endif // !defined(MOVEGEN_H_INCLUDED)
|
||||
|
||||
224
src/movepick.cpp
224
src/movepick.cpp
@@ -23,6 +23,7 @@
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "history.h"
|
||||
@@ -45,14 +46,11 @@ namespace {
|
||||
int MainSearchPhaseIndex;
|
||||
int EvasionsPhaseIndex;
|
||||
int QsearchWithChecksPhaseIndex;
|
||||
int QsearchNoCapturesPhaseIndex;
|
||||
int QsearchWithoutChecksPhaseIndex;
|
||||
int NoMovesPhaseIndex;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
@@ -66,7 +64,7 @@ namespace {
|
||||
/// move ordering is at the current node.
|
||||
|
||||
MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
|
||||
const SearchStack& ss, Depth d, EvalInfo* ei) : pos(p) {
|
||||
const SearchStack& ss, Depth d) : pos(p) {
|
||||
pvNode = pv;
|
||||
ttMove = ttm;
|
||||
mateKiller = (ss.mateKiller == ttm)? MOVE_NONE : ss.mateKiller;
|
||||
@@ -77,27 +75,19 @@ MovePicker::MovePicker(const Position& p, bool pv, Move ttm,
|
||||
numOfMoves = 0;
|
||||
numOfBadCaptures = 0;
|
||||
|
||||
// With EvalInfo we are able to know how many captures are possible before
|
||||
// generating them. So avoid generating in case we know are zero.
|
||||
Color us = pos.side_to_move();
|
||||
Color them = opposite_color(us);
|
||||
bool noCaptures = ei
|
||||
&& (ei->attackedBy[us][0] & pos.pieces_of_color(them)) == 0
|
||||
&& !ei->mi->specialized_eval_exists()
|
||||
&& (pos.ep_square() == SQ_NONE)
|
||||
&& !pos.has_pawn_on_7th(us);
|
||||
|
||||
if (p.is_check())
|
||||
phaseIndex = EvasionsPhaseIndex;
|
||||
else if (depth > Depth(0))
|
||||
phaseIndex = MainSearchPhaseIndex;
|
||||
else if (depth == Depth(0))
|
||||
phaseIndex = (noCaptures ? QsearchNoCapturesPhaseIndex : QsearchWithChecksPhaseIndex);
|
||||
phaseIndex = QsearchWithChecksPhaseIndex;
|
||||
else
|
||||
phaseIndex = (noCaptures ? NoMovesPhaseIndex : QsearchWithoutChecksPhaseIndex);
|
||||
phaseIndex = QsearchWithoutChecksPhaseIndex;
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
|
||||
dc = p.discovered_check_candidates(us);
|
||||
pinned = p.pinned_pieces(p.side_to_move());
|
||||
pinned = p.pinned_pieces(us);
|
||||
|
||||
finished = false;
|
||||
}
|
||||
@@ -141,40 +131,46 @@ Move MovePicker::get_next_move() {
|
||||
assert(move_is_ok(mateKiller));
|
||||
if (move_is_legal(pos, mateKiller, pinned))
|
||||
return mateKiller;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case PH_GOOD_CAPTURES:
|
||||
numOfMoves = generate_captures(pos, moves);
|
||||
score_captures();
|
||||
std::sort(moves, moves + numOfMoves);
|
||||
movesPicked = 0;
|
||||
break;
|
||||
|
||||
case PH_BAD_CAPTURES:
|
||||
badCapturesPicked = 0;
|
||||
// It's probably a good idea to use SEE move ordering here. FIXME
|
||||
movesPicked = 0;
|
||||
break;
|
||||
|
||||
case PH_NONCAPTURES:
|
||||
numOfMoves = generate_noncaptures(pos, moves);
|
||||
score_noncaptures();
|
||||
std::sort(moves, moves + numOfMoves);
|
||||
movesPicked = 0;
|
||||
break;
|
||||
|
||||
case PH_EVASIONS:
|
||||
assert(pos.is_check());
|
||||
numOfMoves = generate_evasions(pos, moves);
|
||||
numOfMoves = generate_evasions(pos, moves, pinned);
|
||||
score_evasions();
|
||||
std::sort(moves, moves + numOfMoves);
|
||||
movesPicked = 0;
|
||||
break;
|
||||
|
||||
case PH_QCAPTURES:
|
||||
numOfMoves = generate_captures(pos, moves);
|
||||
score_qcaptures();
|
||||
std::sort(moves, moves + numOfMoves);
|
||||
movesPicked = 0;
|
||||
break;
|
||||
|
||||
case PH_QCHECKS:
|
||||
numOfMoves = generate_checks(pos, moves, dc);
|
||||
// Perhaps we should order moves move here? FIXME
|
||||
numOfMoves = generate_non_capture_checks(pos, moves, dc);
|
||||
movesPicked = 0;
|
||||
break;
|
||||
|
||||
@@ -310,63 +306,6 @@ void MovePicker::score_qcaptures() {
|
||||
}
|
||||
|
||||
|
||||
/// find_best_index() loops across the moves and returns index of
|
||||
/// the highest scored one. There is also a second version that
|
||||
/// lowers the priority of moves that attack the same square,
|
||||
/// so that if the best move that attack a square fails the next
|
||||
/// move picked attacks a different square if any, not the same one.
|
||||
|
||||
int MovePicker::find_best_index() {
|
||||
|
||||
int bestScore = -10000000, bestIndex = -1;
|
||||
|
||||
for (int i = movesPicked; i < numOfMoves; i++)
|
||||
if (moves[i].score > bestScore)
|
||||
{
|
||||
bestIndex = i;
|
||||
bestScore = moves[i].score;
|
||||
}
|
||||
return bestIndex;
|
||||
}
|
||||
|
||||
int MovePicker::find_best_index(Bitboard* squares, int values[]) {
|
||||
|
||||
int hs;
|
||||
Move m;
|
||||
Square to;
|
||||
int bestScore = -10000000, bestIndex = -1;
|
||||
|
||||
for (int i = movesPicked; i < numOfMoves; i++)
|
||||
{
|
||||
m = moves[i].move;
|
||||
to = move_to(m);
|
||||
|
||||
if (!bit_is_set(*squares, to))
|
||||
{
|
||||
// Init at first use
|
||||
set_bit(squares, to);
|
||||
values[to] = 0;
|
||||
}
|
||||
|
||||
hs = moves[i].score - values[to];
|
||||
if (hs > bestScore)
|
||||
{
|
||||
bestIndex = i;
|
||||
bestScore = hs;
|
||||
}
|
||||
}
|
||||
|
||||
if (bestIndex != -1)
|
||||
{
|
||||
// Raise value of the picked square, so next attack
|
||||
// to the same square will get low priority.
|
||||
to = move_to(moves[bestIndex].move);
|
||||
values[to] += 0xB00;
|
||||
}
|
||||
return bestIndex;
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::pick_move_from_list() picks the move with the biggest score
|
||||
/// from a list of generated moves (moves[] or badCaptures[], depending on
|
||||
/// the current move generation phase). It takes care not to return the
|
||||
@@ -374,79 +313,34 @@ int MovePicker::find_best_index(Bitboard* squares, int values[]) {
|
||||
|
||||
Move MovePicker::pick_move_from_list() {
|
||||
|
||||
int bestIndex;
|
||||
Move move;
|
||||
assert(movesPicked >= 0);
|
||||
assert(!pos.is_check() || PhaseTable[phaseIndex] == PH_EVASIONS || PhaseTable[phaseIndex] == PH_STOP);
|
||||
assert( pos.is_check() || PhaseTable[phaseIndex] != PH_EVASIONS);
|
||||
|
||||
switch (PhaseTable[phaseIndex]) {
|
||||
|
||||
case PH_GOOD_CAPTURES:
|
||||
assert(!pos.is_check());
|
||||
assert(movesPicked >= 0);
|
||||
|
||||
while (movesPicked < numOfMoves)
|
||||
{
|
||||
bestIndex = find_best_index();
|
||||
|
||||
if (bestIndex != -1) // Found a good capture
|
||||
{
|
||||
move = moves[bestIndex].move;
|
||||
moves[bestIndex] = moves[movesPicked++];
|
||||
if ( move != ttMove
|
||||
&& move != mateKiller
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case PH_NONCAPTURES:
|
||||
assert(!pos.is_check());
|
||||
assert(movesPicked >= 0);
|
||||
|
||||
while (movesPicked < numOfMoves)
|
||||
{
|
||||
// If this is a PV node or we have only picked a few moves, scan
|
||||
// the entire move list for the best move. If many moves have already
|
||||
// been searched and it is not a PV node, we are probably failing low
|
||||
// anyway, so we just pick the first move from the list.
|
||||
bestIndex = (pvNode || movesPicked < 12) ? find_best_index() : movesPicked;
|
||||
|
||||
if (bestIndex != -1)
|
||||
{
|
||||
move = moves[bestIndex].move;
|
||||
moves[bestIndex] = moves[movesPicked++];
|
||||
if ( move != ttMove
|
||||
&& move != mateKiller
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
}
|
||||
Move move = moves[movesPicked++].move;
|
||||
if ( move != ttMove
|
||||
&& move != mateKiller
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
}
|
||||
break;
|
||||
|
||||
case PH_EVASIONS:
|
||||
assert(pos.is_check());
|
||||
assert(movesPicked >= 0);
|
||||
if (movesPicked < numOfMoves)
|
||||
return moves[movesPicked++].move;
|
||||
|
||||
while (movesPicked < numOfMoves)
|
||||
{
|
||||
bestIndex = find_best_index();
|
||||
|
||||
if (bestIndex != -1)
|
||||
{
|
||||
move = moves[bestIndex].move;
|
||||
moves[bestIndex] = moves[movesPicked++];
|
||||
return move;
|
||||
}
|
||||
}
|
||||
break;
|
||||
break;
|
||||
|
||||
case PH_BAD_CAPTURES:
|
||||
assert(!pos.is_check());
|
||||
assert(badCapturesPicked >= 0);
|
||||
// It's probably a good idea to use SEE move ordering here, instead
|
||||
// of just picking the first move. FIXME
|
||||
while (badCapturesPicked < numOfBadCaptures)
|
||||
while (movesPicked < numOfBadCaptures)
|
||||
{
|
||||
move = badCaptures[badCapturesPicked++].move;
|
||||
Move move = badCaptures[movesPicked++].move;
|
||||
if ( move != ttMove
|
||||
&& move != mateKiller
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
@@ -455,34 +349,13 @@ Move MovePicker::pick_move_from_list() {
|
||||
break;
|
||||
|
||||
case PH_QCAPTURES:
|
||||
assert(!pos.is_check());
|
||||
assert(movesPicked >= 0);
|
||||
while (movesPicked < numOfMoves)
|
||||
{
|
||||
bestIndex = (movesPicked < 4 ? find_best_index() : movesPicked);
|
||||
|
||||
if (bestIndex != -1)
|
||||
{
|
||||
move = moves[bestIndex].move;
|
||||
moves[bestIndex] = moves[movesPicked++];
|
||||
// Remember to change the line below if we decide to hash the qsearch!
|
||||
// Maybe also postpone the legality check until after futility pruning?
|
||||
if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case PH_QCHECKS:
|
||||
assert(!pos.is_check());
|
||||
assert(movesPicked >= 0);
|
||||
// Perhaps we should do something better than just picking the first
|
||||
// move here? FIXME
|
||||
while (movesPicked < numOfMoves)
|
||||
{
|
||||
move = moves[movesPicked++].move;
|
||||
// Remember to change the line below if we decide to hash the qsearch!
|
||||
if (/* move != ttMove && */ pos.pl_move_is_legal(move, pinned))
|
||||
Move move = moves[movesPicked++].move;
|
||||
// Maybe postpone the legality check until after futility pruning?
|
||||
if ( move != ttMove
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
}
|
||||
break;
|
||||
@@ -494,20 +367,10 @@ Move MovePicker::pick_move_from_list() {
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::current_move_type() returns the type of the just
|
||||
/// picked next move. It can be used in search to further differentiate
|
||||
/// according to the current move type: capture, non capture, escape, etc.
|
||||
MovePicker::MovegenPhase MovePicker::current_move_type() const {
|
||||
|
||||
return PhaseTable[phaseIndex];
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::init_phase_table() initializes the PhaseTable[],
|
||||
/// MainSearchPhaseIndex, EvasionPhaseIndex, QsearchWithChecksPhaseIndex
|
||||
/// QsearchNoCapturesPhaseIndex, QsearchWithoutChecksPhaseIndex and
|
||||
/// NoMovesPhaseIndex variables. It is only called once during program
|
||||
/// startup, and never again while the program is running.
|
||||
/// and QsearchWithoutChecksPhaseIndex. It is only called once during
|
||||
/// program startup, and never again while the program is running.
|
||||
|
||||
void MovePicker::init_phase_table() {
|
||||
|
||||
@@ -532,21 +395,14 @@ void MovePicker::init_phase_table() {
|
||||
|
||||
// Quiescence search with checks
|
||||
QsearchWithChecksPhaseIndex = i - 1;
|
||||
PhaseTable[i++] = PH_TT_MOVE;
|
||||
PhaseTable[i++] = PH_QCAPTURES;
|
||||
PhaseTable[i++] = PH_QCHECKS;
|
||||
PhaseTable[i++] = PH_STOP;
|
||||
|
||||
// Quiescence search with checks only and no captures
|
||||
QsearchNoCapturesPhaseIndex = i - 1;
|
||||
PhaseTable[i++] = PH_QCHECKS;
|
||||
PhaseTable[i++] = PH_STOP;
|
||||
|
||||
// Quiescence search without checks
|
||||
QsearchWithoutChecksPhaseIndex = i - 1;
|
||||
PhaseTable[i++] = PH_TT_MOVE;
|
||||
PhaseTable[i++] = PH_QCAPTURES;
|
||||
PhaseTable[i++] = PH_STOP;
|
||||
|
||||
// Do not generate any move
|
||||
NoMovesPhaseIndex = i - 1;
|
||||
PhaseTable[i++] = PH_STOP;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,8 @@ struct SearchStack;
|
||||
|
||||
class MovePicker {
|
||||
|
||||
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
|
||||
|
||||
public:
|
||||
|
||||
enum MovegenPhase {
|
||||
@@ -63,12 +65,11 @@ public:
|
||||
PH_STOP
|
||||
};
|
||||
|
||||
MovePicker(const Position& p, bool pvnode, Move ttm, const SearchStack& ss, Depth d, EvalInfo* ei = NULL);
|
||||
MovePicker(const Position& p, bool pvnode, Move ttm, const SearchStack& ss, Depth d);
|
||||
Move get_next_move();
|
||||
Move get_next_move(Lock &lock);
|
||||
int number_of_moves() const;
|
||||
int current_move_score() const;
|
||||
MovegenPhase current_move_type() const;
|
||||
Bitboard discovered_check_candidates() const;
|
||||
|
||||
static void init_phase_table();
|
||||
@@ -79,8 +80,6 @@ private:
|
||||
void score_evasions();
|
||||
void score_qcaptures();
|
||||
Move pick_move_from_list();
|
||||
int find_best_index();
|
||||
int find_best_index(Bitboard* squares, int values[]);
|
||||
|
||||
const Position& pos;
|
||||
Move ttMove, mateKiller, killer1, killer2;
|
||||
@@ -90,7 +89,7 @@ private:
|
||||
Depth depth;
|
||||
int phaseIndex;
|
||||
int numOfMoves, numOfBadCaptures;
|
||||
int movesPicked, badCapturesPicked;
|
||||
int movesPicked;
|
||||
bool finished;
|
||||
};
|
||||
|
||||
@@ -105,7 +104,6 @@ private:
|
||||
/// a single reply to check.
|
||||
|
||||
inline int MovePicker::number_of_moves() const {
|
||||
|
||||
return numOfMoves;
|
||||
}
|
||||
|
||||
|
||||
116
src/pawns.cpp
116
src/pawns.cpp
@@ -26,6 +26,7 @@
|
||||
#include <cstring>
|
||||
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
|
||||
|
||||
////
|
||||
@@ -119,14 +120,12 @@ namespace {
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
// Pawn storm open file bonuses by file:
|
||||
const int KStormOpenFileBonus[8] = {
|
||||
45, 45, 30, 0, 0, 0, 0, 0
|
||||
};
|
||||
// Pawn storm open file bonuses by file
|
||||
const int16_t KStormOpenFileBonus[8] = { 45, 45, 30, 0, 0, 0, 0, 0 };
|
||||
const int16_t QStormOpenFileBonus[8] = { 0, 0, 0, 0, 0, 30, 45, 30 };
|
||||
|
||||
const int QStormOpenFileBonus[8] = {
|
||||
0, 0, 0, 0, 0, 30, 45, 30
|
||||
};
|
||||
// Pawn storm lever bonuses by file
|
||||
const int StormLeverBonus[8] = { 20, 20, 10, 0, 0, 10, 20, 20 };
|
||||
|
||||
}
|
||||
|
||||
@@ -199,6 +198,7 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
|
||||
Bitboard ourPawns = pos.pawns(us);
|
||||
Bitboard theirPawns = pos.pawns(them);
|
||||
Bitboard pawns = ourPawns;
|
||||
int bonus;
|
||||
|
||||
// Initialize pawn storm scores by giving bonuses for open files
|
||||
for (File f = FILE_A; f <= FILE_H; f++)
|
||||
@@ -216,7 +216,7 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
|
||||
File f = square_file(s);
|
||||
Rank r = square_rank(s);
|
||||
|
||||
assert(pos.piece_on(s) == pawn_of_color(us));
|
||||
assert(pos.piece_on(s) == piece_of_color_and_type(us, PAWN));
|
||||
|
||||
// The file containing the pawn is not half open
|
||||
pi->halfOpenFiles[us] &= ~(1 << f);
|
||||
@@ -226,54 +226,66 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
|
||||
isolated = pos.pawn_is_isolated(us, s);
|
||||
doubled = pos.pawn_is_doubled(us, s);
|
||||
|
||||
// We calculate kingside and queenside pawn storm scores
|
||||
// for both colors. These are used when evaluating middle
|
||||
// game positions with opposite side castling.
|
||||
// We calculate kingside and queenside pawn storm
|
||||
// scores for both colors. These are used when evaluating
|
||||
// middle game positions with opposite side castling.
|
||||
//
|
||||
// Each pawn is given a base score given by a piece square table
|
||||
// (KStormTable[] or QStormTable[]). This score is increased if
|
||||
// there are enemy pawns on adjacent files in front of the pawn.
|
||||
// This is because we want to be able to open files against the
|
||||
// enemy king, and to avoid blocking the pawn structure (e.g. white
|
||||
// pawns on h6, g5, black pawns on h7, g6, f7).
|
||||
// (KStormTable[] or QStormTable[]). Pawns which seem to have good
|
||||
// chances of creating an open file by exchanging itself against an
|
||||
// enemy pawn on an adjacent file gets an additional bonus.
|
||||
|
||||
// Kingside and queenside pawn storms
|
||||
int KBonus = KStormTable[relative_square(us, s)];
|
||||
int QBonus = QStormTable[relative_square(us, s)];
|
||||
bool outPostFlag = (KBonus > 0 && (outpost_mask(us, s) & theirPawns));
|
||||
bool passedFlag = (QBonus > 0 && (passed_pawn_mask(us, s) & theirPawns));
|
||||
|
||||
switch (f) {
|
||||
|
||||
case FILE_A:
|
||||
QBonus += passedFlag * QBonus / 2;
|
||||
break;
|
||||
|
||||
case FILE_B:
|
||||
QBonus += passedFlag * (QBonus / 2 + QBonus / 4);
|
||||
break;
|
||||
|
||||
case FILE_C:
|
||||
QBonus += passedFlag * QBonus / 2;
|
||||
break;
|
||||
|
||||
case FILE_F:
|
||||
KBonus += outPostFlag * KBonus / 4;
|
||||
break;
|
||||
|
||||
case FILE_G:
|
||||
KBonus += outPostFlag * (KBonus / 2 + KBonus / 4);
|
||||
break;
|
||||
|
||||
case FILE_H:
|
||||
KBonus += outPostFlag * KBonus / 2;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
// Kingside pawn storms
|
||||
bonus = KStormTable[relative_square(us, s)];
|
||||
if (f >= FILE_F)
|
||||
{
|
||||
Bitboard b = outpost_mask(us, s) & theirPawns & (FileFBB | FileGBB | FileHBB);
|
||||
while (b)
|
||||
{
|
||||
Square s2 = pop_1st_bit(&b);
|
||||
if (!(theirPawns & neighboring_files_bb(s2) & rank_bb(s2)))
|
||||
{
|
||||
// The enemy pawn has no pawn beside itself, which makes it
|
||||
// particularly vulnerable. Big bonus, especially against a
|
||||
// weakness on the rook file.
|
||||
if (square_file(s2) == FILE_H)
|
||||
bonus += 4*StormLeverBonus[f] - 8*square_distance(s, s2);
|
||||
else
|
||||
bonus += 2*StormLeverBonus[f] - 4*square_distance(s, s2);
|
||||
} else
|
||||
// There is at least one enemy pawn beside the enemy pawn we look
|
||||
// at, which means that the pawn has somewhat better chances of
|
||||
// defending itself by advancing. Smaller bonus.
|
||||
bonus += StormLeverBonus[f] - 2*square_distance(s, s2);
|
||||
}
|
||||
}
|
||||
pi->ksStormValue[us] += KBonus;
|
||||
pi->qsStormValue[us] += QBonus;
|
||||
pi->ksStormValue[us] += bonus;
|
||||
|
||||
// Queenside pawn storms
|
||||
bonus = QStormTable[relative_square(us, s)];
|
||||
if (f <= FILE_C)
|
||||
{
|
||||
Bitboard b = outpost_mask(us, s) & theirPawns & (FileABB | FileBBB | FileCBB);
|
||||
while (b)
|
||||
{
|
||||
Square s2 = pop_1st_bit(&b);
|
||||
if (!(theirPawns & neighboring_files_bb(s2) & rank_bb(s2)))
|
||||
{
|
||||
// The enemy pawn has no pawn beside itself, which makes it
|
||||
// particularly vulnerable. Big bonus, especially against a
|
||||
// weakness on the rook file.
|
||||
if (square_file(s2) == FILE_A)
|
||||
bonus += 4*StormLeverBonus[f] - 16*square_distance(s, s2);
|
||||
else
|
||||
bonus += 2*StormLeverBonus[f] - 8*square_distance(s, s2);
|
||||
} else
|
||||
// There is at least one enemy pawn beside the enemy pawn we look
|
||||
// at, which means that the pawn has somewhat better chances of
|
||||
// defending itself by advancing. Smaller bonus.
|
||||
bonus += StormLeverBonus[f] - 4*square_distance(s, s2);
|
||||
}
|
||||
}
|
||||
pi->qsStormValue[us] += bonus;
|
||||
|
||||
// Member of a pawn chain (but not the backward one)? We could speed up
|
||||
// the test a little by introducing an array of masks indexed by color
|
||||
|
||||
30
src/pawns.h
30
src/pawns.h
@@ -25,8 +25,8 @@
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "position.h"
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "value.h"
|
||||
|
||||
////
|
||||
//// Types
|
||||
@@ -38,6 +38,7 @@
|
||||
/// to add further information in the future. A lookup to the pawn hash table
|
||||
/// (performed by calling the get_pawn_info method in a PawnInfoTable object)
|
||||
/// returns a pointer to a PawnInfo object.
|
||||
class Position;
|
||||
|
||||
class PawnInfo {
|
||||
|
||||
@@ -49,9 +50,9 @@ public:
|
||||
Value kingside_storm_value(Color c) const;
|
||||
Value queenside_storm_value(Color c) const;
|
||||
Bitboard passed_pawns() const;
|
||||
bool file_is_half_open(Color c, File f) const;
|
||||
bool has_open_file_to_left(Color c, File f) const;
|
||||
bool has_open_file_to_right(Color c, File f) const;
|
||||
int file_is_half_open(Color c, File f) const;
|
||||
int has_open_file_to_left(Color c, File f) const;
|
||||
int has_open_file_to_right(Color c, File f) const;
|
||||
|
||||
private:
|
||||
void clear();
|
||||
@@ -59,7 +60,7 @@ private:
|
||||
Key key;
|
||||
Bitboard passedPawns;
|
||||
int16_t mgValue, egValue;
|
||||
int8_t ksStormValue[2], qsStormValue[2];
|
||||
int16_t ksStormValue[2], qsStormValue[2];
|
||||
uint8_t halfOpenFiles[2];
|
||||
};
|
||||
|
||||
@@ -75,11 +76,11 @@ public:
|
||||
PawnInfoTable(unsigned numOfEntries);
|
||||
~PawnInfoTable();
|
||||
void clear();
|
||||
PawnInfo *get_pawn_info(const Position &pos);
|
||||
PawnInfo* get_pawn_info(const Position& pos);
|
||||
|
||||
private:
|
||||
unsigned size;
|
||||
PawnInfo *entries;
|
||||
PawnInfo* entries;
|
||||
};
|
||||
|
||||
|
||||
@@ -107,23 +108,24 @@ inline Value PawnInfo::queenside_storm_value(Color c) const {
|
||||
return Value(qsStormValue[c]);
|
||||
}
|
||||
|
||||
inline bool PawnInfo::file_is_half_open(Color c, File f) const {
|
||||
inline int PawnInfo::file_is_half_open(Color c, File f) const {
|
||||
return (halfOpenFiles[c] & (1 << int(f)));
|
||||
}
|
||||
|
||||
inline bool PawnInfo::has_open_file_to_left(Color c, File f) const {
|
||||
inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ((1 << int(f)) - 1);
|
||||
}
|
||||
|
||||
inline bool PawnInfo::has_open_file_to_right(Color c, File f) const {
|
||||
inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
|
||||
}
|
||||
|
||||
inline void PawnInfo::clear() {
|
||||
|
||||
Key k = key;
|
||||
memset(this, 0, sizeof(PawnInfo));
|
||||
key = k;
|
||||
passedPawns = EmptyBoardBB;
|
||||
mgValue = egValue = 0;
|
||||
ksStormValue[WHITE] = ksStormValue[BLACK] = 0;
|
||||
qsStormValue[WHITE] = qsStormValue[BLACK] = 0;
|
||||
halfOpenFiles[WHITE] = halfOpenFiles[BLACK] = 0xFF;
|
||||
}
|
||||
|
||||
|
||||
34
src/phase.h
34
src/phase.h
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008 Marco Costalba
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(PHASE_H_INCLUDED)
|
||||
#define PHASE_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
enum Phase {
|
||||
PHASE_ENDGAME = 0,
|
||||
PHASE_MIDGAME = 128
|
||||
};
|
||||
|
||||
|
||||
#endif // !defined(PHASE_H_INCLUDED)
|
||||
@@ -22,74 +22,28 @@
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "piece.h"
|
||||
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
////
|
||||
|
||||
const int SlidingArray[18] = {
|
||||
0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
const SquareDelta Directions[16][16] = {
|
||||
{DELTA_ZERO},
|
||||
{DELTA_NW, DELTA_NE, DELTA_ZERO},
|
||||
{DELTA_SSW, DELTA_SSE, DELTA_SWW, DELTA_SEE,
|
||||
DELTA_NWW, DELTA_NEE, DELTA_NNW, DELTA_NNE, DELTA_ZERO},
|
||||
{DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
|
||||
{DELTA_S, DELTA_E, DELTA_W, DELTA_N, DELTA_ZERO},
|
||||
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
|
||||
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
|
||||
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
|
||||
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
|
||||
{DELTA_ZERO},
|
||||
{DELTA_ZERO},
|
||||
{DELTA_SW, DELTA_SE, DELTA_ZERO},
|
||||
{DELTA_SSW, DELTA_SSE, DELTA_SWW, DELTA_SEE,
|
||||
DELTA_NWW, DELTA_NEE, DELTA_NNW, DELTA_NNE, DELTA_ZERO},
|
||||
{DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
|
||||
{DELTA_S, DELTA_E, DELTA_W, DELTA_N, DELTA_ZERO},
|
||||
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
|
||||
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
|
||||
{DELTA_S, DELTA_E, DELTA_W, DELTA_N,
|
||||
DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
|
||||
};
|
||||
|
||||
const SquareDelta PawnPush[2] = {
|
||||
DELTA_N, DELTA_S
|
||||
};
|
||||
|
||||
using namespace std;
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// Translating piece types to/from English piece letters:
|
||||
/// Translating piece types to/from English piece letters
|
||||
|
||||
static const char PieceChars[] = " pnbrqk";
|
||||
static const string PieceChars(" pnbrqk PNBRQK");
|
||||
|
||||
char piece_type_to_char(PieceType pt, bool upcase) {
|
||||
return upcase? toupper(PieceChars[pt]) : PieceChars[pt];
|
||||
|
||||
return PieceChars[pt + upcase * 7];
|
||||
}
|
||||
|
||||
PieceType piece_type_from_char(char c) {
|
||||
const char *ch = strchr(PieceChars, tolower(c));
|
||||
return ch? PieceType(ch - PieceChars) : NO_PIECE_TYPE;
|
||||
}
|
||||
|
||||
|
||||
/// piece_is_ok() and piece_type_is_ok(), for debugging:
|
||||
|
||||
bool piece_is_ok(Piece pc) {
|
||||
return
|
||||
piece_type_is_ok(type_of_piece(pc)) &&
|
||||
color_is_ok(color_of_piece(pc));
|
||||
}
|
||||
|
||||
bool piece_type_is_ok(PieceType pc) {
|
||||
return pc >= PAWN && pc <= KING;
|
||||
|
||||
size_t idx = PieceChars.find(c);
|
||||
|
||||
return idx != string::npos ? PieceType(idx % 7) : NO_PIECE_TYPE;
|
||||
}
|
||||
|
||||
50
src/piece.h
50
src/piece.h
@@ -26,7 +26,6 @@
|
||||
////
|
||||
|
||||
#include "color.h"
|
||||
#include "misc.h"
|
||||
#include "square.h"
|
||||
|
||||
|
||||
@@ -47,15 +46,12 @@ enum Piece {
|
||||
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
//// Constants
|
||||
////
|
||||
|
||||
const PieceType PieceTypeMin = PAWN;
|
||||
const PieceType PieceTypeMax = KING;
|
||||
|
||||
extern const int SlidingArray[18];
|
||||
extern const SquareDelta Directions[16][16];
|
||||
extern const SquareDelta PawnPush[2];
|
||||
const int SlidingArray[18] = {
|
||||
0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
@@ -83,40 +79,24 @@ inline Piece piece_of_color_and_type(Color c, PieceType pt) {
|
||||
return Piece((int(c) << 3) | int(pt));
|
||||
}
|
||||
|
||||
inline Piece pawn_of_color(Color c) {
|
||||
return piece_of_color_and_type(c, PAWN);
|
||||
}
|
||||
|
||||
inline Piece knight_of_color(Color c) {
|
||||
return piece_of_color_and_type(c, KNIGHT);
|
||||
}
|
||||
|
||||
inline Piece bishop_of_color(Color c) {
|
||||
return piece_of_color_and_type(c, BISHOP);
|
||||
}
|
||||
|
||||
inline Piece rook_of_color(Color c) {
|
||||
return piece_of_color_and_type(c, ROOK);
|
||||
}
|
||||
|
||||
inline Piece queen_of_color(Color c) {
|
||||
return piece_of_color_and_type(c, QUEEN);
|
||||
}
|
||||
|
||||
inline Piece king_of_color(Color c) {
|
||||
return piece_of_color_and_type(c, KING);
|
||||
}
|
||||
|
||||
inline int piece_is_slider(Piece p) {
|
||||
return SlidingArray[int(p)];
|
||||
}
|
||||
|
||||
inline int piece_type_is_slider(PieceType pt) {
|
||||
inline int piece_is_slider(PieceType pt) {
|
||||
return SlidingArray[int(pt)];
|
||||
}
|
||||
|
||||
inline SquareDelta pawn_push(Color c) {
|
||||
return PawnPush[c];
|
||||
return (c == WHITE ? DELTA_N : DELTA_S);
|
||||
}
|
||||
|
||||
inline bool piece_type_is_ok(PieceType pc) {
|
||||
return pc >= PAWN && pc <= KING;
|
||||
}
|
||||
|
||||
inline bool piece_is_ok(Piece pc) {
|
||||
return piece_type_is_ok(type_of_piece(pc)) && color_is_ok(color_of_piece(pc));
|
||||
}
|
||||
|
||||
|
||||
@@ -126,8 +106,6 @@ inline SquareDelta pawn_push(Color c) {
|
||||
|
||||
extern char piece_type_to_char(PieceType pt, bool upcase = false);
|
||||
extern PieceType piece_type_from_char(char c);
|
||||
extern bool piece_is_ok(Piece pc);
|
||||
extern bool piece_type_is_ok(PieceType pt);
|
||||
|
||||
|
||||
#endif // !defined(PIECE_H_INCLUDED)
|
||||
|
||||
987
src/position.cpp
987
src/position.cpp
File diff suppressed because it is too large
Load Diff
178
src/position.h
178
src/position.h
@@ -27,6 +27,10 @@
|
||||
// Forcing value to bool 'true' or 'false' (performance warning)
|
||||
#pragma warning(disable: 4800)
|
||||
|
||||
// Conditional expression is constant
|
||||
#pragma warning(disable: 4127)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
////
|
||||
@@ -38,7 +42,6 @@
|
||||
#include "direction.h"
|
||||
#include "move.h"
|
||||
#include "piece.h"
|
||||
#include "phase.h"
|
||||
#include "square.h"
|
||||
#include "value.h"
|
||||
|
||||
@@ -61,7 +64,7 @@ const int MaxGameLength = 220;
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// Castle rights, encoded as bit fields:
|
||||
/// Castle rights, encoded as bit fields
|
||||
|
||||
enum CastleRights {
|
||||
NO_CASTLES = 0,
|
||||
@@ -72,22 +75,28 @@ enum CastleRights {
|
||||
ALL_CASTLES = 15
|
||||
};
|
||||
|
||||
/// Game phase
|
||||
enum Phase {
|
||||
PHASE_ENDGAME = 0,
|
||||
PHASE_MIDGAME = 128
|
||||
};
|
||||
|
||||
/// The UndoInfo struct stores information we need to restore a Position
|
||||
/// object to its previous state when we retract a move. Whenever a move
|
||||
/// is made on the board (by calling Position::do_move), an UndoInfo object
|
||||
/// must be passed as a parameter. When the move is unmade (by calling
|
||||
/// Position::undo_move), the same UndoInfo object must be passed again.
|
||||
|
||||
struct UndoInfo {
|
||||
int castleRights;
|
||||
Square epSquare;
|
||||
Bitboard checkersBB;
|
||||
/// The StateInfo struct stores information we need to restore a Position
|
||||
/// object to its previous state when we retract a move. Whenever a move
|
||||
/// is made on the board (by calling Position::do_move), an StateInfo object
|
||||
/// must be passed as a parameter.
|
||||
|
||||
struct StateInfo {
|
||||
Key key, pawnKey, materialKey;
|
||||
int rule50;
|
||||
Move lastMove;
|
||||
PieceType capture;
|
||||
int castleRights, rule50;
|
||||
Square epSquare;
|
||||
Value mgValue, egValue;
|
||||
|
||||
PieceType capture;
|
||||
Bitboard checkersBB;
|
||||
Move lastMove;
|
||||
StateInfo* previous;
|
||||
};
|
||||
|
||||
|
||||
@@ -122,11 +131,11 @@ class Position {
|
||||
public:
|
||||
// Constructors
|
||||
Position() {};
|
||||
Position(const Position &pos);
|
||||
Position(const std::string &fen);
|
||||
Position(const Position& pos);
|
||||
Position(const std::string& fen);
|
||||
|
||||
// Text input/output
|
||||
void from_fen(const std::string &fen);
|
||||
void from_fen(const std::string& fen);
|
||||
const std::string to_fen() const;
|
||||
void print(Move m = MOVE_NONE) const;
|
||||
|
||||
@@ -197,6 +206,7 @@ public:
|
||||
|
||||
// Bitboards for pinned pieces and discovered check candidates
|
||||
Bitboard discovered_check_candidates(Color c) const;
|
||||
Bitboard pinned_pieces(Color c, Bitboard& p) const;
|
||||
Bitboard pinned_pieces(Color c) const;
|
||||
|
||||
// Checking pieces
|
||||
@@ -215,7 +225,7 @@ public:
|
||||
template<PieceType>
|
||||
Bitboard piece_attacks_square(Square f, Square t) const; // Dispatch at compile-time
|
||||
|
||||
bool piece_attacks_square(Square f, Square t) const; // Dispatch at run-time
|
||||
bool piece_attacks_square(Piece p, Square f, Square t) const; // Dispatch at run-time
|
||||
|
||||
// Properties of moves
|
||||
bool pl_move_is_legal(Move m) const;
|
||||
@@ -242,13 +252,12 @@ public:
|
||||
bool square_is_weak(Square s, Color c) const;
|
||||
|
||||
// Doing and undoing moves
|
||||
void backup(UndoInfo &u) const;
|
||||
void restore(const UndoInfo &u);
|
||||
void do_move(Move m, UndoInfo &u);
|
||||
void do_move(Move m, UndoInfo &u, Bitboard dcCandidates);
|
||||
void undo_move(Move m, const UndoInfo &u);
|
||||
void do_null_move(UndoInfo &u);
|
||||
void undo_null_move(const UndoInfo &u);
|
||||
void setStartState(const StateInfo& st);
|
||||
void do_move(Move m, StateInfo& st);
|
||||
void do_move(Move m, StateInfo& st, Bitboard dcCandidates);
|
||||
void undo_move(Move m);
|
||||
void do_null_move(StateInfo& st);
|
||||
void undo_null_move();
|
||||
|
||||
// Static exchange evaluation
|
||||
int see(Square from, Square to) const;
|
||||
@@ -268,7 +277,7 @@ public:
|
||||
Value mg_pst_delta(Move m) const;
|
||||
|
||||
// Game termination checks
|
||||
bool is_mate();
|
||||
bool is_mate() const;
|
||||
bool is_draw() const;
|
||||
|
||||
// Check if one side threatens a mate in one
|
||||
@@ -292,6 +301,7 @@ public:
|
||||
static void init_piece_square_tables();
|
||||
|
||||
private:
|
||||
|
||||
// Initialization helper functions (used while setting up a position)
|
||||
void clear();
|
||||
void put_piece(Piece p, Square s);
|
||||
@@ -299,17 +309,20 @@ private:
|
||||
void allow_ooo(Color c);
|
||||
|
||||
// Helper functions for doing and undoing moves
|
||||
void do_capture_move(Move m, PieceType capture, Color them, Square to);
|
||||
void do_capture_move(PieceType capture, Color them, Square to);
|
||||
void do_castle_move(Move m);
|
||||
void do_promotion_move(Move m, UndoInfo &u);
|
||||
void do_promotion_move(Move m);
|
||||
void do_ep_move(Move m);
|
||||
void undo_castle_move(Move m);
|
||||
void undo_promotion_move(Move m, const UndoInfo &u);
|
||||
void undo_promotion_move(Move m);
|
||||
void undo_ep_move(Move m);
|
||||
void find_checkers();
|
||||
|
||||
template<PieceType Piece, bool FindPinned>
|
||||
Bitboard hidden_checks(Color c, Square ksq) const;
|
||||
template<PieceType Piece>
|
||||
void update_checkers(Bitboard* pCheckersBB, Square ksq, Square from, Square to, Bitboard dcCandidates);
|
||||
|
||||
template<bool FindPinned>
|
||||
Bitboard hidden_checkers(Color c) const;
|
||||
|
||||
// Computing hash keys from scratch (for initialization and debugging)
|
||||
Key compute_key() const;
|
||||
@@ -317,15 +330,16 @@ private:
|
||||
Key compute_material_key() const;
|
||||
|
||||
// Computing incremental evaluation scores and material counts
|
||||
Value mg_pst(Color c, PieceType pt, Square s) const;
|
||||
Value eg_pst(Color c, PieceType pt, Square s) const;
|
||||
Value compute_mg_value() const;
|
||||
Value compute_eg_value() const;
|
||||
enum GamePhase {
|
||||
MidGame,
|
||||
EndGame
|
||||
};
|
||||
template<GamePhase> Value pst(Color c, PieceType pt, Square s) const;
|
||||
template<GamePhase> Value compute_value() const;
|
||||
Value compute_non_pawn_material(Color c) const;
|
||||
|
||||
// Bitboards
|
||||
Bitboard byColorBB[2], byTypeBB[8];
|
||||
Bitboard checkersBB;
|
||||
|
||||
// Board
|
||||
Piece board[64];
|
||||
@@ -338,16 +352,14 @@ private:
|
||||
int index[64];
|
||||
|
||||
// Other info
|
||||
Color sideToMove;
|
||||
int castleRights;
|
||||
File initialKFile, initialKRFile, initialQRFile;
|
||||
Square epSquare;
|
||||
Square kingSquare[2];
|
||||
Move lastMove;
|
||||
Key key, pawnKey, materialKey, history[MaxGameLength];
|
||||
int rule50, gamePly;
|
||||
Value mgValue, egValue;
|
||||
Color sideToMove;
|
||||
int gamePly;
|
||||
Key history[MaxGameLength];
|
||||
Value npMaterial[2];
|
||||
File initialKFile, initialKRFile, initialQRFile;
|
||||
StateInfo startState;
|
||||
StateInfo* st;
|
||||
|
||||
// Static variables
|
||||
static int castleRightsMask[64];
|
||||
@@ -498,7 +510,7 @@ inline Square Position::piece_list(Color c, PieceType pt, int index) const {
|
||||
}
|
||||
|
||||
inline Square Position::ep_square() const {
|
||||
return epSquare;
|
||||
return st->epSquare;
|
||||
}
|
||||
|
||||
inline Square Position::king_square(Color c) const {
|
||||
@@ -506,11 +518,11 @@ inline Square Position::king_square(Color c) const {
|
||||
}
|
||||
|
||||
inline bool Position::can_castle_kingside(Color side) const {
|
||||
return castleRights & (1+int(side));
|
||||
return st->castleRights & (1+int(side));
|
||||
}
|
||||
|
||||
inline bool Position::can_castle_queenside(Color side) const {
|
||||
return castleRights & (4+4*int(side));
|
||||
return st->castleRights & (4+4*int(side));
|
||||
}
|
||||
|
||||
inline bool Position::can_castle(Color side) const {
|
||||
@@ -526,7 +538,12 @@ inline Square Position::initial_qr_square(Color c) const {
|
||||
}
|
||||
|
||||
inline Bitboard Position::pawn_attacks(Color c, Square s) const {
|
||||
return StepAttackBB[pawn_of_color(c)][s];
|
||||
return StepAttackBB[piece_of_color_and_type(c, PAWN)][s];
|
||||
}
|
||||
|
||||
template<>
|
||||
inline Bitboard Position::piece_attacks<PAWN>(Square s) const {
|
||||
return StepAttackBB[piece_of_color_and_type(opposite_color(sideToMove), PAWN)][s];
|
||||
}
|
||||
|
||||
template<>
|
||||
@@ -555,11 +572,11 @@ inline Bitboard Position::piece_attacks<KING>(Square s) const {
|
||||
}
|
||||
|
||||
inline Bitboard Position::checkers() const {
|
||||
return checkersBB;
|
||||
return st->checkersBB;
|
||||
}
|
||||
|
||||
inline bool Position::is_check() const {
|
||||
return checkersBB != EmptyBoardBB;
|
||||
return st->checkersBB != EmptyBoardBB;
|
||||
}
|
||||
|
||||
inline bool Position::pawn_attacks_square(Color c, Square f, Square t) const {
|
||||
@@ -571,6 +588,16 @@ Bitboard Position::piece_attacks_square(Square f, Square t) const {
|
||||
return bit_is_set(piece_attacks<Piece>(f), t);
|
||||
}
|
||||
|
||||
inline Bitboard Position::attacks_to(Square s, Color c) const {
|
||||
|
||||
return attacks_to(s) & pieces_of_color(c);
|
||||
}
|
||||
|
||||
inline bool Position::square_is_attacked(Square s, Color c) const {
|
||||
|
||||
return attacks_to(s, c) != EmptyBoardBB;
|
||||
}
|
||||
|
||||
inline bool Position::pawn_is_passed(Color c, Square s) const {
|
||||
return !(pawns(opposite_color(c)) & passed_pawn_mask(c, s));
|
||||
}
|
||||
@@ -596,19 +623,21 @@ inline bool Position::square_is_weak(Square s, Color c) const {
|
||||
}
|
||||
|
||||
inline Key Position::get_key() const {
|
||||
return key;
|
||||
return st->key;
|
||||
}
|
||||
|
||||
inline Key Position::get_pawn_key() const {
|
||||
return pawnKey;
|
||||
return st->pawnKey;
|
||||
}
|
||||
|
||||
inline Key Position::get_material_key() const {
|
||||
return materialKey;
|
||||
return st->materialKey;
|
||||
}
|
||||
|
||||
inline Value Position::mg_pst(Color c, PieceType pt, Square s) const {
|
||||
return MgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
|
||||
template<Position::GamePhase Phase>
|
||||
inline Value Position::pst(Color c, PieceType pt, Square s) const {
|
||||
return (Phase == MidGame ? MgPieceSquareTable[piece_of_color_and_type(c, pt)][s]
|
||||
: EgPieceSquareTable[piece_of_color_and_type(c, pt)][s]);
|
||||
}
|
||||
|
||||
inline Value Position::mg_pst_delta(Move m) const {
|
||||
@@ -616,16 +645,12 @@ inline Value Position::mg_pst_delta(Move m) const {
|
||||
-MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
|
||||
}
|
||||
|
||||
inline Value Position::eg_pst(Color c, PieceType pt, Square s) const {
|
||||
return EgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
|
||||
}
|
||||
|
||||
inline Value Position::mg_value() const {
|
||||
return mgValue;
|
||||
return st->mgValue;
|
||||
}
|
||||
|
||||
inline Value Position::eg_value() const {
|
||||
return egValue;
|
||||
return st->egValue;
|
||||
}
|
||||
|
||||
inline Value Position::non_pawn_material(Color c) const {
|
||||
@@ -634,15 +659,9 @@ inline Value Position::non_pawn_material(Color c) const {
|
||||
|
||||
inline Phase Position::game_phase() const {
|
||||
|
||||
// The purpose of the Value(325) terms below is to make sure the difference
|
||||
// between MidgameLimit and EndgameLimit is a power of 2, which should make
|
||||
// the division at the end of the function a bit faster.
|
||||
static const Value MidgameLimit = 2 * QueenValueMidgame
|
||||
+ 2 * RookValueMidgame
|
||||
+ 6 * BishopValueMidgame
|
||||
+ Value(325);
|
||||
|
||||
static const Value EndgameLimit = 4 * RookValueMidgame - Value(325);
|
||||
// Values modified by Joona Kiiski
|
||||
static const Value MidgameLimit = Value(15713);
|
||||
static const Value EndgameLimit = Value(4428);
|
||||
|
||||
Value npm = non_pawn_material(WHITE) + non_pawn_material(BLACK);
|
||||
|
||||
@@ -657,34 +676,34 @@ inline Phase Position::game_phase() const {
|
||||
inline bool Position::move_is_deep_pawn_push(Move m) const {
|
||||
|
||||
Color c = side_to_move();
|
||||
return piece_on(move_from(m)) == pawn_of_color(c)
|
||||
return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
|
||||
&& relative_rank(c, move_to(m)) > RANK_4;
|
||||
}
|
||||
|
||||
inline bool Position::move_is_pawn_push_to_7th(Move m) const {
|
||||
|
||||
Color c = side_to_move();
|
||||
return piece_on(move_from(m)) == pawn_of_color(c)
|
||||
return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
|
||||
&& relative_rank(c, move_to(m)) == RANK_7;
|
||||
}
|
||||
|
||||
inline bool Position::move_is_passed_pawn_push(Move m) const {
|
||||
|
||||
Color c = side_to_move();
|
||||
return piece_on(move_from(m)) == pawn_of_color(c)
|
||||
return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
|
||||
&& pawn_is_passed(c, move_to(m));
|
||||
}
|
||||
|
||||
inline bool Position::move_was_passed_pawn_push(Move m) const {
|
||||
|
||||
Color c = opposite_color(side_to_move());
|
||||
return piece_on(move_to(m)) == pawn_of_color(c)
|
||||
return piece_on(move_to(m)) == piece_of_color_and_type(c, PAWN)
|
||||
&& pawn_is_passed(c, move_to(m));
|
||||
}
|
||||
|
||||
inline int Position::rule_50_counter() const {
|
||||
|
||||
return rule50;
|
||||
return st->rule50;
|
||||
}
|
||||
|
||||
inline bool Position::opposite_colored_bishops() const {
|
||||
@@ -699,5 +718,14 @@ inline bool Position::has_pawn_on_7th(Color c) const {
|
||||
return pawns(c) & relative_rank_bb(c, RANK_7);
|
||||
}
|
||||
|
||||
inline bool Position::move_is_capture(Move m) const {
|
||||
|
||||
// Move must not be MOVE_NONE !
|
||||
|
||||
return ( !square_is_empty(move_to(m))
|
||||
&& (color_of_piece_on(move_to(m)) != color_of_piece_on(move_from(m)))
|
||||
)
|
||||
|| move_is_ep(m);
|
||||
}
|
||||
|
||||
#endif // !defined(POSITION_H_INCLUDED)
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "position.h"
|
||||
#include "value.h"
|
||||
|
||||
|
||||
|
||||
29
src/san.cpp
29
src/san.cpp
@@ -70,23 +70,28 @@ const std::string move_to_san(const Position& pos, Move m) {
|
||||
assert(pos.is_ok());
|
||||
assert(move_is_ok(m));
|
||||
|
||||
Square from, to;
|
||||
PieceType pt;
|
||||
|
||||
from = move_from(m);
|
||||
to = move_to(m);
|
||||
pt = type_of_piece(pos.piece_on(move_from(m)));
|
||||
|
||||
std::string san = "";
|
||||
|
||||
if (m == MOVE_NONE)
|
||||
return "(none)";
|
||||
else if (m == MOVE_NULL)
|
||||
return "(null)";
|
||||
else if (move_is_long_castle(m))
|
||||
else if (move_is_long_castle(m) || (int(to - from) == -2 && pt == KING))
|
||||
san = "O-O-O";
|
||||
else if (move_is_short_castle(m))
|
||||
else if (move_is_short_castle(m) || (int(to - from) == 2 && pt == KING))
|
||||
san = "O-O";
|
||||
else
|
||||
{
|
||||
Piece pc = pos.piece_on(move_from(m));
|
||||
if (type_of_piece(pc) != PAWN)
|
||||
if (pt != PAWN)
|
||||
{
|
||||
san += piece_type_to_char(type_of_piece(pc), true);
|
||||
Square from = move_from(m);
|
||||
san += piece_type_to_char(pt, true);
|
||||
switch (move_ambiguity(pos, m)) {
|
||||
case AMBIGUITY_NONE:
|
||||
break;
|
||||
@@ -105,7 +110,7 @@ const std::string move_to_san(const Position& pos, Move m) {
|
||||
}
|
||||
if (pos.move_is_capture(m))
|
||||
{
|
||||
if (type_of_piece(pc) == PAWN)
|
||||
if (pt == PAWN)
|
||||
san += file_to_char(square_file(move_from(m)));
|
||||
san += "x";
|
||||
}
|
||||
@@ -119,9 +124,9 @@ const std::string move_to_san(const Position& pos, Move m) {
|
||||
// Is the move check? We don't use pos.move_is_check(m) here, because
|
||||
// Position::move_is_check doesn't detect all checks (not castling moves,
|
||||
// promotions and en passant captures).
|
||||
UndoInfo u;
|
||||
StateInfo st;
|
||||
Position p(pos);
|
||||
p.do_move(m, u);
|
||||
p.do_move(m, st);
|
||||
if (p.is_check())
|
||||
san += p.is_mate()? "#" : "+";
|
||||
|
||||
@@ -290,7 +295,7 @@ Move move_from_san(const Position& pos, const std::string& movestr) {
|
||||
|
||||
const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
|
||||
|
||||
UndoInfo u;
|
||||
StateInfo st;
|
||||
std::stringstream s;
|
||||
std::string moveStr;
|
||||
size_t length = 0;
|
||||
@@ -309,9 +314,9 @@ const std::string line_to_san(const Position& pos, Move line[], int startColumn,
|
||||
s << moveStr << ' ';
|
||||
|
||||
if (line[i] == MOVE_NULL)
|
||||
p.do_null_move(u);
|
||||
p.do_null_move(st);
|
||||
else
|
||||
p.do_move(line[i], u);
|
||||
p.do_move(line[i], st);
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
|
||||
572
src/search.cpp
572
src/search.cpp
File diff suppressed because it is too large
Load Diff
@@ -55,11 +55,13 @@ const int KILLER_MAX = 2;
|
||||
struct SearchStack {
|
||||
Move pv[PLY_MAX];
|
||||
Move currentMove;
|
||||
Value currentMoveCaptureValue;
|
||||
Move mateKiller;
|
||||
Move threatMove;
|
||||
Move killers[KILLER_MAX];
|
||||
Depth reduction;
|
||||
|
||||
void init(int ply);
|
||||
void initKillers();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008 Marco Costalba
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
#include "square.h"
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
|
||||
/// Translating files, ranks and squares to/from characters and strings:
|
||||
|
||||
File file_from_char(char c) {
|
||||
return File(c - 'a') + FILE_A;
|
||||
}
|
||||
|
||||
|
||||
char file_to_char(File f) {
|
||||
return char(f - FILE_A) + 'a';
|
||||
}
|
||||
|
||||
|
||||
Rank rank_from_char(char c) {
|
||||
return Rank(c - '1') + RANK_1;
|
||||
}
|
||||
|
||||
|
||||
char rank_to_char(Rank r) {
|
||||
return char(r - RANK_1) + '1';
|
||||
}
|
||||
|
||||
|
||||
Square square_from_string(const std::string &str) {
|
||||
return make_square(file_from_char(str[0]), rank_from_char(str[1]));
|
||||
}
|
||||
|
||||
|
||||
const std::string square_to_string(Square s) {
|
||||
std::string str;
|
||||
str += file_to_char(square_file(s));
|
||||
str += rank_to_char(square_rank(s));
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/// file_is_ok(), rank_is_ok() and square_is_ok(), for debugging:
|
||||
|
||||
bool file_is_ok(File f) {
|
||||
return f >= FILE_A && f <= FILE_H;
|
||||
}
|
||||
|
||||
|
||||
bool rank_is_ok(Rank r) {
|
||||
return r >= RANK_1 && r <= RANK_8;
|
||||
}
|
||||
|
||||
|
||||
bool square_is_ok(Square s) {
|
||||
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
|
||||
}
|
||||
46
src/square.h
46
src/square.h
@@ -159,21 +159,43 @@ inline int square_distance(Square s1, Square s2) {
|
||||
return Max(file_distance(s1, s2), rank_distance(s1, s2));
|
||||
}
|
||||
|
||||
inline File file_from_char(char c) {
|
||||
return File(c - 'a') + FILE_A;
|
||||
}
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
inline char file_to_char(File f) {
|
||||
return char(f - FILE_A + int('a'));
|
||||
}
|
||||
|
||||
extern File file_from_char(char c);
|
||||
extern char file_to_char(File f);
|
||||
extern Rank rank_from_char(char c);
|
||||
extern char rank_to_char(Rank r);
|
||||
extern Square square_from_string(const std::string &str);
|
||||
extern const std::string square_to_string(Square s);
|
||||
inline Rank rank_from_char(char c) {
|
||||
return Rank(c - '1') + RANK_1;
|
||||
}
|
||||
|
||||
extern bool file_is_ok(File f);
|
||||
extern bool rank_is_ok(Rank r);
|
||||
extern bool square_is_ok(Square s);
|
||||
inline char rank_to_char(Rank r) {
|
||||
return char(r - RANK_1 + int('1'));
|
||||
}
|
||||
|
||||
inline Square square_from_string(const std::string& str) {
|
||||
return make_square(file_from_char(str[0]), rank_from_char(str[1]));
|
||||
}
|
||||
|
||||
inline const std::string square_to_string(Square s) {
|
||||
std::string str;
|
||||
str += file_to_char(square_file(s));
|
||||
str += rank_to_char(square_rank(s));
|
||||
return str;
|
||||
}
|
||||
|
||||
inline bool file_is_ok(File f) {
|
||||
return f >= FILE_A && f <= FILE_H;
|
||||
}
|
||||
|
||||
inline bool rank_is_ok(Rank r) {
|
||||
return r >= RANK_1 && r <= RANK_8;
|
||||
}
|
||||
|
||||
inline bool square_is_ok(Square s) {
|
||||
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
|
||||
}
|
||||
|
||||
#endif // !defined(SQUARE_H_INCLUDED)
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
(c) Copyright 1992 Eric Backus
|
||||
|
||||
This software may be used freely so long as this copyright notice is
|
||||
left intact. There is no warrantee on this software.
|
||||
*/
|
||||
#include <windows.h>
|
||||
#include <time.h>
|
||||
#include "dos.h"
|
||||
|
||||
|
||||
int gettimeofday(struct timeval * tp, struct timezone * tzp)
|
||||
{
|
||||
SYSTEMTIME systime;
|
||||
|
||||
if (tp) {
|
||||
struct tm tmrec;
|
||||
time_t theTime = time(NULL);
|
||||
|
||||
|
||||
tmrec = *localtime(&theTime);
|
||||
tp->tv_sec = mktime(&tmrec);
|
||||
GetLocalTime(&systime); /* system time */
|
||||
|
||||
tp->tv_usec = systime.wMilliseconds * 1000;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
131
src/tt.cpp
131
src/tt.cpp
@@ -33,20 +33,13 @@
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// Constructor
|
||||
TranspositionTable::TranspositionTable() {
|
||||
|
||||
TranspositionTable::TranspositionTable(unsigned mbSize) {
|
||||
|
||||
size = 0;
|
||||
generation = 0;
|
||||
writes = 0;
|
||||
size = writes = 0;
|
||||
entries = 0;
|
||||
set_size(mbSize);
|
||||
generation = 0;
|
||||
}
|
||||
|
||||
|
||||
/// Destructor
|
||||
|
||||
TranspositionTable::~TranspositionTable() {
|
||||
|
||||
delete [] entries;
|
||||
@@ -58,33 +51,33 @@ TranspositionTable::~TranspositionTable() {
|
||||
|
||||
void TranspositionTable::set_size(unsigned mbSize) {
|
||||
|
||||
assert(mbSize >= 4 && mbSize <= 1024);
|
||||
assert(mbSize >= 4 && mbSize <= 4096);
|
||||
|
||||
unsigned newSize = 1024;
|
||||
|
||||
// We store a cluster of 4 TTEntry for each position and newSize is
|
||||
// the maximum number of storable positions
|
||||
for ( ; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20); newSize *= 2);
|
||||
newSize /= 2;
|
||||
while ((2 * newSize) * 4 * (sizeof(TTEntry)) <= (mbSize << 20))
|
||||
newSize *= 2;
|
||||
|
||||
if (newSize != size)
|
||||
{
|
||||
size = newSize;
|
||||
delete [] entries;
|
||||
entries = new TTEntry[size * 4];
|
||||
if (!entries)
|
||||
{
|
||||
std::cerr << "Failed to allocate " << mbSize
|
||||
<< " MB for transposition table."
|
||||
<< std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
clear();
|
||||
size = newSize;
|
||||
delete [] entries;
|
||||
entries = new TTEntry[size * 4];
|
||||
if (!entries)
|
||||
{
|
||||
std::cerr << "Failed to allocate " << mbSize
|
||||
<< " MB for transposition table." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::clear overwrites the entire transposition table
|
||||
/// with zeroes. It is called whenever the table is resized, or when the
|
||||
/// with zeroes. It is called whenever the table is resized, or when the
|
||||
/// user asks the program to clear the table (from the UCI interface).
|
||||
/// Perhaps we should also clear it when the "ucinewgame" command is recieved?
|
||||
|
||||
@@ -96,39 +89,44 @@ void TranspositionTable::clear() {
|
||||
|
||||
/// TranspositionTable::store writes a new entry containing a position,
|
||||
/// a value, a value type, a search depth, and a best move to the
|
||||
/// transposition table. The transposition table is organized in clusters
|
||||
/// of four TTEntry objects, and when a new entry is written, it replaces
|
||||
/// the least valuable of the four entries in a cluster. A TTEntry t1 is
|
||||
/// transposition table. Transposition table is organized in clusters of
|
||||
/// four TTEntry objects, and when a new entry is written, it replaces
|
||||
/// the least valuable of the four entries in a cluster. A TTEntry t1 is
|
||||
/// considered to be more valuable than a TTEntry t2 if t1 is from the
|
||||
/// current search and t2 is from a previous search, or if the depth of t1
|
||||
/// is bigger than the depth of t2.
|
||||
/// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
|
||||
/// never replaces another entry for the same position.
|
||||
|
||||
void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d, Move m) {
|
||||
|
||||
void TranspositionTable::store(const Position &pos, Value v, Depth d,
|
||||
Move m, ValueType type) {
|
||||
TTEntry *tte, *replace;
|
||||
|
||||
tte = replace = first_entry(pos);
|
||||
tte = replace = first_entry(p);
|
||||
for (int i = 0; i < 4; i++, tte++)
|
||||
{
|
||||
if (!tte->key() || tte->key() == pos.get_key()) // empty or overwrite old
|
||||
{
|
||||
if (m == MOVE_NONE)
|
||||
m = tte->move();
|
||||
if (!tte->key() || tte->key() == p.get_key()) // empty or overwrite old
|
||||
{
|
||||
// Do not overwrite when new type is VALUE_TYPE_EVAL
|
||||
if (tte->key() && t == VALUE_TYPE_EVAL)
|
||||
return;
|
||||
|
||||
*tte = TTEntry(pos.get_key(), v, type, d, m, generation);
|
||||
return;
|
||||
}
|
||||
else if (i == 0) // replace would be a no-op in this common case
|
||||
continue;
|
||||
if (m == MOVE_NONE)
|
||||
m = tte->move();
|
||||
|
||||
int c1 = (replace->generation() == generation ? 2 : 0);
|
||||
int c2 = (tte->generation() == generation ? -2 : 0);
|
||||
int c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
||||
*tte = TTEntry(p.get_key(), v, t, d, m, generation);
|
||||
return;
|
||||
}
|
||||
else if (i == 0) // replace would be a no-op in this common case
|
||||
continue;
|
||||
|
||||
if (c1 + c2 + c3 > 0)
|
||||
replace = tte;
|
||||
int c1 = (replace->generation() == generation ? 2 : 0);
|
||||
int c2 = (tte->generation() == generation ? -2 : 0);
|
||||
int c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
||||
|
||||
if (c1 + c2 + c3 > 0)
|
||||
replace = tte;
|
||||
}
|
||||
*replace = TTEntry(pos.get_key(), v, type, d, m, generation);
|
||||
*replace = TTEntry(p.get_key(), v, t, d, m, generation);
|
||||
writes++;
|
||||
}
|
||||
|
||||
@@ -137,15 +135,14 @@ void TranspositionTable::store(const Position &pos, Value v, Depth d,
|
||||
/// transposition table. Returns a pointer to the TTEntry or NULL
|
||||
/// if position is not found.
|
||||
|
||||
const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
|
||||
TTEntry* TranspositionTable::retrieve(const Position& pos) const {
|
||||
|
||||
TTEntry *tte = first_entry(pos);
|
||||
|
||||
for (int i = 0; i < 4; i++, tte++)
|
||||
{
|
||||
if (tte->key() == pos.get_key())
|
||||
return tte;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -153,13 +150,13 @@ const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
|
||||
/// TranspositionTable::first_entry returns a pointer to the first
|
||||
/// entry of a cluster given a position.
|
||||
|
||||
inline TTEntry* TranspositionTable::first_entry(const Position &pos) const {
|
||||
inline TTEntry* TranspositionTable::first_entry(const Position& pos) const {
|
||||
|
||||
return entries + (int(pos.get_key() & (size - 1)) << 2);
|
||||
}
|
||||
|
||||
/// TranspositionTable::new_search() is called at the beginning of every new
|
||||
/// search. It increments the "generation" variable, which is used to
|
||||
/// search. It increments the "generation" variable, which is used to
|
||||
/// distinguish transposition table entries from previous searches from
|
||||
/// entries from the current search.
|
||||
|
||||
@@ -171,19 +168,19 @@ void TranspositionTable::new_search() {
|
||||
|
||||
|
||||
/// TranspositionTable::insert_pv() is called at the end of a search
|
||||
/// iteration, and inserts the PV back into the PV. This makes sure the
|
||||
/// old PV moves are searched first, even if the old TT entries have been
|
||||
/// overwritten.
|
||||
/// iteration, and inserts the PV back into the PV. This makes sure
|
||||
/// the old PV moves are searched first, even if the old TT entries
|
||||
/// have been overwritten.
|
||||
|
||||
void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
|
||||
void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
|
||||
|
||||
UndoInfo u;
|
||||
StateInfo st;
|
||||
Position p(pos);
|
||||
|
||||
for (int i = 0; pv[i] != MOVE_NONE; i++)
|
||||
{
|
||||
store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE);
|
||||
p.do_move(pv[i], u);
|
||||
store(p, VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
|
||||
p.do_move(pv[i], st);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,22 +189,8 @@ void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
|
||||
/// entries which have received at least one write during the current search.
|
||||
/// It is used to display the "info hashfull ..." information in UCI.
|
||||
|
||||
int TranspositionTable::full() {
|
||||
int TranspositionTable::full() const {
|
||||
|
||||
double N = double(size) * 4.0;
|
||||
return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
|
||||
}
|
||||
|
||||
|
||||
/// Constructors
|
||||
|
||||
TTEntry::TTEntry() {
|
||||
}
|
||||
|
||||
TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
|
||||
int generation) :
|
||||
key_ (k), data((m & 0x7FFFF) | (t << 20) | (generation << 23)),
|
||||
value_(v), depth_(int16_t(d)) {}
|
||||
|
||||
|
||||
|
||||
|
||||
51
src/tt.h
51
src/tt.h
@@ -34,18 +34,35 @@
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// The TTEntry class is the class of transposition table entries.
|
||||
/// The TTEntry class is the class of transposition table entries
|
||||
///
|
||||
/// A TTEntry needs 128 bits to be stored
|
||||
///
|
||||
/// bit 0-63: key
|
||||
/// bit 64-95: data
|
||||
/// bit 96-111: value
|
||||
/// bit 112-127: depth
|
||||
///
|
||||
/// the 32 bits of the data field are so defined
|
||||
///
|
||||
/// bit 0-16: move
|
||||
/// bit 17-19: not used
|
||||
/// bit 20-22: value type
|
||||
/// bit 23-31: generation
|
||||
|
||||
class TTEntry {
|
||||
|
||||
public:
|
||||
TTEntry();
|
||||
TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation);
|
||||
TTEntry() {}
|
||||
TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation)
|
||||
: key_ (k), data((m & 0x1FFFF) | (t << 20) | (generation << 23)),
|
||||
value_(int16_t(v)), depth_(int16_t(d)) {}
|
||||
|
||||
Key key() const { return key_; }
|
||||
Depth depth() const { return Depth(depth_); }
|
||||
Move move() const { return Move(data & 0x7FFFF); }
|
||||
Move move() const { return Move(data & 0x1FFFF); }
|
||||
Value value() const { return Value(value_); }
|
||||
ValueType type() const { return ValueType((data >> 20) & 3); }
|
||||
ValueType type() const { return ValueType((data >> 20) & 7); }
|
||||
int generation() const { return (data >> 23); }
|
||||
|
||||
private:
|
||||
@@ -62,32 +79,22 @@ private:
|
||||
class TranspositionTable {
|
||||
|
||||
public:
|
||||
TranspositionTable(unsigned mbSize);
|
||||
TranspositionTable();
|
||||
~TranspositionTable();
|
||||
void set_size(unsigned mbSize);
|
||||
void clear();
|
||||
void store(const Position &pos, Value v, Depth d, Move m, ValueType type);
|
||||
const TTEntry* retrieve(const Position &pos) const;
|
||||
void store(const Position& pos, Value v, ValueType type, Depth d, Move m);
|
||||
TTEntry* retrieve(const Position& pos) const;
|
||||
void new_search();
|
||||
void insert_pv(const Position &pos, Move pv[]);
|
||||
int full();
|
||||
void insert_pv(const Position& pos, Move pv[]);
|
||||
int full() const;
|
||||
|
||||
private:
|
||||
inline TTEntry* first_entry(const Position &pos) const;
|
||||
inline TTEntry* first_entry(const Position& pos) const;
|
||||
|
||||
unsigned size;
|
||||
int writes;
|
||||
unsigned size, writes;
|
||||
TTEntry* entries;
|
||||
uint8_t generation;
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
////
|
||||
|
||||
// Default transposition table size, in megabytes:
|
||||
const int TTDefaultSize = 32;
|
||||
|
||||
|
||||
#endif // !defined(TT_H_INCLUDED)
|
||||
|
||||
22
src/uci.cpp
22
src/uci.cpp
@@ -22,6 +22,7 @@
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -169,7 +170,7 @@ namespace {
|
||||
}
|
||||
else if (token == "key")
|
||||
{
|
||||
std::cout << "key: " << RootPosition.get_key()
|
||||
std::cout << "key: " << std::hex << RootPosition.get_key()
|
||||
<< " material key: " << RootPosition.get_material_key()
|
||||
<< " pawn key: " << RootPosition.get_pawn_key()
|
||||
<< std::endl;
|
||||
@@ -219,15 +220,18 @@ namespace {
|
||||
if (token == "moves")
|
||||
{
|
||||
Move move;
|
||||
UndoInfo u;
|
||||
StateInfo st;
|
||||
while (!uip.eof())
|
||||
{
|
||||
uip >> token;
|
||||
move = move_from_string(RootPosition, token);
|
||||
RootPosition.do_move(move, u);
|
||||
RootPosition.do_move(move, st);
|
||||
if (RootPosition.rule_50_counter() == 0)
|
||||
RootPosition.reset_game_ply();
|
||||
}
|
||||
// Our StateInfo st is about going out of scope,
|
||||
// so save its content before they disappear.
|
||||
RootPosition.setStartState(st);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,11 +251,13 @@ namespace {
|
||||
if (token == "name")
|
||||
{
|
||||
uip >> name;
|
||||
uip >> token;
|
||||
while (!uip.eof() && token != "value")
|
||||
while (!uip.eof())
|
||||
{
|
||||
name += (" " + token);
|
||||
uip >> token;
|
||||
uip >> token;
|
||||
if (token == "value")
|
||||
break;
|
||||
|
||||
name += (" " + token);
|
||||
}
|
||||
if (token == "value")
|
||||
{
|
||||
@@ -321,6 +327,8 @@ namespace {
|
||||
if (moveTime)
|
||||
infinite = true; // HACK
|
||||
|
||||
assert(RootPosition.is_ok());
|
||||
|
||||
think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time,
|
||||
inc, movesToGo, depth, nodes, moveTime, searchMoves);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
@@ -36,7 +38,7 @@
|
||||
//// Variables
|
||||
////
|
||||
|
||||
bool Chess960 = false;
|
||||
bool Chess960;
|
||||
|
||||
|
||||
////
|
||||
@@ -57,90 +59,92 @@ namespace {
|
||||
|
||||
std::string name, defaultValue, currentValue;
|
||||
OptionType type;
|
||||
size_t idx;
|
||||
int minValue, maxValue;
|
||||
ComboValues comboValues;
|
||||
|
||||
Option(const char* name, const char* defaultValue, OptionType = STRING);
|
||||
Option(const char* name, bool defaultValue, OptionType = CHECK);
|
||||
Option(const char* name, int defaultValue, int minValue, int maxValue);
|
||||
Option();
|
||||
Option(const char* defaultValue, OptionType = STRING);
|
||||
Option(bool defaultValue, OptionType = CHECK);
|
||||
Option(int defaultValue, int minValue, int maxValue);
|
||||
|
||||
bool operator<(const Option& o) const { return this->idx < o.idx; }
|
||||
};
|
||||
|
||||
typedef std::vector<Option> Options;
|
||||
typedef std::map<std::string, Option> Options;
|
||||
|
||||
///
|
||||
/// Constants
|
||||
///
|
||||
|
||||
// load_defaults populates the options vector with the hard
|
||||
// load_defaults populates the options map with the hard
|
||||
// coded names and default values.
|
||||
|
||||
void load_defaults(Options& o) {
|
||||
|
||||
o.push_back(Option("Use Search Log", false));
|
||||
o.push_back(Option("Search Log Filename", "SearchLog.txt"));
|
||||
o.push_back(Option("Book File", "book.bin"));
|
||||
o.push_back(Option("Mobility (Middle Game)", 100, 0, 200));
|
||||
o.push_back(Option("Mobility (Endgame)", 100, 0, 200));
|
||||
o.push_back(Option("Pawn Structure (Middle Game)", 100, 0, 200));
|
||||
o.push_back(Option("Pawn Structure (Endgame)", 100, 0, 200));
|
||||
o.push_back(Option("Passed Pawns (Middle Game)", 100, 0, 200));
|
||||
o.push_back(Option("Passed Pawns (Endgame)", 100, 0, 200));
|
||||
o.push_back(Option("Space", 100, 0, 200));
|
||||
o.push_back(Option("Aggressiveness", 100, 0, 200));
|
||||
o.push_back(Option("Cowardice", 100, 0, 200));
|
||||
o.push_back(Option("King Safety Curve", "Quadratic", COMBO));
|
||||
o["Use Search Log"] = Option(false);
|
||||
o["Search Log Filename"] = Option("SearchLog.txt");
|
||||
o["Book File"] = Option("book.bin");
|
||||
o["Mobility (Middle Game)"] = Option(100, 0, 200);
|
||||
o["Mobility (Endgame)"] = Option(100, 0, 200);
|
||||
o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
|
||||
o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
|
||||
o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
|
||||
o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
|
||||
o["Space"] = Option(100, 0, 200);
|
||||
o["Aggressiveness"] = Option(100, 0, 200);
|
||||
o["Cowardice"] = Option(100, 0, 200);
|
||||
o["King Safety Curve"] = Option("Quadratic", COMBO);
|
||||
|
||||
o.back().comboValues.push_back("Quadratic");
|
||||
o.back().comboValues.push_back("Linear"); /*, "From File"*/
|
||||
o["King Safety Curve"].comboValues.push_back("Quadratic");
|
||||
o["King Safety Curve"].comboValues.push_back("Linear"); /*, "From File"*/
|
||||
|
||||
o.push_back(Option("King Safety Coefficient", 40, 1, 100));
|
||||
o.push_back(Option("King Safety X Intercept", 0, 0, 20));
|
||||
o.push_back(Option("King Safety Max Slope", 30, 10, 100));
|
||||
o.push_back(Option("King Safety Max Value", 500, 100, 1000));
|
||||
o.push_back(Option("Queen Contact Check Bonus", 3, 0, 8));
|
||||
o.push_back(Option("Queen Check Bonus", 2, 0, 4));
|
||||
o.push_back(Option("Rook Check Bonus", 1, 0, 4));
|
||||
o.push_back(Option("Bishop Check Bonus", 1, 0, 4));
|
||||
o.push_back(Option("Knight Check Bonus", 1, 0, 4));
|
||||
o.push_back(Option("Discovered Check Bonus", 3, 0, 8));
|
||||
o.push_back(Option("Mate Threat Bonus", 3, 0, 8));
|
||||
o.push_back(Option("Check Extension (PV nodes)", 2, 0, 2));
|
||||
o.push_back(Option("Check Extension (non-PV nodes)", 1, 0, 2));
|
||||
o.push_back(Option("Single Reply Extension (PV nodes)", 2, 0, 2));
|
||||
o.push_back(Option("Single Reply Extension (non-PV nodes)", 2, 0, 2));
|
||||
o.push_back(Option("Mate Threat Extension (PV nodes)", 0, 0, 2));
|
||||
o.push_back(Option("Mate Threat Extension (non-PV nodes)", 0, 0, 2));
|
||||
o.push_back(Option("Pawn Push to 7th Extension (PV nodes)", 1, 0, 2));
|
||||
o.push_back(Option("Pawn Push to 7th Extension (non-PV nodes)", 1, 0, 2));
|
||||
o.push_back(Option("Passed Pawn Extension (PV nodes)", 1, 0, 2));
|
||||
o.push_back(Option("Passed Pawn Extension (non-PV nodes)", 0, 0, 2));
|
||||
o.push_back(Option("Pawn Endgame Extension (PV nodes)", 2, 0, 2));
|
||||
o.push_back(Option("Pawn Endgame Extension (non-PV nodes)", 2, 0, 2));
|
||||
o.push_back(Option("Full Depth Moves (PV nodes)", 14, 1, 100));
|
||||
o.push_back(Option("Full Depth Moves (non-PV nodes)", 3, 1, 100));
|
||||
o.push_back(Option("Threat Depth", 5, 0, 100));
|
||||
o.push_back(Option("Selective Plies", 7, 0, 10));
|
||||
o.push_back(Option("Null driven IID", false));
|
||||
o.push_back(Option("Futility Pruning (Main Search)", true));
|
||||
o.push_back(Option("Futility Pruning (Quiescence Search)", true));
|
||||
o.push_back(Option("Futility Margin (Quiescence Search)", 50, 0, 1000));
|
||||
o.push_back(Option("Futility Margin Scale Factor (Main Search)", 100, 0, 1000));
|
||||
o.push_back(Option("Maximum Razoring Depth", 3, 0, 4));
|
||||
o.push_back(Option("Razoring Margin", 300, 150, 600));
|
||||
o.push_back(Option("LSN filtering", false));
|
||||
o.push_back(Option("LSN Time Margin (sec)", 4, 1, 10));
|
||||
o.push_back(Option("LSN Value Margin", 200, 100, 600));
|
||||
o.push_back(Option("Randomness", 0, 0, 10));
|
||||
o.push_back(Option("Minimum Split Depth", 4, 4, 7));
|
||||
o.push_back(Option("Maximum Number of Threads per Split Point", 5, 4, 8));
|
||||
o.push_back(Option("Threads", 1, 1, 8));
|
||||
o.push_back(Option("Hash", 32, 4, 4096));
|
||||
o.push_back(Option("Clear Hash", false, BUTTON));
|
||||
o.push_back(Option("Ponder", true));
|
||||
o.push_back(Option("OwnBook", true));
|
||||
o.push_back(Option("MultiPV", 1, 1, 500));
|
||||
o.push_back(Option("UCI_ShowCurrLine", false));
|
||||
o.push_back(Option("UCI_Chess960", false));
|
||||
o["King Safety Coefficient"] = Option(40, 1, 100);
|
||||
o["King Safety X Intercept"] = Option(0, 0, 20);
|
||||
o["King Safety Max Slope"] = Option(30, 10, 100);
|
||||
o["King Safety Max Value"] = Option(500, 100, 1000);
|
||||
o["Queen Contact Check Bonus"] = Option(3, 0, 8);
|
||||
o["Queen Check Bonus"] = Option(2, 0, 4);
|
||||
o["Rook Check Bonus"] = Option(1, 0, 4);
|
||||
o["Bishop Check Bonus"] = Option(1, 0, 4);
|
||||
o["Knight Check Bonus"] = Option(1, 0, 4);
|
||||
o["Discovered Check Bonus"] = Option(3, 0, 8);
|
||||
o["Mate Threat Bonus"] = Option(3, 0, 8);
|
||||
o["Check Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
|
||||
o["Single Reply Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
o["Single Reply Extension (non-PV nodes)"] = Option(2, 0, 2);
|
||||
o["Mate Threat Extension (PV nodes)"] = Option(0, 0, 2);
|
||||
o["Mate Threat Extension (non-PV nodes)"] = Option(0, 0, 2);
|
||||
o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
|
||||
o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
|
||||
o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
|
||||
o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
|
||||
o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
|
||||
o["Full Depth Moves (PV nodes)"] = Option(14, 1, 100);
|
||||
o["Full Depth Moves (non-PV nodes)"] = Option(3, 1, 100);
|
||||
o["Threat Depth"] = Option(5, 0, 100);
|
||||
o["Futility Pruning (Main Search)"] = Option(true);
|
||||
o["Futility Pruning (Quiescence Search)"] = Option(true);
|
||||
o["LSN filtering"] = Option(false);
|
||||
o["LSN Time Margin (sec)"] = Option(4, 1, 10);
|
||||
o["LSN Value Margin"] = Option(200, 100, 600);
|
||||
o["Randomness"] = Option(0, 0, 10);
|
||||
o["Minimum Split Depth"] = Option(4, 4, 7);
|
||||
o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
|
||||
o["Threads"] = Option(1, 1, 8);
|
||||
o["Hash"] = Option(32, 4, 4096);
|
||||
o["Clear Hash"] = Option(false, BUTTON);
|
||||
o["Ponder"] = Option(true);
|
||||
o["OwnBook"] = Option(true);
|
||||
o["MultiPV"] = Option(1, 1, 500);
|
||||
o["UCI_ShowCurrLine"] = Option(false);
|
||||
o["UCI_Chess960"] = Option(false);
|
||||
|
||||
// Any option should know its name so to be easily printed
|
||||
for (Options::iterator it = o.begin(); it != o.end(); ++it)
|
||||
it->second.name = it->first;
|
||||
}
|
||||
|
||||
///
|
||||
@@ -149,9 +153,6 @@ namespace {
|
||||
|
||||
Options options;
|
||||
|
||||
// Local functions
|
||||
Options::iterator option_with_name(const std::string& optionName);
|
||||
|
||||
// stringify converts a value of type T to a std::string
|
||||
template<typename T>
|
||||
std::string stringify(const T& v) {
|
||||
@@ -161,13 +162,6 @@ namespace {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// We want conversion from a bool value to be "true" or "false",
|
||||
// not "1" or "0", so add a specialization for bool type.
|
||||
template<>
|
||||
std::string stringify<bool>(const bool& v) {
|
||||
|
||||
return v ? "true" : "false";
|
||||
}
|
||||
|
||||
// get_option_value implements the various get_option_value_<type>
|
||||
// functions defined later, because only the option value
|
||||
@@ -177,30 +171,14 @@ namespace {
|
||||
T get_option_value(const std::string& optionName) {
|
||||
|
||||
T ret = T();
|
||||
Options::iterator it = option_with_name(optionName);
|
||||
|
||||
if (it != options.end())
|
||||
{
|
||||
std::istringstream ss(it->currentValue);
|
||||
ss >> ret;
|
||||
} else
|
||||
assert(false);
|
||||
if (options.find(optionName) == options.end())
|
||||
return ret;
|
||||
|
||||
std::istringstream ss(options[optionName].currentValue);
|
||||
ss >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Unfortunatly we need a specialization to convert "false" and "true"
|
||||
// to proper bool values. The culprit is that we use a non standard way
|
||||
// to store a bool value in a string, in particular we use "false" and
|
||||
// "true" instead of "0" and "1" due to how UCI protocol works.
|
||||
|
||||
template<>
|
||||
bool get_option_value<bool>(const std::string& optionName) {
|
||||
|
||||
Options::iterator it = option_with_name(optionName);
|
||||
|
||||
return it != options.end() && it->currentValue == "true";
|
||||
}
|
||||
}
|
||||
|
||||
////
|
||||
@@ -219,24 +197,19 @@ void init_uci_options() {
|
||||
// According to Ken Dail's tests, Glaurung plays much better with 7 than
|
||||
// with 8 threads. This is weird, but it is probably difficult to find out
|
||||
// why before I have a 8-core computer to experiment with myself.
|
||||
Options::iterator it = option_with_name("Threads");
|
||||
assert(options.find("Threads") != options.end());
|
||||
assert(options.find("Minimum Split Depth") != options.end());
|
||||
|
||||
assert(it != options.end());
|
||||
|
||||
it->defaultValue = stringify(Min(cpu_count(), 7));
|
||||
it->currentValue = stringify(Min(cpu_count(), 7));
|
||||
options["Threads"].defaultValue = stringify(Min(cpu_count(), 7));
|
||||
options["Threads"].currentValue = stringify(Min(cpu_count(), 7));
|
||||
|
||||
// Increase the minimum split depth when the number of CPUs is big.
|
||||
// It would probably be better to let this depend on the number of threads
|
||||
// instead.
|
||||
if(cpu_count() > 4)
|
||||
if (cpu_count() > 4)
|
||||
{
|
||||
it = option_with_name("Minimum Split Depth");
|
||||
|
||||
assert(it != options.end());
|
||||
|
||||
it->defaultValue = "6";
|
||||
it->currentValue = "6";
|
||||
options["Minimum Split Depth"].defaultValue = "6";
|
||||
options["Minimum Split Depth"].currentValue = "6";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,28 +223,40 @@ void print_uci_options() {
|
||||
"spin", "combo", "check", "string", "button"
|
||||
};
|
||||
|
||||
for (Options::iterator it = options.begin(); it != options.end(); ++it)
|
||||
{
|
||||
std::cout << "option name " << it->name
|
||||
<< " type " << optionTypeName[it->type];
|
||||
// Build up a vector out of the options map and sort it according to idx
|
||||
// field, that is the chronological insertion order in options map.
|
||||
std::vector<Option> vec;
|
||||
for (Options::const_iterator it = options.begin(); it != options.end(); ++it)
|
||||
vec.push_back(it->second);
|
||||
|
||||
if (it->type != BUTTON)
|
||||
{
|
||||
std::sort(vec.begin(), vec.end());
|
||||
|
||||
for (std::vector<Option>::const_iterator it = vec.begin(); it != vec.end(); ++it)
|
||||
{
|
||||
std::cout << "\noption name " << it->name
|
||||
<< " type " << optionTypeName[it->type];
|
||||
|
||||
if (it->type == BUTTON)
|
||||
continue;
|
||||
|
||||
if (it->type == CHECK)
|
||||
std::cout << " default " << (it->defaultValue == "1" ? "true" : "false");
|
||||
else
|
||||
std::cout << " default " << it->defaultValue;
|
||||
|
||||
if (it->type == SPIN)
|
||||
std::cout << " min " << it->minValue
|
||||
<< " max " << it->maxValue;
|
||||
if (it->type == SPIN)
|
||||
std::cout << " min " << it->minValue
|
||||
<< " max " << it->maxValue;
|
||||
|
||||
else if (it->type == COMBO)
|
||||
for(ComboValues::iterator itc = it->comboValues.begin();
|
||||
itc != it->comboValues.end(); ++itc)
|
||||
std::cout << " var " << *itc;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
else if (it->type == COMBO)
|
||||
for (ComboValues::const_iterator itc = it->comboValues.begin();
|
||||
itc != it->comboValues.end(); ++itc)
|
||||
std::cout << " var " << *itc;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
/// get_option_value_bool() returns the current value of a UCI parameter of
|
||||
/// type "check".
|
||||
|
||||
@@ -293,7 +278,7 @@ int get_option_value_int(const std::string& optionName) {
|
||||
|
||||
|
||||
/// get_option_value_string() returns the current value of a UCI parameter as
|
||||
/// a string. It is used with parameters of type "combo" and "string".
|
||||
/// a string. It is used with parameters of type "combo" and "string".
|
||||
|
||||
const std::string get_option_value_string(const std::string& optionName) {
|
||||
|
||||
@@ -301,32 +286,23 @@ const std::string get_option_value_string(const std::string& optionName) {
|
||||
}
|
||||
|
||||
|
||||
/// button_was_pressed() tests whether a UCI parameter of type "button" has
|
||||
/// been selected since the last time the function was called.
|
||||
|
||||
bool button_was_pressed(const std::string& buttonName) {
|
||||
|
||||
if (get_option_value<bool>(buttonName))
|
||||
{
|
||||
set_option_value(buttonName, "false");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// set_option_value() inserts a new value for a UCI parameter. Note that
|
||||
/// set_option_value() inserts a new value for a UCI parameter. Note that
|
||||
/// the function does not check that the new value is legal for the given
|
||||
/// parameter: This is assumed to be the responsibility of the GUI.
|
||||
/// parameter: This is assumed to be the responsibility of the GUI.
|
||||
|
||||
void set_option_value(const std::string& optionName,
|
||||
const std::string& newValue) {
|
||||
|
||||
Options::iterator it = option_with_name(optionName);
|
||||
// UCI protocol uses "true" and "false" instead of "1" and "0", so convert
|
||||
// newValue according to standard C++ convention before to store it.
|
||||
std::string v(newValue);
|
||||
if (v == "true")
|
||||
v = "1";
|
||||
else if (v == "false")
|
||||
v = "0";
|
||||
|
||||
if (it != options.end())
|
||||
it->currentValue = newValue;
|
||||
if (options.find(optionName) != options.end())
|
||||
options[optionName].currentValue = v;
|
||||
else
|
||||
std::cout << "No such option: " << optionName << std::endl;
|
||||
}
|
||||
@@ -341,29 +317,33 @@ void push_button(const std::string& buttonName) {
|
||||
}
|
||||
|
||||
|
||||
/// button_was_pressed() tests whether a UCI parameter of type "button" has
|
||||
/// been selected since the last time the function was called, in this case
|
||||
/// it also resets the button.
|
||||
|
||||
bool button_was_pressed(const std::string& buttonName) {
|
||||
|
||||
if (!get_option_value<bool>(buttonName))
|
||||
return false;
|
||||
|
||||
set_option_value(buttonName, "false");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
// Define constructors of Option class.
|
||||
// Define constructors of Option class.
|
||||
|
||||
Option::Option(const char* nm, const char* def, OptionType t)
|
||||
: name(nm), defaultValue(def), currentValue(def), type(t), minValue(0), maxValue(0) {}
|
||||
Option::Option() {} // To allow insertion in a std::map
|
||||
|
||||
Option::Option(const char* nm, bool def, OptionType t)
|
||||
: name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(t), minValue(0), maxValue(0) {}
|
||||
Option::Option(const char* def, OptionType t)
|
||||
: defaultValue(def), currentValue(def), type(t), idx(options.size()), minValue(0), maxValue(0) {}
|
||||
|
||||
Option::Option(const char* nm, int def, int minv, int maxv)
|
||||
: name(nm), defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), minValue(minv), maxValue(maxv) {}
|
||||
Option::Option(bool def, OptionType t)
|
||||
: defaultValue(stringify(def)), currentValue(stringify(def)), type(t), idx(options.size()), minValue(0), maxValue(0) {}
|
||||
|
||||
// option_with_name() tries to find a UCI option with a given
|
||||
// name. It returns an iterator to the UCI option or to options.end(),
|
||||
// depending on whether an option with the given name exists.
|
||||
Option::Option(int def, int minv, int maxv)
|
||||
: defaultValue(stringify(def)), currentValue(stringify(def)), type(SPIN), idx(options.size()), minValue(minv), maxValue(maxv) {}
|
||||
|
||||
Options::iterator option_with_name(const std::string& optionName) {
|
||||
|
||||
for (Options::iterator it = options.begin(); it != options.end(); ++it)
|
||||
if (it->name == optionName)
|
||||
return it;
|
||||
|
||||
return options.end();
|
||||
}
|
||||
}
|
||||
|
||||
29
src/value.h
29
src/value.h
@@ -36,7 +36,8 @@ enum ValueType {
|
||||
VALUE_TYPE_NONE = 0,
|
||||
VALUE_TYPE_UPPER = 1, // Upper bound
|
||||
VALUE_TYPE_LOWER = 2, // Lower bound
|
||||
VALUE_TYPE_EXACT = 3 // Exact score
|
||||
VALUE_TYPE_EXACT = 3, // Exact score
|
||||
VALUE_TYPE_EVAL = 4 // Evaluation cache
|
||||
};
|
||||
|
||||
|
||||
@@ -58,17 +59,19 @@ enum Value {
|
||||
/// Important: If the material values are changed, one must also
|
||||
/// adjust the piece square tables, and the method game_phase() in the
|
||||
/// Position class!
|
||||
///
|
||||
/// Values modified by Joona Kiiski
|
||||
|
||||
const Value PawnValueMidgame = Value(0xCC);
|
||||
const Value PawnValueEndgame = Value(0x100);
|
||||
const Value KnightValueMidgame = Value(0x340);
|
||||
const Value KnightValueEndgame = Value(0x340);
|
||||
const Value BishopValueMidgame = Value(0x340);
|
||||
const Value BishopValueEndgame = Value(0x340);
|
||||
const Value RookValueMidgame = Value(0x505);
|
||||
const Value RookValueEndgame = Value(0x505);
|
||||
const Value QueenValueMidgame = Value(0xA00);
|
||||
const Value QueenValueEndgame = Value(0xA00);
|
||||
const Value PawnValueMidgame = Value(0x0CC);
|
||||
const Value PawnValueEndgame = Value(0x101);
|
||||
const Value KnightValueMidgame = Value(0x332);
|
||||
const Value KnightValueEndgame = Value(0x34E);
|
||||
const Value BishopValueMidgame = Value(0x345);
|
||||
const Value BishopValueEndgame = Value(0x356);
|
||||
const Value RookValueMidgame = Value(0x4F8);
|
||||
const Value RookValueEndgame = Value(0x500);
|
||||
const Value QueenValueMidgame = Value(0x9D5);
|
||||
const Value QueenValueEndgame = Value(0x9FB);
|
||||
|
||||
const Value PieceValueMidgame[17] = {
|
||||
Value(0),
|
||||
@@ -92,8 +95,8 @@ const Value PieceValueEndgame[17] = {
|
||||
|
||||
/// Bonus for having the side to move
|
||||
|
||||
const Value TempoValueMidgame = Value(50);
|
||||
const Value TempoValueEndgame = Value(20);
|
||||
const Value TempoValueMidgame = Value(48);
|
||||
const Value TempoValueEndgame = Value(21);
|
||||
|
||||
|
||||
////
|
||||
|
||||
Reference in New Issue
Block a user