forked from dylhunn/dragontoothmg
/
apply.go
180 lines (169 loc) · 6.67 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
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) {
// 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
// If it is any kind of capture or pawn move, reset halfmove clock.
if IsCapture(m, b) || pieceType == Pawn {
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()
}
if b.canCastleQueenside() {
b.flipQueensideCastle()
}
}
// Rook moves strip castling rights
if pieceType == Rook {
if b.canCastleKingside() && (fromBitboard&onlyFile[7] != 0) &&
fromBitboard&ourStartingRankBb != 0 { // king's rook
b.flipKingsideCastle()
} else if b.canCastleQueenside() && (fromBitboard&onlyFile[0] != 0) &&
fromBitboard&ourStartingRankBb != 0 { // queen's rook
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
if pieceType == Pawn && m.To() == oldEpCaptureSquare && oldEpCaptureSquare != 0 {
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()
} else if m.To()%8 == 0 && toBitboard&oppStartingRankBb != 0 && b.oppCanCastleQueenside() { // queen rooks
b.flipOppQueensideCastle()
}
}
// 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)
}
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
}