Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/8951065 to your computer and use it in GitHub Desktop.
Save atduskgreg/8951065 to your computer and use it in GitHub Desktop.
require 'open3'
require 'io/wait'
require 'thread'
class Uci
attr_accessor :to
attr_accessor :from
def initialize path_to_engine
@to, @from = Open3.popen2e "/usr/local/bin/stockfish"
end
def send command
puts ">#{command}"
to.puts command
end
end
uci = Uci.new "/usr/local/bin/stockfish"
queue = Queue.new
Thread.new do
while true do
if uci.from.ready?
queue.enq uci.from.readline
end
sleep 0.25
end
end
loop do
res = queue.deq # this function blocks.
puts "=>#{res}"
if res =~ /^Stockfish/
uci.send "uci"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment