Class: Stockfish::Engine

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

Constant Summary collapse

COMMANDS =
%w( uci isready setoption ucinewgame position go stop ponderhit quit )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_path = `which stockfish`) ⇒ Engine

Returns a new instance of Engine.



16
17
18
19
20
21
22
23
# File 'lib/stockfish/engine.rb', line 16

def initialize(bin_path = `which stockfish`)
  @stdin, @stdout, @stderr, @wait_threads = Open3.popen3(bin_path)
  @pid = @wait_threads[:pid]
  @version = @stdout.readline.strip
  unless @version =~ /^Stockfish/
    raise InvalidBinary.new("Not a valid Stockfish binary!")
  end
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



12
13
14
# File 'lib/stockfish/engine.rb', line 12

def pid
  @pid
end

#stderrObject (readonly)

Returns the value of attribute stderr.



12
13
14
# File 'lib/stockfish/engine.rb', line 12

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



12
13
14
# File 'lib/stockfish/engine.rb', line 12

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



12
13
14
# File 'lib/stockfish/engine.rb', line 12

def stdout
  @stdout
end

#versionObject (readonly)

Returns the value of attribute version.



12
13
14
# File 'lib/stockfish/engine.rb', line 12

def version
  @version
end

#wait_threadsObject (readonly)

Returns the value of attribute wait_threads.



12
13
14
# File 'lib/stockfish/engine.rb', line 12

def wait_threads
  @wait_threads
end

Instance Method Details

#analyze(fen, options) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/stockfish/engine.rb', line 64

def analyze(fen, options)
  execute "position fen #{fen}"
  %w( depth movetime nodes ).each do |command|
    if (x = options[command.to_sym])
      return execute "go #{command} #{x}"
    end
  end
end

#execute(str) ⇒ Object



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
# File 'lib/stockfish/engine.rb', line 25

def execute(str)
  command = str.split(" ")[0]
  @stdin.puts str
  unless COMMANDS.include?(command)
    raise InvalidCommand.new(@stdout.readline.strip)
  end
  output = ""
  case command
  when "uci"
    loop do
      output << (line = @stdout.readline)
      break if line =~ /^uciok/
    end
  when "go"
    loop do
      output << (line = @stdout.readline)
      break if line =~ /^bestmove/
    end
  when "setoption"
    sleep 0.1
    raise InvalidOption.new(@stdout.readline.strip) if @stdout.ready?
  when "isready"
    output << @stdout.readline
  end
  output
end

#multipv(n) ⇒ Object



52
53
54
# File 'lib/stockfish/engine.rb', line 52

def multipv(n)
  execute "setoption name MultiPV value #{n}"
end

#ready?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/stockfish/engine.rb', line 56

def ready?
  execute("isready").strip == "readyok"
end

#running?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/stockfish/engine.rb', line 60

def running?
  @wait_threads.alive?
end