Created
September 23, 2010 15:02
-
-
Save JEG2/593759 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Go | |
class GTP | |
class Board | |
STONES = {"X" => "black", "O" => "white"} | |
def initialize(board_string) | |
@string = board_string | |
@array = nil | |
end | |
def [](*args) | |
if args.size == 2 and args.all? { |n| n.is_a? Integer } | |
x, y = args | |
to_a[to_a.size - (y + 1)][x] | |
elsif args.size == 1 and args.first =~ /\A([A-Z])(\d{1,2})\z/ | |
self[$1.getbyte(0) - "A".getbyte(0), $2.to_i - 1] | |
else | |
fail ArgumentError, "must index by coordinates or vertex" | |
end | |
end | |
def captures(color) | |
@string[/#{Regexp.escape(color)}(?: \([XO])?\) has captured (\d+)/i, 1] | |
.to_i | |
end | |
def to_s | |
@string | |
end | |
def to_a | |
@array ||= @string.scan(/^\s*(\d+)((?:.[.+XO])+).\1\b/) | |
.map { |_, row| row.chars | |
.each_slice(2) | |
.map { |_, stone| STONES[stone] } } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment