-
Notifications
You must be signed in to change notification settings - Fork 1
/
position.go
405 lines (333 loc) · 11.2 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
package board
import (
"fmt"
"strings"
)
// Placement defines a piece placement.
type Placement struct {
Square Square
Color Color
Piece Piece
}
func (p Placement) String() string {
return fmt.Sprintf("%v@%v", printPiece(p.Color, p.Piece), p.Square)
}
// Position represents a board position suitable for move generation. It includes castling and
// en passant, but not game metadata to determine various Draw conditions.
type Position struct {
pieces [NumColors][NumPieces]Bitboard // Zero piece contains all pieces for color.
rotated RotatedBitboard
castling Castling
enpassant Square // zero if last move was not a Jump
}
func NewPosition(pieces []Placement, castling Castling, ep Square) (*Position, error) {
ret := &Position{castling: castling, enpassant: ep}
for _, p := range pieces {
if !ret.IsEmpty(p.Square) {
return nil, fmt.Errorf("duplicate placement: %v", p)
}
ret.xor(p.Square, p.Color, p.Piece)
}
return ret, nil
}
// Move attempts to make a pseudo-legal move. The attempted move is assumed to be
// pseudo-legal and generated from the position. Returns false if not legal.
func (p *Position) Move(m Move) (*Position, bool) {
ret := *p
// (1) Remove piece from "from" square.
turn, piece, ok := p.Square(m.From)
if !ok {
return nil, false
}
ret.xor(m.From, turn, piece)
// (2) Remove any captured piece.
if m.IsCapture() {
ret.xor(m.To, turn.Opponent(), m.Capture)
}
// (3) Add piece to "to" square.
if m.IsPromotion() {
piece = m.Promotion
}
ret.xor(m.To, turn, piece)
// (4) Handle special moves/captures.
switch m.Type {
case EnPassant:
capture, _ := m.EnPassantCapture()
ret.xor(capture, turn.Opponent(), Pawn)
case KingSideCastle, QueenSideCastle:
for _, sq := range safeCastlingSquares(turn, m.Type) {
if p.IsAttacked(turn, sq) {
return nil, false
}
}
from, to, _ := m.CastlingRookMove()
ret.xor(from, turn, Rook)
ret.xor(to, turn, Rook)
}
// (5) Update EnPassant and castling status.
ret.enpassant, _ = m.EnPassantTarget()
ret.castling &^= m.CastlingRightsLost()
// (7) Validate that move does not leave own king in check.
if ret.IsChecked(turn) {
return nil, false
}
return &ret, true
}
// Castling returns the castling rights.
func (p *Position) Castling() Castling {
return p.castling
}
// EnPassant return the target en passant square, if previous move was a Jump. For example,
// after e2e4, the en passant target square is e3 whether or not black has pawns on d4 or f4.
func (p *Position) EnPassant() (Square, bool) {
return p.enpassant, p.enpassant != ZeroSquare
}
// Rotated returns the rotated bitboard.
func (p *Position) Rotated() RotatedBitboard {
return p.rotated
}
// All returns a bitboard contains all pirces.
func (p *Position) All() Bitboard {
return p.rotated.rot
}
// Color returns the bitboard for a given color.
func (p *Position) Color(c Color) Bitboard {
return p.pieces[c][NoPiece]
}
// Piece returns the bitboard for a given color/piece.
func (p *Position) Piece(c Color, piece Piece) Bitboard {
return p.pieces[c][piece]
}
// PieceSquares returns the squares for a given color/piece.
func (p *Position) PieceSquares(c Color, piece Piece) []Square {
return p.pieces[c][piece].ToSquares()
}
// KingSquare returns the square for a given color. Must be valid and unique.
func (p *Position) KingSquare(c Color) Square {
return p.pieces[c][King].LastPopSquare()
}
// Square returns the content of the given square. Returns false is no piece present.
func (p *Position) Square(sq Square) (Color, Piece, bool) {
if p.IsEmpty(sq) {
return 0, 0, false
}
for c := ZeroColor; c < NumColors; c++ {
if !p.pieces[c][NoPiece].IsSet(sq) {
continue
}
for piece := ZeroPiece; piece < NumPieces; piece++ {
if p.pieces[c][piece].IsSet(sq) {
return c, piece, true
}
}
}
return 0, 0, false
}
// IsEmpty returns true iff the square is empty.
func (p *Position) IsEmpty(sq Square) bool {
return !p.rotated.Mask().IsSet(sq)
}
// IsDefended returns true iff the square is defended by the color.
func (p *Position) IsDefended(c Color, sq Square) bool {
return p.IsAttacked(c.Opponent(), sq)
}
// IsAttacked returns true iff the square is attacked by the opposing color. Does not include en passant.
func (p *Position) IsAttacked(c Color, sq Square) bool {
opp := c.Opponent()
for _, piece := range KingQueenRookKnightBishop {
if pieces := p.pieces[opp][piece]; pieces != 0 && Attackboard(p.rotated, sq, piece)&pieces != 0 {
return true
}
}
return PawnCaptureboard(opp, p.pieces[opp][Pawn])&BitMask(sq) != 0
}
// IsChecked returns true iff the color is in check. Convenient for IsAttacked(King).
func (p *Position) IsChecked(c Color) bool {
if pos := p.pieces[c][King].LastPopSquare(); pos != NumSquares {
return p.IsAttacked(c, pos)
}
return false
}
// IsCheckMate returns true iff the color is checkmate. Convenient for IsChecked && len(LegalMoves)==0.
func (p *Position) IsCheckMate(c Color) bool {
return p.IsChecked(c) && len(p.LegalMoves(c)) == 0
}
var (
whiteSquareMask = Bitboard(0xaaaaaaaaaaaaaaaa)
)
// HasInsufficientMaterial returns true iff there is not sufficient material for either side to win.
// The cases are: K v K, KN v K, KB v KB (or KBB v K) w/ Bishops on same square color. Assumes 2 kings.
func (p *Position) HasInsufficientMaterial() bool {
switch p.rotated.rot.PopCount() {
case 2:
return true
case 3:
weak := p.pieces[White][Knight] | p.pieces[Black][Knight] | p.pieces[White][Bishop] | p.pieces[Black][Bishop]
return weak.PopCount() == 1
case 4:
bishops := p.pieces[White][Bishop] | p.pieces[Black][Bishop]
return bishops.PopCount() == 2 && (whiteSquareMask&bishops).PopCount() != 1
default:
return false
}
}
var (
whiteKingSideCastlingMask = BitMask(G1) | BitMask(F1)
whiteQueenSideCastlingMask = BitMask(B1) | BitMask(C1) | BitMask(D1)
blackKingSideCastlingMask = BitMask(G8) | BitMask(F8)
blackQueenSideCastlingMask = BitMask(B8) | BitMask(C8) | BitMask(D8)
)
// LegalMoves returns a list of all legal moves. Convenience function.
func (p *Position) LegalMoves(turn Color) []Move {
var ret []Move
for _, m := range p.PseudoLegalMoves(turn) {
if _, ok := p.Move(m); ok {
ret = append(ret, m)
}
}
return ret
}
// PseudoLegalMoves returns a list of all pseudo-legal moves. The move may not respect
// either side being in check, which must be validated subsequently.
func (p *Position) PseudoLegalMoves(turn Color) []Move {
mask := ^p.pieces[turn][NoPiece] // cannot capture own pieces
captures := p.pieces[turn.Opponent()][NoPiece]
moves := ^captures
jumps := PawnJumpRank(turn)
promos := PawnPromotionRank(turn)
ret := make([]Move, 0, 50)
for _, piece := range QueenRookKnightBishop {
pieces := p.pieces[turn][piece]
for pieces != EmptyBitboard {
from := pieces.LastPopSquare()
pieces ^= BitMask(from)
attackboard := Attackboard(p.rotated, from, piece) & mask
p.emitMove(turn, Normal, piece, from, attackboard&moves, &ret)
p.emitMove(turn, Capture, piece, from, attackboard&captures, &ret)
}
}
pawns := p.pieces[turn][Pawn]
for pawns != EmptyBitboard {
from := pawns.LastPopSquare()
origin := BitMask(from)
pawns ^= origin
captureboard := PawnCaptureboard(turn, origin) & mask
pushboard := PawnMoveboard(p.rotated.rot, turn, origin)
jumpboard := PawnMoveboard(p.rotated.rot, turn, pushboard) & jumps
p.emitMove(turn, Capture, Pawn, from, captureboard&captures&^promos, &ret)
p.emitMove(turn, Push, Pawn, from, pushboard&^promos, &ret)
p.emitMove(turn, Jump, Pawn, from, jumpboard, &ret)
p.emitPromo(turn, CapturePromotion, Pawn, from, captureboard&captures&promos, &ret)
p.emitPromo(turn, Promotion, Pawn, from, pushboard&promos, &ret)
if p.enpassant != ZeroSquare {
p.emitMove(turn, EnPassant, Pawn, from, captureboard&BitMask(p.enpassant), &ret)
}
}
if king := p.pieces[turn][King]; king != EmptyBitboard {
from := king.LastPopSquare()
attackboard := KingAttackboard(from) & mask
p.emitMove(turn, Normal, King, from, attackboard&moves, &ret)
p.emitMove(turn, Capture, King, from, attackboard&captures, &ret)
if turn == White {
if p.castling.IsAllowed(WhiteKingSideCastle) && (whiteKingSideCastlingMask&p.rotated.rot) == 0 && p.pieces[turn][Rook]&BitMask(H1) != 0 {
p.emitMove(turn, KingSideCastle, King, from, BitMask(G1), &ret)
}
if p.castling.IsAllowed(WhiteQueenSideCastle) && (whiteQueenSideCastlingMask&p.rotated.rot) == 0 && p.pieces[turn][Rook]&BitMask(A1) != 0 {
p.emitMove(turn, QueenSideCastle, King, from, BitMask(C1), &ret)
}
} else {
if p.castling.IsAllowed(BlackKingSideCastle) && (blackKingSideCastlingMask&p.rotated.rot) == 0 && p.pieces[turn][Rook]&BitMask(H8) != 0 {
p.emitMove(turn, KingSideCastle, King, from, BitMask(G8), &ret)
}
if p.castling.IsAllowed(BlackQueenSideCastle) && (blackQueenSideCastlingMask&p.rotated.rot) == 0 && p.pieces[turn][Rook]&BitMask(A8) != 0 {
p.emitMove(turn, QueenSideCastle, King, from, BitMask(C8), &ret)
}
}
}
return ret
}
func (p *Position) emitMove(turn Color, t MoveType, piece Piece, from Square, attackboard Bitboard, out *[]Move) {
for attackboard != EmptyBitboard {
to := attackboard.LastPopSquare()
attackboard ^= BitMask(to)
capture := NoPiece
if t == Capture {
capture = p.captureAt(to, turn)
}
*out = append(*out, Move{Type: t, Piece: piece, From: from, To: to, Capture: capture})
}
}
func (p *Position) emitPromo(turn Color, t MoveType, piece Piece, from Square, attackboard Bitboard, out *[]Move) {
for attackboard != EmptyBitboard {
to := attackboard.LastPopSquare()
attackboard ^= BitMask(to)
capture := NoPiece
if t == CapturePromotion {
capture = p.captureAt(to, turn)
}
// Emit under-promotions as well.
for _, pc := range QueenRookKnightBishop {
*out = append(*out, Move{Type: t, Piece: piece, From: from, To: to, Capture: capture, Promotion: pc})
}
}
}
func (p *Position) captureAt(sq Square, turn Color) Piece {
for piece := ZeroPiece; piece < NumPieces; piece++ {
if p.pieces[turn.Opponent()][piece].IsSet(sq) {
return piece
}
}
return NoPiece
}
func (p *Position) String() string {
var sb strings.Builder
for i := ZeroSquare; i < NumSquares; i++ {
if i != 0 && i%8 == 0 {
sb.WriteRune('/')
}
if color, piece, ok := p.Square(NumSquares - i - 1); ok {
sb.WriteString(printPiece(color, piece))
} else {
sb.WriteRune('-')
}
}
ep := "-"
if p.enpassant != ZeroSquare {
ep = p.enpassant.String()
}
return fmt.Sprintf("%v %v(%v)", sb.String(), p.castling, ep)
}
func (p *Position) xor(sq Square, color Color, piece Piece) {
p.rotated = p.rotated.Xor(sq)
p.pieces[color][NoPiece] ^= BitMask(sq)
p.pieces[color][piece] ^= BitMask(sq)
}
func printPiece(c Color, p Piece) string {
if c == White {
return strings.ToUpper(p.String())
}
return strings.ToLower(p.String())
}
// safeCastlingSquares returns the squares that must not be in check to castle.
// Does not include the king to square.
func safeCastlingSquares(c Color, t MoveType) []Square {
if c == White {
switch t {
case KingSideCastle:
return []Square{E1, F1}
case QueenSideCastle:
return []Square{E1, D1}
default:
return nil
}
} else {
switch t {
case KingSideCastle:
return []Square{E8, F8}
case QueenSideCastle:
return []Square{E8, D8}
default:
return nil
}
}
}