-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuci.go
245 lines (206 loc) · 4.73 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
package main
import (
"bufio"
"fmt"
"github.com/dylhunn/dragontoothmg"
"log"
"math"
"os"
"strconv"
"strings"
)
var game dragontoothmg.Board
var debug bool = false
var uciOutput bool = false
// http://page.mi.fu-berlin.de/block/uci.htm
// Implements the universal chess interface
type UCI interface { //all functions called by chess gui or engine tester
uci()
stop()
gouci([]string)
isReady()
sendId()
sendOptions()
perft([]string)
parseUciCommand([]string)
validateUciCommand([]string)
}
type UCIs struct {
}
// Starts the UCI process, waiting for command line input from other software
func (u *UCIs) Start() {
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString('\n')
if err != nil {
printLog("Error in UCI command.")
}
args := strings.Fields(line)
if debug {
log.Println("<- ", args)
}
if !u.parseUciCommand(args) {
u.quit()
break
}
}
}
// Parses UCI command and excecutes
func (u *UCIs) parseUciCommand(args []string) bool {
switch args[0] {
case "quit":
return false
case "uci":
uciOutput = true
u.sendId()
u.sendOptions()
case "debug":
debug = true
case "setoption":
//TODO as soon as options are avaibale
case "position":
setGamePosition(&game, args[1:])
case "go":
u.go_(args[1:])
case "eval":
fmt.Println(getBoardValue(&game))
case "stop":
//TODO: stop engine search, return bestmove
case "ponderhit":
//TODO, maybe?
case "isready":
u.isReady()
default:
printMessage("UCI Command not recognized.")
}
return true
}
// GUI/Runner sends information about the position and moves played from that position
// position can be sent via "startpos" or fen string
// for game setup position is sent without moves
func setGamePosition(g *dragontoothmg.Board, args []string) {
movesLocation := getMovesLocation(args)
if movesLocation == -1 { //no moves sent just set position
*g = getGameFromFen(args)
} else {
*g = getGameFromFen(args[:movesLocation])
// get moves and apply to board
for _, m := range args[movesLocation+1:] {
move, err := dragontoothmg.ParseMove(m)
if err != nil {
panic(err)
}
g.Apply(move)
}
}
//log.Println("debug", game.ToFen())
}
func getGameFromFen(args []string) dragontoothmg.Board {
if args[0] == "fen" {
fen := strings.Join(args[1:], " ")
return dragontoothmg.ParseFen(fen)
} else if args[0] == "startpos" {
return dragontoothmg.ParseFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
} else {
printMessage("ERROR: Invalid command, fen or startpos needed.") //TODO: maybe check this in validateUciCommand?
}
panic("s")
}
func (u *UCIs) go_(args []string) {
setGameTimes(args)
bestMove := calculateBestMove(game, 0)
printMessage("bestmove " + bestMove.String())
}
func (u *UCIs) quit() {
printMessage("Exiting")
}
func (u *UCIs) sendId() {
printMessage("id name Spot " + BuildVersion + " (" + CommitHash + ")")
printMessage("id author Phil Baum")
}
func (u *UCIs) sendOptions() {
//TODO: sent options which can be changed eg. hashsize
printMessage("uciok")
}
func (u *UCIs) debug() {
printMessage("debug")
}
func (u *UCIs) uci() {
u.sendId()
u.sendOptions()
}
func (u *UCIs) isReady() {
printMessage("readyok")
}
func printMessage(cmd string) {
if debug {
log.Println("-> ", cmd)
}
if uciOutput {
fmt.Println(cmd)
}
}
func printLog(msg string) {
if debug {
log.Println("::DBG:: ", msg)
}
}
func printUCIInfo(move string, depth int, ms int, nodes int, score int, pv []string) {
var sb strings.Builder
sb.WriteString("info depth " + strconv.Itoa(depth) + " time " + strconv.Itoa(ms) + " nodes " + strconv.Itoa(nodes))
if move != "" {
sb.WriteString(" currmove " + move)
}
if score != 0 {
sb.WriteString(fmt.Sprintf(" score cp %.1f", math.Round(float64(score))/100))
}
if pv != nil && len(pv) > 0 {
pvReversed := reverseStringSlice(pv)
sb.WriteString(" pv " + strings.Join(pvReversed, " "))
}
printMessage(sb.String())
}
func getMovesLocation(args []string) int {
for i, a := range args {
if a == "moves" {
return i
}
}
return -1
}
func setGameTimes(args []string) {
for i, a := range args {
if a == "wtime" {
if game.Wtomove {
mytime = convertTime(args[i+1])
} else {
opptime = convertTime(args[i+1])
}
} else if a == "btime" {
if game.Wtomove {
opptime = convertTime(args[i+1])
} else {
mytime = convertTime(args[i+1])
}
} else if a == "winc" {
if game.Wtomove {
myinc = convertTime(args[i+1])
} else {
oppinc = convertTime(args[i+1])
}
} else if a == "binc" {
if game.Wtomove {
oppinc = convertTime(args[i+1])
} else {
myinc = convertTime(args[i+1])
}
}
}
}
func convertTime(time string) int64 {
s, err := strconv.Atoi(time)
if err != nil {
panic(err)
}
return int64(s)
}