Last active
August 29, 2015 13:56
-
-
Save atduskgreg/8951065 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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