Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
Naive Puzzle.to_s implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiemccarthy committed Jun 25, 2016
1 parent 3b16a3c commit 645ae2c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/models/puzzle.rb
Expand Up @@ -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|
Expand All @@ -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;
Expand Down

0 comments on commit 645ae2c

Please sign in to comment.