nonograms 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nonograms.gemspec
4
+ gemspec
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,60 @@
1
+ # Nonograms
2
+
3
+ Solve the puzzle game nonograms.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nonograms'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nonograms
18
+
19
+ ## Usage
20
+
21
+ For example:
22
+
23
+ 2 1
24
+ 1 1 1 1
25
+ +---+---+---+---+---+
26
+ 1 | | | | | |
27
+ +---+---+---+---+---+
28
+ 2 1 | | | | | |
29
+ +---+---+---+---+---+
30
+ 2 1 | | | | | |
31
+ +---+---+---+---+---+
32
+ 2 1 | | | | | |
33
+ +---+---+---+---+---+
34
+
35
+ We should get result
36
+
37
+ 2 1
38
+ 1 1 1 1
39
+ +---+---+---+---+---+
40
+ 1 | | # | | | |
41
+ +---+---+---+---+---+
42
+ 2 1 | | # | # | | # |
43
+ +---+---+---+---+---+
44
+ 2 1 | # | | | | |
45
+ +---+---+---+---+---+
46
+ 2 1 | | # | | | # |
47
+ +---+---+---+---+---+
48
+
49
+ in line result should be "01000"+"01101"+"10000"+"01001"
50
+
51
+ You can solve this example when you write the code below
52
+
53
+ > require "nonograms"
54
+
55
+ > vertical = [[1], [2, 1], [1], [], [1, 1]]
56
+ > horizontal = [[1], [2, 1], [1], [1, 1]]
57
+ > @nonograms = Nonograms.new(vertical, horizontal)
58
+ > @nonograms.solve
59
+ > @nonograms.results #=> ["01000"+"01101"+"10000"+"01001", ...]
60
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/nonograms.rb ADDED
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+ require "nonograms/version"
3
+
4
+ # class can solve the puzzle game Nonograms
5
+ class Nonograms
6
+
7
+ attr_reader :results
8
+
9
+ def initialize(vertical, horizontal)
10
+ @vertical = vertical
11
+ @horizontal = horizontal
12
+ @amount_row = horizontal.length
13
+ @amount_column = vertical.length
14
+ @puzzle = []
15
+ @amount_row.times do |index|
16
+ @puzzle << [0]*@amount_column
17
+ end
18
+ @results = []
19
+ end
20
+
21
+ def solve(row = 0, column = 0)
22
+ if vertical_acceptable?(row, column) and horizontal_acceptable?(row, column)
23
+ if row == @amount_row-1 and column == @amount_column-1
24
+ @results << Marshal.load(Marshal.dump(@puzzle)).flatten.join("")
25
+ return @amount_column
26
+ end
27
+ next_cell_set(0, row, column)
28
+ next_cell_set(1, row, column)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def next_cell_set(value, row, column)
35
+ new_row = (row*@amount_column + column + 1) / @amount_column
36
+ new_column = (row*@amount_column + column + 1) % @amount_column
37
+ @puzzle[new_row][new_column] = value
38
+ solve(new_row, new_column)
39
+ @puzzle[new_row][new_column] = 0
40
+ end
41
+
42
+ # count values '1' in vertical
43
+ def count_vertical(column)
44
+ result, counter = [], 0
45
+ @amount_row.times do |index|
46
+ if @puzzle[index][column] == 1
47
+ counter += 1
48
+ else
49
+ result << counter unless counter == 0
50
+ counter = 0
51
+ end
52
+ result << counter if index == @amount_row-1 and not counter == 0
53
+ end
54
+ result
55
+ end
56
+
57
+ # count values '1' in horizontal
58
+ def count_horizontal(row)
59
+ result, counter = [], 0
60
+ @amount_column.times do |index|
61
+ if @puzzle[row][index] == 1
62
+ counter += 1
63
+ else
64
+ result << counter unless counter == 0
65
+ counter = 0
66
+ end
67
+ result << counter if index == @amount_column-1 and not counter == 0
68
+ end
69
+ result
70
+ end
71
+
72
+ def vertical_acceptable?(row, column)
73
+ unless row == @amount_row-1
74
+ vector_acceptable?( @vertical[column], count_vertical(column) )
75
+ else
76
+ return ( @vertical[column] == count_vertical(column) )
77
+ end
78
+ end
79
+
80
+ def horizontal_acceptable?(row, column)
81
+ unless column == @amount_column-1
82
+ vector_acceptable?( @horizontal[row], count_horizontal(row) )
83
+ else
84
+ return ( @horizontal[row] == count_horizontal(row))
85
+ end
86
+ end
87
+
88
+ def vector_acceptable?(origin, piece)
89
+ return false if piece.length > origin.length
90
+ piece.each_with_index do |value, index|
91
+ if index == piece.length-1
92
+ return false unless origin[index] >= piece[index]
93
+ else
94
+ return false unless origin[index] == piece[index]
95
+ end
96
+ end
97
+ return true
98
+ end
99
+
100
+ end
@@ -0,0 +1,3 @@
1
+ class Nonograms
2
+ VERSION = "0.1.0"
3
+ end
data/nonograms.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/nonograms/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Michał Szyma"]
6
+ gem.email = ["raglub.ruby@gmail.com"]
7
+ gem.description = %q{solve the puzzle game nonograms.}
8
+ gem.summary = %q{Solve Nonograms.}
9
+ gem.date = "2012-07-03"
10
+ gem.homepage = "https://github.com/raglub/nonograms"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "nonograms"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Nonograms::VERSION
17
+ gem.platform = Gem::Platform::RUBY
18
+
19
+ gem.add_development_dependency "rspec", ">= 2.10.0"
20
+ end
@@ -0,0 +1,15 @@
1
+ require 'nonograms'
2
+
3
+ describe "Nonograms" do
4
+
5
+ before(:each) do
6
+ vertical = [[1], [2, 1], [1], [], [1, 1]]
7
+ horizontal = [[1], [2, 1], [1], [1, 1]]
8
+ @nonograms = Nonograms.new(vertical, horizontal)
9
+ end
10
+
11
+ it "should properly solve the nonograms" do
12
+ @nonograms.solve
13
+ @nonograms.results.should include("01000"+"01101"+"10000"+"01001")
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nonograms
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-07-03 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: solve the puzzle game nonograms.
31
+ email:
32
+ - raglub.ruby@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - lib/nonograms.rb
43
+ - lib/nonograms/version.rb
44
+ - nonograms.gemspec
45
+ - spec/nonograms_spec.rb
46
+ homepage: https://github.com/raglub/nonograms
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Solve Nonograms.
70
+ test_files:
71
+ - spec/nonograms_spec.rb