-
Notifications
You must be signed in to change notification settings - Fork 14
/
evaluate_endgame.go
295 lines (247 loc) · 8.14 KB
/
evaluate_endgame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright (c) 2014-2018 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.
package donna
func (e *Evaluation) evaluateEndgame() int {
score := e.material.endgame(e)
if e.position.color == Black {
return -score
}
return score
}
func (e *Evaluation) inspectEndgame() {
switch markdown := e.material.endgame(e); markdown {
case ExistingScore:
return
case DrawScore:
e.score = Score{0, 0}
case WhiteWinning:
e.score = Score{WhiteWinning, WhiteWinning}
case BlackWinning:
e.score = Score{-BlackWinning, -BlackWinning}
default:
mul, div := markdown >> 16, markdown & 0xFFFF
if div != 0 {
if mul != 0 {
e.score.endgame = e.score.endgame * mul
}
e.score.endgame /= div
}
}
}
// Packs fractional markdown value as expected by inspectEndgame().
func (e *Evaluation) fraction(mul, div int) int {
if mul == 1 {
return div
}
return (mul << 16) | div
}
func (e *Evaluation) strongerSide() int {
if e.score.endgame > 0 {
return White
}
return Black
}
// Known endgames where we calculate the exact score.
func (e *Evaluation) winAgainstBareKing() int { // STUB.
return e.score.blended(e.material.phase)
}
func (e *Evaluation) knightAndBishopVsBareKing() int { // STUB.
return e.score.blended(e.material.phase)
}
func (e *Evaluation) twoBishopsVsBareKing() int { // STUB.
return e.score.blended(e.material.phase)
}
func (e *Evaluation) kingAndPawnVsBareKing() int {
var color, wKing, bKing, wPawn int
stronger := e.strongerSide()
if stronger == White {
color = e.position.color
wKing = e.position.king[White]
bKing = e.position.king[Black]
wPawn = e.position.outposts[Pawn].last()
} else {
color = e.position.color ^ 1
wKing = 64 + ^e.position.king[Black]
bKing = 64 + ^e.position.king[White]
wPawn = 64 + ^e.position.outposts[BlackPawn].last()
}
index := color + (wKing << 1) + (bKing << 7) + ((wPawn - 8) << 13)
if (bitbase[index / 64] & bit[index & 0x3F]).empty() {
return DrawScore
}
if stronger == Black {
return BlackWinning
}
return WhiteWinning
}
// Lesser known endgames where we calculate endgame score markdown.
func (e *Evaluation) kingAndPawnsVsBareKing() int {
color := e.strongerSide()
pawns := e.position.outposts[pawn(color)]
row, col := coordinate(e.position.king[color^1])
// Pawns on A file with bare king opposing them.
if (pawns & ^maskFile[A1]).empty() && (pawns & ^maskInFront[color^1][row * 8]).empty() && col <= B1 {
return DrawScore
}
// Pawns on H file with bare king opposing them.
if (pawns & ^maskFile[H1]).empty() && (pawns & ^maskInFront[color^1][row * 8 + 7]).empty() && col >= G1 {
return DrawScore
}
return ExistingScore
}
// Bishop-only endgame: drop the score if we have opposite-colored bishops.
func (e *Evaluation) bishopsAndPawns() int {
if e.oppositeBishops() {
outposts := &e.position.outposts
if abs(outposts[Pawn].count() - outposts[BlackPawn].count()) == 1 {
return e.fraction(1, 8) // 1/8
}
return e.fraction(1, 2) // 1/2
}
return ExistingScore
}
// Single bishops plus some other pieces: drop the score if we have opposite-colored
// bishops but only if other minors/majors are balanced.
func (e *Evaluation) drawishBishops() int {
if e.oppositeBishops() {
outposts := &e.position.outposts
wN, bN := outposts[Knight].count(), outposts[BlackKnight].count()
wR, bR := outposts[Rook].count(), outposts[BlackRook].count()
extraPawns := abs(outposts[Pawn].count() - outposts[BlackPawn].count())
if wN == bN && wR == bR && extraPawns <= 2 {
return e.fraction(1, 4) // 1/4
}
}
return ExistingScore
}
func (e *Evaluation) kingAndPawnVsKingAndPawn() int {
if e.score.endgame == 0 {
return ExistingScore
}
p := e.position
unstoppable := func(color int, square int) bool {
if (p.outposts[color^1] & maskInFront[color][square]).empty() {
mask := Bitmask(0)
if p.color == color {
mask = maskSquare[color][square]
} else {
mask = maskSquareEx[color][square]
}
return (mask & p.outposts[king(color^1)]).empty()
}
return false
}
// Check if either side has unstoppable pawn.
white := unstoppable(White, p.outposts[pawn(White)].first())
black := unstoppable(Black, p.outposts[pawn(Black)].first())
if white {
e.score.endgame = WhiteWinning
}
if black {
e.score.endgame = BlackWinning
}
if white || black {
return ExistingScore
}
// Try to evaluate the endgame using KPK bitbase. If the opposite side is not loosing
// without the pawn it's unlikely the game is lost with the pawn present.
our := p.color
piece := pawn(our)
pawns := p.outposts[piece]
square := pawns.first()
if rank(our, pawns.first()) < A5H5 || (pawns & (maskFile[0] | maskFile[7])).any() {
// Temporarily remove opponent's pawn.
piece = pawn(our^1)
pawns = p.outposts[piece] // -> Save: opponent's pawn bitmask.
square = pawns.first() // -> Save: opponent's pawn square.
p.outposts[piece] = Bitmask(0)
p.pieces[square] = Piece(0)
// Temporarily adjust score so that when e.strongerSide() gets called
// by kingAndPawnVsBareKing() it returns side to move.
score := e.score.endgame // -> Save: endgame score.
e.score.endgame = let(our == White, 1, -1)
// When we're done restore original endgame score and opponent's pawn.
defer func() {
e.score.endgame = score // <- Restore: endgame score.
p.pieces[square] = piece // <- Restore: opponent's pawn square.
p.outposts[piece] = pawns // <- Restore: opponent's pawn bitmask.
}()
if e.kingAndPawnVsBareKing() == DrawScore {
return DrawScore
}
}
return ExistingScore
}
func (e *Evaluation) bishopAndPawnVsBareKing() int { // STUB.
return ExistingScore
}
func (e *Evaluation) rookAndPawnVsRook() int { // STUB.
return ExistingScore
}
func (e *Evaluation) queenVsRookAndPawns() int { // STUB.
return ExistingScore
}
// One side has 1 pawn and the other side has 1 or more pawns: reduce
// score if both sides have exactly 1 pawn.
func (e *Evaluation) lastPawnLeft() int {
outposts := &e.position.outposts
if outposts[Pawn].single() && outposts[BlackPawn].single() {
return e.fraction(3, 4) // 3/4
}
return ExistingScore
}
// One side has 0 pawn and the other side has 0 or more pawns.
func (e *Evaluation) noPawnsLeft() int {
color := e.strongerSide()
outposts := &e.position.outposts
whiteMinorOnly := outposts[Queen].empty() && outposts[Rook].empty() && (outposts[Bishop] | outposts[Knight]).single()
blackMinorOnly := outposts[BlackQueen].empty() && outposts[BlackRook].empty() && (outposts[BlackBishop] | outposts[BlackKnight]).single()
// Check for opposite bishops first.
if whiteMinorOnly && blackMinorOnly && outposts[Knight].empty() && outposts[BlackKnight].empty() && e.oppositeBishops() {
pawn := outposts[pawn(color)] // The passer.
king := outposts[king(color^1)] // Defending king.
path := maskInFront[color][pawn.first()] // Path in front of the passer.
safe := maskDark // Safe squares for king to block on.
if (outposts[bishop(color)] & maskDark).any() {
safe = ^maskDark
}
// Draw if king blocks the passer on safe square.
if (king & safe).any() && (king & path).any() {
return DrawScore
}
// Draw if bishop attacks a square in front of the passer.
if (e.position.bishopAttacks(color^1) & path).any() {
return DrawScore
}
}
if color == White && outposts[Pawn].empty() {
if whiteMinorOnly {
// There is a theoretical chance of winning if opponent's pawns are on
// edge files (ex. some puzzles).
if (outposts[BlackPawn] & (maskFile[0] | maskFile[7])).any() {
return e.fraction(1, 64) // 1/64
}
return DrawScore
} else if blackMinorOnly {
return e.fraction(1, 16) // 1/16
}
return e.fraction(3, 16) // 3/16
}
if color == Black && outposts[BlackPawn].empty() {
if blackMinorOnly {
if (outposts[Pawn] & (maskFile[0] | maskFile[7])).any() {
return e.fraction(1, 64) // 1/64
}
return DrawScore
} else if whiteMinorOnly {
return e.fraction(1, 16) // 1/16
}
return e.fraction(3, 16) // 3/16
}
return ExistingScore
}