-
Notifications
You must be signed in to change notification settings - Fork 1
/
piece.go
71 lines (64 loc) · 1.2 KB
/
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
64
65
66
67
68
69
70
71
package board
// Piece represents a chess piece (King, Pawn, etc) with no color. Zero indicates "No Piece". 3 bits.
type Piece uint8
const (
NoPiece Piece = iota
Pawn
Bishop
Knight
Rook
Queen
King
)
const (
ZeroPiece Piece = 1
NumPieces Piece = 7
)
var (
KingQueen = []Piece{King, Queen}
KingQueenRookKnightBishop = []Piece{King, Queen, Rook, Knight, Bishop}
QueenRookBishop = []Piece{Queen, Rook, Bishop}
QueenRookKnightBishop = []Piece{Queen, Rook, Knight, Bishop}
QueenRookKnightBishopPawn = []Piece{Queen, Rook, Knight, Bishop, Pawn}
)
func ParsePiece(r rune) (Piece, bool) {
switch r {
case 'p', 'P':
return Pawn, true
case 'b', 'B':
return Bishop, true
case 'n', 'N':
return Knight, true
case 'r', 'R':
return Rook, true
case 'q', 'Q':
return Queen, true
case 'k', 'K':
return King, true
default:
return NoPiece, false
}
}
func (p Piece) IsValid() bool {
return Pawn <= p && p <= King
}
func (p Piece) String() string {
switch p {
case NoPiece:
return "-"
case Pawn:
return "P"
case Bishop:
return "B"
case Knight:
return "N"
case Rook:
return "R"
case Queen:
return "Q"
case King:
return "K"
default:
return "?"
}
}