class Stockfish::Engine

Constants

COMMANDS

Attributes

pid[R]
stderr[R]
stdin[R]
stdout[R]
version[R]
wait_threads[R]

Public Class Methods

new(bin_path = `which stockfish`) click to toggle source
# 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

Public Instance Methods

analyze(fen, options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/stockfish/engine.rb, line 52
def multipv(n)
  execute "setoption name MultiPV value #{n}"
end
ready?() click to toggle source
# File lib/stockfish/engine.rb, line 56
def ready?
  execute("isready").strip == "readyok"
end
running?() click to toggle source
# File lib/stockfish/engine.rb, line 60
def running?
  @wait_threads.alive?
end