Class: Stone

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



4
5
6
7
# File 'lib/stone.rb', line 4

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

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



3
4
5
# File 'lib/stone.rb', line 3

def color
  @color
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
# File 'lib/stone.rb', line 60

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

#empty?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/stone.rb', line 15

def empty?
  @color == :empty
end

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



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

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



31
32
33
# File 'lib/stone.rb', line 31

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

#place_on_board(board) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/stone.rb', line 19

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



27
28
29
# File 'lib/stone.rb', line 27

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

#to_coordObject



48
49
50
# File 'lib/stone.rb', line 48

def to_coord
  [@x, @y]
end

#to_sObject



56
57
58
# File 'lib/stone.rb', line 56

def to_s
  to_str
end

#to_sgfObject



9
10
11
12
13
# File 'lib/stone.rb', line 9

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

#to_strObject



52
53
54
# File 'lib/stone.rb', line 52

def to_str
  Board::Colors[@color] 
end