Class: Sudoku::Grid
- Inherits:
-
Object
- Object
- Sudoku::Grid
- Defined in:
- lib/sudoku/model/grid.rb
Overview
Represents the Sudoku Grid model.
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
Instance Method Summary collapse
-
#add_cell(cell) ⇒ Object
Will add new cell to the grid.
-
#filled? ⇒ Boolean
Will return true, if all filled values are other than 0.
-
#generate_default_cell(coordinates) ⇒ Object
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. -
#get_cell(coordinates) ⇒ Object
Will return particular cell based on given coordinates.
-
#get_col(x) ⇒ Object
Will return array of cells stored in a given column of this grid.
-
#get_row(y) ⇒ Object
Will return array of cells stored on a row with given coordinate.
-
#initialize(cells = {}) ⇒ Grid
constructor
Will initialize this grid by given hash of cells.
-
#random_cell ⇒ Object
Will return cell at random position from this grid.
-
#reset_cell(coordinates) ⇒ Object
Will reset cell on given coordinates by creating the default new one.
-
#rules_violated? ⇒ Boolean
Will return true, if rules of Sudoku are violated in this grid.
-
#solved? ⇒ Boolean
Will return true, it this gird is solved.
-
#to_s ⇒ Object
Will return matrix of values of this Grid.
Constructor Details
#initialize(cells = {}) ⇒ Grid
Will initialize this grid by given hash of cells. If no cells are given, the grid will have empty values and default cells.
11 12 13 14 15 16 |
# 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 |
Instance Attribute Details
#cells ⇒ Object (readonly)
Returns the value of attribute cells.
6 7 8 |
# File 'lib/sudoku/model/grid.rb', line 6 def cells @cells end |
Instance Method Details
#add_cell(cell) ⇒ Object
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
79 80 81 |
# File 'lib/sudoku/model/grid.rb', line 79 def add_cell(cell) cells[cell.coordinates] = cell end |
#filled? ⇒ Boolean
Will return true, if all filled values are other than 0.
19 20 21 |
# File 'lib/sudoku/model/grid.rb', line 19 def filled? !cells.values.collect(&:value).include?(0) end |
#generate_default_cell(coordinates) ⇒ Object
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
53 54 55 |
# File 'lib/sudoku/model/grid.rb', line 53 def generate_default_cell(coordinates) Cell.new(coordinates, false, 0) end |
#get_cell(coordinates) ⇒ Object
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.
38 39 40 |
# File 'lib/sudoku/model/grid.rb', line 38 def get_cell(coordinates) cells[coordinates] end |
#get_col(x) ⇒ Object
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.
69 70 71 72 73 |
# 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) ⇒ Object
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.
60 61 62 63 64 |
# 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 ⇒ Object
Will return cell at random position from this grid.
30 31 32 |
# File 'lib/sudoku/model/grid.rb', line 30 def random_cell get_cell(CellCoordinates.random) end |
#reset_cell(coordinates) ⇒ Object
Will reset cell on given coordinates by creating the default new one.
-
coordinates
for the newly created cell
45 46 47 |
# File 'lib/sudoku/model/grid.rb', line 45 def reset_cell(coordinates) add_cell(generate_default_cell(coordinates)) end |
#rules_violated? ⇒ Boolean
Will return true, if rules of Sudoku are violated in this grid. Ignores values 0.
25 26 27 |
# File 'lib/sudoku/model/grid.rb', line 25 def rules_violated? Solver.new(self).rules_violated? end |
#solved? ⇒ Boolean
Will return true, it this gird is solved.
84 85 86 |
# File 'lib/sudoku/model/grid.rb', line 84 def solved? Solver.new(self).solved? end |
#to_s ⇒ Object
Will return matrix of values of this Grid.
89 90 91 92 93 94 95 96 |
# 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 |