sudoku_builder 1.2.0 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51dabd8ea0238ed578d3c3a67e1e15b3fdbe1dc2
4
- data.tar.gz: 955fbb1af033d8b111829802eb0ccfc2fb6fc7b2
3
+ metadata.gz: c631c3c2ce23002dc161396f1c2774911fb88f56
4
+ data.tar.gz: 361a3e813a48ab370543216c29b717e0ea3c8aab
5
5
  SHA512:
6
- metadata.gz: f818785a7502d21892c6b9d17e1ba017210fbd6d1d155216637fa7484608ce111d33db8dcddee01beb47ba2cda69662dd88f0dfdf00484622d54f52f8d4f8279
7
- data.tar.gz: 6ab1c9693c53690c63b2008b280bb1915cafb0253f826e2d48fdd8b01b0be0d35a24fb05a7b4185f53ae10f872632008713173f03964b8e75903b2f3427a8e39
6
+ metadata.gz: 318dc0b0b6f15d2f22d8764e9272ef7976c7d736b462246bf2209a26ddf2c06553bcf6c5cc90e1cffa4a7208da67cdcd6fc27d24000af3e2f0d0373d270aa1c0
7
+ data.tar.gz: 8a869b16081a7df9efe823dc84c5062e7def462712377c31326be085365a3e67963971f024a4d69687cb5c4fd5959d3d9adddd2787b7a839bc4c9626c173b2ce
data/README.md CHANGED
@@ -1,35 +1,77 @@
1
1
  # SudokuBuilder
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sudoku_builder`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
2
  ```ruby
12
- gem 'sudoku_builder'
3
+ $ gem install sudoku_builder
4
+ $ irb
5
+
6
+ > puzzle = SudokuBuilder.create
7
+ > puzzle.pretty_print
8
+ +-----------------------------+
9
+ | 7 9 2 | 5 1 4 | 8 6 3 |
10
+ | 3 4 1 | 7 8 6 | 2 5 9 |
11
+ | 5 6 8 | 3 9 2 | 7 1 4 |
12
+ +-----------------------------+
13
+ | 4 5 7 | 6 3 8 | 9 2 1 |
14
+ | 6 1 9 | 2 5 7 | 4 3 8 |
15
+ | 2 8 3 | 1 4 9 | 6 7 5 |
16
+ +-----------------------------+
17
+ | 9 7 5 | 4 2 1 | 3 8 6 |
18
+ | 8 3 6 | 9 7 5 | 1 4 2 |
19
+ | 1 2 4 | 8 6 3 | 5 9 7 |
20
+ +-----------------------------+
21
+
22
+ > puzzle.hard.pretty_print
23
+ +-----------------------------+
24
+ | _ 2 6 | _ _ _ | _ 8 _ |
25
+ | _ _ _ | _ 2 _ | 1 3 7 |
26
+ | _ _ _ | 9 _ _ | 4 _ _ |
27
+ +-----------------------------+
28
+ | 1 5 _ | _ _ 7 | _ _ _ |
29
+ | 9 4 2 | _ _ _ | _ _ _ |
30
+ | 6 _ _ | 1 4 _ | _ _ 2 |
31
+ +-----------------------------+
32
+ | _ _ _ | 4 7 2 | 6 1 9 |
33
+ | _ 6 4 | _ _ _ | _ 5 3 |
34
+ | 2 _ _ | _ _ 6 | 7 _ 8 |
35
+ +-----------------------------+
36
+
37
+ > puzzle = SudokuBuilder.create
38
+ > puzzle.easy.pretty_print
39
+ +-----------------------------+
40
+ | 8 6 _ | 9 2 5 | _ _ 4 |
41
+ | _ 1 _ | _ _ _ | 5 2 7 |
42
+ | 2 4 5 | 7 _ 3 | 9 8 _ |
43
+ +-----------------------------+
44
+ | 6 _ 4 | _ _ 1 | 7 5 9 |
45
+ | _ _ 1 | 3 7 _ | _ 6 8 |
46
+ | _ 7 8 | 5 6 _ | 2 1 3 |
47
+ +-----------------------------+
48
+ | _ 8 3 | 4 _ 2 | 6 9 1 |
49
+ | _ _ _ | _ 3 7 | 8 _ _ |
50
+ | 4 5 6 | 1 _ _ | 3 7 2 |
51
+ +-----------------------------+
52
+
53
+ > to_solve = SudokuBuilder.create.hard.to_a
54
+ > solved = SudokuBuilder.solve(to_solve).pretty_print
55
+ +-----------------------------+
56
+ | 3 2 1 | 9 8 4 | 6 5 7 |
57
+ | 7 8 4 | 6 3 5 | 1 2 9 |
58
+ | 9 5 6 | 7 1 2 | 8 4 3 |
59
+ +-----------------------------+
60
+ | 8 1 7 | 4 6 9 | 5 3 2 |
61
+ | 6 3 2 | 1 5 7 | 9 8 4 |
62
+ | 4 9 5 | 8 2 3 | 7 6 1 |
63
+ +-----------------------------+
64
+ | 5 7 9 | 2 4 8 | 3 1 6 |
65
+ | 2 6 3 | 5 7 1 | 4 9 8 |
66
+ | 1 4 8 | 3 9 6 | 2 7 5 |
67
+ +-----------------------------+
68
+
69
+ > puzzle = SudokuBuilder.create
70
+ > print puzzle.to_flat_a
71
+ # an array from 0..80 with each value of the sudoku
72
+ [1, 7, 8, 6, 5, 3, 4, 2, 9, 4, 5, 6, 1, 2, 9, 3, 8, 7, 9, 3, 2, 8, 7, 4, 6, 5, 1, 2, 6, 5, 4, 9, 8, 7, 1, 3, 3, 1, 7, 5, 6, 2, 9, 4, 8, 8, 4, 9, 3, 1, 7, 2, 6, 5, 5, 9, 1, 7, 4, 6, 8, 3, 2, 6, 2, 3, 9, 8, 1, 5, 7, 4, 7, 8, 4, 2, 3, 5, 1, 9, 6]
13
73
  ```
14
74
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install sudoku_builder
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
75
  ## Contributing
34
76
 
35
77
  1. Fork it ( https://github.com/[my-github-username]/sudoku_builder/fork )
@@ -5,12 +5,7 @@ class SudokuBuilder
5
5
  loop do
6
6
  return @sud if @loc == [9,0,0]
7
7
 
8
- poss = []
9
- (1..9).each do |i|
10
- if check?(i)
11
- poss << i
12
- end
13
- end
8
+ poss = get_possibilities
14
9
 
15
10
  if !poss.empty?
16
11
  write(poss.sample)
@@ -24,11 +19,11 @@ class SudokuBuilder
24
19
  end
25
20
 
26
21
  def poke(num)
27
- sud = to_flat_a
28
22
  num.times do
29
- sud[rand(0..80)] = nil
23
+ @loc = [rand(0..8),rand(0..2),rand(0..2)]
24
+ only_write(nil)
30
25
  end
31
- sud
26
+ return self
32
27
  end
33
28
 
34
29
  def hard
@@ -1,13 +1,5 @@
1
1
  class SudokuBuilder
2
2
 
3
- def pretty
4
- @sud.each_with_index do |row|
5
- print row.join(' ')
6
- puts ' '
7
- end
8
- puts ' '
9
- end
10
-
11
3
  def to_a
12
4
  @sud
13
5
  end
@@ -16,4 +8,37 @@ class SudokuBuilder
16
8
  @sud.flatten
17
9
  end
18
10
 
11
+ def to_rows
12
+ rows = []
13
+ @sud.each do |row|
14
+ rows << row.flatten
15
+ end
16
+ return rows
17
+ end
18
+
19
+ def pretty_print
20
+ b = []
21
+ to_flat_a.each do |i|
22
+ if i
23
+ b << i
24
+ else
25
+ b << '_'
26
+ end
27
+ end
28
+
29
+ puts "+-----------------------------+"
30
+ puts "| #{b[0]} #{b[1]} #{b[2]} | #{b[3]} #{b[4]} #{b[5]} | #{b[6]} #{b[7]} #{b[8]} |"
31
+ puts "| #{b[9]} #{b[10]} #{b[11]} | #{b[12]} #{b[13]} #{b[14]} | #{b[15]} #{b[16]} #{b[17]} |"
32
+ puts "| #{b[18]} #{b[19]} #{b[20]} | #{b[21]} #{b[22]} #{b[23]} | #{b[24]} #{b[25]} #{b[26]} |"
33
+ puts "+-----------------------------+"
34
+ puts "| #{b[27]} #{b[28]} #{b[29]} | #{b[30]} #{b[31]} #{b[32]} | #{b[33]} #{b[34]} #{b[35]} |"
35
+ puts "| #{b[36]} #{b[37]} #{b[38]} | #{b[39]} #{b[40]} #{b[41]} | #{b[42]} #{b[43]} #{b[44]} |"
36
+ puts "| #{b[45]} #{b[46]} #{b[47]} | #{b[48]} #{b[49]} #{b[50]} | #{b[51]} #{b[52]} #{b[53]} |"
37
+ puts "+-----------------------------+"
38
+ puts "| #{b[54]} #{b[55]} #{b[56]} | #{b[57]} #{b[58]} #{b[59]} | #{b[60]} #{b[61]} #{b[62]} |"
39
+ puts "| #{b[63]} #{b[64]} #{b[65]} | #{b[66]} #{b[67]} #{b[68]} | #{b[69]} #{b[70]} #{b[71]} |"
40
+ puts "| #{b[72]} #{b[73]} #{b[74]} | #{b[75]} #{b[76]} #{b[77]} | #{b[78]} #{b[79]} #{b[80]} |"
41
+ puts "+-----------------------------+"
42
+ end
43
+
19
44
  end
@@ -16,13 +16,7 @@ class SudokuBuilder
16
16
 
17
17
  else
18
18
 
19
- poss = []
20
- (1..9).each do |i|
21
- if check?(i)
22
- poss << i
23
- end
24
- end
25
-
19
+ poss = get_possibilities
26
20
  if !poss.empty?
27
21
  write(poss.sample)
28
22
  increment ; up = true
@@ -37,4 +31,26 @@ class SudokuBuilder
37
31
 
38
32
  end
39
33
 
34
+ def parse_for_solve(puzzle)
35
+ if puzzle.class == SudokuBuilder
36
+ flattened = puzzle.to_a.flatten
37
+ else
38
+ flattened = puzzle.flatten
39
+ end
40
+
41
+ if flattened.count == 81
42
+
43
+ @loc = [0,0,0] ; @sud = blank
44
+ flattened.each do |val|
45
+ if val and val <= 9 and val >= 1
46
+ write(val)
47
+ end
48
+ increment
49
+ end
50
+
51
+ else
52
+ raise PuzzleFormatError, 'Must have exactly 81 spots in the array.'
53
+ end
54
+ end
55
+
40
56
  end
@@ -59,9 +59,8 @@ class SudokuBuilder
59
59
 
60
60
  def valid?
61
61
  @loc = [0,0,0]
62
- validity = []
63
62
  loop do
64
- if check?(value)
63
+ unless check?(value)
65
64
  return false
66
65
  end
67
66
  increment
@@ -71,26 +70,16 @@ class SudokuBuilder
71
70
  end
72
71
 
73
72
  def check?(val)
74
- !(grid.include?(val) || column.include?(val) ||
75
- row.include?(val) || used.include?(val) )
76
- end
77
-
78
- def parse_for_solve(puzzle)
79
- flattened = puzzle.flatten
80
-
81
- if flattened.count == 81
82
-
83
- @loc = [0,0,0] ; @sud = blank
84
- flattened.each do |val|
85
- if val and val <= 9 and val >= 1
86
- write(val)
87
- end
88
- increment
89
- end
90
-
73
+ if val and !(val < 1 or val > 9)
74
+ val = [val] unless val.class == Array
75
+ !((grid - val).include?(val) || (column - val).include?(val) || (row - val).include?(val))
91
76
  else
92
- raise PuzzleFormatError, 'Must have exactly 81 spots in the array.'
77
+ false
93
78
  end
94
79
  end
95
80
 
81
+ def get_possibilities
82
+ [1,2,3,4,5,6,7,8,9] - used - (grid + column + row).uniq
83
+ end
84
+
96
85
  end
@@ -9,6 +9,10 @@ class SudokuBuilder
9
9
  @used[@loc[0]][@loc[1]][@loc[2]] << val unless used.include?(val)
10
10
  end
11
11
 
12
+ def only_write(val)
13
+ @sud[@loc[0]][@loc[1]][@loc[2]] = val
14
+ end
15
+
12
16
  def value
13
17
  @sud[@loc[0]][@loc[1]][@loc[2]]
14
18
  end
@@ -1,3 +1,3 @@
1
1
  class SudokuBuilder
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sudoku_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Walker
@@ -62,6 +62,7 @@ files:
62
62
  - lib/sudoku_builder/values.rb
63
63
  - lib/sudoku_builder/version.rb
64
64
  - sudoku_builder-1.1.0.gem
65
+ - sudoku_builder-1.2.0.gem
65
66
  - sudoku_builder.gemspec
66
67
  homepage: https://github.com/ColDog/sudoku-gem
67
68
  licenses: []