Class: Stockshark::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/stockshark/engine.rb

Overview

The public façade for talking to a UCI engine — the only class most callers should ever need to touch. Hides process management (Process) and protocol parsing (UciProtocol) entirely: callers only ever see "give me a FEN and a depth/movetime, get back an evaluation."

Defined Under Namespace

Classes: AnalysisResult, Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = Stockshark::Configuration.new, process: nil) ⇒ Engine

Returns a new instance of Engine.



21
22
23
24
25
26
# File 'lib/stockshark/engine.rb', line 21

def initialize(configuration = Stockshark::Configuration.new, process: nil)
  @configuration = configuration
  @process = process || Stockshark::Process.new(configuration.path)
  @multipv = 1
  handshake
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/stockshark/engine.rb', line 19

def name
  @name
end

Instance Method Details

#analyze(fen:, depth: nil, movetime_ms: nil, multipv: 1) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
# File 'lib/stockshark/engine.rb', line 28

def analyze(fen:, depth: nil, movetime_ms: nil, multipv: 1)
  raise ArgumentError, "must give depth or movetime_ms" unless depth || movetime_ms

  set_multipv(multipv)
  send_line("position fen #{fen}")
  send_line(depth ? "go depth #{depth}" : "go movetime #{movetime_ms}")

  collect_result(search_timeout(movetime_ms))
end

#quitObject

Safe to call more than once, and safe to call even if the engine was never fully brought up (e.g. handshake itself failed).



40
41
42
# File 'lib/stockshark/engine.rb', line 40

def quit
  @process.stop
end