/
king.go
53 lines (45 loc) · 1.54 KB
/
king.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
package board
// -------------
// KING ♔
// -------------
// getKingMoves returns a move slice with all the legal moves of a king from the bitboard passed
func getKingMoves(b *Bitboard, pos *Position, side Color) (moves []Move) {
movesBB := kingMoves(b, pos, side)
pieces := ^pos.EmptySquares()
from := Bsf(*b)
piece := pieceOfColor[King][side]
for movesBB > 0 {
to := movesBB.NextBit()
move := newMove().
setFromSq(from).
setToSq(Bsf(to)).
setPiece(piece).
setMoveType(Normal).
setEpTargetBefore(pos.enPassantTarget).
setRule50Before(pos.halfmoveClock).
setCastleRightsBefore(pos.castlingRights)
if to&pieces > 0 {
capturedPiece, _ := pos.PieceAt(squareReference[Bsf(to)])
// NOTE: the captured piece will stay set but not used if move != CAPTURE
move.setMoveType(Capture).setCapturedPiece(capturedPiece)
}
moves = append(moves, *move)
}
return
}
// kingMoves returns a bitboard with the legal moves of the king from the bitboard passed
func kingMoves(k *Bitboard, pos *Position, side Color) (moves Bitboard) {
withoutKing := *pos
withoutKing.RemovePiece(*k)
moves = kingAttacks(k) & ^withoutKing.AttackedSquares(side.Opponent()) & ^pos.Pieces(side)
return
}
// kingAttacks returns a bitboard with the squares the king attacks from the passed bitboard
func kingAttacks(k *Bitboard) (attacks Bitboard) {
notInHFile := *k & ^(*k & files[7])
notInAFile := *k & ^(*k & files[0])
attacks = notInAFile<<7 | *k<<8 | notInHFile<<9 |
notInHFile<<1 | notInAFile>>1 | notInHFile>>7 |
*k>>8 | notInAFile>>9
return
}