Class: GTP
- Inherits:
-
Object
- Object
- GTP
- Defined in:
- lib/gg.rb
Class Method Summary collapse
Instance Method Summary collapse
- #clean_gtp_response ⇒ Object
- #final_score ⇒ Object
- #genmove(color) ⇒ Object
-
#initialize(io) ⇒ GTP
constructor
A new instance of GTP.
- #list_dead_stones ⇒ Object
- #list_stones(color) ⇒ Object
- #loadsgf(path) ⇒ Object
- #protocol_version ⇒ Object
- #quit ⇒ Object
- #score(path) ⇒ Object
- #send_command(command, *arguments) ⇒ Object
Constructor Details
#initialize(io) ⇒ GTP
Returns a new instance of GTP.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gg.rb', line 15 def initialize(io) @io = io if block_given? begin yield self ensure quit end end end |
Class Method Details
.run(bot_type, &command) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/gg.rb', line 3 def self.run(bot_type, &command) if bot_type == 'gnugo' new(IO.popen("gnugo --mode gtp", "r+"), &command) elsif bot_type == 'fuego' new(IO.popen("fuego", "r+"), &command) else ## All falls here to accept commands from the yaml directly new(IO.popen(bot_type, "r+"), &command) end end |
Instance Method Details
#clean_gtp_response ⇒ Object
63 64 65 66 |
# File 'lib/gg.rb', line 63 def clean_gtp_response() response = @io.take_while { |line| line != "\n" }.join(" ") return response.sub(/^=\s+/, "") end |
#final_score ⇒ Object
53 54 55 56 |
# File 'lib/gg.rb', line 53 def final_score() @io.puts [:final_score] return self.clean_gtp_response end |
#genmove(color) ⇒ Object
36 37 38 |
# File 'lib/gg.rb', line 36 def genmove(color) send_command(:genmove, color) end |
#list_dead_stones ⇒ Object
58 59 60 61 |
# File 'lib/gg.rb', line 58 def list_dead_stones() @io.puts [:final_status_list, "dead"].join(" ") return self.clean_gtp_response end |
#list_stones(color) ⇒ Object
48 49 50 51 |
# File 'lib/gg.rb', line 48 def list_stones(color) @io.puts [:list_stones, color].join(" ") return self.clean_gtp_response end |
#loadsgf(path) ⇒ Object
40 41 42 |
# File 'lib/gg.rb', line 40 def loadsgf(path) send_command(:loadsgf, path) end |
#protocol_version ⇒ Object
32 33 34 |
# File 'lib/gg.rb', line 32 def protocol_version send_command(:protocol_version) end |
#quit ⇒ Object
27 28 29 30 |
# File 'lib/gg.rb', line 27 def quit send_command(:quit) @io.close end |
#score(path) ⇒ Object
44 45 46 |
# File 'lib/gg.rb', line 44 def score(path) send_command(:score, "aftermath", path) end |
#send_command(command, *arguments) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gg.rb', line 68 def send_command(command, *arguments) @io.puts [command, *arguments].join(" ") result = @io.take_while { |line| line != "\n" }.join rc = result.scan(/^=\s[a-zA-Z]*[0-9]*$/) if rc.first.nil? return rc else return rc.first.sub(/^=\s/, "").sub(/\n/, "") end end |