class ILoveSudoku::BlockOfNine

Attributes

cells[R]
made_progress[R]

Public Class Methods

new() click to toggle source
# File lib/block_of_nine.rb, line 4
def initialize
  @cells = []
  @made_progress = false
end
new_from(nine_cells) click to toggle source
# File lib/block_of_nine.rb, line 9
def self.new_from(nine_cells)
  block = self.new
  nine_cells.each { |cell| block.add_cell(cell) }
  block
end

Public Instance Methods

add_cell(cell) click to toggle source
# File lib/block_of_nine.rb, line 15
def add_cell(cell)
  @cells << cell
end
hit!() click to toggle source
# File lib/block_of_nine.rb, line 19
def hit!
  @made_progress = false
  sweep_clean!
  heat_seek!
  made_progress
end

Private Instance Methods

cells_that_might_be(n) click to toggle source
# File lib/block_of_nine.rb, line 61
def cells_that_might_be(n)
  unsolved_cells.select { |cell| cell.possibilities.include?(n) }
end
found(n) click to toggle source
# File lib/block_of_nine.rb, line 57
def found(n)
  found_values.include?(n)
end
found_values() click to toggle source
# File lib/block_of_nine.rb, line 65
def found_values
  solved_cells.map { |cell| cell.value }.sort
end
heat_seek!() click to toggle source
# File lib/block_of_nine.rb, line 43
def heat_seek!
  looking_for = unfound_values
  looking_for.each { |n| try_to_solve_only_remaining(n) }
end
remove_from_unsolved_cells(n) click to toggle source
# File lib/block_of_nine.rb, line 35
def remove_from_unsolved_cells(n)
  removed = []
  unsolved_cells.each do |cell|
    removed << cell.remove!(n)
  end
  @made_progress = removed.any? unless made_progress
end
solved_cells() click to toggle source
# File lib/block_of_nine.rb, line 77
def solved_cells
  cells.select { |cell| cell.value }
end
sweep_clean!() click to toggle source
# File lib/block_of_nine.rb, line 31
def sweep_clean!
  found_values.each { |n| remove_from_unsolved_cells(n) }
end
try_to_solve_only_remaining(n) click to toggle source
# File lib/block_of_nine.rb, line 48
def try_to_solve_only_remaining(n)
  solved = false
  capture = cells_that_might_be(n)
  if capture.length == 1
    solved = capture[0].solve!(n)
  end
  @made_progress = solved unless made_progress
end
unfound_values() click to toggle source
# File lib/block_of_nine.rb, line 69
def unfound_values
  (1..9).to_a - found_values
end
unsolved_cells() click to toggle source
# File lib/block_of_nine.rb, line 73
def unsolved_cells
  cells.reject { |cell| cell.value }
end