hanoi-jane 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hanoi/jane.rb +1 -0
- data/lib/hanoi/jane/console.rb +55 -0
- data/lib/hanoi/jane/towers.rb +2 -42
- data/lib/hanoi/jane/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06e62c5a4e7ec717a18279751453e39469c3d42f
|
4
|
+
data.tar.gz: c71cbcd679a30a248911b9599669af6af6586341
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0f41539d1b867578d6f41ae78dbbe65cae61ae9cca5173cf395b4c25a1163f5489ddee21905392b0801263f74382b0e4e199521452e1fd11b46ed1381eec458
|
7
|
+
data.tar.gz: f2d42d4e7ff7a7a58042c81041ae9d7a6069cabfa90ceaca5ce3a1e715642211a04ee91e0d151aed6a0ba5404aa70ebaf4f8f2c3ac4ceece2e51cf6dbfc4e360
|
data/lib/hanoi/jane.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
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
|
data/lib/hanoi/jane/towers.rb
CHANGED
@@ -3,7 +3,7 @@ module Hanoi
|
|
3
3
|
class Towers
|
4
4
|
include Enumerable
|
5
5
|
|
6
|
-
attr_reader :total, :stacks
|
6
|
+
attr_reader :total, :stacks, :discs
|
7
7
|
|
8
8
|
def initialize discs
|
9
9
|
@discs = discs
|
@@ -27,16 +27,7 @@ module Hanoi
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def console
|
30
|
-
|
31
|
-
x = self.stacks.clone
|
32
|
-
y = x.map { |s| s.clone }
|
33
|
-
|
34
|
-
(Towers.rotate y.map { |s| (Towers.pad s, @discs).reverse }).each do |stack|
|
35
|
-
s += stack.map { |s| Towers.make_disc s, (Towers.scale @discs) }.join ' '
|
36
|
-
s += "\n"
|
37
|
-
end
|
38
|
-
|
39
|
-
s
|
30
|
+
(Console.new self).to_s
|
40
31
|
end
|
41
32
|
|
42
33
|
def inspect
|
@@ -112,37 +103,6 @@ module Hanoi
|
|
112
103
|
def Towers.rebase value, base, width
|
113
104
|
'%0*d' % [width, value.to_s(base)]
|
114
105
|
end
|
115
|
-
|
116
|
-
def Towers.pad array, length
|
117
|
-
until array.length == length
|
118
|
-
array.push nil
|
119
|
-
end
|
120
|
-
|
121
|
-
array.reverse
|
122
|
-
end
|
123
|
-
|
124
|
-
def Towers.make_disc width, space, char = 'o'
|
125
|
-
unless width
|
126
|
-
return ' ' * space
|
127
|
-
end
|
128
|
-
|
129
|
-
count = Towers.scale width
|
130
|
-
padding = (space - count) / 2
|
131
|
-
|
132
|
-
'%s%s%s' % [
|
133
|
-
' ' * padding,
|
134
|
-
char * count,
|
135
|
-
' ' * padding
|
136
|
-
]
|
137
|
-
end
|
138
|
-
|
139
|
-
def Towers.scale size
|
140
|
-
(size * 2) + 1
|
141
|
-
end
|
142
|
-
|
143
|
-
def Towers.rotate stacks
|
144
|
-
stacks.map { |s| s.clone }.transpose.reverse
|
145
|
-
end
|
146
106
|
end
|
147
107
|
end
|
148
108
|
end
|
data/lib/hanoi/jane/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanoi-jane
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pikesley
|
@@ -147,6 +147,7 @@ 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
|
150
151
|
- lib/hanoi/jane/constrained_towers.rb
|
151
152
|
- lib/hanoi/jane/matrix.rb
|
152
153
|
- lib/hanoi/jane/towers.rb
|