-
Notifications
You must be signed in to change notification settings - Fork 0
/
uci.go
172 lines (158 loc) · 3.88 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
package chesskimo
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
type UCI struct {
newGame bool
}
func (u *UCI) RunInputOutputLoop(engine *Engine) {
reader := bufio.NewReader(os.Stdin)
if reader == nil {
panic("Cannot read from std in.")
}
for {
command, err := reader.ReadString('\n')
engine.logger.Print("<-- cmd: ", command)
if err == io.EOF {
break
} else if err == nil && len(command) > 0 {
// Split input string into command parts.
input := strings.Split(strings.Trim(command, " \t\r\n"), " ")
cmd := input[0]
switch cmd {
case "quit":
// TODO -> shutdown engine.
os.Exit(0)
case "uci":
// Enable UCI mode and identify yourself.
u.cmdUci(engine)
case "isready":
// Check if engine can receive commands/is active.
u.cmdIsready()
case "ucinewgame":
u.cmdNewGame(engine)
case "position":
u.cmdPosition(engine, input[1:])
case "go":
u.cmdGo(engine, input[1:])
case "stop":
u.cmdStop(engine)
}
}
}
// TODO -> shutdown engine.
os.Exit(0)
}
func (u *UCI) cmdStop(engine *Engine) {
// TODO stop engine
}
func (u *UCI) cmdGo(engine *Engine, args []string) {
for len(args) > 0 {
cmd := args[0]
args = args[1:]
switch cmd {
case "searchmoves":
case "ponder":
case "wtime":
case "btime":
case "winc":
case "binc":
case "movestogo":
case "depth":
case "nodes":
case "mate":
case "movetime":
case "infinite":
}
}
// moves := engine.GetLegalMoves()
// r := rand.Intn(int(moves.Size))
// bm := moves.Moves[r]
ts := SearchSettings{}
dostop := uint32(0)
sr := engine.search(engine, &ts, &dostop)
engine.logger.Println("--> best move:", sr.Move.MiniNotation())
engine.board.MakeLegalMove(sr.Move)
engine.logger.Print(engine.board.String())
fmt.Println("bestmove", sr.Move.MiniNotation())
}
func (u *UCI) cmdPosition(engine *Engine, args []string) {
if len(args) > 1 {
if !u.newGame {
// Continue ongoing game by just extracting the last move.
engine.logger.Print("*** continue ongoing game")
last := args[len(args)-1]
err := engine.MakeMove(last)
if err != nil {
// UCI is crap.
engine.logger.Print("*** move ", last, " impossible: ", err.Error())
}
} else {
first := args[0]
if first == "startpos" {
args = args[1:]
engine.board.SetStartingPosition()
} else {
if first == "fen" {
// Some frontends say "position fen" then specify the actual fen, some specify
// the actual fen directly after "position", so we have to check both ways..
// no wonder with this as official doc: http://wbec-ridderkerk.nl/html/UCIProtocol.html
// *sigh*
args = args[1:]
if len(args) > 0 {
first = args[0]
} else {
return // Invalid.. missing arguments
}
}
fen := ""
// We now have to concat all FEN parts.
for i := 0; i < len(args); i++ {
if args[i] != "moves" {
// TODO - remove all quotes... just to be sure.
fen += args[i] + " "
} else {
// Finished FEN.. discard fen arguments.
args = args[i:]
}
}
err := engine.board.SetFEN(fen)
if err != nil {
fmt.Println("--> error: ", err.Error())
return // UCI ignores bad commands.
}
}
// Last check for "moves" subcommand.
// Must at least have length 2 (including a move).
if len(args) > 1 && args[0] == "moves" {
moves := args[1:]
for _, m := range moves {
err := engine.MakeMove(m)
if err != nil {
// UCI is crap.
engine.logger.Print("*** move ", m, " impossible: ", err.Error())
}
}
}
}
u.newGame = false
}
}
func (u *UCI) cmdNewGame(engine *Engine) {
u.newGame = true
engine.NewGame()
}
func (u *UCI) cmdIsready() {
// TODO wait for engine ?
fmt.Println("readyok")
}
func (u *UCI) cmdUci(engine *Engine) {
fmt.Println("id name", engine.name)
fmt.Println("id author", engine.author)
// TODO -> add all possible options here.
fmt.Println("uciok")
}