-
Notifications
You must be signed in to change notification settings - Fork 14
/
position_moves.go
258 lines (211 loc) · 6.81 KB
/
position_moves.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
// 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 (p *Position) movePiece(piece Piece, from, to int) *Position {
p.pieces[from], p.pieces[to] = 0, piece
p.outposts[piece] ^= bit[from] | bit[to]
p.outposts[piece.color()] ^= bit[from] | bit[to]
// Update position's hash values.
random := piece.polyglot(from) ^ piece.polyglot(to)
p.id ^= random
if piece.isPawn() {
p.pawnId ^= random
}
// Update positional score.
p.tally.sub(pst[piece][from]).add(pst[piece][to])
return p
}
func (p *Position) promotePawn(pawn Piece, from, to int, promo Piece) *Position {
p.pieces[from], p.pieces[to] = 0, promo
p.outposts[pawn] ^= bit[from]
p.outposts[promo] ^= bit[to]
p.outposts[pawn.color()] ^= bit[from] | bit[to]
// Update position's hash values and material balance.
random := pawn.polyglot(from)
p.id ^= random ^ promo.polyglot(to)
p.pawnId ^= random
p.balance += materialBalance[promo] - materialBalance[pawn]
// Update positional score.
p.tally.sub(pst[pawn][from]).add(pst[promo][to])
return p
}
func (p *Position) capturePiece(capture Piece, from, to int) *Position {
p.outposts[capture] ^= bit[to]
p.outposts[capture.color()] ^= bit[to]
// Update position's hash values and material balance.
random := capture.polyglot(to)
p.id ^= random
if capture.isPawn() {
p.pawnId ^= random
}
p.balance -= materialBalance[capture]
// Update positional score.
p.tally.sub(pst[capture][to])
return p
}
func (p *Position) captureEnpassant(capture Piece, from, to int) *Position {
enpassant := to - up[capture.color()^1]
p.pieces[enpassant] = 0
p.outposts[capture] ^= bit[enpassant]
p.outposts[capture.color()] ^= bit[enpassant]
// Update position's hash values and material balance.
random := capture.polyglot(enpassant)
p.id ^= random
p.pawnId ^= random
p.balance -= materialBalance[capture]
// Update positional score.
p.tally.sub(pst[capture][enpassant])
return p
}
func (p *Position) makeMove(move Move) *Position {
color := move.color()
from, to, piece, capture := move.split()
// Copy over the contents of previous tree node to the current one.
node++
tree[node] = *p // => tree[node] = tree[node - 1]
pp := &tree[node]
pp.enpassant, pp.reversible = 0, true
if capture != 0 && (to == 0 || to != p.enpassant) {
pp.count50, pp.reversible = 0, false
pp.capturePiece(capture, from, to)
}
if piece.isPawn() {
pp.count50, pp.reversible = 0, false
if to != 0 && to == p.enpassant {
pp.captureEnpassant(pawn(color^1), from, to)
pp.id ^= hashEnpassant[p.enpassant & 7] // p.enpassant column.
}
if promo := move.promo(); promo != 0 {
pp.promotePawn(piece, from, to, promo)
} else {
pp.movePiece(piece, from, to)
if move.isEnpassant() {
pp.enpassant = from + up[color] // Save the en-passant square.
pp.id ^= hashEnpassant[pp.enpassant & 7]
}
}
} else if piece.isKing() {
pp.movePiece(piece, from, to)
pp.count50++
pp.king[color] = to
if move.isCastle() {
pp.reversible = false
if to == from + 2 {
pp.movePiece(rook(color), to + 1, to - 1)
} else if to == from - 2 {
pp.movePiece(rook(color), to - 2, to + 1)
}
}
} else {
pp.movePiece(piece, from, to)
pp.count50++
}
// Set up the board bitmask, update castle rights, finish off incremental
// hash value, and flip the color.
pp.board = pp.outposts[White] | pp.outposts[Black]
pp.castles &= castleRights[from] & castleRights[to]
pp.id ^= hashCastle[p.castles] ^ hashCastle[pp.castles]
pp.id ^= polyglotRandomWhite
pp.color ^= 1 // <-- Flip side to move.
pp.score = Unknown
return &tree[node] // pp
}
// Makes "null" move by copying over previous node position (i.e. preserving all pieces
// intact) and flipping the color.
func (p *Position) makeNullMove() *Position {
node++
tree[node] = *p // => tree[node] = tree[node - 1]
pp := &tree[node]
// Flipping side to move obviously invalidates the enpassant square.
if pp.enpassant != 0 {
pp.id ^= hashEnpassant[pp.enpassant & 7]
pp.enpassant = 0
}
pp.id ^= polyglotRandomWhite
pp.color ^= 1 // <-- Flip side to move.
pp.count50++
return &tree[node] // pp
}
// Restores previous position effectively taking back the last move made.
func (p *Position) undoLastMove() *Position {
if node > 0 {
node--
}
return &tree[node]
}
func (p *Position) isInCheck(color int) bool {
return p.isAttacked(color ^ 1, p.king[color])
}
func (p *Position) isNull() bool {
return node > 0 && tree[node].board == tree[node-1].board
}
func (p *Position) fifty() bool {
return p.count50 >= 100
}
func (p *Position) repetition() bool {
if !p.reversible || node < 1 {
return false
}
for previous := node - 1; previous >= 0; previous-- {
if !tree[previous].reversible {
return false
}
if tree[previous].id == p.id {
return true
}
}
return false
}
func (p *Position) thirdRepetition() bool {
if !p.reversible || node < 4 {
return false
}
for previous, repetitions := node - 2, 1; previous >= 0; previous -= 2 {
if !tree[previous].reversible || !tree[previous + 1].reversible {
return false
}
if tree[previous].id == p.id {
repetitions++
if repetitions == 3 {
return true
}
}
}
return false
}
// Returns a pair of booleans that indicate whether given side is allowed to
// castle kingside and queenside.
func (p *Position) canCastle(color int) (kingside, queenside bool) {
// Start off with simple checks.
kingside = (p.castles & castleKingside[color] != 0) && (gapKing[color] & p.board).empty()
queenside = (p.castles & castleQueenside[color] != 0) && (gapQueen[color] & p.board).empty()
// If it still looks like the castles are possible perform more expensive
// final check.
if kingside || queenside {
attacks := p.allAttacks(color^1)
kingside = kingside && (castleKing[color] & attacks).empty()
queenside = queenside && (castleQueen[color] & attacks).empty()
}
return kingside, queenside
}
// Returns a bitmask of all pinned pieces preventing a check for the king on
// given square. The color of the pieces match the color of the king.
func (p *Position) pins(square int) (bitmask Bitmask) {
our := p.pieces[square].color()
their := our^1
attackers := (p.outposts[bishop(their)] | p.outposts[queen(their)]) & bishopMagicMoves[square][0]
attackers |= (p.outposts[rook(their)] | p.outposts[queen(their)]) & rookMagicMoves[square][0]
for bm := attackers; bm.any(); bm = bm.pop() {
attackSquare := bm.first()
blockers := maskBlock[square][attackSquare] & ^bit[attackSquare] & p.board
if blockers.single() {
bitmask |= blockers & p.outposts[our] // Only friendly pieces are pinned.
}
}
return bitmask
}