sudoku_wizard 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sudoku_wizard.rb +49 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea6dbb8a08374d88e71ff0fd4df196405aed9796
|
4
|
+
data.tar.gz: e36e4a7bcd2df738efde63028f8ee73d5cb5f4e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a95ca5590169c33fe40b0932b790d74bf435f1e260f87924a1afb978e2e99a6727f18d0daf0ebf641390d79dd4898527a3bdb4624a8a01cdbab2752f2a4bb6f3
|
7
|
+
data.tar.gz: 15aaa232a7b86c8821ecaabd197937df6aadb43aa03c9bfe1e595ba1ca75493bc0686616092c25949a967b0041b48c06fbc51a7cfae6c5c805558716764957b5
|
data/lib/sudoku_wizard.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'pry'
|
1
|
+
# require 'pry'
|
2
2
|
|
3
3
|
class Sudoku
|
4
4
|
attr_accessor :starting_board, :solution, :removed_values, :difficulty
|
@@ -15,13 +15,31 @@ class Sudoku
|
|
15
15
|
[0, 0, 0, 0, 0, 0, 0, 0, 0]
|
16
16
|
]
|
17
17
|
|
18
|
-
def initialize(holes =
|
19
|
-
|
20
|
-
@fail_safe = fail_safe
|
18
|
+
def initialize( holes = 40, status_messages = false )
|
19
|
+
start_time = Time.now
|
21
20
|
holes > 64 ? 64 : holes
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
@status_messages = status_messages
|
22
|
+
|
23
|
+
puts "Generating Game..." if @status_messages
|
24
|
+
generate_game(holes)
|
25
|
+
|
26
|
+
return if !@status_messages
|
27
|
+
puts "Board Generated in"
|
28
|
+
puts "#{ format_number(@iteration_counter) } Iterations"
|
29
|
+
puts " #{ Time.now - start_time } seconds"
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_game(holes)
|
33
|
+
begin
|
34
|
+
# @start_time = Time.now
|
35
|
+
@iteration_counter = 0
|
36
|
+
self.solution = new_solved_board
|
37
|
+
self.removed_values, self.starting_board = poke_holes(self.solution.map(&:clone), holes)
|
38
|
+
self.difficulty = holes
|
39
|
+
rescue
|
40
|
+
puts "#{ format_number(@iteration_counter) } iterations, Restarting" if @status_messages
|
41
|
+
generate_game(holes)
|
42
|
+
end
|
25
43
|
end
|
26
44
|
|
27
45
|
def new_solved_board
|
@@ -34,9 +52,10 @@ class Sudoku
|
|
34
52
|
empty_cell = find_next_empty_cell(puzzle_matrix)
|
35
53
|
return puzzle_matrix if !empty_cell #If no empty cells, we are done. Return the completed puzzle
|
36
54
|
|
37
|
-
# Fill in the empty cell
|
55
|
+
# Fill in the empty cell
|
38
56
|
for num in (1..9).to_a.shuffle do
|
39
|
-
|
57
|
+
@iteration_counter += 1
|
58
|
+
raise if (@iteration_counter > 1_000_000)
|
40
59
|
if safe(puzzle_matrix, empty_cell, num) # For a number, check if it safe to place that number in the empty cell
|
41
60
|
puzzle_matrix[empty_cell[:row_i]][empty_cell[:col_i]] = num # if safe, place number
|
42
61
|
return puzzle_matrix if solve(puzzle_matrix) # Recursively call solve method again.
|
@@ -66,13 +85,11 @@ class Sudoku
|
|
66
85
|
end
|
67
86
|
|
68
87
|
def row_safe (puzzle_matrix, empty_cell, num)
|
69
|
-
|
70
|
-
return true
|
88
|
+
!puzzle_matrix[ empty_cell[:row_i] ].find_index(num)
|
71
89
|
end
|
72
90
|
|
73
91
|
def col_safe (puzzle_matrix, empty_cell, num)
|
74
|
-
|
75
|
-
return true
|
92
|
+
!puzzle_matrix.any?{|row| row[ empty_cell[:col_i] ] == num}
|
76
93
|
end
|
77
94
|
|
78
95
|
def box_safe (puzzle_matrix, empty_cell, num)
|
@@ -133,7 +150,24 @@ class Sudoku
|
|
133
150
|
"
|
134
151
|
end
|
135
152
|
|
153
|
+
private
|
154
|
+
|
155
|
+
def format_number(integer)
|
156
|
+
number_of_digits = Math.log(integer, 10).floor + 1
|
157
|
+
number_of_segments = (number_of_digits / 3.0).ceil
|
158
|
+
int_as_array = integer.to_s.split("")
|
159
|
+
formatted_array = []
|
160
|
+
index = 1
|
161
|
+
|
162
|
+
while index <= number_of_digits do
|
163
|
+
formatted_array.unshift(int_as_array[ index * -1 ] )
|
164
|
+
formatted_array.unshift("_") if index % 3 == 0 && index != number_of_digits
|
165
|
+
index += 1
|
166
|
+
end
|
167
|
+
formatted_array.join("")
|
168
|
+
end
|
169
|
+
|
136
170
|
end
|
137
171
|
|
138
|
-
binding.pry
|
139
|
-
false
|
172
|
+
# binding.pry
|
173
|
+
# false
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sudoku_wizard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Sasse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-02-
|
11
|
+
date: 2021-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Uses backtracking algorithm to generate random boards of specified difficulty
|
14
14
|
and find solutions
|