forked from malbrecht/chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
126 lines (102 loc) · 3.88 KB
/
engine.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
// package engine defines a generic interface for communicating with chess
// engines.
package engine
import (
"errors"
"time"
"github.com/jonpchin/chess"
)
var (
// ErrTimeout indicates a timeout in communicating with the engine.
ErrTimeout = errors.New("timeout in engine communication")
// ErrExited indicates that the engine was closed.
ErrExited = errors.New("engine was closed")
)
// Engine provides a generic interface to a running chess engine.
type Engine interface {
// SetPosition sets the position to search.
SetPosition(board *chess.Board)
// Search starts an infinite search of the position; that is, until
// Stop is called. During the search, Info's will be sent on the
// channel that is returned. The last Info on the channel, and only the
// last, will either return a non-nil Info.Err or an Info.BestMove,
// after which the channel is closed.
Search() <-chan Info
// SearchDepth is like Search but tells the engine to stop at a certain
// depth.
SearchDepth(depth int) <-chan Info
// SearchTime is like Search but tells the engine to stop after the
// given time.
SearchTime(t time.Duration) <-chan Info
// SearchClock is like Search but informs the engine of the time
// controls of the game and lets the engine decide how much time to
// use. movesToGo is the number of moves to the next time control.
SearchClock(wtime, btime, winc, binc time.Duration, movesToGo int) <-chan Info
// Stop stops a search started by one of the SearchXXX functions.
Stop()
// Quit quits the engine process.
Quit()
// Ping pings the engine process to check that it is still responding.
Ping() error
// Options returns the settable options of the engine.
Options() map[string]Option
}
// Pv holds the details of a principal variation from an engine search.
type Pv struct {
Moves []chess.Move // principal variation moves
Score int // centipawns or moves-to-mate; positive is good for white
Mate bool // if yes then Score is moves-to-mate
Upperbound bool // Score is a upperbound
Lowerbound bool // Score is a lowerbound
Rank int // 0-based rank of the pv in a MultiPV search
}
// Stats holds statistics from an engine search.
type Stats struct {
Depth int // depth in plies
SelDepth int // selective depth
Nodes int // number of nodes searched so far
Time time.Duration // amount of time searched so far
}
// Info represents a generic information "event" sent over an Info channel
// while an engine search is in progress.
type Info interface {
// Err returns an error if one occured. This should be the first thing
// to check, before calling the other methods.
Err() error
// BestMove returns the best move found by the engine. It returns !ok
// if no best move has been found yet.
BestMove() (move chess.Move, ok bool)
BestMoveRaw() (string, bool)
// Pv returns the principal variation of this Info. It can be nil if no
// pv information is available.
Pv() *Pv
// Stats returns statistics of the search so far. Any of the Stats
// fields can be zero, meaning the information is not present (or
// actually zero).
Stats() *Stats
}
// Engine options
// Option represents a generic settable engine option.
type Option interface {
String() string // current value
StringDefault() string // default value
Set(value string) // change the value
}
// StringOption represents string option.
type StringOption interface {
Option
}
// IntOption represents a number option, possibly with a limited range.
type IntOption interface {
Int() int // current value
SetInt(int) // change value
Default() int // default value
Min() int // minimum value
Max() int // maximum value (0 = no maximum)
}
// BoolOption represents a togglable option.
type BoolOption interface {
Bool() bool // current value
SetBool(bool) // change value
Default() bool // default value
}