class Sudoku::GridFactory

This class handles the creation of the Grid classes.

Constants

CELLS_TO_OPEN

How many cells will be opened by this factory.

PARSE_ATTEMPTS

Number of attempts for parsing sample sudoku file.

SAMPLES_PATH

Path to directory, where the sample grids are stored.

Public Instance Methods

create_full_grid() click to toggle source

This method will create full (solved) Sudoku grid, where all cells are filled and fixed.

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

This method will create random, simple and solvable sudoku grid.

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

Will generate all possible coordinates, which can exist in the sudoku grid.

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

Will try to parse file on given path and will return initialized Grid on success. Will return nil on fail.

  • path path to file to parse.

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

Will try to parse given line, where the Sudoku sample should be. Will return initialized instanceof Grid on success, nil otherwise.

  • line to parse

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

Will take full solved grid, and will randomly fix some cells and clean others.

  • grid grid to randomize.

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

Will return path of one of the stored sudoku files in this project.

# 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