class ILoveSudoku::Cell

Attributes

state[R]

Public Class Methods

new(value = nil) click to toggle source
# File lib/cell.rb, line 4
def initialize(value = nil)
  @state = set_state(value)
end

Public Instance Methods

possibilities() click to toggle source
# File lib/cell.rb, line 12
def possibilities
  solved? ? nil : state
end
remove!(n) click to toggle source
# File lib/cell.rb, line 16
def remove!(n)
  able_to_remove = can_take_action_on(n)
  state.delete(n) if able_to_remove
  able_to_remove
end
solve!(n) click to toggle source
# File lib/cell.rb, line 22
def solve!(n)
  able_to_solve = can_take_action_on(n)
  @state = [n] if able_to_solve
  able_to_solve
end
value() click to toggle source
# File lib/cell.rb, line 8
def value
  solved? ? state[0] : nil
end

Private Instance Methods

can_take_action_on(n) click to toggle source
# File lib/cell.rb, line 37
def can_take_action_on(n)
  !solved? && state.include?(n)
end
set_state(value) click to toggle source
# File lib/cell.rb, line 41
def set_state(value)
  if value.nil?
    (1..9).to_a
  else
    [value]
  end
end
solved?() click to toggle source
# File lib/cell.rb, line 33
def solved?
  state.length == 1
end