Class: Sudoku::GridFactory
- Inherits:
-
Object
- Object
- Sudoku::GridFactory
- Includes:
- Logging
- Defined in:
- lib/sudoku/grid_factory.rb
Overview
This class handles the creation of the Grid classes.
Constant Summary collapse
- CELLS_TO_OPEN =
How many cells will be opened by this factory.
2- SAMPLES_PATH =
Path to directory, where the sample grids are stored.
'data'- PARSE_ATTEMPTS =
Number of attempts for parsing sample sudoku file.
5
Constants included from Logging
Instance Method Summary collapse
-
#create_full_grid ⇒ Object
This method will create full (solved) Sudoku grid, where all cells are filled and fixed.
-
#create_random ⇒ Object
This method will create random, simple and solvable sudoku grid.
-
#generate_all_coordinates ⇒ Object
Will generate all possible coordinates, which can exist in the sudoku grid.
-
#parse_file(path) ⇒ Object
Will try to parse file on given path and will return initialized Grid on success.
-
#parse_line(line) ⇒ Object
Will try to parse given line, where the Sudoku sample should be.
-
#randomize_grid(grid) ⇒ Object
Will take full solved grid, and will randomly fix some cells and clean others.
-
#sample_file ⇒ Object
Will return path of one of the stored sudoku files in this project.
Methods included from Logging
#disable_log, #error, #fatal, #info
Instance Method Details
#create_full_grid ⇒ Object
This method will create full (solved) Sudoku grid, where all cells are filled and fixed.
61 62 63 64 65 66 67 68 69 |
# File 'lib/sudoku/grid_factory.rb', line 61 def create_full_grid grid = nil (0..PARSE_ATTEMPTS).each do |i| info "Attempt #{i}/#{PARSE_ATTEMPTS} to parse grid." grid = parse_file(sample_file) return grid unless grid.nil? end fail IOError, "Failed in creating sample Sudoku after #{i} attempts" end |
#create_random ⇒ Object
This method will create random, simple and solvable sudoku grid.
53 54 55 56 57 |
# File 'lib/sudoku/grid_factory.rb', line 53 def create_random result = create_full_grid result = create_full_grid until Sudoku::Solver.new(result).solved? randomize_grid(result) end |
#generate_all_coordinates ⇒ Object
Will generate all possible coordinates, which can exist in the sudoku grid.
85 86 87 88 89 90 91 |
# File 'lib/sudoku/grid_factory.rb', line 85 def generate_all_coordinates result = [] (0..8).to_a.repeated_permutation(2).to_a.each do |x| result << Sudoku::CellCoordinates.new(x[0], x[1]) end result.sort end |
#parse_file(path) ⇒ Object
Will try to parse file on given path and will return initialized Grid on success. Will return nil on fail.
pathpath to file to parse.
29 30 31 32 33 34 35 36 37 |
# File 'lib/sudoku/grid_factory.rb', line 29 def parse_file(path) info "Trying to parse file #{path} with sample sudoku." File.open(path).each_with_index do |line, index| info "Parsing line #{index}" grid = parse_line(line) return grid unless grid.nil? end nil end |
#parse_line(line) ⇒ Object
Will try to parse given line, where the Sudoku sample should be. Will return initialized instanceof Grid on success, nil otherwise.
lineto parse
43 44 45 46 47 48 49 |
# File 'lib/sudoku/grid_factory.rb', line 43 def parse_line(line) grid = Grid.new generate_all_coordinates.zip(line.split('')).each do |coordinate, value| grid.add_cell(Cell.new(coordinate, true, value.to_i)) end grid end |
#randomize_grid(grid) ⇒ Object
Will take full solved grid, and will randomly fix some cells and clean others.
gridgrid to randomize.
74 75 76 77 78 79 80 81 |
# File 'lib/sudoku/grid_factory.rb', line 74 def randomize_grid(grid) (1..CELLS_TO_OPEN).each do cell = grid.random_cell cell = grid.random_cell until cell.fixed grid.add_cell(Cell.new(cell.coordinates, false, 0)) end grid end |
#sample_file ⇒ Object
Will return path of one of the stored sudoku files in this project.
20 21 22 23 24 |
# File 'lib/sudoku/grid_factory.rb', line 20 def sample_file "#{SAMPLES_PATH}/#{Dir.entries(SAMPLES_PATH) .select { |x| File.extname(x) == '.txt' } .sample}" end |