Permalink
Browse files

Adding some convenience commands.

  • Loading branch information...
1 parent 92c5c4a commit 394e0e8ad2207afe781b033ab00f8c1d9e86f79b @JEG2 committed Oct 2, 2010
Showing with 38 additions and 3 deletions.
  1. +1 −3 example/play_go.rb
  2. +16 −0 lib/go/gtp.rb
  3. +21 −0 spec/gtp_spec.rb
View
4 example/play_go.rb
@@ -5,7 +5,6 @@ class IllegalMoveError < RuntimeError; end
COLORS = %w[black white]
PLAYERS = Hash[COLORS.zip(%w[computer player].sample(2))]
-passes = 0
begin
Go::GTP.run_gnugo(arguments: ARGV.empty? ? nil : ARGV) do |go|
@@ -32,8 +31,7 @@ class IllegalMoveError < RuntimeError; end
puts "Move for #{color}: #{move}"
end
puts
- passes = move =~ /\bpass\b/i ? passes + 1 : 0
- break if move =~ /\bresign\b/i or passes >= 2
+ break if go.over?
end
puts "Final score: #{go.final_score}"
end
View
16 lib/go/gtp.rb
@@ -97,6 +97,16 @@ def play(color, vertex)
end
alias_method :play?, :play
+ def replay(vertices)
+ colors = %w[black white].cycle
+ vertices.each do |vertex|
+ play(colors.next, vertex)
+ return success? unless success?
+ end
+ success?
+ end
+ alias_method :replay?, :replay
+
def fixed_handicap(number_of_stones)
extract_vertices(send_command(:fixed_handicap, number_of_stones))
end
@@ -160,6 +170,12 @@ def move_history
extract_moves(send_command(:move_history))
end
+ 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
+
def invariant_hash
send_command(:invariant_hash)
end
View
21 spec/gtp_spec.rb
@@ -141,5 +141,26 @@ def add_input(input)
add_input("=1 help\nknown_command")
@go.help.should == %w[help known_command]
end
+
+ it "should detect a resignation as a game over condition" do
+ add_input("=1 black RESIGN")
+ @go.should be_over
+ end
+
+ it "should detect two passes as a game over condition" do
+ add_input("=1 black PASS\nwhite PASS")
+ @go.should be_over
+ end
+
+ it "should not any other game over conditions" do
+ add_input("=1 black E4\nwhite PASS")
+ @go.should_not be_over
+ end
+
+ it "should allow you to replay a series of moves" do
+ add_input("=1\n\n=2\n\n=3\n\n=4 black E6")
+ @go.replay(%w[E4 E5 E6]).should be(true)
+ @go.last_move.should == %w[black E6]
+ end
end
end

0 comments on commit 394e0e8

Please sign in to comment.