Module: Sudoku::Generator
- Included in:
- Grid
- Defined in:
- lib/sudoku/generator.rb
Overview
Permet de generer rapidement un sudoku
Instance Method Summary collapse
-
#make_diagonal ⇒ self
Remplit la diagonale descendante de 1 a self.size.
-
#make_valid ⇒ self
Remplit tout le sudoku de maniere a ce qu’il soit valide.
-
#make_valid_incomplete(seed = 2) ⇒ self
Cree un sudoku valide et laisse des cases vides au hasard.
Instance Method Details
#make_diagonal ⇒ self
Remplit la diagonale descendante de 1 a self.size
6 7 8 9 |
# File 'lib/sudoku/generator.rb', line 6 def make_diagonal each{|x,y,val| set x,x,x+1 if x==y} self end |
#make_valid ⇒ self
Remplit tout le sudoku de maniere a ce qu’il soit valide
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sudoku/generator.rb', line 13 def make_valid pattern = Array.new(size){|i| i+1}.shuffle size.times do |y| size.times do |x| set x, y, pattern[x] end base.times{|i| pattern.push pattern.shift} pattern.push pattern.shift if base - (y%base) == 1 end self end |
#make_valid_incomplete(seed = 2) ⇒ self
Cree un sudoku valide et laisse des cases vides au hasard
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sudoku/generator.rb', line 28 def make_valid_incomplete seed=2 pattern = Array.new(size){|i| i+1}.shuffle size.times do |y| size.times do |x| set x, y, pattern[x] if rand(seed) == seed-1 end base.times{|i| pattern.push pattern.shift} pattern.push pattern.shift if base - (y%base) == 1 end self end |