class Sudoku::Grid
Attributes
Public Class Methods
Will initialize this grid by given hash of cells. If no cells are given, the grid will have empty values and default cells.
# File lib/sudoku/model/grid.rb, line 11 def initialize(cells = {}) @cells = cells GridFactory.new.generate_all_coordinates.each do |x| add_cell(generate_default_cell(x)) end if cells.empty? end
Public Instance Methods
Will add new cell to the grid. If there is already a cell with the same coordintates, it will be replaced by the new one and the old one will be returned.
-
cell
cell to add
# File lib/sudoku/model/grid.rb, line 79 def add_cell(cell) cells[cell.coordinates] = cell end
Will return true, if all filled values are other than 0.
# File lib/sudoku/model/grid.rb, line 19 def filled? !cells.values.collect(&:value).include?(0) end
Will generate default cell, which will have the given coordinates, will be ediatable (fixed = false) and will be empty (value = 0)
-
coordinates
coordinates for the new cell
# File lib/sudoku/model/grid.rb, line 53 def generate_default_cell(coordinates) Cell.new(coordinates, false, 0) end
Will return particular cell based on given coordinates. If there is not cell on given coordinates, default cell will be created (not filled, editable).
-
coordinates
cell coordinates.
# File lib/sudoku/model/grid.rb, line 38 def get_cell(coordinates) cells[coordinates] end
Will return array of cells stored in a given column of this grid.
-
x
coordinate of the desired column. Must be in the 0-8 interval.
# File lib/sudoku/model/grid.rb, line 69 def get_col(x) fail ArgumentException, 'Invalid column access coordinate.' unless Coordinate.valid?(x) cells.values.select { |cell| cell.coordinates.coordinate_x == x }.sort end
Will return array of cells stored on a row with given coordinate.
-
y
coordinate of the desired row. Must be in the 0-8 interval.
# File lib/sudoku/model/grid.rb, line 60 def get_row(y) fail ArgumentException, 'Invalid row access coordinate.' unless Coordinate.valid?(y) cells.values.select { |cell| cell.coordinates.coordinate_y == y }.sort end
Will return cell at random position from this grid.
# File lib/sudoku/model/grid.rb, line 30 def random_cell get_cell(CellCoordinates.random) end
Will reset cell on given coordinates by creating the default new one.
-
coordinates
for the newly created cell
# File lib/sudoku/model/grid.rb, line 45 def reset_cell(coordinates) add_cell(generate_default_cell(coordinates)) end
Will return true, if rules of Sudoku
are violated in this grid. Ignores values 0.
# File lib/sudoku/model/grid.rb, line 25 def rules_violated? Solver.new(self).rules_violated? end
Will return true, it this gird is solved.
# File lib/sudoku/model/grid.rb, line 84 def solved? Solver.new(self).solved? end
Will return matrix of values of this Grid
.
# File lib/sudoku/model/grid.rb, line 89 def to_s out = '' GridFactory.new.generate_all_coordinates.each_with_index do |x, index| out << get_cell(x).value.to_s << x.to_s (index + 1).modulo(9).zero? ? out << "\n" : out << ' ' end out end