Module: Sudoku::Generator

Included in:
Grid
Defined in:
lib/sudoku/generator.rb

Overview

Permet de generer rapidement un sudoku

Instance Method Summary collapse

Instance Method Details

#make_diagonalself

Remplit la diagonale descendante de 1 a self.size

Returns:

  • (self)


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_validself

Remplit tout le sudoku de maniere a ce qu’il soit valide

Returns:

  • (self)


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

Parameters:

  • seed (Fixnum) (defaults to: 2)

    Le parametre a utiliser pour rand()

Returns:

  • (self)


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