hanoi-jane 0.3.0 → 0.3.1

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: c8a1d92da6a843d6068462542eb6728d80995eed
4
- data.tar.gz: dbac7d2f1d65726fc56aad01a1653f8818915e22
3
+ metadata.gz: 6f2267c39b116fc8119cb14359ee81d972c1be73
4
+ data.tar.gz: c1ca93a586a49d31f41f7581dc5c7e63312da921
5
5
  SHA512:
6
- metadata.gz: 49ed23e2f5f6abe0f6585cee6e023098ddbff0cbf70ad90c09fe499f2483264a3cce98d2046dc8324a6e219fdbe7b71931f6800856d1877f2864585a60f46e09
7
- data.tar.gz: e2b44701f251b4c172c8f01880878c8f61a789b75e3d1526e1f006846a05c0dcafd4bd22f99af214f1f3de8e8d5e0768f31b24b330e1d81b96d5ce2c09463561
6
+ metadata.gz: 4d6a51f4f2cb063c3d2f8d9277821f91106561e9e5bf289970581c9c19279a9f3bca855ba5a996112242afc98c751c25e233bdfa90ba469fe391e8bd3df4e68b
7
+ data.tar.gz: cabe1718e0f925edfaca697d4bfc552510762ede7058be9bdb1062f5710ee37471cf5258100f1297349eec835d319b098b1b7604256226fd3905e503ce6d0e8e
@@ -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/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
 
@@ -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
- at = AnimatedTowers.new do |a|
43
- a.towers = options[:constrained] ? ConstrainedTowers : Towers
44
- a.discs = options[:discs]
45
- a.height = options[:discs] + options[:height]
46
- end
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
- at.each do |frame|
49
- system('clear')
49
+ at.each do |frame|
50
+ system('clear')
50
51
 
51
- c = Formatters::Console.new do |c|
52
- c.stacks = frame.stacks
53
- c.fancy = options[:fancy]
54
- end
52
+ c = Formatters::Console.new do |c|
53
+ c.stacks = frame.stacks
54
+ c.fancy = options[:fancy]
55
+ end
55
56
 
56
- puts frame.value
57
- puts c
57
+ puts frame.value
58
+ puts c
58
59
 
59
- interval = options[:interval]
60
- if frame.type == :tween
61
- interval = interval * 0.1
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
- puts '%d moves to solve for %d discs' % [at.towers.total, at.discs]
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
@@ -104,14 +104,6 @@ module Hanoi
104
104
  ((5 - (size + 1)) / 2).round
105
105
  end
106
106
  end
107
-
108
- class MatrixException < Exception
109
- attr_reader :text
110
-
111
- def initialize text
112
- @text = text
113
- end
114
- end
115
107
  end
116
108
  end
117
109
  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)' % [@height, @discs]
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,8 +1,8 @@
1
1
  module Hanoi
2
2
  module Jane
3
- class ConstrainedTowers < Towers
3
+ class ConstrainedTowers < RegularTowers
4
4
  extend ConstrainedStackFinder
5
-
5
+
6
6
  def initialize discs = 3
7
7
  super
8
8
  @base = 3
@@ -1,6 +1,6 @@
1
1
  module Hanoi
2
2
  module Jane
3
- class Towers
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 = Towers.starter_stacks @discs
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 = Towers.starter_stacks @discs
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 = Towers.diff before, after
28
+ @disc = self.class.diff before, after
29
29
 
30
- @from = Towers.find_disc @stacks, @disc
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
- Towers.rebase @total, @base, @discs
76
+ self.class.rebase @total, @base, @discs
77
77
  end
78
78
 
79
- private
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 Towers.diff this, that
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 Towers.rebase value, base, width
91
+ def RegularTowers.rebase value, base, width
94
92
  '%0*d' % [width, value.to_s(base)]
95
93
  end
96
94
 
97
- def Towers.find_disc stacks, disc
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
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
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.0
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