diff --git a/app/models/puzzle.rb b/app/models/puzzle.rb index 9b519af..e05981d 100644 --- a/app/models/puzzle.rb +++ b/app/models/puzzle.rb @@ -10,6 +10,18 @@ class Puzzle < ApplicationRecord # root**6 total possibilities. validates :root, :numericality => { :greater_than_or_equal_to => 2, :less_than_or_equal_to => 10 } + def to_s + rows = [] + (1..root**2).each do |row| + cols = [] + (1..root**2).each do |col| + cols << (confirmed_symbol(col, row) || '.') + end + rows << cols.join("") + end + rows.join("\n") + end + def build_cells (1..root**2).each do |row| (1..root**2).each do |col| @@ -29,6 +41,10 @@ def set!(col, row, symbol) end end + def confirmed_symbol(col, row) + cells.in_col(col).in_row(row).is_confirmed.first.try(:symbol) + end + # This method encapsulates the logic at the heart of sudoku, which is that # defining a symbol locks down other symbols in four ways: # * no other symbol is possible in that cell;