/
piece.go
63 lines (54 loc) · 963 Bytes
/
piece.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
package core
import "fmt"
// A Piece represents a chess piece.
type Piece uint64
// White piece constants.
const (
WhitePawn Piece = iota
WhiteKnight
WhiteBishop
WhiteRook
WhiteQueen
WhiteKing
BlackPawn
BlackKnight
BlackBishop
BlackRook
BlackQueen
BlackKing
)
// NewPiece returns a new piece.
func NewPiece(c Color, pt PieceType) Piece {
return Piece(c.Uint64()*6 + uint64(pt))
}
func (p Piece) Valid() bool {
return p <= BlackKing
}
var pieceNames = [...]string{
"WhitePawn",
"WhiteKnight",
"WhiteBishop",
"WhiteRook",
"WhiteQueen",
"WhiteKing",
"BlackPawn",
"BlackKnight",
"BlackBishop",
"BlackRook",
"BlackQueen",
"BlackKing",
}
func (p Piece) String() string {
if p.Valid() {
return pieceNames[p]
}
return fmt.Sprintf("Piece(%d)", p)
}
// Type returns a piece's type.
func (p Piece) Type() PieceType {
return PieceType(p % 6)
}
// Color returns a piece's color.
func (p Piece) Color() Color {
return p >= BlackPawn
}