-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
137 lines (126 loc) · 3.11 KB
/
decode.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package chesscode
import (
"errors"
"fmt"
"reflect"
"sort"
"github.com/notnil/chess"
)
var (
maxPieces = startingPositionPieceCount()
)
func Decode(board *chess.Board) (s string, err error) {
m := board.SquareMap()
sqs := allSquares()
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprint(r))
}
}()
for _, p := range pieceOrder {
psqs := squaresForPiece(m, p)
idxs := indexesForSquares(sqs, psqs)
if len(idxs) > maxPieces[p] {
return "", errors.New("chesscode: invalid number of " + p.String())
}
if len(idxs) == 0 {
return s, nil
}
switch p {
case chess.WhiteKing, chess.BlackKing, chess.WhiteQueen, chess.BlackQueen:
code := idxs[0]
s += charset[code : code+1]
case chess.WhiteRook, chess.BlackRook, chess.WhiteBishop, chess.BlackBishop:
combos := combo2(len(sqs))
comboIdx := searchCombos(idxs, combos)
sub := char2RevLookup[comboIdx]
s += sub
case chess.WhiteKnight:
bSqs := squaresForPiece(m, chess.BlackKnight)
bIdxs := indexesForSquares(sqs, bSqs)
r := []combo2by2Result{}
for _, idx := range idxs {
r = append(r, combo2by2Result{idx: idx, piece: chess.WhiteKnight})
}
for _, idx := range bIdxs {
r = append(r, combo2by2Result{idx: idx, piece: chess.BlackKnight})
}
sort.Slice(r, func(i, j int) bool {
return r[i].idx < r[j].idx
})
comboIdx := search2by2Combos(r, combo2by2Lookup)
sub := char3RevLookup[comboIdx]
s += sub
case chess.WhitePawn, chess.BlackPawn:
leftSqs := leftSquares(sqs)
leftIdxs := indexesForSquares(leftSqs, psqs)
combos := combo4(len(leftSqs))
comboIdx := searchCombos(leftIdxs, combos)
sub := char2RevLookup[comboIdx]
s += sub
rightSqs := rightSqares(sqs)
rightIdxs := indexesForSquares(rightSqs, psqs)
combos = combo4(len(rightSqs))
comboIdx = searchCombos(rightIdxs, combos)
sub = char2RevLookup[comboIdx]
s += sub
}
sort.Sort(sort.Reverse(sort.IntSlice(idxs)))
for _, idx := range idxs {
sqs = popSquare(sqs, idx)
}
}
return s, nil
}
func searchCombos(idxs []int, combos [][]int) int {
for i, c := range combos {
if reflect.DeepEqual(idxs, c) {
return i
}
}
panic("unreachable")
}
func search2by2Combos(idxs []combo2by2Result, combos [][]combo2by2Result) int {
for i, c := range combos {
if reflect.DeepEqual(idxs, c) {
return i
}
}
panic("unreachable")
}
func squaresForPiece(m map[chess.Square]chess.Piece, p chess.Piece) []chess.Square {
sqs := []chess.Square{}
for sq, sqp := range m {
if p == sqp {
sqs = append(sqs, sq)
}
}
return sqs
}
func indexesForSquares(sqs []chess.Square, p []chess.Square) []int {
results := []int{}
for _, sq := range p {
idx := indexOfSquare(sqs, sq)
if idx != -1 {
results = append(results, idx)
}
}
sort.IntSlice(results).Sort()
return results
}
func indexOfSquare(sqs []chess.Square, sq chess.Square) int {
for i, sq2 := range sqs {
if sq2 == sq {
return i
}
}
return -1
}
func startingPositionPieceCount() map[chess.Piece]int {
m := map[chess.Piece]int{}
sqMap := chess.StartingPosition().Board().SquareMap()
for _, p := range sqMap {
m[p] += 1
}
return m
}