Class: Stone
- Inherits:
-
Object
- Object
- Stone
- Defined in:
- lib/gorb/stone.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#group ⇒ Object
Returns the value of attribute group.
-
#point ⇒ Object
readonly
Returns the value of attribute point.
Instance Method Summary collapse
-
#find_group ⇒ Object
Find the Group of the Stone or create a new one.
-
#initialize(board, point, color) ⇒ Stone
constructor
A new instance of Stone.
-
#liberties ⇒ Object
Return the liberties of the Stone.
-
#neighbors ⇒ Object
Return the neighboring points of the Stone.
- #to_s ⇒ Object
Constructor Details
#initialize(board, point, color) ⇒ Stone
Returns a new instance of Stone.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/gorb/stone.rb', line 8 def initialize(board, point, color) @board = board @point = point @color = color if (@point[1, 2].to_i > @board.size.split('x')[0].to_i or not @board.letters.index(@point[0])) raise ArgumentError, "Invalid point" end @group = self.find_group end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
6 7 8 |
# File 'lib/gorb/stone.rb', line 6 def board @board end |
#color ⇒ Object (readonly)
Returns the value of attribute color.
6 7 8 |
# File 'lib/gorb/stone.rb', line 6 def color @color end |
#group ⇒ Object
Returns the value of attribute group.
5 6 7 |
# File 'lib/gorb/stone.rb', line 5 def group @group end |
#point ⇒ Object (readonly)
Returns the value of attribute point.
6 7 8 |
# File 'lib/gorb/stone.rb', line 6 def point @point end |
Instance Method Details
#find_group ⇒ Object
Find the Group of the Stone or create a new one. If this Stone connects one or more groups, merge them together to a single Group.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gorb/stone.rb', line 36 def find_group groups = [] stones = @board.search(neighbors) stones.each do |stone| if stone.color == @color and not groups.include? stone.group groups << stone.group end end if groups.empty? return Group.new(@board, self) else group = groups.pop group.merge(groups) group << self return group end end |
#liberties ⇒ Object
Return the liberties of the Stone.
27 28 29 30 31 32 |
# File 'lib/gorb/stone.rb', line 27 def liberties liberties = neighbors stones = @board.search(neighbors) stones.each {|stone| liberties.delete(stone.point)} return liberties end |
#neighbors ⇒ Object
Return the neighboring points of the Stone.
22 23 24 |
# File 'lib/gorb/stone.rb', line 22 def neighbors @board.neighbors(@point) end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/gorb/stone.rb', line 54 def to_s @point end |