class Sudoku::Grid

Represents the Sudoku Grid model.

Attributes

cells[R]

Public Class Methods

new(cells = {}) click to toggle source

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

add_cell(cell) click to toggle source

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
filled?() click to toggle source

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
generate_default_cell(coordinates) click to toggle source

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
get_cell(coordinates) click to toggle source

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
get_col(x) click to toggle source

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
get_row(y) click to toggle source

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
random_cell() click to toggle source

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
reset_cell(coordinates) click to toggle source

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
rules_violated?() click to toggle source

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
solved?() click to toggle source

Will return true, it this gird is solved.

# File lib/sudoku/model/grid.rb, line 84
def solved?
  Solver.new(self).solved?
end
to_s() click to toggle source

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