-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode.go
More file actions
178 lines (165 loc) · 3.78 KB
/
encode.go
File metadata and controls
178 lines (165 loc) · 3.78 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package chesscode
import (
"fmt"
"sort"
"strings"
"github.com/notnil/chess"
)
const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ. "
var (
pieceOrder = []chess.Piece{
chess.WhiteKing,
chess.BlackKing,
chess.WhiteQueen,
chess.BlackQueen,
chess.WhiteRook,
chess.BlackRook,
chess.WhiteBishop,
chess.BlackBishop,
chess.WhiteKnight,
chess.BlackKnight,
chess.WhitePawn,
chess.BlackPawn,
}
)
func Encode(s string) (*chess.Board, error) {
s = formatString(s)
if err := validInputString(s); err != nil {
return nil, err
}
sqs := allSquares()
sqMap := map[chess.Square]chess.Piece{}
blackKnightUsed := []int{}
for _, p := range pieceOrder {
if len(s) == 0 {
break
}
used := []int{}
chars := 0
switch p {
case chess.WhiteKing, chess.BlackKing, chess.WhiteQueen, chess.BlackQueen:
code := string(s[0])
idx := strings.Index(charset, code)
used = append(used, idx)
chars = 1
case chess.WhiteRook, chess.BlackRook, chess.WhiteBishop, chess.BlackBishop:
codes := s[:2]
idx := char2Lookup[codes]
combos := combo2(len(sqs))
used = combos[idx]
chars = 2
case chess.WhiteKnight:
codes := s[:3]
idx := char3Lookup[codes]
combo := combo2by2Lookup[idx]
for _, c := range combo {
switch c.piece {
case chess.WhiteKnight:
used = append(used, c.idx)
case chess.BlackKnight:
blackKnightUsed = append(blackKnightUsed, c.idx-len(used))
}
}
chars = 0
case chess.BlackKnight:
used = append(used, blackKnightUsed...)
chars = 3
case chess.WhitePawn, chess.BlackPawn:
leftSqs := leftSquares(sqs)
codes := s[:2]
lookupIdx := char2Lookup[codes]
combos := combo4(len(leftSqs))
combo := combos[lookupIdx]
for i, c := range combo {
sq := leftSqs[c]
combo[i] = indexOfSquare(sqs, sq)
}
used = append(used, combo...)
rightSqs := rightSqares(sqs)
rightCodes := s[2:4]
lookupIdx = char2Lookup[rightCodes]
combos = combo4(len(rightSqs))
combo = combos[lookupIdx]
for i, c := range combo {
sq := rightSqs[c]
combo[i] = indexOfSquare(sqs, sq)
}
used = append(used, combo...)
chars = 4
}
sort.Sort(sort.Reverse(sort.IntSlice(used)))
for _, idx := range used {
sq := sqs[idx]
sqs = popSquare(sqs, idx)
sqMap[sq] = p
}
s = s[chars:]
}
return chess.NewBoard(sqMap), nil
}
func formatString(s string) string {
s = strings.ToUpper(s)
l := len(s)
boundaries := []int{0, 1, 2, 3, 4, 6, 8, 10, 12, 15, 19, 23}
for _, b := range boundaries {
if l == b {
return s
}
if l < b {
s += strings.Repeat(" ", b-l)
break
}
}
return s
}
func validInputString(s string) error {
if len(s) > 23 {
return fmt.Errorf("chesscode: maximum encoding length is 23 characters but got %d %s", len(s), s)
}
for _, c := range s {
idx := strings.Index(charset, string(c))
if idx == -1 {
return fmt.Errorf("chesscode: invalid character %s in input %s", string(c), s)
}
}
return nil
}
func allSquares() []chess.Square {
sqs := []chess.Square{}
for i := 0; i < 64; i++ {
sqs = append(sqs, chess.Square(i))
}
return sqs
}
func leftSquares(sqs []chess.Square) []chess.Square {
return filterSqs(sqs, func(s chess.Square) bool {
switch s.File() {
case chess.FileA, chess.FileB, chess.FileC,
chess.FileD:
return true
}
return false
})
}
func rightSqares(sqs []chess.Square) []chess.Square {
return filterSqs(sqs, func(s chess.Square) bool {
switch s.File() {
case chess.FileE, chess.FileF, chess.FileG,
chess.FileH:
return true
}
return false
})
}
func filterSqs(sqs []chess.Square, fn func(chess.Square) bool) []chess.Square {
cp := []chess.Square{}
for _, sq := range sqs {
if fn(sq) {
cp = append(cp, sq)
}
}
return cp
}
func popSquare(sqs []chess.Square, idx int) []chess.Square {
return append(sqs[:idx], sqs[idx+1:]...)
}