Top Level Namespace

Defined Under Namespace

Classes: ConfigNode, GTP, KayaBot, Node, SGF

Constant Summary collapse

SGF_FILE_PATH =
"bot_games/"

Instance Method Summary collapse

Instance Method Details

#ai_move(game_id, game_sgf, color) ⇒ Object

return a move coordinates, such as “c17”, or “PASS”, or “resign”



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gg.rb', line 107

def ai_move(game_id, game_sgf, color)

  re = nil
  if Dir[SGF_FILE_PATH].empty?
    Dir.mkdir(SGF_FILE_PATH)
  end
  filepath = SGF_FILE_PATH + "#{game_id}.sgf"
  File.open(filepath, "w") do |f|
    f.write game_sgf
  end

  size = 19
  GTP.run(@bot) do |gtp|
    gtp.loadsgf filepath
    size = @master_node.match(/SZ\[(\d+)\]/)[1]
    re = gtp.genmove color
  end
  $stdout.puts "Bot generated #{re}"
  move = convert_move(re,size)

  File.delete(filepath)
  return move
end

#convert_move(move, size = 19) ⇒ Object

switches GTP move into sgf-like coordinate



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gg.rb', line 132

def convert_move(move, size=19)
  if move.downcase == "pass"
    return 'pass'
  elsif move.downcase == "resign"
    return 'resign'
  else


    alphabet = "ABCDEFGHIJKLMNOPQRS"[0..size.to_i - 1]
	  	
    sgf_alphabet = "ABCDEFGHJKLMNOPQRST"[0..size.to_i - 1]
	  	
    return alphabet["ABCDEFGHJKLMNOPQRST".index(move[0])].downcase + alphabet.reverse[(move[1].to_s + move[2].to_s).to_i - 1].downcase
  end
end

#score_game(game_id, game_sgf) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gg.rb', line 83

def score_game(game_id, game_sgf)
  re = nil
  if Dir[SGF_FILE_PATH].empty?
    Dir.mkdir(SGF_FILE_PATH)
  end
  filepath = SGF_FILE_PATH + "#{game_id}.sgf"
  File.open(filepath, "w") do |f|
    f.write game_sgf
  end
  
  dead_stones = ""
  GTP.run(@bot) do |gtp|
    gtp.loadsgf filepath
    dead_stones = gtp.list_dead_stones
    re = gtp.final_score
  end

  $stdout.puts "dead stones are #{dead_stones}"

  File.delete(filepath)
  return {:score => re, :dead_stones => dead_stones}
end