-
Notifications
You must be signed in to change notification settings - Fork 7
/
movetrack.go
91 lines (73 loc) · 1.7 KB
/
movetrack.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/json"
"github.com/malbrecht/chess"
"github.com/malbrecht/chess/pgn"
)
type FromTo struct {
From chess.Sq
To chess.Sq
}
type FromTos map[FromTo]int
func (ft FromTos) MarshalJSON() ([]byte, error) {
r := make(map[string]int)
for k, v := range ft {
key := k.From.String() + "-" + k.To.String()
val := v
r[key] = val
}
return json.Marshal(r)
}
type PieceTracker struct {
PieceMoves map[chess.Sq]FromTos
squareMap map[chess.Sq]chess.Sq
}
func NewPieceTracker() *PieceTracker {
sqmap := make(map[chess.Sq]chess.Sq)
for file := 0; file < 8; file++ {
for rank := 0; rank < 8; rank++ {
sqmap[chess.Square(file, rank)] = chess.NoSquare
}
}
return &PieceTracker{
PieceMoves: make(map[chess.Sq]FromTos),
squareMap: sqmap,
}
}
func (pt PieceTracker) Track(ptr *pgn.Node) {
move := ptr.Move
if move == chess.NullMove {
return
}
if pt.squareMap[move.From] == chess.NoSquare {
pt.squareMap[move.To] = move.From
} else {
pt.squareMap[move.To] = pt.squareMap[move.From]
}
origin := pt.squareMap[move.To]
fromto := FromTo{move.From, move.To}
if pt.PieceMoves[origin] == nil {
pt.PieceMoves[origin] = make(FromTos)
}
pt.PieceMoves[origin][fromto]++
if move.Promotion != chess.NoPiece {
pt.squareMap[move.To] = move.To
}
}
func (pt PieceTracker) Add(ptr *PieceTracker) {
for sq, fromtos := range ptr.PieceMoves {
for fromto, count := range fromtos {
if pt.PieceMoves[sq] == nil {
pt.PieceMoves[sq] = make(FromTos)
}
pt.PieceMoves[sq][fromto] += count
}
}
}
func (pt PieceTracker) MarshalJSON() ([]byte, error) {
jz := make(map[string]FromTos)
for k, v := range pt.PieceMoves {
jz[k.String()] = v
}
return json.Marshal(jz)
}