-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.go
504 lines (432 loc) · 13.4 KB
/
position.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package chess
import (
"errors"
"strconv"
"strings"
)
// Position represents a specific position of a chess game, including piece
// placement in board, active en passant, player turn, move counter, etc.
type Position struct {
board Board
playerToMove Color
castlingRights CastlingRights
enPassantSq *Square
halfmoveClock uint8
fullmoveCounter uint
isChecked bool
captures []Piece // Only used via API. Perft ignores this.
}
// Note: enPassantSq is not cloned, as it's not needed.
func (p *Position) clone() Position {
return Position{
board: p.board,
playerToMove: p.playerToMove,
castlingRights: p.castlingRights.clone(),
enPassantSq: nil,
halfmoveClock: p.halfmoveClock,
fullmoveCounter: p.fullmoveCounter,
isChecked: false,
captures: p.captures,
}
}
// CastlingRights represents the position's current castling rights,
// of both players.
type CastlingRights struct {
queenSide map[Color]bool
kingSide map[Color]bool
}
func (cr *CastlingRights) clone() CastlingRights {
return CastlingRights{
queenSide: map[Color]bool{
Color_White: cr.queenSide[Color_White],
Color_Black: cr.queenSide[Color_Black],
},
kingSide: map[Color]bool{
Color_White: cr.kingSide[Color_White],
Color_Black: cr.kingSide[Color_Black],
},
}
}
// CastlingRights returns the position's current castling rights,
// of both players.
func (p Position) CastlingRights() CastlingRights {
return p.castlingRights
}
// QueenSide returns whether the passed color (player/side) has
// castling rights on queenside or not.
func (cr CastlingRights) QueenSide(color Color) bool {
return cr.queenSide[color]
}
// KingSide returns whether the passed color (player/side) has
// castling rights on kingside or not.
func (cr CastlingRights) KingSide(color Color) bool {
return cr.kingSide[color]
}
// HalfmoveClock returns the position's halfmove clock.
func (p Position) HalfmoveClock() uint8 {
return p.halfmoveClock
}
// FullmoveCounter returns the position's fullmove counter.
func (p Position) FullmoveCounter() uint {
return p.fullmoveCounter
}
// Captures returns a slice containing all the Pieces captured
// until the position, in order.
func (p Position) Captures() []Piece {
return p.captures
}
// HasActiveEnPassant reports whether this possition has an active
// en passsant oportunity.
func (p Position) HasActiveEnPassant() bool {
return p.enPassantSq != nil
}
// EnPassantSquare returns the current en passant square.
//
// If there is no en passant in the current position, it will
// return an empty Square and the error.
func (p Position) EnPassantSquare() (Square, error) {
if p.enPassantSq == nil {
return Square{}, errors.New("The current position does not have an active en passant.")
}
return *p.enPassantSq, nil
}
// Turn returns the position's player/side to move.
func (p Position) Turn() Color {
return p.playerToMove
}
// Board returns the position's board.
func (p Position) Board() Board {
return p.board
}
// IsChecked reports whether the current position's turn is
// under check or not.
func (p Position) IsChecked() bool {
return p.isChecked
}
// Fen returns the position's Forsyth–Edwards Notation, as an string, containing
// the board piece placement, player to move, castling rights, en passant, halfmove clock
// and fullmove counter.
//
// For example, for a starting chess position:
//
// "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
func (p Position) Fen() string {
var sb strings.Builder
sb.WriteString(p.board.Fen())
sb.WriteRune(' ')
sb.WriteRune(p.playerToMove.Rune())
if p.castlingRights.queenSide[Color_White] && p.castlingRights.kingSide[Color_White] && p.castlingRights.queenSide[Color_Black] && p.castlingRights.kingSide[Color_Black] {
sb.WriteRune(' ')
if p.castlingRights.kingSide[Color_White] {
sb.WriteRune('K')
}
if p.castlingRights.queenSide[Color_White] {
sb.WriteRune('Q')
}
if p.castlingRights.kingSide[Color_Black] {
sb.WriteRune('k')
}
if p.castlingRights.queenSide[Color_Black] {
sb.WriteRune('q')
}
sb.WriteRune(' ')
} else {
sb.WriteString(" - ")
}
if p.enPassantSq != nil {
sb.WriteString(p.enPassantSq.Algebraic())
} else {
sb.WriteRune('-')
}
sb.WriteRune(' ')
sb.WriteString(strconv.Itoa(int(p.halfmoveClock)))
sb.WriteRune(' ')
sb.WriteString(strconv.Itoa(int(p.fullmoveCounter)))
return sb.String()
}
func newPositionFromFen(fen string) (Position, error) {
parsedFen, err := parseFen(fen)
if err != nil {
return Position{}, err
}
return Position{
board: newBoardFromFen(parsedFen.placementData),
playerToMove: parsedFen.activeColor,
castlingRights: CastlingRights{
queenSide: map[Color]bool{
Color_White: parsedFen.whiteCanQueenSideCastling,
Color_Black: parsedFen.blackCanQueenSideCastling,
},
kingSide: map[Color]bool{
Color_White: parsedFen.whiteCanKingSideCastling,
Color_Black: parsedFen.blackCanKingSideCastling,
},
},
enPassantSq: parsedFen.enPassantSq,
halfmoveClock: parsedFen.halfmoveClock,
fullmoveCounter: parsedFen.fulmoveCounter,
captures: make([]Piece, 0),
}, nil
}
func (p Position) computePseudoMovements(color Color, doCastlingCheck bool) ([]Movement, [8][8]bool) {
movements := make([]Movement, 0, 256)
var attackMatrix [8][8]bool
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
attackMatrix[i][j] = false
}
}
for _, row := range p.board {
for _, piece := range row {
if piece.Color == color {
p.computePiecePseudoMovements(piece, &movements, doCastlingCheck, &attackMatrix)
}
}
}
return movements, attackMatrix
}
func (p Position) computePiecePseudoMovements(piece Piece, movements *[]Movement, doCastlingCheck bool, attackMatrix *[8][8]bool) {
switch piece.Kind {
case Kind_Bishop:
p.computeDirectionPseudoMovements(piece, movements, attackMatrix, bishopDirections)
return
case Kind_Rook:
p.computeDirectionPseudoMovements(piece, movements, attackMatrix, rookDirections)
return
case Kind_Queen:
p.computeDirectionPseudoMovements(piece, movements, attackMatrix, bishopDirections)
p.computeDirectionPseudoMovements(piece, movements, attackMatrix, rookDirections)
return
case Kind_King:
for _, offset := range kingOffsets {
targetRow, targetCol := int8(piece.Square.I)+offset[0], int8(piece.Square.J)+offset[1]
if targetRow >= 0 && targetCol >= 0 && targetRow < 8 && targetCol < 8 {
row := uint8(targetRow)
col := uint8(targetCol)
pieceAt := p.board[row][col]
if pieceAt.Kind == Kind_None {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
))
} else if pieceAt.Color != piece.Color {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
).withTakingPiece(pieceAt))
}
}
}
if doCastlingCheck {
castlingRow := 7
if piece.Color == Color_Black {
castlingRow = 0
}
_, enemyAttackBoard := p.computePseudoMovements(piece.Color.Opposite(), false)
// If king is not in check, continue
if enemyAttackBoard[piece.Square.I][piece.Square.J] == false {
if p.castlingRights.queenSide[piece.Color] {
// Check if space to rook is empty
canCastle := true
for j := piece.Square.J - 1; j >= piece.Square.J-3; j-- {
if p.board[piece.Square.I][j].Kind != Kind_None {
canCastle = false
break
}
}
if canCastle {
// Extra check: Position is not being attacked by enemy
// On queen side, positions that cannot be attacked to castle:
// On the left of the King, in d1 and c1 [castlingRow, 2], [castlingRow, 3]
if enemyAttackBoard[castlingRow][2] == false && enemyAttackBoard[castlingRow][3] == false {
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(piece.Square.I, piece.Square.J-2),
).withCastling(true, false))
}
}
}
if p.castlingRights.kingSide[piece.Color] {
// Check if space to rook is empty
canCastle := true
for j := piece.Square.J + 1; j < 7; j++ {
if p.board[piece.Square.I][j].Kind != Kind_None {
canCastle = false
break
}
}
if canCastle {
// Extra check: Position is not being attacked by enemy
// On King side, positions that cannot be attacked to castle:
// On the right of the King, in f1 and g1 [castlingRow, 5], [castlingRow, 6]
if enemyAttackBoard[castlingRow][5] == false && enemyAttackBoard[castlingRow][6] == false {
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(piece.Square.I, piece.Square.J+2),
).withCastling(false, true))
}
}
}
}
}
return
case Kind_Knight:
for _, offset := range knightOffsets {
targetRow := int8(piece.Square.I) + offset[0]
targetCol := int8(piece.Square.J) + offset[1]
if targetRow >= 0 && targetCol >= 0 && targetRow < 8 && targetCol < 8 {
row := uint8(targetRow)
col := uint8(targetCol)
pieceAt := p.board[row][col]
if pieceAt.Kind == Kind_None {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
))
} else if pieceAt.Color != piece.Color {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
).withTakingPiece(pieceAt))
}
}
}
return
case Kind_Pawn:
// Straight moves
var maxDistance int8 = 2
if piece.Square.I != pawnStartingRows[piece.Color] {
maxDistance = 1
}
promotionRow := uint8(0)
if piece.Color == Color_Black {
promotionRow = 7
}
for i := int8(1); i <= maxDistance; i++ {
targetRow := int8(piece.Square.I) + pawnMoveRowDirections[piece.Color]*i
if targetRow >= 0 && targetRow < 8 {
row := uint8(targetRow)
pieceAt := p.board[row][piece.Square.J]
if pieceAt.Kind != Kind_None {
break
}
if row == promotionRow {
for _, kind := range promotableKinds {
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, piece.Square.J),
).withPawn(i == 2).withPawnPromotion(kind))
}
} else {
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, piece.Square.J),
).withPawn(i == 2))
}
}
}
// Diagonal move
for _, offset := range pawnAttackOffsets[piece.Color] {
targetRow := int8(piece.Square.I) + offset[0]
targetCol := int8(piece.Square.J) + offset[1]
if targetRow >= 0 && targetCol >= 0 && targetRow < 8 && targetCol < 8 {
row := uint8(targetRow)
col := uint8(targetCol)
pieceAt := p.board[row][col]
if pieceAt.Kind != Kind_None && pieceAt.Color != piece.Color {
if row == pawnPromotionRows[piece.Color] {
for _, kind := range promotableKinds {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
).withTakingPiece(pieceAt).withPawn(false).withPawnPromotion(kind))
}
} else {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
).withTakingPiece(pieceAt).withPawn(false))
}
} else {
// If there is no piece in the diagonal, we cannot move to it, but mark the position as being attacked
attackMatrix[row][col] = true
}
// En passant available check in current diagonal
if pieceAt.Kind == Kind_None && p.enPassantSq != nil && p.enPassantSq.I == row && p.enPassantSq.J == col {
enPassantPieceSquare := newSquare(p.enPassantSq.I+1, p.enPassantSq.J)
if piece.Color == Color_Black {
enPassantPieceSquare.I = p.enPassantSq.I - 1
}
pieceAt := p.board[enPassantPieceSquare.I][enPassantPieceSquare.J]
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
).withTakingPiece(pieceAt).withPawn(false))
}
}
}
return
}
}
func (p Position) computeDirectionPseudoMovements(piece Piece, movements *[]Movement, attackMatrix *[8][8]bool, directions [4][2]int8) {
for _, dir := range directions {
i := int8(piece.Square.I) + dir[0]
j := int8(piece.Square.J) + dir[1]
for i >= 0 && j >= 0 && i < 8 && j < 8 {
row := uint8(i)
col := uint8(j)
pieceAt := p.board[row][col]
if pieceAt.Kind == Kind_None {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
))
} else {
if pieceAt.Color != piece.Color {
attackMatrix[row][col] = true
*movements = append(*movements,
*newMovement(piece,
piece.Square,
newSquare(row, col),
).withTakingPiece(pieceAt))
}
break
}
i += dir[0]
j += dir[1]
}
}
return
}
// TODO: Save king's positions (in Position{}, or get them at computePseudoMovements() to reuse the loop, if possible)
func (p Position) checkForCheck(allyColor Color, opponentAttackMatrix *[8][8]bool) bool {
for i := uint8(0); i < 8; i++ {
for j := uint8(0); j < 8; j++ {
if p.board[i][j].Kind == Kind_King && p.board[i][j].Color == allyColor {
return opponentAttackMatrix[i][j]
}
}
}
// Won't get here, unless there is no King piece ??
return false
}