-
Notifications
You must be signed in to change notification settings - Fork 7
/
apply.go
265 lines (246 loc) · 9.9 KB
/
apply.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
package dragontoothmg
// Applies a move to the board, and returns a function that can be used to unapply it.
// This function assumes that the given move is valid (i.e., is in the set of moves found by GenerateLegalMoves()).
// If the move is not valid, this function has undefined behavior.
func (b *Board) Apply(m Move) func() {
// Configure data about which pieces move
var ourBitboardPtr, oppBitboardPtr *Bitboards
var epDelta int8 // add this to the e.p. square to find the captured pawn
var oppStartingRankBb, ourStartingRankBb uint64 // the starting rank of out opponent's major pieces
// the constant that represents the index into pieceSquareZobristC for the pawn of our color
var ourPiecesPawnZobristIndex int
var oppPiecesPawnZobristIndex int
if b.Wtomove {
ourBitboardPtr = &(b.White)
oppBitboardPtr = &(b.Black)
epDelta = -8
oppStartingRankBb = onlyRank[7]
ourStartingRankBb = onlyRank[0]
ourPiecesPawnZobristIndex = 0
oppPiecesPawnZobristIndex = 6
} else {
ourBitboardPtr = &(b.Black)
oppBitboardPtr = &(b.White)
epDelta = 8
oppStartingRankBb = onlyRank[0]
ourStartingRankBb = onlyRank[7]
b.Fullmoveno++ // increment after black's move
ourPiecesPawnZobristIndex = 6
oppPiecesPawnZobristIndex = 0
}
fromBitboard := (uint64(1) << m.From())
toBitboard := (uint64(1) << m.To())
pieceType, pieceTypeBitboard := determinePieceType(ourBitboardPtr, fromBitboard)
castleStatus := 0
var oldRookLoc, newRookLoc uint8
var flippedKsCastle, flippedQsCastle, flippedOppKsCastle, flippedOppQsCastle bool
// If it is any kind of capture or pawn move, reset halfmove clock.
resetHalfmoveClockFrom := -1
if IsCapture(m, b) || pieceType == Pawn {
resetHalfmoveClockFrom = int(b.Halfmoveclock)
b.Halfmoveclock = 0 // reset halfmove clock
} else {
b.Halfmoveclock++
}
// King moves strip castling rights
if pieceType == King {
// TODO(dylhunn): do this without a branch
if m.To()-m.From() == 2 { // castle short
castleStatus = 1
oldRookLoc = m.To() + 1
newRookLoc = m.To() - 1
} else if int(m.To())-int(m.From()) == -2 { // castle long
castleStatus = -1
oldRookLoc = m.To() - 2
newRookLoc = m.To() + 1
}
// King moves always strip castling rights
if b.canCastleKingside() {
b.flipKingsideCastle()
flippedKsCastle = true
}
if b.canCastleQueenside() {
b.flipQueensideCastle()
flippedQsCastle = true
}
}
// Rook moves strip castling rights
if pieceType == Rook {
if b.canCastleKingside() && (fromBitboard&onlyFile[7] != 0) &&
fromBitboard&ourStartingRankBb != 0 { // king's rook
flippedKsCastle = true
b.flipKingsideCastle()
} else if b.canCastleQueenside() && (fromBitboard&onlyFile[0] != 0) &&
fromBitboard&ourStartingRankBb != 0 { // queen's rook
flippedQsCastle = true
b.flipQueensideCastle()
}
}
// Apply the castling rook movement
if castleStatus != 0 {
ourBitboardPtr.Rooks |= (uint64(1) << newRookLoc)
ourBitboardPtr.All |= (uint64(1) << newRookLoc)
ourBitboardPtr.Rooks &= ^(uint64(1) << oldRookLoc)
ourBitboardPtr.All &= ^(uint64(1) << oldRookLoc)
// Update rook location in hash
// (Rook - 1) assumes that "Nothing" precedes "Rook" in the Piece constants list
b.hash ^= pieceSquareZobristC[ourPiecesPawnZobristIndex+(Rook-1)][oldRookLoc]
b.hash ^= pieceSquareZobristC[ourPiecesPawnZobristIndex+(Rook-1)][newRookLoc]
}
// Is this an e.p. capture? Strip the opponent pawn and reset the e.p. square
oldEpCaptureSquare := b.enpassant
var actuallyPerformedEpCapture bool = false
if pieceType == Pawn && m.To() == oldEpCaptureSquare && oldEpCaptureSquare != 0 {
actuallyPerformedEpCapture = true
epOpponentPawnLocation := uint8(int8(oldEpCaptureSquare) + epDelta)
oppBitboardPtr.Pawns &= ^(uint64(1) << epOpponentPawnLocation)
oppBitboardPtr.All &= ^(uint64(1) << epOpponentPawnLocation)
// Remove the opponent pawn from the board hash.
b.hash ^= pieceSquareZobristC[oppPiecesPawnZobristIndex][epOpponentPawnLocation]
}
// Update the en passant square
if pieceType == Pawn && (int8(m.To())+2*epDelta == int8(m.From())) { // pawn double push
b.enpassant = uint8(int8(m.To()) + epDelta)
} else {
b.enpassant = 0
}
// Is this a promotion?
var destTypeBitboard *uint64
var promotedToPieceType Piece // if not promoted, same as pieceType
switch m.Promote() {
case Queen:
destTypeBitboard = &(ourBitboardPtr.Queens)
promotedToPieceType = Queen
case Knight:
destTypeBitboard = &(ourBitboardPtr.Knights)
promotedToPieceType = Knight
case Rook:
destTypeBitboard = &(ourBitboardPtr.Rooks)
promotedToPieceType = Rook
case Bishop:
destTypeBitboard = &(ourBitboardPtr.Bishops)
promotedToPieceType = Bishop
default:
destTypeBitboard = pieceTypeBitboard
promotedToPieceType = pieceType
}
// Apply the move
capturedPieceType, capturedBitboard := determinePieceType(oppBitboardPtr, toBitboard)
ourBitboardPtr.All &= ^fromBitboard // remove at "from"
ourBitboardPtr.All |= toBitboard // add at "to"
*pieceTypeBitboard &= ^fromBitboard // remove at "from"
*destTypeBitboard |= toBitboard // add at "to"
if capturedPieceType != Nothing { // This does not account for e.p. captures
*capturedBitboard &= ^toBitboard
oppBitboardPtr.All &= ^toBitboard
b.hash ^= pieceSquareZobristC[oppPiecesPawnZobristIndex+(int(capturedPieceType)-1)][m.To()] // remove the captured piece from the hash
}
b.hash ^= pieceSquareZobristC[(int(pieceType)-1)+ourPiecesPawnZobristIndex][m.From()] // remove piece at "from"
b.hash ^= pieceSquareZobristC[(int(promotedToPieceType)-1)+ourPiecesPawnZobristIndex][m.To()] // add piece at "to"
// If a rook was captured, it strips castling rights
if capturedPieceType == Rook {
if m.To()%8 == 7 && toBitboard&oppStartingRankBb != 0 && b.oppCanCastleKingside() { // captured king rook
b.flipOppKingsideCastle()
flippedOppKsCastle = true
} else if m.To()%8 == 0 && toBitboard&oppStartingRankBb != 0 && b.oppCanCastleQueenside() { // queen rooks
b.flipOppQueensideCastle()
flippedOppQsCastle = true
}
}
// flip the side to move in the hash
b.hash ^= whiteToMoveZobristC
b.Wtomove = !b.Wtomove
// remove the old en passant square from the hash, and add the new one
b.hash ^= uint64(oldEpCaptureSquare)
b.hash ^= uint64(b.enpassant)
// Return the unapply function (closure)
unapply := func() {
// Flip the player to move
b.hash ^= whiteToMoveZobristC
b.Wtomove = !b.Wtomove
// Restore the halfmove clock
if resetHalfmoveClockFrom == -1 {
b.Halfmoveclock--
} else {
b.Halfmoveclock = uint8(resetHalfmoveClockFrom)
}
// Unapply move
ourBitboardPtr.All &= ^toBitboard // remove at "to"
ourBitboardPtr.All |= fromBitboard // add at "from"
*destTypeBitboard &= ^toBitboard // remove at "to"
*pieceTypeBitboard |= fromBitboard // add at "from"
b.hash ^= pieceSquareZobristC[(int(promotedToPieceType)-1)+ourPiecesPawnZobristIndex][m.To()] // remove the piece at "to"
b.hash ^= pieceSquareZobristC[(int(pieceType)-1)+ourPiecesPawnZobristIndex][m.From()] // add the piece at "from"
// Restore captured piece (excluding e.p.)
if capturedPieceType != Nothing { // doesn't consider e.p. captures
*capturedBitboard |= toBitboard
oppBitboardPtr.All |= toBitboard
// restore the captured piece to the hash (excluding e.p.)
b.hash ^= pieceSquareZobristC[oppPiecesPawnZobristIndex+(int(capturedPieceType)-1)][m.To()]
}
// Restore rooks from castling move
if castleStatus != 0 {
ourBitboardPtr.Rooks &= ^(uint64(1) << newRookLoc)
ourBitboardPtr.All &= ^(uint64(1) << newRookLoc)
ourBitboardPtr.Rooks |= (uint64(1) << oldRookLoc)
ourBitboardPtr.All |= (uint64(1) << oldRookLoc)
// Revert castling rook move
b.hash ^= pieceSquareZobristC[ourPiecesPawnZobristIndex+(Rook-1)][oldRookLoc]
b.hash ^= pieceSquareZobristC[ourPiecesPawnZobristIndex+(Rook-1)][newRookLoc]
}
// Unapply en-passant square change, and capture if necessary
b.hash ^= uint64(b.enpassant) // undo the new en passant square from the hash
b.hash ^= uint64(oldEpCaptureSquare) // restore the old one to the hash
b.enpassant = oldEpCaptureSquare
if actuallyPerformedEpCapture {
epOpponentPawnLocation := uint8(int8(oldEpCaptureSquare) + epDelta)
oppBitboardPtr.Pawns |= (uint64(1) << epOpponentPawnLocation)
oppBitboardPtr.All |= (uint64(1) << epOpponentPawnLocation)
// Add the opponent pawn to the board hash.
b.hash ^= pieceSquareZobristC[oppPiecesPawnZobristIndex][epOpponentPawnLocation]
}
// Decrement move clock
if !b.Wtomove {
b.Fullmoveno-- // decrement after undoing black's move
}
// Restore castling flags
// Must update castling flags AFTER turn swap
if flippedKsCastle {
b.flipKingsideCastle()
}
if flippedQsCastle {
b.flipQueensideCastle()
}
if flippedOppKsCastle {
b.flipOppKingsideCastle()
}
if flippedOppQsCastle {
b.flipOppQueensideCastle()
}
}
return unapply
}
func determinePieceType(ourBitboardPtr *Bitboards, squareMask uint64) (Piece, *uint64) {
var pieceType Piece = Nothing
pieceTypeBitboard := &(ourBitboardPtr.All)
if squareMask&ourBitboardPtr.Pawns != 0 {
pieceType = Pawn
pieceTypeBitboard = &(ourBitboardPtr.Pawns)
} else if squareMask&ourBitboardPtr.Knights != 0 {
pieceType = Knight
pieceTypeBitboard = &(ourBitboardPtr.Knights)
} else if squareMask&ourBitboardPtr.Bishops != 0 {
pieceType = Bishop
pieceTypeBitboard = &(ourBitboardPtr.Bishops)
} else if squareMask&ourBitboardPtr.Rooks != 0 {
pieceType = Rook
pieceTypeBitboard = &(ourBitboardPtr.Rooks)
} else if squareMask&ourBitboardPtr.Queens != 0 {
pieceType = Queen
pieceTypeBitboard = &(ourBitboardPtr.Queens)
} else if squareMask&ourBitboardPtr.Kings != 0 {
pieceType = King
pieceTypeBitboard = &(ourBitboardPtr.Kings)
}
return pieceType, pieceTypeBitboard
}