Class: Gobstones::Runner::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/gobstones/runner/cell.rb

Instance Method Summary collapse

Constructor Details

#initializeCell

Returns a new instance of Cell.



6
7
8
# File 'lib/gobstones/runner/cell.rb', line 6

def initialize
  @values = Hash[Color.all.map { |color| [color, 0] }]
end

Instance Method Details

#are_there_balls?(color) ⇒ Boolean

Returns:



22
23
24
25
# File 'lib/gobstones/runner/cell.rb', line 22

def are_there_balls?(color)
  check(color)
  number_of_balls(color).positive?
end

#cloneObject



40
41
42
43
44
45
46
# File 'lib/gobstones/runner/cell.rb', line 40

def clone
  self.class.new.tap do |copy|
    Color.all.map(&:new).each do |color|
      number_of_balls(color).times { copy.put(color) }
    end
  end
end

#empty!Object



32
33
34
# File 'lib/gobstones/runner/cell.rb', line 32

def empty!
  initialize
end

#empty?Boolean

Returns:



36
37
38
# File 'lib/gobstones/runner/cell.rb', line 36

def empty?
  @values.values.all?(&:zero?)
end

#number_of_balls(color) ⇒ Object



27
28
29
30
# File 'lib/gobstones/runner/cell.rb', line 27

def number_of_balls(color)
  check(color)
  lookup(color)
end

#put(color) ⇒ Object



10
11
12
13
# File 'lib/gobstones/runner/cell.rb', line 10

def put(color)
  check(color)
  lookup(color) { |value| value + 1 }
end

#take_out(color) ⇒ Object

Raises:



15
16
17
18
19
20
# File 'lib/gobstones/runner/cell.rb', line 15

def take_out(color)
  check(color)
  raise EmptyCellError unless are_there_balls?(color)

  lookup(color) { |value| value - 1 }
end