-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_sudoku.rb
127 lines (95 loc) · 2.58 KB
/
generate_sudoku.rb
1
2
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require_relative 'puzzle/starting_matrix'
require_relative 'puzzle/set_boxes'
require_relative 'puzzle/puzzle_boxes'
require_relative 'puzzle/puzzle_columns'
require_relative 'puzzle/puzzle_rows'
require_relative 'puzzle/set_variables'
require_relative 'clear_all'
require_relative 'check_sizes'
require_relative 'total_numbers_remaining'
require_relative 'ranges'
require_relative 'solve_for_one'
require_relative 'solve_for_two'
require_relative 'solve_for_three'
require_relative 'solve_for_four'
require_relative 'validation'
require_relative 'puzzle/print_puzzle'
require_relative 'solve_for'
def initial_setup(new_puzzle)
set_boxes(new_puzzle)
clear_all(new_puzzle)
set_variables(new_puzzle)
#print_initial_puzzle(new_puzzle)
@history = []
@loops = 0
@pairs = []
@box2finished = 0
@other_path = 0
new_puzzle
end
def generate_puzzle(array, section = nil)
set_variables(array)
clear_all(array)
total_numbers_remaining?(array)
check_sizes(array)
@loop_once = 0
if (@size1 > 0) || @number_totals_remaining.include?(1)
solve_for_one(array, section)
elsif @size2 > 0
solve_for_two(array, section)
elsif @size3 > 0
solve_for_three(array, section)
else
solve_for_four(array, section)
end
done = array.flatten.inject(0) { |total, value| total + value }
@loops += 1
if @loops == 75
@loops = 0
@new_puzzle = starting_matrix
initial_setup(@new_puzzle)
generate_puzzle(@new_puzzle)
return
elsif done == 405 && no_arrays?(array)
@history << "box2 not finished for #{@box2finished} loops"
@history << "puzzle solved after #{@loops} recursions"
@pairs = []
@box2finished = 0
puts
return
else
generate_puzzle(array, section)
array
end
end
@saved_puzzles = []
@failed_puzzles = []
@valid_total = 0
@failed_total = 0
amount = 10
amount.times do
@new_puzzle = starting_matrix
possibly_bad_puzzle = initial_setup(@new_puzzle)
generate_puzzle(@new_puzzle)
@history << valid_puzzle?(@new_puzzle)
print_history
print_final_puzzle(@new_puzzle)
if !valid_puzzle?(@new_puzzle) || !no_arrays?(@new_puzzle)
@failed_total += 1
@failed_puzzles << possibly_bad_puzzle
end
if valid_puzzle?(@new_puzzle) && no_arrays?(@new_puzzle)
@valid_total += 1
@saved_puzzles << @new_puzzle
end
end
puts
# @failed_puzzles.each do |puzzle|
# p puzzle
# end
# @saved_puzzles.each do |puzzle|
# print_final_puzzle(puzzle)
# end
p "There were #{@valid_total} valid puzzles generated out of #{amount}!"
puts
puts "Success rate: #{(@valid_total.to_f / amount) * 100} percent"