-
Notifications
You must be signed in to change notification settings - Fork 2
/
uci.go
284 lines (254 loc) · 6.92 KB
/
uci.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package uci
import (
"bufio"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
type Engine struct {
stdin *bufio.Writer
stdout *bufio.Scanner
Meta Meta
Side int
StartPos string
}
// Meta data about the Engine
type Meta struct {
Name string // Name of Engine
Author string // Author of the Engine
Options []Option // Available options for this Engine
}
// Available options to set on this Engine
type Option struct {
Name string // Name of option
Type string // Type of option
Default interface{} // Default value of option
Min int // Min value of option
Max int // Max value of option
Vars []string // Enum vars for option
}
// Options for creating a new game
type NewGameOpts struct {
Variant struct {
Key string
}
InitialFen string
Moves string
Side int // Which side should the Engine play as. Must be uci.W or uci.B
}
// Options to pass when looking for best move
type GoOpts struct {
SearchMoves string // <move1> .... <movei>. restrict search to this moves only
Ponder bool // start searching in pondering mode
Wtime int // number of ms white has left
Btime int // number of ms black has left
Winc int // number of ms white increases by each move
Binc int // number of ms black increases by each move
MovesToGo int // number of moves until next time control
Depth int // maximum search depth
Nodes int // maximum search nodes
Mate int // search for mate in x moves
MoveTime int // number of ms exactly to search for
}
// Response from searching
type GoResp struct {
Bestmove string // Best move the Engine could find
Ponder string // Ponder move
}
const (
White int = 0 // Side to play as. White
Black int = 1 // Side to play as. Black
)
var execCommand = exec.Command
// Create a new Engine. Requires the path to a uci chess Engine such as stockfish
func NewEngine(path string) (*Engine, error) {
eng := Engine{}
cmd := execCommand(path)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
eng.stdin = bufio.NewWriter(stdin)
eng.stdout = bufio.NewScanner(stdout)
eng.Meta = eng.getMeta()
return &eng, nil
}
func (eng *Engine) getMeta() (meta Meta) {
eng.send("uci")
lines := eng.receive("uciok")
namePrefix := "id name "
authorPrefix := "id author "
optionPrefix := "option "
for _, line := range lines {
if strings.HasPrefix(line, namePrefix) {
meta.Name = strings.TrimPrefix(line, namePrefix)
} else if strings.HasPrefix(line, authorPrefix) {
meta.Author = strings.TrimPrefix(line, authorPrefix)
} else if strings.HasPrefix(line, optionPrefix) {
meta.Options = append(meta.Options, newOption(strings.TrimPrefix(line, optionPrefix)))
}
}
return meta
}
func getOption(line, regex string) interface{} {
rr := regexp.MustCompile(regex)
results := rr.FindStringSubmatch(line)
if len(results) == 2 {
return results[1]
}
return nil
}
func newOption(line string) (option Option) {
option.Name, _ = getOption(line, `name (.*) type`).(string)
option.Type, _ = getOption(line, `type (\w+)`).(string)
option.Default = getOption(line, `default (\w+)`)
minStr, _ := getOption(line, `min (\w+)`).(string)
maxStr, _ := getOption(line, `max (\w+)`).(string)
option.Min, _ = strconv.Atoi(minStr)
option.Max, _ = strconv.Atoi(maxStr)
varRegex := regexp.MustCompile(`var (\w+)`)
vars := []string{}
for _, v := range varRegex.FindAllStringSubmatch(line, -1) {
vars = append(vars, v[1])
}
option.Vars = vars
return
}
// Pass an option to the underlying Engine
func (eng *Engine) SetOption(name string, value interface{}) bool {
for _, option := range eng.Meta.Options {
if option.Name == name {
var v string
switch value.(type) {
case string:
v, _ = value.(string)
case int:
vv, _ := value.(int)
if (vv < option.Min) {
vv = option.Min
} else if (vv > option.Max) {
vv = option.Max
}
v = strconv.Itoa(vv)
case bool:
vv, _ := value.(bool)
if vv {
v = "true"
} else {
v = "false"
}
}
eng.send("setoption name " + name + " value " + v)
return true
}
}
return false
}
// Check if Engine is ready to start receiving commands
func (eng *Engine) IsReady() bool {
eng.send("isready")
lines := eng.receive("readyok")
return lines[0] == "readyok"
}
func (eng *Engine) send(input string) {
_, err := eng.stdin.WriteString(input + "\n")
if err == nil {
eng.stdin.Flush()
}
}
func (eng *Engine) receive(stopPrefix string) (lines []string) {
scanner := eng.stdout
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
if strings.HasPrefix(line, stopPrefix) {
break
}
}
if err := eng.stdout.Err(); err != nil {
log.Fatal("reading standard input:", err)
}
return
}
// Start a new game. Only one game should be played at a time
func (eng *Engine) NewGame(opts NewGameOpts) {
if opts.Variant.Key == "chess960" || opts.Variant.Key == "fromPosition" {
eng.SetOption("UCI_Variant", "chess")
eng.SetOption("UCI_Chess960", "true")
} else {
if opts.Variant.Key == "threeCheck" {
eng.SetOption("UCI_Variant", "3check")
} else if opts.Variant.Key == "standard" {
eng.SetOption("UCI_Variant", "chess")
} else {
eng.SetOption("UCI_Variant", strings.ToLower(opts.Variant.Key))
}
eng.SetOption("UCI_Chess960", "false")
}
if opts.InitialFen == "" || opts.InitialFen == "startpos" {
eng.StartPos = "position startpos moves "
} else {
eng.StartPos = "position fen " + opts.InitialFen + " moves "
}
eng.Position(opts.Moves)
eng.Side = opts.Side
}
// Set the position of the game after the initial start position. Algrebriac notiation, e.g `e2e4 e7e6`
func (eng *Engine) Position(moves string) {
eng.send(eng.StartPos + moves)
}
func addOpt(name string, value int) string {
if value > 0 {
return name + " " + strconv.Itoa(value) + " "
}
return ""
}
// Search for the bestmove
func (eng *Engine) Go(opts GoOpts) GoResp {
goCmd := "go "
if opts.Ponder {
goCmd += "ponder "
}
goCmd += addOpt("wtime", opts.Wtime)
goCmd += addOpt("btime", opts.Btime)
goCmd += addOpt("winc", opts.Winc)
goCmd += addOpt("binc", opts.Binc)
goCmd += addOpt("movestogo", opts.MovesToGo)
goCmd += addOpt("depth", opts.Depth)
goCmd += addOpt("nodes", opts.Nodes)
goCmd += addOpt("mate", opts.Mate)
goCmd += addOpt("movetime", opts.MoveTime)
eng.send(goCmd)
lines := eng.receive("bestmove")
words := strings.Split(lines[len(lines)-1], " ")
bestmove := ""
ponder := ""
if len(words) >= 2 {
bestmove = words[1]
}
if len(words) >= 4 {
ponder = words[3]
}
return GoResp{
Bestmove: bestmove,
Ponder: ponder,
}
}
// Quit the Engine. Engine struct cannot be used after this command has been sent
func (eng *Engine) Quit() {
eng.send("quit")
eng.stdin = nil
eng.stdout = nil
eng.Meta = Meta{}
eng.Side = 0
eng.StartPos = ""
}