nonograms 0.2.1 → 0.2.2

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/README.md CHANGED
@@ -27,9 +27,9 @@ For example:
27
27
  +---+---+---+---+---+
28
28
  2 1 | | | | | |
29
29
  +---+---+---+---+---+
30
- 2 1 | | | | | |
30
+ 1 | | | | | |
31
31
  +---+---+---+---+---+
32
- 2 1 | | | | | |
32
+ 1 1 | | | | | |
33
33
  +---+---+---+---+---+
34
34
 
35
35
  We should get result
@@ -41,9 +41,9 @@ We should get result
41
41
  +---+---+---+---+---+
42
42
  2 1 | | # | # | | # |
43
43
  +---+---+---+---+---+
44
- 2 1 | # | | | | |
44
+ 1 | # | | | | |
45
45
  +---+---+---+---+---+
46
- 2 1 | | # | | | # |
46
+ 1 1 | | # | | | # |
47
47
  +---+---+---+---+---+
48
48
 
49
49
  in line result should be "01000"+"01101"+"10000"+"01001"
@@ -64,17 +64,21 @@ Check whether entered data are properly
64
64
  Display result on the console
65
65
 
66
66
  > @nonograms.display
67
+
67
68
  #=> result: 0
68
- #=>
69
- #=>
70
- #=> ■ ■ ■
71
- #=> ■
72
- #=> ■
73
- #=>
74
- #=> result: 1
75
- #=>
76
- #=> ■
77
- #=> ■ ■ ■
78
- #=> ■
79
- #=> ■ ■
69
+ #=> 1 2 1 1
70
+ #=> 1 1
71
+ #=> ----------
72
+ #=> 1 |
73
+ #=> 2 1 | ■ ■
74
+ #=> 1 |■
75
+ #=> 1 1 | ■ ■
80
76
 
77
+ #=> result: 1
78
+ #=> 1 2 1 1
79
+ #=> 1 1
80
+ #=> ----------
81
+ #=> 1 | ■
82
+ #=> 2 1 |■ ■ ■
83
+ #=> 1 | ■
84
+ #=> 1 1 | ■ ■
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
  require "nonograms/version"
3
+ require "nonograms/logic"
3
4
  require "nonograms/display"
4
5
  require "nonograms/checker"
5
- require "nonograms/matrix"
6
6
 
7
7
  # class can solve the matrix game Nonograms
8
8
  class Nonograms
@@ -12,7 +12,7 @@ class Nonograms
12
12
  @horizontal = horizontal
13
13
  @amount_row = horizontal.length
14
14
  @amount_column = vertical.length
15
- @matrix = Nonograms::Matrix.new(@amount_row, @amount_column)
15
+ @logic = Nonograms::Logic.new(@horizontal, @vertical)
16
16
  @results = []
17
17
  end
18
18
 
@@ -25,60 +25,36 @@ class Nonograms
25
25
 
26
26
  # display the result on console
27
27
  def display
28
- Nonograms::Display.new(@results, @amount_row, @amount_column)
28
+ Nonograms::Display.new(@results, @horizontal, @vertical)
29
29
  end
30
30
 
31
31
  def properly_data_entered?
32
32
  Nonograms::Checker.new(@vertical, @horizontal).properly_data_entered?
33
33
  end
34
34
 
35
- def vertical_acceptable?(row, column)
36
- unless row == @amount_row-1
37
- vector_acceptable?( @vertical[column], @matrix.count_vertical(column) )
38
- else
39
- return ( @vertical[column] == @matrix.count_vertical(column) )
40
- end
41
- end
42
-
43
- def horizontal_acceptable?(row, column)
44
- unless column == @amount_column-1
45
- vector_acceptable?( @horizontal[row], @matrix.count_horizontal(row) )
46
- else
47
- return ( @horizontal[row] == @matrix.count_horizontal(row))
48
- end
49
- end
50
-
51
- def vector_acceptable?(origin, piece)
52
- return false if piece.length > origin.length
53
- piece.each_with_index do |value, index|
54
- if index == piece.length-1
55
- return false unless origin[index] >= piece[index]
56
- else
57
- return false unless origin[index] == piece[index]
58
- end
59
- end
60
- return true
61
- end
62
-
63
35
  private
64
36
 
65
37
  # run recursion from fixed position row and column if @matrix is acceptable
66
38
  def run_recursion(row = 0, column = 0)
67
- return unless vertical_acceptable?(row, column) and horizontal_acceptable?(row, column)
68
- if row == @amount_row-1 and column == @amount_column-1
69
- @results << Marshal.load(Marshal.dump(@matrix.get)).flatten.join("")
70
- return nil
71
- end
39
+ return unless @logic.cross_acceptable?(row, column)
40
+ return if last_cell?(row, column)
72
41
  next_cell_set(0, row, column)
73
42
  next_cell_set(1, row, column)
74
43
  end
75
44
 
45
+ def last_cell?(row, column)
46
+ return false unless row == @amount_row-1 and column == @amount_column-1
47
+ @results << Marshal.load(Marshal.dump(@logic.matrix.get)).flatten.join("")
48
+ true
49
+ end
50
+
76
51
  def next_cell_set(value, row, column)
77
52
  new_row = (row*@amount_column + column + 1) / @amount_column
78
53
  new_column = (row*@amount_column + column + 1) % @amount_column
79
- @matrix.set(new_row, new_column, value)
54
+
55
+ @logic.matrix.set(new_row, new_column, value)
80
56
  run_recursion(new_row, new_column)
81
- @matrix.set(new_row, new_column, 0)
57
+ @logic.matrix.set(new_row, new_column, 0)
82
58
  end
83
59
 
84
60
  end
@@ -2,12 +2,15 @@
2
2
 
3
3
  class Nonograms
4
4
 
5
+ #Display result on the console
5
6
  class Display
6
7
 
7
- def initialize(results, amount_row, amount_column)
8
+ def initialize(results, horizontal, vertical)
8
9
  @results = results
9
- @amount_row = amount_row
10
- @amount_column = amount_column
10
+ @horizontal = horizontal
11
+ @vertical = vertical
12
+ @amount_row = horizontal.length
13
+ @amount_column = vertical.length
11
14
  show_results(results)
12
15
  end
13
16
 
@@ -18,13 +21,46 @@ class Nonograms
18
21
  end
19
22
  end
20
23
 
24
+ def max_length(array)
25
+ array.inject(0) { |result, value| result = value.length if value.length > result; result}
26
+ end
27
+
21
28
  def show_result(result)
29
+ print_header
22
30
  result.split("").each_with_index do |value, index|
23
- print "\n" if index % @amount_column == 0
31
+ if index % @amount_column == 0
32
+ print "\n"
33
+ print_margin(index/@amount_column)
34
+ end
24
35
  print "■ " if value == "1"
25
36
  print " " if value == "0"
26
37
  end
27
38
  print "\n\n"
28
39
  end
40
+
41
+ def print_header
42
+ max_length(@vertical).times.each do |index|
43
+ print_margin(-1)
44
+ @vertical.each do |element|
45
+ print element[index] || " "
46
+ print " "
47
+ end
48
+ print "\n"
49
+ end
50
+ print_margin(-1)
51
+ @vertical.length.times.each { |index| print "--" }
52
+ end
53
+
54
+ def print_margin(index)
55
+ if index == -1
56
+ print " "*max_length(@horizontal) + " "
57
+ else
58
+ max_length(@horizontal).times do |sub_index|
59
+ print @horizontal[index][sub_index] || " "
60
+ print " "
61
+ end
62
+ print "|"
63
+ end
64
+ end
29
65
  end
30
66
  end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require 'nonograms/matrix'
3
+
4
+ class Nonograms
5
+
6
+ class Logic
7
+
8
+ def initialize(horizontal, vertical)
9
+ @vertical = vertical
10
+ @horizontal = horizontal
11
+ @amount_row = horizontal.length
12
+ @amount_column = vertical.length
13
+ @matrix = Nonograms::Matrix.new(@amount_row, @amount_column)
14
+ end
15
+
16
+ def matrix
17
+ @matrix
18
+ end
19
+
20
+ def cross_acceptable?(row, column)
21
+ vertical_acceptable?(row, column) and horizontal_acceptable?(row, column)
22
+ end
23
+
24
+ def vertical_acceptable?(row, column)
25
+ unless row == @amount_row-1
26
+ vector_acceptable?( @vertical[column], @matrix.count_vertical(column) )
27
+ else
28
+ return ( @vertical[column] == @matrix.count_vertical(column) )
29
+ end
30
+ end
31
+
32
+ def horizontal_acceptable?(row, column)
33
+ unless column == @amount_column-1
34
+ vector_acceptable?( @horizontal[row], @matrix.count_horizontal(row) )
35
+ else
36
+ return ( @horizontal[row] == @matrix.count_horizontal(row))
37
+ end
38
+ end
39
+
40
+ def vector_acceptable?(origin, piece)
41
+ return false if piece.length > origin.length
42
+ piece.each_with_index do |value, index|
43
+ if index == piece.length-1
44
+ return false unless origin[index] >= piece[index]
45
+ else
46
+ return false unless origin[index] == piece[index]
47
+ end
48
+ end
49
+ return true
50
+ end
51
+
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  class Nonograms
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -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-08"
9
+ gem.date = "2012-07-10"
10
10
  gem.homepage = "https://github.com/raglub/nonograms"
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'nonograms'
3
+
4
+ describe Nonograms::Logic do
5
+
6
+ before(:each) do
7
+ vertical = [[1], [2, 1], [1], [], [1, 1]]
8
+ horizontal = [[1], [2, 1], [1], [1, 1]]
9
+ @logic = Nonograms::Logic.new(horizontal, vertical)
10
+ end
11
+
12
+ it "for vectors acceptable" do
13
+ origin = [3, 1, 2]
14
+ @logic.vector_acceptable?(origin, [3, 1]).should be_true
15
+ @logic.vector_acceptable?(origin, [3, 1, 2]).should be_true
16
+ @logic.vector_acceptable?(origin, [3, 1, 1]).should be_true
17
+ @logic.vector_acceptable?(origin, []).should be_true
18
+ end
19
+
20
+ it "for vectors don't acceptable" do
21
+ origin = [3, 1, 2]
22
+ @logic.vector_acceptable?(origin, [4, 1]).should be_false
23
+ @logic.vector_acceptable?(origin, [3, 1, 2, 4]).should be_false
24
+ @logic.vector_acceptable?(origin, [3, 1, 3]).should be_false
25
+ end
26
+
27
+ end
@@ -15,20 +15,4 @@ describe Nonograms do
15
15
  it "should properly entered data" do
16
16
  @nonograms.properly_data_entered?.should be_true
17
17
  end
18
-
19
- it "for vectors acceptable" do
20
- origin = [3, 1, 2]
21
- @nonograms.vector_acceptable?(origin, [3, 1]).should be_true
22
- @nonograms.vector_acceptable?(origin, [3, 1, 2]).should be_true
23
- @nonograms.vector_acceptable?(origin, [3, 1, 1]).should be_true
24
- @nonograms.vector_acceptable?(origin, []).should be_true
25
- end
26
-
27
- it "for vectors don't acceptable" do
28
- origin = [3, 1, 2]
29
- @nonograms.vector_acceptable?(origin, [4, 1]).should be_false
30
- @nonograms.vector_acceptable?(origin, [3, 1, 2, 4]).should be_false
31
- @nonograms.vector_acceptable?(origin, [3, 1, 3]).should be_false
32
- end
33
-
34
18
  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.1
4
+ version: 0.2.2
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-08 00:00:00.000000000 Z
12
+ date: 2012-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -42,10 +42,12 @@ files:
42
42
  - lib/nonograms.rb
43
43
  - lib/nonograms/checker.rb
44
44
  - lib/nonograms/display.rb
45
+ - lib/nonograms/logic.rb
45
46
  - lib/nonograms/matrix.rb
46
47
  - lib/nonograms/version.rb
47
48
  - nonograms.gemspec
48
49
  - spec/checker_spec.rb
50
+ - spec/logic_spec.rb
49
51
  - spec/matrix_spec.rb
50
52
  - spec/nonograms_spec.rb
51
53
  homepage: https://github.com/raglub/nonograms
@@ -74,5 +76,6 @@ specification_version: 3
74
76
  summary: Solve Nonograms.
75
77
  test_files:
76
78
  - spec/checker_spec.rb
79
+ - spec/logic_spec.rb
77
80
  - spec/matrix_spec.rb
78
81
  - spec/nonograms_spec.rb