Class: GTP

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

Class Method Summary collapse

Instance Method Summary collapse

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_responseObject



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_scoreObject



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_stonesObject



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_versionObject



32
33
34
# File 'lib/gg.rb', line 32

def protocol_version
  send_command(:protocol_version)
end

#quitObject



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