-
Notifications
You must be signed in to change notification settings - Fork 1
/
castling.go
57 lines (48 loc) · 1.21 KB
/
castling.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
package board
import "strings"
// Castling represents the set of castling rights. 4 bits.
type Castling uint8
const (
WhiteKingSideCastle Castling = 1 << iota
WhiteQueenSideCastle
BlackKingSideCastle
BlackQueenSideCastle
)
const (
NoCastlingRights = Castling(0)
WhiteCastlingRights = WhiteKingSideCastle | WhiteQueenSideCastle
BlackCastlingRights = BlackKingSideCastle | BlackQueenSideCastle
FullCastingRights = WhiteCastlingRights | BlackCastlingRights
ZeroCastling = NoCastlingRights
NumCastling = FullCastingRights + 1
)
// IsAllowed returns true iff all the given rights are allowed.
func (c Castling) IsAllowed(right Castling) bool {
return c&right != 0
}
func (c Castling) String() string {
if c == 0 {
return "-"
}
var sb strings.Builder
if c.IsAllowed(WhiteKingSideCastle) {
sb.WriteString("K")
}
if c.IsAllowed(WhiteQueenSideCastle) {
sb.WriteString("Q")
}
if c.IsAllowed(BlackKingSideCastle) {
sb.WriteString("k")
}
if c.IsAllowed(BlackQueenSideCastle) {
sb.WriteString("q")
}
return sb.String()
}
// CastlingRights returns the castling rights for the given color.
func CastlingRights(c Color) Castling {
if c == White {
return WhiteCastlingRights
}
return BlackCastlingRights
}