nsudoku 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,17 +18,26 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ Usage class NSudoku for example:
22
+
23
+ EXAMPLE = [
24
+ "012000000",
25
+ "304052000",
26
+ "605170000",
27
+ "070208090",
28
+ "920700084",
29
+ "050906010",
30
+ "000320908",
31
+ "000580107",
32
+ "000000240"].join
33
+
34
+ Solve game the sudoku
35
+
21
36
  > 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"
37
+ > NSudoku.new(EXAMPLE).solve #=> "712460850394852671685170420070208596926715384050906712540320968269584137030690245"
38
+
39
+ Check whether your array has got that same values in rows, columns or sub blocks. When your array is correct you should expect result nil.
40
+ In other situation you should received array of position with that same value.
41
+
42
+ > NSudoku::Checker.new(EXAMPLE).repeat_in #=> nil
34
43
 
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ class NSudoku
4
+ class Checker
5
+ def initialize(sudoku)
6
+ @sudoku = sudoku
7
+ @width = Math.sqrt(@sudoku.length).to_i
8
+ @block_width = Math.sqrt(@width).to_i
9
+ end
10
+
11
+ def repeat_in
12
+ @width.times do |index|
13
+ result = repeat_in_row(index)
14
+ return result if result
15
+ result = repeat_in_col(index)
16
+ return result if result
17
+ result = repeat_in_block(index)
18
+ return result if result
19
+ end
20
+ nil
21
+ end
22
+
23
+ # check whether values repeated in row
24
+ def repeat_in_row(row)
25
+ sub_sudoku = @sudoku[row*@width..(row+1)*@width-1].split("").map{|element| element.to_i}
26
+ result = []
27
+ @width.times do |col|
28
+ if sub_sudoku.count(col+1) > 1
29
+ sub_sudoku.each_with_index do |element, index|
30
+ result << row*@width + index if element == col+1
31
+ end
32
+ return result
33
+ end
34
+ end
35
+ nil
36
+ end
37
+
38
+ # check whether values repeated in col
39
+ def repeat_in_col(col)
40
+ sub_sudoku = []
41
+ @width.times do |index|
42
+ sub_sudoku << @sudoku[index*@width + col].to_i
43
+ end
44
+ result = []
45
+ @width.times do |row|
46
+ if sub_sudoku.count(row+1) > 1
47
+ sub_sudoku.each_with_index do |element, index|
48
+ result << index*@width + col if element == row+1
49
+ end
50
+ return result
51
+ end
52
+ end
53
+ nil
54
+ end
55
+
56
+ # check whether values repeated in block
57
+ def repeat_in_block(block)
58
+ sub_sudoku = []
59
+ @width.times do |index|
60
+ sub_sudoku << @sudoku[position_for_block(block, index)].to_i
61
+ end
62
+
63
+ result = []
64
+ @width.times do |sub_block|
65
+ if sub_sudoku.count(sub_block+1) > 1
66
+ sub_sudoku.each_with_index do |element, index|
67
+ result << position_for_block(block, index) if element == sub_block+1
68
+ end
69
+ return result
70
+ end
71
+ end
72
+ nil
73
+ end
74
+
75
+ def position_for_block(block, sub_block)
76
+ first_col = block.modulo(@block_width)*@block_width
77
+ first_row = block.div(@block_width)*@block_width
78
+ col = first_col + sub_block.modulo(@block_width)
79
+ row = first_row + sub_block.div(@block_width)
80
+ row*@width + col
81
+ end
82
+ end
83
+ end
@@ -1,3 +1,3 @@
1
1
  class NSudoku
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/nsudoku.rb CHANGED
@@ -1,4 +1,5 @@
1
- require "nsudoku/version"
1
+ require_relative "nsudoku/version"
2
+ require_relative "nsudoku/checker"
2
3
 
3
4
  class NSudoku
4
5
 
@@ -19,11 +20,11 @@ class NSudoku
19
20
  erase_in_vertical_three(index)
20
21
  end
21
22
 
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)
23
+ 9.times do |row|
24
+ 9.times do |column|
25
+ erase_in_vertical_one(row, column)
26
+ erase_in_horizontal_one(row, column)
27
+ erase_in_block_one(row, column)
27
28
  end
28
29
  end
29
30
  end
data/nsudoku.gemspec CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.platform = Gem::Platform::RUBY
6
6
  gem.authors = ["Michał Szyma"]
7
7
  gem.email = ["raglub.ruby@gmail.com"]
8
- gem.date = "2012-06-21"
8
+ gem.date = "2012-10-09"
9
9
  gem.description = %q{This gem solve puzzle game sudoku 9x9}
10
10
  gem.summary = %q{This gem solve puzzle game sudoku}
11
11
  gem.homepage = "http://github.com/raglub/nsudoku"
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'nsudoku'
4
+
5
+ describe NSudoku::Checker do
6
+
7
+ it "row 1 has repeat value" do
8
+ example = "0000" +
9
+ "1202" +
10
+ "0000" +
11
+ "2022"
12
+ NSudoku::Checker.new(example).repeat_in_row(1).should eql([5,7])
13
+ end
14
+
15
+ it "column 3 has repeat value" do
16
+ example = "0001" +
17
+ "1202" +
18
+ "0003" +
19
+ "2022"
20
+ NSudoku::Checker.new(example).repeat_in_col(3).should eql([7,15])
21
+ end
22
+
23
+ it "block 3 has repeat value" do
24
+ example = "0001" +
25
+ "1202" +
26
+ "0003" +
27
+ "2022"
28
+ NSudoku::Checker.new(example).repeat_in_block(3).should eql([14,15])
29
+ end
30
+
31
+ it "the array of sudoku has got at positions 5 and 7 that same value" do
32
+ example = "0001" +
33
+ "1202" +
34
+ "0003" +
35
+ "2022"
36
+ NSudoku::Checker.new(example).repeat_in.should eql([5, 7])
37
+ end
38
+
39
+ it "the array of sudoku is correct" do
40
+ example = "0001" +
41
+ "1002" +
42
+ "0003" +
43
+ "2010"
44
+ NSudoku::Checker.new(example).repeat_in.should be_nil
45
+ end
46
+
47
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nsudoku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.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-06-21 00:00:00.000000000 Z
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -41,9 +41,11 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - lib/nsudoku.rb
44
+ - lib/nsudoku/checker.rb
44
45
  - lib/nsudoku/version.rb
45
46
  - lib/sudoku.rb~
46
47
  - nsudoku.gemspec
48
+ - spec/checker_spec.rb
47
49
  - spec/nsudoku_spec.rb
48
50
  homepage: http://github.com/raglub/nsudoku
49
51
  licenses: []
@@ -70,4 +72,5 @@ signing_key:
70
72
  specification_version: 3
71
73
  summary: This gem solve puzzle game sudoku
72
74
  test_files:
75
+ - spec/checker_spec.rb
73
76
  - spec/nsudoku_spec.rb