Class: Go::GTP

Inherits:
Object
  • Object
show all
Defined in:
lib/go/gtp.rb,
lib/go/gtp/board.rb,
lib/go/gtp/point.rb

Defined Under Namespace

Classes: Board, Point

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ GTP

Returns a new instance of GTP.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/go/gtp.rb', line 19

def initialize(io)
  @io         = io
  @id         = 0
  @last_error = nil
  
  if block_given?
    begin
      yield self
    ensure
      quit
    end
  end
end

Instance Attribute Details

#last_errorObject (readonly)

Returns the value of attribute last_error.



33
34
35
# File 'lib/go/gtp.rb', line 33

def last_error
  @last_error
end

Class Method Details

.run_gnugo(options = { }, &commands) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/go/gtp.rb', line 7

def self.run_gnugo(options = { }, &commands)
  directory    = options.fetch(:directory,    nil)
  command      = options.fetch(:command,      "gnugo --mode gtp")
  arguments    = options.fetch(:arguments,    nil)
  redirections = options.fetch(:redirections, "2>&1")
  path         = [ File.join(*[directory, command].compact),
                   arguments,
                   redirections ].compact.join(" ")

  new(IO.popen(path, "r+"), &commands)
end

Instance Method Details

#accuratelib(color, vertex) ⇒ Object



148
149
150
# File 'lib/go/gtp.rb', line 148

def accuratelib(color, vertex)
  extract_vertices(send_command(:accuratelib, color, vertex))
end

#advance_random_seed(games) ⇒ Object Also known as: advance_random_seed?



402
403
404
405
# File 'lib/go/gtp.rb', line 402

def advance_random_seed(games)
  send_command(:advance_random_seed, games)
  success?
end


157
158
159
# File 'lib/go/gtp.rb', line 157

def all_legal(color)
  extract_vertices(send_command(:all_legal, color))
end

#boardsize(boardsize) ⇒ Object Also known as: boardsize?



58
59
60
61
# File 'lib/go/gtp.rb', line 58

def boardsize(boardsize)
  send_command(:boardsize, boardsize)
  success?
end

#captures(color) ⇒ Object



161
162
163
# File 'lib/go/gtp.rb', line 161

def captures(color)
  extract_integer(send_command(:captures, color))
end

#clear_boardObject Also known as: clear_board?



68
69
70
71
# File 'lib/go/gtp.rb', line 68

def clear_board
  send_command(:clear_board)
  success?
end

#clear_cacheObject Also known as: clear_cache?



205
206
207
208
# File 'lib/go/gtp.rb', line 205

def clear_cache
  send_command(:clear_cache)
  success?
end

#color(vertex) ⇒ Object



132
133
134
# File 'lib/go/gtp.rb', line 132

def color(vertex)
  extract_color(send_command(:color, vertex))
end

#countlib(vertex) ⇒ Object



140
141
142
# File 'lib/go/gtp.rb', line 140

def countlib(vertex)
  extract_integer(send_command(:countlib, vertex))
end

#cputimeObject



346
347
348
# File 'lib/go/gtp.rb', line 346

def cputime
  send_command(:cputime)
end

#decrease_depthsObject Also known as: decrease_depths?



219
220
221
222
# File 'lib/go/gtp.rb', line 219

def decrease_depths
  send_command(:decrease_depths)
  success?
end

#draw_search_areaObject



434
435
436
# File 'lib/go/gtp.rb', line 434

def draw_search_area
  send_command(:draw_search_area)
end

#echo(string) ⇒ Object



370
371
372
# File 'lib/go/gtp.rb', line 370

def echo(string)
  send_command(:echo, string)
end

#echo_err(string) ⇒ Object



374
375
376
# File 'lib/go/gtp.rb', line 374

def echo_err(string)
  send_command(:echo_err, string)
end

#estimate_scoreObject



296
297
298
# File 'lib/go/gtp.rb', line 296

def estimate_score
  send_command(:estimate_score)
end

#experimental_score(color) ⇒ Object



300
301
302
# File 'lib/go/gtp.rb', line 300

def experimental_score(color)
  send_command(:experimental_score, color)
end

#final_score(random_seed = nil) ⇒ Object



283
284
285
# File 'lib/go/gtp.rb', line 283

def final_score(random_seed = nil)
  send_command(:final_score, *[random_seed].compact)
end

#final_status(vertex, random_seed = nil) ⇒ Object



287
288
289
# File 'lib/go/gtp.rb', line 287

def final_status(vertex, random_seed = nil)
  send_command(:final_status, *[vertex, random_seed].compact)
end

#final_status_list(status, random_seed = nil) ⇒ Object



291
292
293
294
# File 'lib/go/gtp.rb', line 291

def final_status_list(status, random_seed = nil)
  extract_vertices( send_command( :final_status_list,
                                  *[status, random_seed].compact ) )
end

#findlib(vertex) ⇒ Object



144
145
146
# File 'lib/go/gtp.rb', line 144

def findlib(vertex)
  extract_vertices(send_command(:findlib, vertex))
end

#fixed_handicap(number_of_stones) ⇒ Object



110
111
112
# File 'lib/go/gtp.rb', line 110

def fixed_handicap(number_of_stones)
  extract_vertices(send_command(:fixed_handicap, number_of_stones))
end

#genmove(color) ⇒ Object



233
234
235
# File 'lib/go/gtp.rb', line 233

def genmove(color)
  send_command(:genmove, color)
end

#get_connection_node_counterObject



340
341
342
# File 'lib/go/gtp.rb', line 340

def get_connection_node_counter
  send_command(:reset_connection_node_counter)
end

#get_handicapObject



124
125
126
# File 'lib/go/gtp.rb', line 124

def get_handicap
  send_command(:get_handicap)
end

#get_komiObject



90
91
92
# File 'lib/go/gtp.rb', line 90

def get_komi
  send_command(:get_komi)
end

#get_owl_node_counterObject



310
311
312
# File 'lib/go/gtp.rb', line 310

def get_owl_node_counter
  send_command(:reset_owl_node_counter)
end

#get_random_seedObject



392
393
394
# File 'lib/go/gtp.rb', line 392

def get_random_seed
  send_command(:get_random_seed)
end

#get_reading_node_counterObject



320
321
322
# File 'lib/go/gtp.rb', line 320

def get_reading_node_counter
  send_command(:reset_reading_node_counter)
end

#get_trymove_node_counterObject



330
331
332
# File 'lib/go/gtp.rb', line 330

def get_trymove_node_counter
  send_command(:reset_trymove_node_counter)
end

#gg_genmove(color, random_seed = nil) ⇒ Object



241
242
243
# File 'lib/go/gtp.rb', line 241

def gg_genmove(color, random_seed = nil)
  send_command(:gg_genmove, *[color, random_seed].compact)
end

#gg_undo(moves = nil) ⇒ Object Also known as: gg_undo?



265
266
267
268
# File 'lib/go/gtp.rb', line 265

def gg_undo(moves = nil)
  send_command(:gg_undo, *[moves].compact)
  success?
end

#helpObject



378
379
380
# File 'lib/go/gtp.rb', line 378

def help
  extract_lines(send_command(:help))
end

#increase_depthsObject Also known as: increase_depths?



213
214
215
216
# File 'lib/go/gtp.rb', line 213

def increase_depths
  send_command(:increase_depths)
  success?
end

#invariant_hashObject



179
180
181
# File 'lib/go/gtp.rb', line 179

def invariant_hash
  send_command(:invariant_hash)
end

#invariant_hash_for_moves(color) ⇒ Object



183
184
185
# File 'lib/go/gtp.rb', line 183

def invariant_hash_for_moves(color)
  extract_moves(send_command(:invariant_hash_for_moves, color))
end


152
153
154
# File 'lib/go/gtp.rb', line 152

def is_legal(color, vertex)
  extract_boolean(send_command(:is_legal, color, vertex))
end

#kgs_genmove_cleanup(color) ⇒ Object



249
250
251
# File 'lib/go/gtp.rb', line 249

def kgs_genmove_cleanup(color)
  send_command(:kgs_genmove_cleanup, color)
end

#known_command(command) ⇒ Object Also known as: known_command?



382
383
384
# File 'lib/go/gtp.rb', line 382

def known_command(command)
  extract_boolean(send_command(:known_command, command))
end

#komi(komi) ⇒ Object Also known as: komi?



84
85
86
87
# File 'lib/go/gtp.rb', line 84

def komi(komi)
  send_command(:komi, komi)
  success?
end

#last_moveObject



165
166
167
# File 'lib/go/gtp.rb', line 165

def last_move
  extract_move(send_command(:last_move))
end

#level(level) ⇒ Object Also known as: level?



253
254
255
256
# File 'lib/go/gtp.rb', line 253

def level(level)
  send_command(:level, level)
  success?
end

#limit_search(value) ⇒ Object Also known as: limit_search?



422
423
424
425
# File 'lib/go/gtp.rb', line 422

def limit_search(value)
  send_command(:limit_search, value)
  success?
end

#list_stones(color) ⇒ Object



136
137
138
# File 'lib/go/gtp.rb', line 136

def list_stones(color)
  extract_vertices(send_command(:list_stones, color))
end

#loadsgf(path, move_number_or_vertex = nil) ⇒ Object



128
129
130
# File 'lib/go/gtp.rb', line 128

def loadsgf(path, move_number_or_vertex = nil)
  send_command(:loadsgf, *[path, move_number_or_vertex].compact)
end

#move_historyObject



169
170
171
# File 'lib/go/gtp.rb', line 169

def move_history
  extract_moves(send_command(:move_history))
end

#nameObject



50
51
52
# File 'lib/go/gtp.rb', line 50

def name
  send_command(:name)
end

#orientation(orientation) ⇒ Object Also known as: orientation?



74
75
76
77
# File 'lib/go/gtp.rb', line 74

def orientation(orientation)
  send_command(:orientation, orientation)
  success?
end

#over?Boolean

Returns:

  • (Boolean)


173
174
175
176
177
# File 'lib/go/gtp.rb', line 173

def over?
  last_two_moves = move_history.first(2)
  Array(last_two_moves.first).last.to_s.upcase         == "RESIGN" or
  last_two_moves.map { |m| Array(m).last.to_s.upcase } == %w[PASS PASS]
end

#place_free_handicap(number_of_stones) ⇒ Object



114
115
116
# File 'lib/go/gtp.rb', line 114

def place_free_handicap(number_of_stones)
  extract_vertices(send_command(:place_free_handicap, number_of_stones))
end

#play(color, vertex) ⇒ Object Also known as: play?



94
95
96
97
# File 'lib/go/gtp.rb', line 94

def play(color, vertex)
  send_command(:play, color, vertex)
  success?
end

#popgoObject Also known as: popgo?



199
200
201
202
# File 'lib/go/gtp.rb', line 199

def popgo
  send_command(:popgo)
  success?
end

#printsgf(path = nil) ⇒ Object Also known as: printsgf?



356
357
358
359
360
361
362
363
# File 'lib/go/gtp.rb', line 356

def printsgf(path = nil)
  if path
    send_command(:printsgf, path)
    success?
  else
    send_command(:printsgf)
  end
end

#protocol_versionObject



46
47
48
# File 'lib/go/gtp.rb', line 46

def protocol_version
  send_command(:protocol_version)
end

#query_boardsizeObject



64
65
66
# File 'lib/go/gtp.rb', line 64

def query_boardsize
  send_command(:query_boardsize)
end

#query_orientationObject



80
81
82
# File 'lib/go/gtp.rb', line 80

def query_orientation
  send_command(:query_orientation)
end

#quitObject Also known as: quit?



39
40
41
42
43
# File 'lib/go/gtp.rb', line 39

def quit
  send_command(:quit)
  @io.close
  success?
end

#reg_genmove(color) ⇒ Object



237
238
239
# File 'lib/go/gtp.rb', line 237

def reg_genmove(color)
  send_command(:reg_genmove, color)
end

#replay(vertices) ⇒ Object Also known as: replay?



100
101
102
103
104
105
106
107
# File 'lib/go/gtp.rb', line 100

def replay(vertices)
  colors = %w[black white].cycle
  vertices.each do |vertex|
    play(colors.next, vertex)
    return success? unless success?
  end
  success?
end

#report_uncertainty(on_or_off) ⇒ Object Also known as: report_uncertainty?



387
388
389
# File 'lib/go/gtp.rb', line 387

def report_uncertainty(on_or_off)
  send_command(:report_uncertainty, on_or_off)
end

#reset_connection_node_counterObject Also known as: reset_connection_node_counter?



334
335
336
337
# File 'lib/go/gtp.rb', line 334

def reset_connection_node_counter
  send_command(:reset_connection_node_counter)
  success?
end

#reset_owl_node_counterObject Also known as: reset_owl_node_counter?



304
305
306
307
# File 'lib/go/gtp.rb', line 304

def reset_owl_node_counter
  send_command(:reset_owl_node_counter)
  success?
end

#reset_reading_node_counterObject Also known as: reset_reading_node_counter?



314
315
316
317
# File 'lib/go/gtp.rb', line 314

def reset_reading_node_counter
  send_command(:reset_reading_node_counter)
  success?
end

#reset_search_maskObject Also known as: reset_search_mask?



416
417
418
419
# File 'lib/go/gtp.rb', line 416

def reset_search_mask
  send_command(:reset_search_mask)
  success?
end

#reset_trymove_node_counterObject Also known as: reset_trymove_node_counter?



324
325
326
327
# File 'lib/go/gtp.rb', line 324

def reset_trymove_node_counter
  send_command(:reset_trymove_node_counter)
  success?
end

#restricted_genmove(color, *vertices) ⇒ Object



245
246
247
# File 'lib/go/gtp.rb', line 245

def restricted_genmove(color, *vertices)
  send_command(:restricted_genmove, color, *vertices)
end

#set_free_handicap(*vertices) ⇒ Object Also known as: set_free_handicap?



118
119
120
121
# File 'lib/go/gtp.rb', line 118

def set_free_handicap(*vertices)
  send_command(:set_free_handicap, *vertices)
  success?
end

#set_random_seed(random_seed) ⇒ Object Also known as: set_random_seed?



396
397
398
399
# File 'lib/go/gtp.rb', line 396

def set_random_seed(random_seed)
  send_command(:set_random_seed, random_seed)
  success?
end

#set_search_diamond(position) ⇒ Object Also known as: set_search_diamond?



410
411
412
413
# File 'lib/go/gtp.rb', line 410

def set_search_diamond(position)
  send_command(:set_search_diamond, position)
  success?
end

#set_search_limit(position) ⇒ Object Also known as: set_search_limit?



428
429
430
431
# File 'lib/go/gtp.rb', line 428

def set_search_limit(position)
  send_command(:set_search_limit, position)
  success?
end

#showboardObject



350
351
352
# File 'lib/go/gtp.rb', line 350

def showboard
  Board.new(send_command(:showboard))
end

#success?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/go/gtp.rb', line 35

def success?
  @last_error.nil?
end

#time_left(color, time, stones) ⇒ Object Also known as: time_left?



277
278
279
280
# File 'lib/go/gtp.rb', line 277

def time_left(color, time, stones)
  send_command(:time_left, color, time, stones)
  success?
end

#time_settings(main_time, byo_yomi_time, byo_yomi_stones) ⇒ Object Also known as: time_settings?



271
272
273
274
# File 'lib/go/gtp.rb', line 271

def time_settings(main_time, byo_yomi_time, byo_yomi_stones)
  send_command(:time_settings, main_time, byo_yomi_time, byo_yomi_stones)
  success?
end

#tryko(color, vertex) ⇒ Object Also known as: tryko?



193
194
195
196
# File 'lib/go/gtp.rb', line 193

def tryko(color, vertex)
  send_command(:tryko, color, vertex)
  success?
end

#trymove(color, vertex) ⇒ Object Also known as: trymove?



187
188
189
190
# File 'lib/go/gtp.rb', line 187

def trymove(color, vertex)
  send_command(:trymove, color, vertex)
  success?
end

#tune_move_ordering(*move_ordering_parameters) ⇒ Object



366
367
368
# File 'lib/go/gtp.rb', line 366

def tune_move_ordering(*move_ordering_parameters)
  send_command(:tune_move_ordering, *move_ordering_parameters)
end

#unconditional_status(vertex) ⇒ Object



227
228
229
# File 'lib/go/gtp.rb', line 227

def unconditional_status(vertex)
  send_command(:unconditional_status, vertex)
end

#undoObject Also known as: undo?



259
260
261
262
# File 'lib/go/gtp.rb', line 259

def undo
  send_command(:undo)
  success?
end

#versionObject



54
55
56
# File 'lib/go/gtp.rb', line 54

def version
  send_command(:version)
end