nsudoku 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rvmrc +6 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +34 -0
- data/Rakefile +2 -0
- data/lib/nsudoku/version.rb +3 -0
- data/lib/nsudoku.rb +193 -0
- data/lib/sudoku.rb~ +161 -0
- data/nsudoku.gemspec +20 -0
- data/spec/nsudoku_spec.rb +148 -0
- metadata +73 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michał Szyma
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Sudoku
|
2
|
+
|
3
|
+
This gem solve puzzle game sudoku 9x9
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
> gem 'nsudoku'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nsudoku
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
> require 'nsudoku'
|
22
|
+
> EXAMPLE = [
|
23
|
+
> "012000000",
|
24
|
+
> "304052000",
|
25
|
+
> "605170000",
|
26
|
+
> "070208090",
|
27
|
+
> "920700084",
|
28
|
+
> "050906010",
|
29
|
+
> "000320908",
|
30
|
+
> "000580107",
|
31
|
+
> "000000240"].join
|
32
|
+
|
33
|
+
> NSudoku.new(EXAMPLE) #=> "712460850394852671685170420070208596926715384050906712540320968269584137030690245"
|
34
|
+
|
data/Rakefile
ADDED
data/lib/nsudoku.rb
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
require "nsudoku/version"
|
2
|
+
|
3
|
+
class NSudoku
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@revers_table = create_revers_table(data.split(""))
|
7
|
+
end
|
8
|
+
|
9
|
+
def solve
|
10
|
+
revers_table = nil
|
11
|
+
while not revers_table == @revers_table
|
12
|
+
revers_table = Marshal.load(Marshal.dump(@revers_table))
|
13
|
+
|
14
|
+
9.times do |index|
|
15
|
+
cells_with_two_elements_for('row', index)
|
16
|
+
cells_with_two_elements_for('column', index)
|
17
|
+
cells_with_two_elements_for('block', index)
|
18
|
+
erase_in_horizontal_three(index)
|
19
|
+
erase_in_vertical_three(index)
|
20
|
+
end
|
21
|
+
|
22
|
+
9.times do |row_index|
|
23
|
+
9.times do |column_index|
|
24
|
+
erase_in_vertical_one(row_index, column_index)
|
25
|
+
erase_in_horizontal_one(row_index, column_index)
|
26
|
+
erase_in_block_one(row_index, column_index)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
revers_table_to_result
|
31
|
+
end
|
32
|
+
|
33
|
+
def show_table_pretty
|
34
|
+
@revers_table.each do |cell|
|
35
|
+
puts cell.inspect
|
36
|
+
end
|
37
|
+
end
|
38
|
+
private
|
39
|
+
|
40
|
+
# table = [[[1, 2, 3, 4, 5], [], ..., []],
|
41
|
+
# [[], ..., []],
|
42
|
+
# ...
|
43
|
+
# [[], ..., []]
|
44
|
+
# ]
|
45
|
+
def create_revers_table(data)
|
46
|
+
result = 9.times.map{|row_element| 9.times.map{|col_element| []}}
|
47
|
+
data.each_with_index do |cell, index|
|
48
|
+
column = index % 9
|
49
|
+
row = (index - column)/9 % 9
|
50
|
+
cell = cell.to_i
|
51
|
+
result[row][column] = [cell]
|
52
|
+
result[row][column] = 9.times.map{|element| element + 1} if cell == 0
|
53
|
+
end
|
54
|
+
return result
|
55
|
+
end
|
56
|
+
|
57
|
+
# revers table @revers_table into string where cell [n] return n and [a1, a2, ..., ak] return 0
|
58
|
+
def revers_table_to_result
|
59
|
+
result = ""
|
60
|
+
@revers_table.flatten(1).each do |element|
|
61
|
+
result << (element.size == 1 ? element.first.to_s : "0")
|
62
|
+
end
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def positions_for(type, index0, index1)
|
68
|
+
case type
|
69
|
+
when 'column'
|
70
|
+
return {
|
71
|
+
row: index0,
|
72
|
+
column: index1
|
73
|
+
}
|
74
|
+
when 'row'
|
75
|
+
return {
|
76
|
+
row: index0,
|
77
|
+
column: index1
|
78
|
+
}
|
79
|
+
when 'block'
|
80
|
+
return {
|
81
|
+
row: (index0/3)*3 + index1/3,
|
82
|
+
column: (index0%3)*3 + index1%3
|
83
|
+
}
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def positions_of_values(kind, value)
|
88
|
+
result = {}
|
89
|
+
row, column = nil, nil
|
90
|
+
9.times do |index|
|
91
|
+
case kind
|
92
|
+
when 'column'
|
93
|
+
row = index
|
94
|
+
column = value
|
95
|
+
when 'row'
|
96
|
+
row = value
|
97
|
+
column = index
|
98
|
+
when 'block'
|
99
|
+
row = (value/3)*3 + index/3
|
100
|
+
column = (value%3)*3 + index%3
|
101
|
+
end
|
102
|
+
next if row.nil? or column.nil?
|
103
|
+
@revers_table[row][column].each do |value|
|
104
|
+
result[value] ||= []
|
105
|
+
result[value] << index
|
106
|
+
end
|
107
|
+
end
|
108
|
+
result
|
109
|
+
end
|
110
|
+
|
111
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
112
|
+
# type: "block", "row","column"
|
113
|
+
def cells_with_two_elements_for(type, index)
|
114
|
+
only_two_positions = positions_of_values(type, index).select {|k,v| v.length == 2}
|
115
|
+
result = {}
|
116
|
+
only_two_positions.each do |value, positions|
|
117
|
+
result[positions] ||= []
|
118
|
+
result[positions] << value
|
119
|
+
end
|
120
|
+
result.each do |key, value|
|
121
|
+
if value.size == 2
|
122
|
+
if type == 'block'
|
123
|
+
positions = positions_for('block', index, key[0])
|
124
|
+
@revers_table[positions[:row]][positions[:column]] = value.clone
|
125
|
+
positions = positions_for('block', index, key[1])
|
126
|
+
@revers_table[positions[:row]][positions[:column]] = value.clone
|
127
|
+
end
|
128
|
+
if type == 'row'
|
129
|
+
@revers_table[index][key[0]] = value.clone
|
130
|
+
@revers_table[index][key[1]] = value.clone
|
131
|
+
end
|
132
|
+
if type == 'column'
|
133
|
+
@revers_table[key[0]][index] = value.clone
|
134
|
+
@revers_table[key[1]][index] = value.clone
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
142
|
+
def erase_in_vertical_one(row, column)
|
143
|
+
return unless @revers_table[row][column].size == 1
|
144
|
+
cell = @revers_table[row][column][0]
|
145
|
+
9.times do |index|
|
146
|
+
@revers_table[index][column].delete(cell) unless index == row
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# erase values in hozizontal, which are that same like value from positnio row, column
|
151
|
+
def erase_in_horizontal_one(row, column)
|
152
|
+
return unless @revers_table[row][column].size == 1
|
153
|
+
cell = @revers_table[row][column][0]
|
154
|
+
9.times do |index|
|
155
|
+
@revers_table[row][index].delete(cell) unless index == column
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# erase values in block, which are that same like value from positnio row, column
|
160
|
+
def erase_in_block_one(row, column)
|
161
|
+
return unless @revers_table[row][column].size == 1
|
162
|
+
cell = @revers_table[row][column][0]
|
163
|
+
first_row_block = (row/3)*3
|
164
|
+
first_column_block = (column/3)*3
|
165
|
+
3.times do |row_index|
|
166
|
+
row_block = first_row_block + row_index
|
167
|
+
3.times do |column_index|
|
168
|
+
column_block = first_column_block + column_index
|
169
|
+
@revers_table[row_block][column_block].delete(cell) unless row_block == row and column_block == column
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
175
|
+
def erase_in_vertical_three(column)
|
176
|
+
positions_of_values('column', column).each do |value, positions|
|
177
|
+
@revers_table[positions.first][column] = [value] if positions.size == 1
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# erase values in horizontal, which are that same like value from positnio row, column
|
182
|
+
def erase_in_horizontal_three(row)
|
183
|
+
positions_of_values('row', row).each do |value, positions|
|
184
|
+
@revers_table[row][positions.first] = [value] if positions.size == 1
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def cell_revers_table(row, column, value = nil)
|
189
|
+
@revers_table[row][column] = value unless value.nil?
|
190
|
+
@revers_table[row][column]
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
data/lib/sudoku.rb~
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
require "sudoku/version"
|
2
|
+
|
3
|
+
class Sudoku
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@revers_table = create_revers_table(data)
|
7
|
+
end
|
8
|
+
|
9
|
+
def solve
|
10
|
+
revers_table = nil
|
11
|
+
while not revers_table == @revers_table
|
12
|
+
revers_table = Marshal.load(Marshal.dump(@revers_table))
|
13
|
+
|
14
|
+
9.times do |row_index|
|
15
|
+
# erase_in_vertical_two(row_index)
|
16
|
+
# erase_in_horizontal_two(row_index)
|
17
|
+
# erase_in_block_two(row_index)
|
18
|
+
9.times do |column_index|
|
19
|
+
erase_in_vertical_one(row_index, column_index)
|
20
|
+
erase_in_horizontal_one(row_index, column_index)
|
21
|
+
erase_in_block_one(row_index, column_index)
|
22
|
+
puts "row_index : #{row_index} column_index: #{column_index}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
puts revers_table_to_result
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# table = [[[1, 2, 3, 4, 5], [], ..., []],
|
32
|
+
# [[], ..., []],
|
33
|
+
# ...
|
34
|
+
# [[], ..., []]
|
35
|
+
# ]
|
36
|
+
def create_revers_table(data)
|
37
|
+
result = 9.times.map{|row_element| 9.times.map{|col_element| []}}
|
38
|
+
data.each_with_index do |cell, index|
|
39
|
+
column = index % 9
|
40
|
+
row = (index - column)/9 % 9
|
41
|
+
cell = cell.to_i
|
42
|
+
result[row][column] = [cell]
|
43
|
+
result[row][column] = 9.times.map{|element| element+1} if cell == 0
|
44
|
+
end
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
|
48
|
+
def revers_table_to_result
|
49
|
+
revers_table = Marshal.load(Marshal.dump(@revers_table))
|
50
|
+
9.times.map do |row|
|
51
|
+
9.times.map do |columnt|
|
52
|
+
if revers_table[row][columnt].size == 1
|
53
|
+
revers_table[row][columnt] = revers_table[row][columnt].first
|
54
|
+
else
|
55
|
+
revers_table[row][columnt] = 0
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
revers_table.join.split("")
|
60
|
+
end
|
61
|
+
|
62
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
63
|
+
def erase_in_block_two(number)
|
64
|
+
start_row = (number/3)*3
|
65
|
+
start_column = (number%3)*3
|
66
|
+
|
67
|
+
# zamieniamy na tablicę z pozycjami dla poszczególnych wartośći w pionie
|
68
|
+
reverse = ([[]]*9).map{|each| []}
|
69
|
+
9.times do |index|
|
70
|
+
block_row = index/3 + start_row
|
71
|
+
block_column = index%3 + start_column
|
72
|
+
@revers_table[block_row][block_column].each { |value| reverse[value-1] << index }
|
73
|
+
end
|
74
|
+
reverse.map!{|value| value.sort}
|
75
|
+
|
76
|
+
result = []
|
77
|
+
reverse.each do |positions|
|
78
|
+
next unless reverse.count(positions) == 2
|
79
|
+
reverse.each_with_index { |value, index| result << index+1 if value == positions }
|
80
|
+
positions.each do |position|
|
81
|
+
block_row = position/3 + start_row
|
82
|
+
block_column = position%3 + start_column
|
83
|
+
@revers_table[block_row][block_column] = result.uniq
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
90
|
+
def erase_in_vertical_two(column)
|
91
|
+
# zamieniamy na tablicę z pozycjami dla poszczególnych wartośći w pionie
|
92
|
+
reverse = ([[]]*9).map{|each| []}
|
93
|
+
9.times do |index|
|
94
|
+
@revers_table[index][column].each { |value| reverse[value-1] << index }
|
95
|
+
end
|
96
|
+
reverse.map!{|value| value.sort}
|
97
|
+
|
98
|
+
result = []
|
99
|
+
reverse.each do |positions|
|
100
|
+
next unless reverse.count(positions) == 2
|
101
|
+
reverse.each_with_index { |value, index| result << index+1 if value == positions }
|
102
|
+
positions.each { |position| @revers_table[position][column] = result.uniq }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
107
|
+
def erase_in_horizontal_two(row)
|
108
|
+
# zamieniamy na tablicę z pozycjami dla poszczególnych wartośći w pionie
|
109
|
+
reverse = ([[]]*9).map{|each| []}
|
110
|
+
9.times do |index|
|
111
|
+
@revers_table[row][index].each { |value| reverse[value-1] << index }
|
112
|
+
end
|
113
|
+
reverse.map!{|value| value.sort}
|
114
|
+
|
115
|
+
result = []
|
116
|
+
reverse.each do |positions|
|
117
|
+
next unless reverse.count(positions) == 2
|
118
|
+
reverse.each_with_index { |value, index| result << index+1 if value == positions }
|
119
|
+
positions.each { |position| @revers_table[row][position] = result.uniq }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# erase values in vertical, which are that same like value from positnio row, column
|
124
|
+
def erase_in_vertical_one(row, column)
|
125
|
+
return unless @revers_table[row][column].size == 1
|
126
|
+
cell = @revers_table[row][column][0]
|
127
|
+
9.times do |index|
|
128
|
+
@revers_table[index][column].delete(cell) unless index == row
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# erase values in hozizontal, which are that same like value from positnio row, column
|
133
|
+
def erase_in_horizontal_one(row, column)
|
134
|
+
return unless @revers_table[row][column].size == 1
|
135
|
+
cell = @revers_table[row][column][0]
|
136
|
+
9.times do |index|
|
137
|
+
@revers_table[row][index].delete(cell) unless index == column
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# erase values in block, which are that same like value from positnio row, column
|
142
|
+
def erase_in_block_one(row, column)
|
143
|
+
return unless @revers_table[row][column].size == 1
|
144
|
+
cell = @revers_table[row][column][0]
|
145
|
+
first_row_block = (row/3)*3
|
146
|
+
first_column_block = (column/3)*3
|
147
|
+
3.times do |row_index|
|
148
|
+
row_block = first_row_block + row_index
|
149
|
+
3.times do |column_index|
|
150
|
+
column_block = first_column_block + column_index
|
151
|
+
@revers_table[row_block][column_block].delete(cell) unless row_block == row and column_block == column
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def cell_revers_table(row, column, value = nil)
|
157
|
+
@revers_table[row][column] = value unless value.nil?
|
158
|
+
@revers_table[row][column]
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
data/nsudoku.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nsudoku/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.platform = Gem::Platform::RUBY
|
6
|
+
gem.authors = ["Michał Szyma"]
|
7
|
+
gem.email = ["raglub.ruby@gmail.com"]
|
8
|
+
gem.date = "2012-06-21"
|
9
|
+
gem.description = %q{This gem solve puzzle game sudoku 9x9}
|
10
|
+
gem.summary = %q{This gem solve puzzle game sudoku}
|
11
|
+
gem.homepage = "http://github.com/raglub/nsudoku"
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "nsudoku"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = NSudoku::VERSION
|
18
|
+
|
19
|
+
gem.add_development_dependency "rspec", ">= 2.10.0"
|
20
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'nsudoku'
|
4
|
+
|
5
|
+
EXAMPLE = [
|
6
|
+
"012000000",
|
7
|
+
"304052000",
|
8
|
+
"605170000",
|
9
|
+
"070208090",
|
10
|
+
"920700084",
|
11
|
+
"050906010",
|
12
|
+
"000320908",
|
13
|
+
"000580107",
|
14
|
+
"000000240"].join
|
15
|
+
|
16
|
+
TABLE_EMPTY = [
|
17
|
+
"000000000",
|
18
|
+
"000000000",
|
19
|
+
"000000000",
|
20
|
+
"000000000",
|
21
|
+
"000000000",
|
22
|
+
"000000000",
|
23
|
+
"000000000",
|
24
|
+
"000000000",
|
25
|
+
"000000000"].join
|
26
|
+
|
27
|
+
describe NSudoku do
|
28
|
+
|
29
|
+
it "should properly reverse base namber of table" do
|
30
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
31
|
+
sudoku.send(:cell_revers_table, 1, 1, [1])
|
32
|
+
sudoku.send(:cell_revers_table, 8, 8, [3])
|
33
|
+
sudoku.send(:cell_revers_table, 1, 1).should eql([1])
|
34
|
+
sudoku.send(:cell_revers_table, 1 ,0).should eql([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
35
|
+
end
|
36
|
+
|
37
|
+
context "should properly erase values which are that same like in cell with single element" do
|
38
|
+
it "in column (exist cell with single element)" do
|
39
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
40
|
+
sudoku.send(:cell_revers_table, 3, 4, [5])
|
41
|
+
sudoku.send(:erase_in_vertical_one, 3, 4)
|
42
|
+
sudoku.send(:cell_revers_table, 0, 4).should eql([1, 2, 3, 4, 6, 7, 8, 9])
|
43
|
+
sudoku.send(:cell_revers_table, 5, 4).should eql([1, 2, 3, 4, 6, 7, 8, 9])
|
44
|
+
sudoku.send(:cell_revers_table, 8, 4).should eql([1, 2, 3, 4, 6, 7, 8, 9])
|
45
|
+
sudoku.send(:cell_revers_table, 3, 4).should eql([5])
|
46
|
+
end
|
47
|
+
|
48
|
+
it "in column (don't exist cell with single element)" do
|
49
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
50
|
+
sudoku.send(:erase_in_vertical_one, 3, 4)
|
51
|
+
sudoku.send(:cell_revers_table, 5, 4).should eql([1, 2, 3, 4,5 , 6, 7, 8, 9])
|
52
|
+
sudoku.send(:cell_revers_table, 3, 4).should eql([1, 2, 3, 4,5 , 6, 7, 8, 9])
|
53
|
+
end
|
54
|
+
|
55
|
+
it "in row (exist cell with single element)" do
|
56
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
57
|
+
sudoku.send(:cell_revers_table, 3, 4, [5])
|
58
|
+
sudoku.send(:erase_in_horizontal_one, 3, 4)
|
59
|
+
sudoku.send(:cell_revers_table, 3, 2).should eql([1, 2, 3, 4, 6, 7, 8, 9])
|
60
|
+
sudoku.send(:cell_revers_table, 3, 4).should eql([5])
|
61
|
+
end
|
62
|
+
|
63
|
+
it "in column (don't exist cell with single element)" do
|
64
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
65
|
+
sudoku.send(:erase_in_horizontal_one, 3, 4)
|
66
|
+
sudoku.send(:cell_revers_table, 3, 2).should eql([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
67
|
+
sudoku.send(:cell_revers_table, 3, 4).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
68
|
+
end
|
69
|
+
|
70
|
+
it "in block (exist cell with single element)" do
|
71
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
72
|
+
sudoku.send(:cell_revers_table, 7, 3, [8])
|
73
|
+
sudoku.send(:erase_in_block_one, 7, 3)
|
74
|
+
sudoku.send(:cell_revers_table, 6, 2).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
75
|
+
sudoku.send(:cell_revers_table, 5, 6).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
76
|
+
sudoku.send(:cell_revers_table, 5, 3).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
77
|
+
sudoku.send(:cell_revers_table, 7, 6).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
78
|
+
|
79
|
+
sudoku.send(:cell_revers_table, 6, 4).should eql([1, 2, 3, 4 ,5, 6, 7, 9])
|
80
|
+
sudoku.send(:cell_revers_table, 8, 3).should eql([1, 2, 3, 4 ,5, 6, 7, 9])
|
81
|
+
sudoku.send(:cell_revers_table, 8, 5).should eql([1, 2, 3, 4 ,5, 6, 7, 9])
|
82
|
+
sudoku.send(:cell_revers_table, 7, 3).should eql([8])
|
83
|
+
end
|
84
|
+
|
85
|
+
it "in block (don't exist cell with single element)" do
|
86
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
87
|
+
sudoku.send(:erase_in_block_one, 7, 3)
|
88
|
+
sudoku.send(:cell_revers_table, 6, 2).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
89
|
+
sudoku.send(:cell_revers_table, 5, 6).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
90
|
+
|
91
|
+
sudoku.send(:cell_revers_table, 6, 4).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
92
|
+
sudoku.send(:cell_revers_table, 8, 3).should eql([1, 2, 3, 4 ,5, 6, 7, 8, 9])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "select cells with two elements (rest erase)" do
|
97
|
+
|
98
|
+
it "in block" do
|
99
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
100
|
+
3.times do |row|
|
101
|
+
3.times do |column|
|
102
|
+
sudoku.send(:cell_revers_table, row, column, [2, 3, 4, 5, 6, 7, 9])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
sudoku.send(:cell_revers_table, 0, 0, [1, 5, 8])
|
107
|
+
sudoku.send(:cell_revers_table, 2, 2, [1, 3, 8])
|
108
|
+
|
109
|
+
sudoku.send(:cells_with_two_elements_for, "block", 0)
|
110
|
+
sudoku.send(:cell_revers_table, 0, 0).should eql([1, 8])
|
111
|
+
sudoku.send(:cell_revers_table, 2, 2).should eql([1, 8])
|
112
|
+
sudoku.send(:cell_revers_table, 1, 2).should eql([2, 3, 4, 5, 6, 7, 9])
|
113
|
+
end
|
114
|
+
|
115
|
+
it "in column" do
|
116
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
117
|
+
sudoku.send(:cell_revers_table, 0, 4, [1, 5, 8])
|
118
|
+
sudoku.send(:cell_revers_table, 1, 4, [1, 3, 8])
|
119
|
+
(2..8).each do |index|
|
120
|
+
sudoku.send(:cell_revers_table, index, 4, [2, 3, 4, 5, 6, 7, 9])
|
121
|
+
end
|
122
|
+
|
123
|
+
sudoku.send(:cells_with_two_elements_for, "column", 4)
|
124
|
+
sudoku.send(:cell_revers_table, 0, 4).should eql([1, 8])
|
125
|
+
sudoku.send(:cell_revers_table, 1, 4).should eql([1, 8])
|
126
|
+
sudoku.send(:cell_revers_table, 2, 4).should eql([2, 3, 4, 5, 6, 7, 9])
|
127
|
+
end
|
128
|
+
|
129
|
+
it "in row" do
|
130
|
+
sudoku = NSudoku.new(TABLE_EMPTY)
|
131
|
+
sudoku.send(:cell_revers_table, 4, 0, [1, 5, 8])
|
132
|
+
sudoku.send(:cell_revers_table, 4, 1, [1, 3, 8])
|
133
|
+
(2..8).each do |index|
|
134
|
+
sudoku.send(:cell_revers_table, 4, index, [2, 3, 4, 5, 6, 7, 9])
|
135
|
+
end
|
136
|
+
|
137
|
+
sudoku.send(:cells_with_two_elements_for, "row", 4)
|
138
|
+
sudoku.send(:cell_revers_table, 4, 0).should eql([1, 8])
|
139
|
+
sudoku.send(:cell_revers_table, 4, 1).should eql([1, 8])
|
140
|
+
sudoku.send(:cell_revers_table, 4, 2).should eql([2, 3, 4, 5, 6, 7, 9])
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
it "solve whole sudoku" do
|
146
|
+
NSudoku.new(EXAMPLE).solve.should eql("712460850394852671685170420070208596926715384050906712540320968269584137030690245")
|
147
|
+
end
|
148
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nsudoku
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michał Szyma
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.10.0
|
30
|
+
description: This gem solve puzzle game sudoku 9x9
|
31
|
+
email:
|
32
|
+
- raglub.ruby@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rvmrc
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/nsudoku.rb
|
44
|
+
- lib/nsudoku/version.rb
|
45
|
+
- lib/sudoku.rb~
|
46
|
+
- nsudoku.gemspec
|
47
|
+
- spec/nsudoku_spec.rb
|
48
|
+
homepage: http://github.com/raglub/nsudoku
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: This gem solve puzzle game sudoku
|
72
|
+
test_files:
|
73
|
+
- spec/nsudoku_spec.rb
|