-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.go
74 lines (63 loc) · 1.7 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
72
73
74
package chess
type PieceType int8
// Piece
const (
NoPieceType PieceType = iota
Pawn
Knight
Bishop
Rook
Queen
King
)
func (p PieceType) Name() string {
return [7]string{"No piece", "Pawn", "Knight", "Bishop", "Rook", "Queen", "King"}[int(p)]
}
// Piece represent chess piece
type Piece int8
// Color - retrieves piece color
func (p Piece) Color() Color { return Color(p >> 3) }
// Type - retrieves piece type
func (p Piece) Type() PieceType { return PieceType(int8(p) & 0b111) }
func (p Piece) Name() string {
return p.Color().Name() + p.Type().Name()
}
func (p Piece) String() string {
return pieceCharacters[p]
}
func getPiece(color Color, pieceType PieceType) Piece {
return Piece(int8(color)<<3 + int8(pieceType))
}
// Pieces
const (
NoPiece = Piece(int8(NoColor)<<3 + int8(NoPieceType))
WhitePawn = Piece(int8(White)<<3 + int8(Pawn))
WhiteKnight = Piece(int8(White)<<3 + int8(Knight))
WhiteBishop = Piece(int8(White)<<3 + int8(Bishop))
WhiteRook = Piece(int8(White)<<3 + int8(Rook))
WhiteQueen = Piece(int8(White)<<3 + int8(Queen))
WhiteKing = Piece(int8(White)<<3 + int8(King))
BlackPawn = Piece(int8(Black)<<3 + int8(Pawn))
BlackKnight = Piece(int8(Black)<<3 + int8(Knight))
BlackBishop = Piece(int8(Black)<<3 + int8(Bishop))
BlackRook = Piece(int8(Black)<<3 + int8(Rook))
BlackQueen = Piece(int8(Black)<<3 + int8(Queen))
BlackKing = Piece(int8(Black)<<3 + int8(King))
)
var (
pieceCharacters = map[Piece]string{
NoPiece: " ",
WhiteKing: "♔",
WhiteQueen: "♕",
WhiteRook: "♖",
WhiteBishop: "♗",
WhiteKnight: "♘",
WhitePawn: "♙",
BlackKing: "♚",
BlackQueen: "♛",
BlackRook: "♜",
BlackBishop: "♝",
BlackKnight: "♞",
BlackPawn: "♟",
}
)