nonograms 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -1
- data/lib/nonograms.rb +13 -37
- data/lib/nonograms/display.rb +1 -0
- data/lib/nonograms/matrix.rb +60 -0
- data/lib/nonograms/version.rb +1 -1
- data/nonograms.gemspec +1 -1
- data/spec/matrix_spec.rb +25 -0
- data/spec/nonograms_spec.rb +4 -4
- metadata +5 -2
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
For example:
|
22
22
|
|
23
23
|
2 1
|
24
24
|
1 1 1 1
|
@@ -57,6 +57,9 @@ You can solve this example when you write the code below
|
|
57
57
|
> @nonograms = Nonograms.new(horizontal, vertical)
|
58
58
|
> @nonograms.solve #=> ["01000"+"01101"+"10000"+"01001", ...]
|
59
59
|
|
60
|
+
Check whether entered data are properly
|
61
|
+
|
62
|
+
> @nonograms.properly_data_entered? #=> true
|
60
63
|
|
61
64
|
Display result on the console
|
62
65
|
|
data/lib/nonograms.rb
CHANGED
@@ -2,8 +2,9 @@
|
|
2
2
|
require "nonograms/version"
|
3
3
|
require "nonograms/display"
|
4
4
|
require "nonograms/checker"
|
5
|
+
require "nonograms/matrix"
|
5
6
|
|
6
|
-
# class can solve the
|
7
|
+
# class can solve the matrix game Nonograms
|
7
8
|
class Nonograms
|
8
9
|
|
9
10
|
def initialize(horizontal, vertical)
|
@@ -11,7 +12,7 @@ class Nonograms
|
|
11
12
|
@horizontal = horizontal
|
12
13
|
@amount_row = horizontal.length
|
13
14
|
@amount_column = vertical.length
|
14
|
-
@
|
15
|
+
@matrix = Nonograms::Matrix.new(@amount_row, @amount_column)
|
15
16
|
@results = []
|
16
17
|
end
|
17
18
|
|
@@ -27,39 +28,23 @@ class Nonograms
|
|
27
28
|
Nonograms::Display.new(@results, @amount_row, @amount_column)
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
# * vector : [0, 1, 1, 0, 0, 1, 0]
|
33
|
-
# * return: [2, 1]
|
34
|
-
def count_vector(vector)
|
35
|
-
vector.join("").scan(/[1]+/).map{|element| element.length}
|
36
|
-
end
|
37
|
-
|
38
|
-
# count amount values '1' in vertical vector
|
39
|
-
def count_vertical(column)
|
40
|
-
vertical_vector = @puzzle[0...@amount_row].map{|vector| vector[column]}
|
41
|
-
count_vector(vertical_vector)
|
42
|
-
end
|
43
|
-
|
44
|
-
# count amount values '1' in vertical vector
|
45
|
-
def count_horizontal(row)
|
46
|
-
horizontal_vector = @puzzle[row][0...@amount_column]
|
47
|
-
count_vector(horizontal_vector)
|
31
|
+
def properly_data_entered?
|
32
|
+
Nonograms::Checker.new(@vertical, @horizontal).properly_data_entered?
|
48
33
|
end
|
49
34
|
|
50
35
|
def vertical_acceptable?(row, column)
|
51
36
|
unless row == @amount_row-1
|
52
|
-
vector_acceptable?( @vertical[column], count_vertical(column) )
|
37
|
+
vector_acceptable?( @vertical[column], @matrix.count_vertical(column) )
|
53
38
|
else
|
54
|
-
return ( @vertical[column] == count_vertical(column) )
|
39
|
+
return ( @vertical[column] == @matrix.count_vertical(column) )
|
55
40
|
end
|
56
41
|
end
|
57
42
|
|
58
43
|
def horizontal_acceptable?(row, column)
|
59
44
|
unless column == @amount_column-1
|
60
|
-
vector_acceptable?( @horizontal[row], count_horizontal(row) )
|
45
|
+
vector_acceptable?( @horizontal[row], @matrix.count_horizontal(row) )
|
61
46
|
else
|
62
|
-
return ( @horizontal[row] == count_horizontal(row))
|
47
|
+
return ( @horizontal[row] == @matrix.count_horizontal(row))
|
63
48
|
end
|
64
49
|
end
|
65
50
|
|
@@ -75,22 +60,13 @@ class Nonograms
|
|
75
60
|
return true
|
76
61
|
end
|
77
62
|
|
78
|
-
# get the matrix with cells values zero
|
79
|
-
def empty_puzzle
|
80
|
-
result = []
|
81
|
-
@amount_row.times do |index|
|
82
|
-
result << [0]*@amount_column
|
83
|
-
end
|
84
|
-
result
|
85
|
-
end
|
86
|
-
|
87
63
|
private
|
88
64
|
|
89
|
-
# run recursion from fixed position row and column if @
|
65
|
+
# run recursion from fixed position row and column if @matrix is acceptable
|
90
66
|
def run_recursion(row = 0, column = 0)
|
91
67
|
return unless vertical_acceptable?(row, column) and horizontal_acceptable?(row, column)
|
92
68
|
if row == @amount_row-1 and column == @amount_column-1
|
93
|
-
@results << Marshal.load(Marshal.dump(@
|
69
|
+
@results << Marshal.load(Marshal.dump(@matrix.get)).flatten.join("")
|
94
70
|
return nil
|
95
71
|
end
|
96
72
|
next_cell_set(0, row, column)
|
@@ -100,9 +76,9 @@ private
|
|
100
76
|
def next_cell_set(value, row, column)
|
101
77
|
new_row = (row*@amount_column + column + 1) / @amount_column
|
102
78
|
new_column = (row*@amount_column + column + 1) % @amount_column
|
103
|
-
@
|
79
|
+
@matrix.set(new_row, new_column, value)
|
104
80
|
run_recursion(new_row, new_column)
|
105
|
-
@
|
81
|
+
@matrix.set(new_row, new_column, 0)
|
106
82
|
end
|
107
83
|
|
108
84
|
end
|
data/lib/nonograms/display.rb
CHANGED
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class Nonograms
|
4
|
+
|
5
|
+
class Matrix
|
6
|
+
|
7
|
+
def initialize(height, width)
|
8
|
+
@height = height
|
9
|
+
@width = width
|
10
|
+
@matrix = create_matrix
|
11
|
+
end
|
12
|
+
|
13
|
+
# set cell of position row and column some value
|
14
|
+
def set(row, column, value)
|
15
|
+
@matrix[row][column] = value
|
16
|
+
end
|
17
|
+
|
18
|
+
# get the matrix
|
19
|
+
def get
|
20
|
+
@matrix
|
21
|
+
end
|
22
|
+
|
23
|
+
# count amount values '1' in vector
|
24
|
+
# for example:
|
25
|
+
# * vector : [0, 1, 1, 0, 0, 1, 0]
|
26
|
+
# * return: [2, 1]
|
27
|
+
def count_vector(vector)
|
28
|
+
vector.join("").scan(/[1]+/).map{|element| element.length}
|
29
|
+
end
|
30
|
+
|
31
|
+
# get vector from index of column
|
32
|
+
def vertical_vector(index)
|
33
|
+
@matrix[0...@height].map{|vector| vector[index]}
|
34
|
+
end
|
35
|
+
|
36
|
+
# count amount values '1' in vertical vector
|
37
|
+
def count_vertical(column)
|
38
|
+
count_vector(vertical_vector(column))
|
39
|
+
end
|
40
|
+
|
41
|
+
# get vector from index of row
|
42
|
+
def horizontal_vector(index)
|
43
|
+
@matrix[index][0...@width]
|
44
|
+
end
|
45
|
+
|
46
|
+
# count amount values '1' in horizontal vector
|
47
|
+
def count_horizontal(row)
|
48
|
+
count_vector(horizontal_vector(row))
|
49
|
+
end
|
50
|
+
|
51
|
+
# get the matrix with cells values zero
|
52
|
+
def create_matrix
|
53
|
+
result = []
|
54
|
+
@height.times do |index|
|
55
|
+
result << [0]*@width
|
56
|
+
end
|
57
|
+
result
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/nonograms/version.rb
CHANGED
data/nonograms.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = ["raglub.ruby@gmail.com"]
|
7
7
|
gem.description = %q{solve the puzzle game nonograms.}
|
8
8
|
gem.summary = %q{Solve Nonograms.}
|
9
|
-
gem.date = "2012-07-
|
9
|
+
gem.date = "2012-07-08"
|
10
10
|
gem.homepage = "https://github.com/raglub/nonograms"
|
11
11
|
|
12
12
|
gem.files = `git ls-files`.split($\)
|
data/spec/matrix_spec.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'nonograms'
|
2
|
+
|
3
|
+
describe Nonograms::Matrix do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@matrix = Nonograms::Matrix.new(5, 6)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should properly count amount of numbers 1 in vector" do
|
10
|
+
vector = [0, 0, 1, 1, 1, 0, 1]
|
11
|
+
@matrix.count_vector(vector).should eql([3, 1])
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should properly count amount of numbers 1 in vertical vector" do
|
15
|
+
@matrix.set(0, 2, 1)
|
16
|
+
@matrix.set(4, 2, 1)
|
17
|
+
@matrix.count_vertical(2).should eql([1, 1])
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should properly count amount of numbers 1 in horizontal vector" do
|
21
|
+
@matrix.set(2, 1, 1)
|
22
|
+
@matrix.set(2, 5, 1)
|
23
|
+
@matrix.count_horizontal(2).should eql([1, 1])
|
24
|
+
end
|
25
|
+
end
|
data/spec/nonograms_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'nonograms'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Nonograms do
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
vertical = [[1], [2, 1], [1], [], [1, 1]]
|
@@ -12,9 +12,8 @@ describe "Nonograms" do
|
|
12
12
|
@nonograms.solve.should include("01000"+"01101"+"10000"+"01001")
|
13
13
|
end
|
14
14
|
|
15
|
-
it "should properly
|
16
|
-
|
17
|
-
@nonograms.count_vector(vector).should eql([3, 1])
|
15
|
+
it "should properly entered data" do
|
16
|
+
@nonograms.properly_data_entered?.should be_true
|
18
17
|
end
|
19
18
|
|
20
19
|
it "for vectors acceptable" do
|
@@ -31,4 +30,5 @@ describe "Nonograms" do
|
|
31
30
|
@nonograms.vector_acceptable?(origin, [3, 1, 2, 4]).should be_false
|
32
31
|
@nonograms.vector_acceptable?(origin, [3, 1, 3]).should be_false
|
33
32
|
end
|
33
|
+
|
34
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nonograms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -42,9 +42,11 @@ files:
|
|
42
42
|
- lib/nonograms.rb
|
43
43
|
- lib/nonograms/checker.rb
|
44
44
|
- lib/nonograms/display.rb
|
45
|
+
- lib/nonograms/matrix.rb
|
45
46
|
- lib/nonograms/version.rb
|
46
47
|
- nonograms.gemspec
|
47
48
|
- spec/checker_spec.rb
|
49
|
+
- spec/matrix_spec.rb
|
48
50
|
- spec/nonograms_spec.rb
|
49
51
|
homepage: https://github.com/raglub/nonograms
|
50
52
|
licenses: []
|
@@ -72,4 +74,5 @@ specification_version: 3
|
|
72
74
|
summary: Solve Nonograms.
|
73
75
|
test_files:
|
74
76
|
- spec/checker_spec.rb
|
77
|
+
- spec/matrix_spec.rb
|
75
78
|
- spec/nonograms_spec.rb
|