hanoi-jane 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06e62c5a4e7ec717a18279751453e39469c3d42f
4
- data.tar.gz: c71cbcd679a30a248911b9599669af6af6586341
3
+ metadata.gz: c83c6e60d1e0f4428dd46e92914cc1d5d0f5d30a
4
+ data.tar.gz: f3d23854bebd0539e22a78252c7e1837291b6d7f
5
5
  SHA512:
6
- metadata.gz: a0f41539d1b867578d6f41ae78dbbe65cae61ae9cca5173cf395b4c25a1163f5489ddee21905392b0801263f74382b0e4e199521452e1fd11b46ed1381eec458
7
- data.tar.gz: f2d42d4e7ff7a7a58042c81041ae9d7a6069cabfa90ceaca5ce3a1e715642211a04ee91e0d151aed6a0ba5404aa70ebaf4f8f2c3ac4ceece2e51cf6dbfc4e360
6
+ metadata.gz: 00f5f0260b30c83b267b424e5d7ee3509ae4399cfe990aa61c3ae10c3586138eae73db046208296619e20b08927673f3093aaadea712283d79f74f8b893924f9
7
+ data.tar.gz: 26ec239964715266ddc6f320b0d1d8150e52b3e9813205612a5f2285026c21cdc6da60d3187bf23ac79cc0492ba1ad960616b699f3f400e998e6414d83e64eae
data/lib/hanoi/jane.rb CHANGED
@@ -2,10 +2,12 @@ require 'thor'
2
2
  require 'httparty'
3
3
 
4
4
  require 'hanoi/jane/version'
5
+
5
6
  require 'hanoi/jane/towers'
6
7
  require 'hanoi/jane/constrained_towers'
7
- require 'hanoi/jane/matrix'
8
- require 'hanoi/jane/console'
8
+
9
+ require 'hanoi/jane/formatters/matrix'
10
+ require 'hanoi/jane/formatters/console'
9
11
 
10
12
  module Hanoi
11
13
  module Jane
@@ -0,0 +1,53 @@
1
+ module Hanoi
2
+ module Jane
3
+ module Formatters
4
+ class Console
5
+ def initialize towers
6
+ @discs = towers.discs
7
+ @stacks = towers.stacks.clone.map { |s| s.clone }
8
+ end
9
+
10
+ def to_s
11
+ s = ''
12
+ (Console.rotate @stacks.map { |s| (Console.pad s, @discs).reverse }).each do |stack|
13
+ s += stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join ' '
14
+ s += "\n"
15
+ end
16
+
17
+ s
18
+ end
19
+
20
+ def Console.pad array, length
21
+ until array.length == length
22
+ array.push nil
23
+ end
24
+
25
+ array.reverse
26
+ end
27
+
28
+ def Console.make_disc width, space, char = 'o'
29
+ unless width
30
+ return ' ' * space
31
+ end
32
+
33
+ count = Console.scale width
34
+ padding = (space - count) / 2
35
+
36
+ '%s%s%s' % [
37
+ ' ' * padding,
38
+ char * count,
39
+ ' ' * padding
40
+ ]
41
+ end
42
+
43
+ def Console.scale size
44
+ (size * 2) + 1
45
+ end
46
+
47
+ def Console.rotate stacks
48
+ stacks.map { |s| s.clone }.transpose.reverse
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,82 @@
1
+ module Hanoi
2
+ module Jane
3
+ module Formatters
4
+ class Matrix < Array
5
+ def initialize towers
6
+ @stacks = towers.stacks
7
+ @digits = towers.rebased
8
+
9
+ 7.times do
10
+ self.push [0] * 45
11
+ end
12
+
13
+ offset = 0
14
+ @stacks.each do |stack|
15
+ total = 0
16
+ stack.each do |disc|
17
+ shim = ((5 - (disc + 1)) / 2).round
18
+ (disc + 1).times do |i|
19
+ self[6 - total][i + offset + shim] = 1
20
+ end
21
+ total += 1
22
+ end
23
+ offset += 8
24
+ end
25
+
26
+ @bit_offset = 24
27
+ @bit_side = :right
28
+ @digits.chars.each do |bit|
29
+ digit bit
30
+
31
+ if @bit_side == :right
32
+ @bit_side = :left
33
+ @bit_offset += 8
34
+ else
35
+ @bit_side = :right
36
+ end
37
+ end
38
+ end
39
+
40
+ def digit value
41
+ digits = {
42
+ 0 => [
43
+ [1, 1, 1],
44
+ [1, 0, 1],
45
+ [1, 1, 1]
46
+ ],
47
+
48
+ 1 => [
49
+ [0, 1, 0],
50
+ [0, 1, 0],
51
+ [0, 1, 0]
52
+ ],
53
+
54
+ 2 => [
55
+ [1, 1, 0],
56
+ [0, 1, 0],
57
+ [0, 1, 1]
58
+ ]
59
+ }
60
+
61
+ @column = @bit_offset
62
+ @row = 0
63
+ if @bit_side == :right
64
+ @column += 2
65
+ @row = 4
66
+ end
67
+
68
+ insert digits[value.to_i]
69
+
70
+ end
71
+
72
+ def insert grid
73
+ 3.times do |i|
74
+ 3.times do |j|
75
+ self[@row + i][@column + j] = grid[i][j]
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -23,11 +23,11 @@ module Hanoi
23
23
  end
24
24
 
25
25
  def matrix
26
- Matrix.new self
26
+ Formatters::Matrix.new self
27
27
  end
28
28
 
29
29
  def console
30
- (Console.new self).to_s
30
+ (Formatters::Console.new self).to_s
31
31
  end
32
32
 
33
33
  def inspect
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanoi-jane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-23 00:00:00.000000000 Z
11
+ date: 2018-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -147,9 +147,9 @@ files:
147
147
  - hanoi-jane.gemspec
148
148
  - lib/hanoi/jane.rb
149
149
  - lib/hanoi/jane/cli.rb
150
- - lib/hanoi/jane/console.rb
151
150
  - lib/hanoi/jane/constrained_towers.rb
152
- - lib/hanoi/jane/matrix.rb
151
+ - lib/hanoi/jane/formatters/console.rb
152
+ - lib/hanoi/jane/formatters/matrix.rb
153
153
  - lib/hanoi/jane/towers.rb
154
154
  - lib/hanoi/jane/version.rb
155
155
  homepage: http://sam.pikesley.org/projects/hanoi-jane/
@@ -1,55 +0,0 @@
1
- module Hanoi
2
- module Jane
3
- class Console
4
- attr_reader :content
5
-
6
- def initialize towers
7
- @stacks = towers.stacks
8
- @discs = towers.discs
9
- @content = ''
10
- x = @stacks.clone
11
- y = x.map { |s| s.clone }
12
-
13
- (Console.rotate y.map { |s| (Console.pad s, @discs).reverse }).each do |stack|
14
- @content += stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join ' '
15
- @content += "\n"
16
- end
17
- end
18
-
19
- def to_s
20
- @content
21
- end
22
-
23
- def Console.pad array, length
24
- until array.length == length
25
- array.push nil
26
- end
27
-
28
- array.reverse
29
- end
30
-
31
- def Console.make_disc width, space, char = 'o'
32
- unless width
33
- return ' ' * space
34
- end
35
-
36
- count = Console.scale width
37
- padding = (space - count) / 2
38
-
39
- '%s%s%s' % [
40
- ' ' * padding,
41
- char * count,
42
- ' ' * padding
43
- ]
44
- end
45
-
46
- def Console.scale size
47
- (size * 2) + 1
48
- end
49
-
50
- def Console.rotate stacks
51
- stacks.map { |s| s.clone }.transpose.reverse
52
- end
53
- end
54
- end
55
- end
@@ -1,80 +0,0 @@
1
- module Hanoi
2
- module Jane
3
- class Matrix < Array
4
- def initialize towers
5
- @stacks = towers.stacks
6
- @digits = towers.rebased
7
-
8
- 7.times do
9
- self.push [0] * 45
10
- end
11
-
12
- offset = 0
13
- @stacks.each do |stack|
14
- total = 0
15
- stack.each do |disc|
16
- shim = ((5 - (disc + 1)) / 2).round
17
- (disc + 1).times do |i|
18
- self[6 - total][i + offset + shim] = 1
19
- end
20
- total += 1
21
- end
22
- offset += 8
23
- end
24
-
25
- @bit_offset = 24
26
- @bit_side = :right
27
- @digits.chars.each do |bit|
28
- digit bit
29
-
30
- if @bit_side == :right
31
- @bit_side = :left
32
- @bit_offset += 8
33
- else
34
- @bit_side = :right
35
- end
36
- end
37
- end
38
-
39
- def digit value
40
- digits = {
41
- 0 => [
42
- [1, 1, 1],
43
- [1, 0, 1],
44
- [1, 1, 1]
45
- ],
46
-
47
- 1 => [
48
- [0, 1, 0],
49
- [0, 1, 0],
50
- [0, 1, 0]
51
- ],
52
-
53
- 2 => [
54
- [1, 1, 0],
55
- [0, 1, 0],
56
- [0, 1, 1]
57
- ]
58
- }
59
-
60
- @column = @bit_offset
61
- @row = 0
62
- if @bit_side == :right
63
- @column += 2
64
- @row = 4
65
- end
66
-
67
- insert digits[value.to_i]
68
-
69
- end
70
-
71
- def insert grid
72
- 3.times do |i|
73
- 3.times do |j|
74
- self[@row + i][@column + j] = grid[i][j]
75
- end
76
- end
77
- end
78
- end
79
- end
80
- end