Class: Sudoku::Solver
- Inherits:
-
Object
- Object
- Sudoku::Solver
- Defined in:
- lib/sudoku/solver.rb
Overview
This class provides method for solving or checking solution of the Sudoku game.
Instance Method Summary collapse
-
#areas_violated? ⇒ Boolean
Will return true, if in some area of the grid are duplicit or illegal numbers.
-
#cells_violated?(cells) ⇒ Boolean
Will return true, if given set of cells does not contain duplicities in values other than 0.
-
#cols_violated? ⇒ Boolean
Will return true, if all cols are correctly solved.
-
#grid_filled? ⇒ Boolean
Will return true, if the grid is filled, which means, that all of the values are other than 0.
-
#initialize(grid) ⇒ Solver
constructor
Will initialize this solver with given grid.
-
#rows_violated? ⇒ Boolean
Will return true, if all rows are correctly solved.
-
#rules_violated? ⇒ Boolean
Will check if the sudoku rules are violated, but the 0 value is ignored.
-
#solved? ⇒ Boolean
Will return true, if all cells of the grid are filled and no Sudoku rule is violated.
Constructor Details
#initialize(grid) ⇒ Solver
Will initialize this solver with given grid. Exception is thrown if the given grid is not kind of Sudoku::Grid
9 10 11 12 13 |
# File 'lib/sudoku/solver.rb', line 9 def initialize(grid) fail ArgumentError, 'Invalid grid passed to solver' unless grid.is_a?(Grid) @grid = grid end |
Instance Method Details
#areas_violated? ⇒ Boolean
Will return true, if in some area of the grid are duplicit or illegal numbers. Returns false otherwise.
53 54 55 56 57 58 59 |
# File 'lib/sudoku/solver.rb', line 53 def areas_violated? [(0..2), (3..5), (6..8)].repeated_permutation(2).each do |x, y| cells = @grid.cells.select { |cell| cell.in_area(x, y) } return true if cells_violated?(cells) end false end |
#cells_violated?(cells) ⇒ Boolean
Will return true, if given set of cells does not contain duplicities in values other than 0.
-
cells
set of cells to check.
46 47 48 49 |
# File 'lib/sudoku/solver.rb', line 46 def cells_violated?(cells) values = cells.select { |x| x.value != 0 }.collect(&:value) values.uniq.size != values.size end |
#cols_violated? ⇒ Boolean
Will return true, if all cols are correctly solved.
39 40 41 |
# File 'lib/sudoku/solver.rb', line 39 def cols_violated? (0..8).to_a.collect { |x|cells_violated?(@grid.get_col(x)) }.any? end |
#grid_filled? ⇒ Boolean
Will return true, if the grid is filled, which means, that all of the values are other than 0. False otherwise.
29 30 31 |
# File 'lib/sudoku/solver.rb', line 29 def grid_filled? @grid.filled? end |
#rows_violated? ⇒ Boolean
Will return true, if all rows are correctly solved.
34 35 36 |
# File 'lib/sudoku/solver.rb', line 34 def rows_violated? (0..8).to_a.collect { |x|cells_violated?(@grid.get_row(x)) }.any? end |
#rules_violated? ⇒ Boolean
Will check if the sudoku rules are violated, but the 0 value is ignored. Returns true if violated, false otherwise.
17 18 19 |
# File 'lib/sudoku/solver.rb', line 17 def rules_violated? rows_violated? || cols_violated? || areas_violated? end |
#solved? ⇒ Boolean
Will return true, if all cells of the grid are filled and no Sudoku rule is violated.
23 24 25 |
# File 'lib/sudoku/solver.rb', line 23 def solved? !rules_violated? && grid_filled? end |