hanoi-jane 0.3.0 → 0.3.1
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 +4 -4
- data/lib/hanoi/jane.rb +2 -1
- data/lib/hanoi/jane/cli.rb +33 -27
- data/lib/hanoi/jane/exceptions.rb +27 -0
- data/lib/hanoi/jane/formatters/matrix.rb +0 -8
- data/lib/hanoi/jane/towers/animated_towers.rb +1 -9
- data/lib/hanoi/jane/towers/constrained_towers.rb +2 -2
- data/lib/hanoi/jane/towers/{towers.rb → regular_towers.rb} +10 -20
- data/lib/hanoi/jane/version.rb +1 -1
- metadata +3 -3
- data/TODO.md +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f2267c39b116fc8119cb14359ee81d972c1be73
|
4
|
+
data.tar.gz: c1ca93a586a49d31f41f7581dc5c7e63312da921
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d6a51f4f2cb063c3d2f8d9277821f91106561e9e5bf289970581c9c19279a9f3bca855ba5a996112242afc98c751c25e233bdfa90ba469fe391e8bd3df4e68b
|
7
|
+
data.tar.gz: cabe1718e0f925edfaca697d4bfc552510762ede7058be9bdb1062f5710ee37471cf5258100f1297349eec835d319b098b1b7604256226fd3905e503ce6d0e8e
|
data/lib/hanoi/jane.rb
CHANGED
@@ -8,9 +8,10 @@ require 'httparty'
|
|
8
8
|
require 'hanoi/jane/version'
|
9
9
|
|
10
10
|
require 'hanoi/jane/config'
|
11
|
+
require 'hanoi/jane/exceptions'
|
11
12
|
|
12
13
|
require 'hanoi/jane/towers/stack_finders'
|
13
|
-
require 'hanoi/jane/towers/
|
14
|
+
require 'hanoi/jane/towers/regular_towers'
|
14
15
|
require 'hanoi/jane/towers/constrained_towers'
|
15
16
|
require 'hanoi/jane/towers/animated_towers'
|
16
17
|
|
data/lib/hanoi/jane/cli.rb
CHANGED
@@ -10,9 +10,9 @@ module Hanoi
|
|
10
10
|
map %w(-v --version) => :version
|
11
11
|
|
12
12
|
desc 'phat', "Solve the towers against the pHAT's webserver"
|
13
|
-
option :phat, type: :string, required: true
|
14
|
-
option :constrained, type: :boolean
|
15
|
-
option :interval, type: :numeric, default: 0.1
|
13
|
+
option :phat, type: :string, required: true, desc: 'Address of the pHAT you want to hit'
|
14
|
+
option :constrained, type: :boolean, desc: 'Solve the constrained variant'
|
15
|
+
option :interval, type: :numeric, default: 0.1, desc: 'Time between frames (ish)'
|
16
16
|
|
17
17
|
def phat
|
18
18
|
at = AnimatedTowers.new do |a|
|
@@ -32,38 +32,44 @@ module Hanoi
|
|
32
32
|
end
|
33
33
|
|
34
34
|
desc 'console', 'Solve the towers on the console'
|
35
|
-
option :discs, type: :numeric, default: 3
|
36
|
-
option :constrained, type: :boolean, default: true
|
37
|
-
option :interval, type: :numeric, default: 0.5
|
38
|
-
option :height, type: :numeric, default: 2
|
39
|
-
option :fancy, type: :boolean, default: false
|
35
|
+
option :discs, type: :numeric, default: 3, desc: 'Number of discs'
|
36
|
+
option :constrained, type: :boolean, default: true, desc: 'Solve the constrained variant'
|
37
|
+
option :interval, type: :numeric, default: 0.5, desc: 'Time between frames (ish)'
|
38
|
+
option :height, type: :numeric, default: 2, desc: 'Additional height above towers'
|
39
|
+
option :fancy, type: :boolean, default: false, desc: 'Draw the towers using emojis'
|
40
40
|
|
41
41
|
def console
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
begin
|
43
|
+
at = AnimatedTowers.new do |a|
|
44
|
+
a.towers = options[:constrained] ? ConstrainedTowers : RegularTowers
|
45
|
+
a.discs = options[:discs]
|
46
|
+
a.height = options[:discs] + options[:height]
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
49
|
+
at.each do |frame|
|
50
|
+
system('clear')
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
c = Formatters::Console.new do |c|
|
53
|
+
c.stacks = frame.stacks
|
54
|
+
c.fancy = options[:fancy]
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
puts frame.value
|
58
|
+
puts c
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
interval = options[:interval]
|
61
|
+
if frame.type == :tween
|
62
|
+
interval = interval * 0.1
|
63
|
+
end
|
64
|
+
sleep interval
|
62
65
|
end
|
63
|
-
sleep interval
|
64
|
-
end
|
65
66
|
|
66
|
-
|
67
|
+
puts '%d moves to solve for %d discs' % [at.towers.total, at.discs]
|
68
|
+
|
69
|
+
rescue HanoiException => he
|
70
|
+
puts he.text
|
71
|
+
system('exit')
|
72
|
+
end
|
67
73
|
end
|
68
74
|
end
|
69
75
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Hanoi
|
2
|
+
module Jane
|
3
|
+
class HanoiException < Exception
|
4
|
+
attr_reader :text
|
5
|
+
|
6
|
+
def initialize text
|
7
|
+
@text = text
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class SearchException < HanoiException
|
12
|
+
attr_reader :text
|
13
|
+
|
14
|
+
def initialize text
|
15
|
+
@text = text
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class MatrixException < HanoiException
|
20
|
+
attr_reader :text
|
21
|
+
|
22
|
+
def initialize text
|
23
|
+
@text = text
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -9,7 +9,7 @@ module Hanoi
|
|
9
9
|
yield self if block_given?
|
10
10
|
|
11
11
|
if @discs > @height
|
12
|
-
raise HanoiException.new 'number_of_discs (%d) > height (%d)' % [@
|
12
|
+
raise HanoiException.new 'number_of_discs (%d) > height (%d)' % [@discs, @height]
|
13
13
|
end
|
14
14
|
|
15
15
|
@towers = @towers.new @discs
|
@@ -48,13 +48,5 @@ module Hanoi
|
|
48
48
|
@type = type
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
52
|
-
class HanoiException < Exception
|
53
|
-
attr_reader :text
|
54
|
-
|
55
|
-
def initialize text
|
56
|
-
@text = text
|
57
|
-
end
|
58
|
-
end
|
59
51
|
end
|
60
52
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Hanoi
|
2
2
|
module Jane
|
3
|
-
class
|
3
|
+
class RegularTowers
|
4
4
|
extend StackFinder
|
5
5
|
|
6
6
|
attr_reader :stacks, :total, :base, :disc, :from, :to
|
@@ -10,14 +10,14 @@ module Hanoi
|
|
10
10
|
@discs = discs
|
11
11
|
@total = 0
|
12
12
|
@base = 2
|
13
|
-
@stacks =
|
13
|
+
@stacks = self.class.starter_stacks @discs
|
14
14
|
|
15
15
|
yield self if block_given?
|
16
16
|
end
|
17
17
|
|
18
18
|
def discs= discs
|
19
19
|
@discs = discs
|
20
|
-
@stacks =
|
20
|
+
@stacks = self.class.starter_stacks @discs
|
21
21
|
end
|
22
22
|
|
23
23
|
def move
|
@@ -25,9 +25,9 @@ module Hanoi
|
|
25
25
|
@total += 1
|
26
26
|
after = binary
|
27
27
|
|
28
|
-
@disc =
|
28
|
+
@disc = self.class.diff before, after
|
29
29
|
|
30
|
-
@from =
|
30
|
+
@from = self.class.find_disc @stacks, @disc
|
31
31
|
@to = self.class.find_stack stacks: @stacks, from: @from, disc: @disc, total: @total
|
32
32
|
@stacks[@to].push @stacks[@from].pop
|
33
33
|
end
|
@@ -73,16 +73,14 @@ module Hanoi
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def rebased
|
76
|
-
|
76
|
+
self.class.rebase @total, @base, @discs
|
77
77
|
end
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
def Towers.starter_stacks discs
|
79
|
+
def RegularTowers.starter_stacks discs
|
82
80
|
[(0...discs).to_a.reverse, [], []]
|
83
81
|
end
|
84
82
|
|
85
|
-
def
|
83
|
+
def RegularTowers.diff this, that
|
86
84
|
this.chars.reverse.each_with_index do |bit, index|
|
87
85
|
if bit < that.chars.reverse[index]
|
88
86
|
return index
|
@@ -90,11 +88,11 @@ module Hanoi
|
|
90
88
|
end
|
91
89
|
end
|
92
90
|
|
93
|
-
def
|
91
|
+
def RegularTowers.rebase value, base, width
|
94
92
|
'%0*d' % [width, value.to_s(base)]
|
95
93
|
end
|
96
94
|
|
97
|
-
def
|
95
|
+
def RegularTowers.find_disc stacks, disc
|
98
96
|
stacks.each_with_index do |stack, index|
|
99
97
|
return index if stack.index disc
|
100
98
|
end
|
@@ -102,13 +100,5 @@ module Hanoi
|
|
102
100
|
raise SearchException.new '%s not found in stacks' % disc
|
103
101
|
end
|
104
102
|
end
|
105
|
-
|
106
|
-
class SearchException < Exception
|
107
|
-
attr_reader :text
|
108
|
-
|
109
|
-
def initialize text
|
110
|
-
@text = text
|
111
|
-
end
|
112
|
-
end
|
113
103
|
end
|
114
104
|
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pikesley
|
@@ -141,7 +141,6 @@ files:
|
|
141
141
|
- LICENSE.md
|
142
142
|
- README.md
|
143
143
|
- Rakefile
|
144
|
-
- TODO.md
|
145
144
|
- bin/console
|
146
145
|
- bin/hanoi
|
147
146
|
- bin/setup
|
@@ -154,12 +153,13 @@ files:
|
|
154
153
|
- lib/hanoi/jane/animation/padded_stacks.rb
|
155
154
|
- lib/hanoi/jane/cli.rb
|
156
155
|
- lib/hanoi/jane/config.rb
|
156
|
+
- lib/hanoi/jane/exceptions.rb
|
157
157
|
- lib/hanoi/jane/formatters/console.rb
|
158
158
|
- lib/hanoi/jane/formatters/matrix.rb
|
159
159
|
- lib/hanoi/jane/towers/animated_towers.rb
|
160
160
|
- lib/hanoi/jane/towers/constrained_towers.rb
|
161
|
+
- lib/hanoi/jane/towers/regular_towers.rb
|
161
162
|
- lib/hanoi/jane/towers/stack_finders.rb
|
162
|
-
- lib/hanoi/jane/towers/towers.rb
|
163
163
|
- lib/hanoi/jane/version.rb
|
164
164
|
homepage: http://sam.pikesley.org/projects/hanoi-jane/
|
165
165
|
licenses:
|
data/TODO.md
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Can I conditionally include a module at constructor time? To e.g. pull in the ConstrainedTowers-specific search methods
|