-
Notifications
You must be signed in to change notification settings - Fork 14
/
engine_repl.go
205 lines (186 loc) · 5.78 KB
/
engine_repl.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
// Copyright (c) 2014-2018 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.
package donna
import(
`fmt`
`io/ioutil`
`regexp`
`runtime`
`strconv`
`strings`
`time`
)
var (
ansiRed = "\033[0;31m"
ansiGreen = "\033[0;32m"
ansiTeal = "\033[0;36m"
ansiWhite = "\033[0;37m"
ansiNone = "\033[0m"
)
func (e *Engine) replBestMove(move Move) *Engine {
fmt.Printf(ansiTeal + "Donna's move: %s", move)
if game.nodes == 0 {
fmt.Printf(" (book)")
}
fmt.Println(ansiNone + "\n")
return e
}
func (e *Engine) replPrincipal(depth, score, status int, duration int64) {
fmt.Printf(`%2d %s %9d %9d %8.1fK %6.1f%% `, depth, ms(duration), game.nodes, game.qnodes, float32(nps(duration)) / 1000.0, float32(hashfull()) / 10.0)
switch status {
case WhiteWon:
fmt.Println(`1-0 White Checkmates`)
case BlackWon:
fmt.Println(`0-1 Black Checkmates`)
case Stalemate:
fmt.Println(`1/2 Stalemate`)
case Repetition:
fmt.Println(`1/2 Repetition`)
case FiftyMoves:
fmt.Println(`1/2 Fifty Moves`)
case WhiteWinning, BlackWinning: // Show moves till checkmate.
fmt.Printf("%6dX %v Checkmate\n", (Checkmate - abs(score)) / 2 + 1, game.rootpv.moves[0:game.rootpv.size])
default:
fmt.Printf("%7.2f %v\n", float32(score) / float32(onePawn), game.rootpv.moves[0:game.rootpv.size])
}
}
// There are two types of command interfaces in the world of computing: good
// interfaces and user interfaces. -- Daniel J. Bernstein
func (e *Engine) Repl() *Engine {
var game *Game
var position *Position
// Suppress ANSI colors when running Windows.
if runtime.GOOS == `windows` {
ansiRed, ansiGreen, ansiTeal, ansiNone = ``, ``, ``, ``
}
setup := func() {
if game == nil || position == nil {
game = NewGame()
position = game.start()
fmt.Printf("%s\n", position)
}
}
think := func() {
if move := game.Think(); move != 0 {
position = position.makeMove(move)
fmt.Printf("%s\n", position)
}
}
book := func(fileName string) {
if e.bookFile = fileName; e.bookFile == `` {
fmt.Println(`Using no opening book`)
} else {
fmt.Printf("Using opening book %s\n", fileName)
}
}
benchmark := func(fileName string) {
maxDepth, moveTime := e.options.maxDepth, e.options.moveTime
e.options.maxDepth, e.options.moveTime = 0, 10000
defer func() {
e.options.maxDepth, e.options.moveTime = maxDepth, moveTime
if err := recover(); err != nil {
fmt.Printf("Error loading %s\n", fileName)
}
}()
content, err := ioutil.ReadFile(fileName)
if err == nil {
total, solved := 0, 0
re := regexp.MustCompile(`[\s\+\?!]`)
NextLine:
for _, line := range strings.Split(string(content), "\n") {
if len(line) > 0 && line[0] != '#' {
total++
game := NewGame(line)
position := game.start()
best := strings.Split(line, ` # `)[1] // TODO: add support for "am" (avoid move).
fmt.Printf(ansiTeal + "%d) %s for %s" + ansiNone + "\n%s\n", total, best, C(position.color), position)
move := game.Think()
for _, nextBest := range strings.Split(best, ` `) {
if move.str() == re.ReplaceAllLiteralString(nextBest, ``) {
solved++
fmt.Printf(ansiGreen + "%d) Solved (%d/%d %2.1f%%)\n\n\n" + ansiNone, total, solved, total - solved, float32(solved) * 100.0 / float32(total))
continue NextLine
}
}
fmt.Printf(ansiRed + "%d) Not solved (%d/%d %2.1f%%)\n\n\n" + ansiNone, total, solved, total - solved, float32(solved) * 100.0 / float32(total))
}
}
} else {
fmt.Printf("Could not open benchmark file '%s'\n", fileName)
}
}
perft := func(parameter string) {
if parameter == `` {
parameter = `5`
}
if depth, err := strconv.Atoi(parameter); err == nil {
position := NewGame().start()
start := time.Now()
total := position.Perft(depth)
finish := since(start)
fmt.Printf(" Depth: %d\n", depth)
fmt.Printf(" Nodes: %d\n", total)
fmt.Printf("Elapsed: %s\n", ms(finish))
fmt.Printf("Nodes/s: %dK\n", total / finish)
}
}
fmt.Printf("Donna v%s Copyright (c) 2014-2018 by Michael Dvorkin. All Rights Reserved.\nType ? for help.\n\n", Version)
for command, parameter := ``, ``; ; command, parameter = ``, `` {
fmt.Print(`donna> `)
fmt.Scanln(&command, ¶meter)
switch command {
case ``:
case `bench`:
benchmark(parameter)
case `book`:
book(parameter)
case `exit`, `quit`:
return e
case `go`:
setup()
think()
case `help`, `?`:
fmt.Println("The commands are:\n\n" +
" bench <file> Run benchmarks\n" +
" book <file> Use opening book\n" +
" exit Exit the program\n" +
" go Take side and make a move\n" +
" help Display this help\n" +
" new Start new game\n" +
" perft [depth] Run perft test\n" +
" score Show evaluation summary\n" +
" undo Undo last move\n\n" +
"To make a move use algebraic notation, for example e2e4, Ng1f3, or e7e8Q\n")
case `new`:
game, position = nil, nil
setup()
case `perft`:
perft(parameter)
case `score`:
setup()
_, metrics := position.EvaluateWithTrace()
Summary(metrics)
case `undo`:
if position != nil {
position = position.undoLastMove()
fmt.Printf("%s\n", position)
}
default:
setup()
if move, validMoves := NewMoveFromString(position, command); move != 0 {
position = position.makeMove(move)
think()
} else { // Invalid move or non-evasion on check.
fancy := e.fancy; e.fancy = false
fmt.Printf("%s appears to be an invalid move; valid moves are %v\n", command, validMoves)
e.fancy = fancy
}
}
}
return e
}