-
Notifications
You must be signed in to change notification settings - Fork 0
/
pieces.go
52 lines (45 loc) · 1.4 KB
/
pieces.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
package main
import (
"URLchess/shf"
"strings"
"github.com/andrewbackes/chess/piece"
)
var playablePiecesType = []piece.Type{piece.Pawn, piece.Rook, piece.Knight, piece.Bishop, piece.Queen, piece.King}
var promotablePiecesType = []piece.Type{piece.Rook, piece.Knight, piece.Bishop, piece.Queen}
var thrownOutPiecesOrderType = []piece.Type{piece.Pawn, piece.Knight, piece.Bishop, piece.Rook, piece.Queen}
var pieceTypesToName = map[piece.Type]string{
piece.Pawn: "pawn",
piece.Rook: "rook",
piece.Knight: "knight",
piece.Bishop: "bishop",
piece.Queen: "queen",
piece.King: "king",
}
var pieceNamesToType map[string]piece.Type = func() map[string]piece.Type {
res := make(map[string]piece.Type, len(pieceTypesToName))
for k, v := range pieceTypesToName {
if _, ok := res[v]; ok {
panic("error creating pieceNamesToType map: duplicate name: " + v)
}
res[v] = k
}
return res
}()
func pieceElement(p piece.Piece) shf.Element {
elm := shf.CreateElement("span")
elm.Get("classList").Call("add", "piece")
if p.Color != piece.NoColor {
elm.Get("classList").Call("add", strings.ToLower(p.Color.String()))
}
if p.Type != piece.None {
elm.Get("classList").Call("add", pieceTypesToName[p.Type])
}
elm.Set("textContent", p.Figurine())
return elm
}
func complementColor(c piece.Color) piece.Color {
if c == piece.White || c == piece.Black {
return piece.Colors[(int(c)+1)%2]
}
return piece.NoColor
}