-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_move.go
388 lines (347 loc) · 9.19 KB
/
make_move.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package main
import "errors"
// ClearPiece clears the piece when making a move
func ClearPiece(sq int, pos *Board) error {
targetPceNum := -1
if !SqOnBoard(sq) {
return errors.New("Square to be cleared is not on board")
}
err := pos.Check()
if err != nil {
return err
}
piece := pos.Pieces[sq]
if !PieceValid(piece) {
return errors.New("Piece to be cleared is invalid")
}
colour := PieceCol[piece]
if !SideValid(colour) {
return errors.New("Side is invalid")
}
// Hash out hash key of piece from PosKey
pos.PosKey ^= PieceKeys[piece][sq]
// Clear values for the piece from arrays representing the board
pos.Pieces[sq] = Empty
pos.Material[colour] -= PieceVal[piece]
if PieceBig[piece] == True {
pos.BigPce[colour]--
if PieceMaj[piece] == True {
pos.MajPce[colour]--
} else {
pos.MinPce[colour]--
}
} else {
pawnCol := Bitboard(pos.Pawns[colour])
(&pawnCol).Clear(SQ64(sq))
pos.Pawns[colour] = uint64(pawnCol)
pawnBoth := Bitboard(pos.Pawns[Both])
(&pawnBoth).Clear(SQ64(sq))
pos.Pawns[Both] = uint64(pawnBoth)
}
// Remove the piece from PceNum and PList of the board
for index := 0; index < pos.PceNum[piece]; index++ {
if pos.PList[piece][index] == sq {
targetPceNum = index
break
}
}
if targetPceNum == -1 {
return errors.New("Piece not found in piece list, invalid board structure")
}
if targetPceNum < 0 || targetPceNum >= 10 {
return errors.New("Index of piece invalid, invalid board structure")
}
pos.PceNum[piece]--
pos.PList[piece][targetPceNum] = pos.PList[piece][pos.PceNum[piece]]
return nil
}
// AddPiece adds the piece when making a move
func AddPiece(sq, piece int, pos *Board) error {
if !SqOnBoard(sq) {
return errors.New("Square where piece is to be added is not on board")
}
err := pos.Check()
if err != nil {
return err
}
if !PieceValid(piece) {
return errors.New("Piece to be added is invalid")
}
colour := PieceCol[piece]
if !SideValid(colour) {
return errors.New("Side is invalid")
}
// Hash in hash key of piece into PosKey
pos.PosKey ^= PieceKeys[piece][sq]
// Add values for the piece to arrays representing the board
pos.Pieces[sq] = piece
pos.Material[colour] += PieceVal[piece]
if PieceBig[piece] == True {
pos.BigPce[colour]++
if PieceMaj[piece] == True {
pos.MajPce[colour]++
} else {
pos.MinPce[colour]++
}
} else {
pawnCol := Bitboard(pos.Pawns[colour])
(&pawnCol).Set(SQ64(sq))
pos.Pawns[colour] = uint64(pawnCol)
pawnBoth := Bitboard(pos.Pawns[Both])
(&pawnBoth).Set(SQ64(sq))
pos.Pawns[Both] = uint64(pawnBoth)
}
pos.PList[piece][pos.PceNum[piece]] = sq
pos.PceNum[piece]++
return nil
}
// MovePiece moves the piece from "from" sq to "to" sq on the board
func MovePiece(from, to int, pos *Board) error {
if !SqOnBoard(from) {
return errors.New("Square from where piece is to be moved is not on board")
}
if !SqOnBoard(to) {
return errors.New("Square where piece is to be moved is not on board")
}
piece := pos.Pieces[from]
if !PieceValid(piece) {
return errors.New("Piece to be added is invalid")
}
colour := PieceCol[piece]
if !SideValid(colour) {
return errors.New("Side is invalid")
}
targetPceNum := false
// Hash out hash key of piece at "from" square from PosKey
pos.PosKey ^= PieceKeys[piece][from]
pos.Pieces[from] = Empty
// Hash in hash key of piece at "to" square into PosKey
pos.PosKey ^= PieceKeys[piece][to]
pos.Pieces[to] = piece
// Material and big pieces remain the same
// Set and clear the pawn bitboard for "to" and "from" squares respectively
if PieceBig[piece] == False {
pawnCol := Bitboard(pos.Pawns[colour])
(&pawnCol).Clear(SQ64(from))
(&pawnCol).Set(SQ64(to))
pos.Pawns[colour] = uint64(pawnCol)
pawnBoth := Bitboard(pos.Pawns[Both])
(&pawnBoth).Clear(SQ64(from))
(&pawnBoth).Set(SQ64(to))
pos.Pawns[Both] = uint64(pawnBoth)
}
// Move the piece in Plist
for index := 0; index < pos.PceNum[piece]; index++ {
if pos.PList[piece][index] == from {
pos.PList[piece][index] = to
targetPceNum = true
break
}
}
if targetPceNum == false {
return errors.New("Piece not found in piece list, invalid board structure")
}
return nil
}
func MakeMove(move int, pos *Board) (bool, error) {
err := pos.Check()
if err != nil {
return false, err
}
fromSq := FromSq(move)
toSq := ToSq(move)
side := pos.Side
capturedPiece := Captured(move)
promotedPiece := Promoted(move)
if !SqOnBoard(fromSq) {
return false, errors.New("Square from where piece is to be moved is not on board")
}
if !SqOnBoard(toSq) {
return false, errors.New("Square where piece is to be moved is not on board")
}
if !SideValid(side) {
return false, errors.New("Side is invalid")
}
if !PieceValid(pos.Pieces[fromSq]) {
return false, errors.New("Piece to be added is invalid")
}
pos.History[pos.HisPly].PosKey = pos.PosKey
if (move & MFlagEP) > 0 {
if side == White {
ClearPiece(toSq-10, pos)
} else {
ClearPiece(toSq+10, pos)
}
} else if (move & MFlagCA) > 0 {
switch toSq {
case C1:
MovePiece(A1, D1, pos)
break
case C8:
MovePiece(A8, D8, pos)
break
case G1:
MovePiece(H1, F1, pos)
break
case G8:
MovePiece(H8, F8, pos)
break
default:
return false, errors.New("Error in move")
}
}
if pos.EnPas != NoSq {
// Hash out hash key of enPas piece from PosKey
pos.PosKey ^= PieceKeys[Empty][pos.EnPas]
}
// Hash out hash key of castling from PosKey
pos.PosKey ^= CastleKeys[pos.CastlePerm]
pos.History[pos.HisPly].Move = move
pos.History[pos.HisPly].FiftyMove = pos.FiftyMove
pos.History[pos.HisPly].EnPas = pos.EnPas
pos.History[pos.HisPly].CastlePerm = pos.CastlePerm
pos.CastlePerm &= CastlePerm[fromSq]
pos.CastlePerm &= CastlePerm[toSq]
pos.EnPas = NoSq
pos.FiftyMove++
pos.HisPly++
pos.Ply++
// Hash in hash key of castling into PosKey
pos.PosKey ^= CastleKeys[pos.CastlePerm]
if capturedPiece != Empty {
if !PieceValid(capturedPiece) {
return false, errors.New("Piece to be captured is invalid")
}
ClearPiece(toSq, pos)
pos.FiftyMove = 0
}
if PiecePawn[pos.Pieces[fromSq]] == True {
pos.FiftyMove = 0
if (move & MFlagPS) > 0 {
if side == White {
pos.EnPas = fromSq + 10
if RanksBrd[pos.EnPas] != Rank3 {
return false, errors.New("Rank of enPas should be rank 3")
}
} else {
pos.EnPas = fromSq - 10
if RanksBrd[pos.EnPas] != Rank6 {
return false, errors.New("Rank of enPas should be rank 6")
}
}
// Hash in hash key of enPas piece into PosKey
pos.PosKey ^= PieceKeys[Empty][pos.EnPas]
}
}
err = MovePiece(fromSq, toSq, pos)
if err != nil {
return false, err
}
if promotedPiece != Empty {
if !PieceValid(promotedPiece) || PiecePawn[promotedPiece] == True {
return false, errors.New("Piece to be promoted is invalid")
}
ClearPiece(toSq, pos)
AddPiece(toSq, promotedPiece, pos)
}
if PieceKing[pos.Pieces[toSq]] == True {
pos.KingSq[pos.Side] = toSq
}
pos.Side ^= 1
pos.PosKey ^= SideKey
err = pos.Check()
if err != nil {
return false, err
}
if attacked, _ := SqAttacked(pos.KingSq[side], pos.Side, pos); attacked {
TakeMove(pos)
return false, errors.New("Move taken back as King will be attacked by the move")
}
return true, nil
}
func TakeMove(pos *Board) error {
err := pos.Check()
if err != nil {
return err
}
pos.HisPly--
pos.Ply--
move := pos.History[pos.HisPly].Move
fromSq := FromSq(move)
toSq := ToSq(move)
capturedPiece := Captured(move)
promotedPiece := Promoted(move)
if !SqOnBoard(fromSq) {
return errors.New("Square from where piece is to be moved is not on board")
}
if !SqOnBoard(toSq) {
return errors.New("Square where piece is to be moved is not on board")
}
if pos.EnPas != NoSq {
// Hash out hash key of enPas piece from PosKey
pos.PosKey ^= PieceKeys[Empty][pos.EnPas]
}
// Hash out hash key of castling from PosKey
pos.PosKey ^= CastleKeys[pos.CastlePerm]
pos.CastlePerm = pos.History[pos.HisPly].CastlePerm
pos.FiftyMove = pos.History[pos.HisPly].FiftyMove
pos.EnPas = pos.History[pos.HisPly].EnPas
if pos.EnPas != NoSq {
// Hash in hash key of enPas piece into PosKey
pos.PosKey ^= PieceKeys[Empty][pos.EnPas]
}
// Hash in hash key of castling into PosKey
pos.PosKey ^= CastleKeys[pos.CastlePerm]
pos.Side ^= 1
pos.PosKey ^= SideKey
if (move & MFlagEP) > 0 {
if pos.Side == White {
AddPiece(toSq-10, Bp, pos)
} else {
AddPiece(toSq+10, Wp, pos)
}
} else if (move & MFlagCA) > 0 {
switch toSq {
case C1:
MovePiece(D1, A1, pos)
break
case C8:
MovePiece(D8, A8, pos)
break
case G1:
MovePiece(F1, H1, pos)
break
case G8:
MovePiece(F8, H8, pos)
break
default:
return errors.New("Error in move")
}
}
MovePiece(toSq, fromSq, pos)
if PieceKing[pos.Pieces[fromSq]] == True {
pos.KingSq[pos.Side] = fromSq
}
if capturedPiece != Empty {
if !PieceValid(capturedPiece) {
return errors.New("Piece to be captured is invalid")
}
AddPiece(toSq, capturedPiece, pos)
}
if promotedPiece != Empty {
if !PieceValid(promotedPiece) || PiecePawn[promotedPiece] == True {
return errors.New("Piece to be promoted is invalid")
}
ClearPiece(fromSq, pos)
if PieceCol[promotedPiece] == White {
AddPiece(fromSq, Wp, pos)
} else {
AddPiece(fromSq, Bp, pos)
}
}
err = pos.Check()
if err != nil {
return err
}
return nil
}