Class: RubyGo::Stone

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-go/stone.rb

Direct Known Subclasses

BlackStone, Liberty, NullStone, WhiteStone

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Stone

Returns a new instance of Stone.



5
6
7
8
# File 'lib/ruby-go/stone.rb', line 5

def initialize(x, y)
  @x, @y = x, y
  @color = :none
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



4
5
6
# File 'lib/ruby-go/stone.rb', line 4

def color
  @color
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/ruby-go/stone.rb', line 61

def ==(other)
  (self.color == other.color) and (self.to_coord == other.to_coord)
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ruby-go/stone.rb', line 16

def empty?
  @color == :empty
end

#group(board, stones = []) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-go/stone.rb', line 36

def group(board, stones = [])
  return stones if stones.any? {|stone| stone.eql?(self)} 
  stones << self

  board.around(self).each do |stone|
    if stone.color == @color
      stone.group(board, stones)
    end
  end

  stones
end

#liberties(board) ⇒ Object



32
33
34
# File 'lib/ruby-go/stone.rb', line 32

def liberties(board)
  board.around(self).select {|stone| stone.empty?} 
end

#place_on_board(board) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/ruby-go/stone.rb', line 20

def place_on_board(board)
  unless board.at(@x, @y).empty? 
    raise Game::IllegalMove, 
      "You cannot place a stone on top of another stone."  
  end
  board.board[@y][@x] = self
end

#remove_from_board(board) ⇒ Object



28
29
30
# File 'lib/ruby-go/stone.rb', line 28

def remove_from_board(board)
  board.board[@y][@x] = Liberty.new(*self.to_coord)
end

#to_coordObject



49
50
51
# File 'lib/ruby-go/stone.rb', line 49

def to_coord
  [@x, @y]
end

#to_sObject



57
58
59
# File 'lib/ruby-go/stone.rb', line 57

def to_s
  to_str
end

#to_sgfObject



10
11
12
13
14
# File 'lib/ruby-go/stone.rb', line 10

def to_sgf
  x = Game::LETTERS[@x]
  y = Game::LETTERS[@y]
  ";#{color.to_s[0].upcase}[#{x+y}]"
end

#to_strObject



53
54
55
# File 'lib/ruby-go/stone.rb', line 53

def to_str
  Board::Colors[@color] 
end