Class: SudokuBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sudoku_builder.rb,
lib/sudoku_builder/tools.rb,
lib/sudoku_builder/errors.rb,
lib/sudoku_builder/solver.rb,
lib/sudoku_builder/values.rb,
lib/sudoku_builder/builder.rb,
lib/sudoku_builder/version.rb,
lib/sudoku_builder/presenter.rb

Defined Under Namespace

Classes: PuzzleFormatError, SolverError

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSudokuBuilder

Returns a new instance of SudokuBuilder.



12
13
14
15
16
# File 'lib/sudoku_builder.rb', line 12

def initialize
  @sud  = blank
  @used = blank
  @loc = [0,0,0] ; @tot = 0 ; @res = 0
end

Instance Attribute Details

#resObject (readonly)

Returns the value of attribute res.



10
11
12
# File 'lib/sudoku_builder.rb', line 10

def res
  @res
end

#totObject (readonly)

Returns the value of attribute tot.



10
11
12
# File 'lib/sudoku_builder.rb', line 10

def tot
  @tot
end

Class Method Details

.createObject



18
19
20
21
22
# File 'lib/sudoku_builder.rb', line 18

def self.create
  sudoku = SudokuBuilder.new
  sudoku.build
  sudoku
end

.solve(puzzle) ⇒ Object



24
25
26
27
28
29
# File 'lib/sudoku_builder.rb', line 24

def self.solve(puzzle)
  sudoku = SudokuBuilder.new
  sudoku.parse_for_solve(puzzle)
  sudoku.solve
  sudoku
end

Instance Method Details

#blankObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sudoku_builder/tools.rb', line 46

def blank
  [
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
    [ [ [], [], [] ], [ [], [], [] ], [ [], [], [] ] ],
  ]
end

#buildObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sudoku_builder/builder.rb', line 3

def build
  @loc = [0,0,0] ; @tot = 0 ; @res = 0
  loop do
    return @sud if @loc == [9,0,0]

    poss = []
    (1..9).each do |i|
      if check?(i)
        poss << i
      end
    end

    if !poss.empty?
      write(poss.sample)
      increment
    else
      de_increment
    end

    @tot += 1
  end
end

#check?(val) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/sudoku_builder/tools.rb', line 73

def check?(val)
  !(grid.include?(val) || column.include?(val) ||
        row.include?(val) || used.include?(val) )
end

#columnObject



20
21
22
23
24
25
26
27
# File 'lib/sudoku_builder/values.rb', line 20

def column
  c = @loc[2] ; g = @loc[1]
  cols = []
  @sud.each do |row|
    cols << row[g][c]
  end
  cols.flatten
end

#de_incrementObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sudoku_builder/tools.rb', line 25

def de_increment
  r = @loc[0] ; g = @loc[1] ; c = @loc[2]
  if r == 0 && g == 0 && c == 0
    # reset everything
    @res += 1
    reset
  elsif c == 0 && g == 0
    # de increment row
    @loc[0] -= 1
    @loc[1] = 2
    @loc[2] = 2
  elsif c == 0 && r >= 0
    # de increment grid
    @loc[1] -= 1
    @loc[2] = 2
  elsif c > 0
    # de increment column
    @loc[2] -= 1
  end
end

#easyObject



42
43
44
# File 'lib/sudoku_builder/builder.rb', line 42

def easy
  poke(35)
end

#gridObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sudoku_builder/values.rb', line 29

def grid
  g = @loc[1] ; r = @loc[0] ; grids = []
  if r <= 2
    i = 0
  elsif r <= 5 && r > 2
    i = 3
  elsif r <= 8 && r > 5
    i = 6
  end
  3.times do
    grids << @sud[i][g]
    i += 1
  end
  return grids.flatten
end

#hardObject



34
35
36
# File 'lib/sudoku_builder/builder.rb', line 34

def hard
  poke(65)
end

#incrementObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sudoku_builder/tools.rb', line 8

def increment
  r = @loc[0] ; g = @loc[1] ; c = @loc[2]
  if g == 2 && c == 2
    # increment row
    @loc[0] += 1
    @loc[1] = 0
    @loc[2] = 0
  elsif c == 2 && g < 3
    # increment grid
    @loc[1] += 1
    @loc[2] = 0
  elsif c < 2
    # increment column
    @loc[2] += 1
  end
end

#mediumObject



38
39
40
# File 'lib/sudoku_builder/builder.rb', line 38

def medium
  poke(50)
end

#parse_for_solve(puzzle) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sudoku_builder/tools.rb', line 78

def parse_for_solve(puzzle)
  flattened = puzzle.flatten
  if flattened.count == 81
    @loc = [0,0,0] ; @sud = blank
    flattened.each do |val|
      write(val) if val
      increment
    end
  else
    raise PuzzleFormatError, 'Must have exactly 81 spots in the array.'
  end
end

#poke(num) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/sudoku_builder/builder.rb', line 26

def poke(num)
  sud = to_flat_a
  num.times do
    sud[rand(0..80)] = nil
  end
  sud
end

#prettyObject



3
4
5
6
7
8
9
# File 'lib/sudoku_builder/presenter.rb', line 3

def pretty
  @sud.each_with_index do |row|
    print row.join(' ')
    puts ' '
  end
  puts ' '
end

#resetObject



3
4
5
6
# File 'lib/sudoku_builder/tools.rb', line 3

def reset
  @sud  = blank
  @used = blank
end

#rowObject



16
17
18
# File 'lib/sudoku_builder/values.rb', line 16

def row
  @sud[@loc[0]].flatten
end

#solveObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sudoku_builder/solver.rb', line 3

def solve
  @loc = [0,0,0] ; @tot = 0 ; @res = 0 ; up = true

  loop do
    return @sud if @loc == [9,0,0]

    if value.class == Fixnum

      if up == true
        increment
      elsif up == false
        de_increment
      end

    else

      poss = []
      (1..9).each do |i|
        if check?(i)
          poss << i
        end
      end

      if !poss.empty?
        write(poss.sample)
        increment ; up = true
      else
        de_increment ; up = false
      end

    end

    @tot += 1
  end

end

#to_aObject



11
12
13
# File 'lib/sudoku_builder/presenter.rb', line 11

def to_a
  @sud
end

#to_flat_aObject



15
16
17
# File 'lib/sudoku_builder/presenter.rb', line 15

def to_flat_a
  @sud.flatten
end

#usedObject



3
4
5
# File 'lib/sudoku_builder/values.rb', line 3

def used
  @used[@loc[0]][@loc[1]][@loc[2]]
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sudoku_builder/tools.rb', line 60

def valid?
  @loc = [0,0,0]
  validity = []
  loop do
    if check?(value)
      return false
    end
    increment
    break if @loc == [9,0,0]
  end
  return true
end

#valueObject



12
13
14
# File 'lib/sudoku_builder/values.rb', line 12

def value
  @sud[@loc[0]][@loc[1]][@loc[2]]
end

#write(val) ⇒ Object



7
8
9
10
# File 'lib/sudoku_builder/values.rb', line 7

def write(val)
  @sud[@loc[0]][@loc[1]][@loc[2]] = val
  @used[@loc[0]][@loc[1]][@loc[2]] << val unless used.include?(val)
end