hanoi-jane 0.2.3 → 0.2.5

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: 6784220653ef2903d69fca3be4290dffb8b02c10
4
- data.tar.gz: 9da04a7d95acdd741ec597ecb7dfa49cac7dd263
3
+ metadata.gz: 8d7f114e32d1a1e8f3a9f59cf07c86c0d68c9a17
4
+ data.tar.gz: b78e6dbd9b7deffc063e60049e22888183742a7f
5
5
  SHA512:
6
- metadata.gz: 5ff0d22b64d36b613ed87957586fb24a1282443787b2ea632285f46382c02984b8e18c394a9fa8570bbc03548052528821cad8f076048f003e851c5ea0c8940e
7
- data.tar.gz: b4732d47a93152b5939490fffebddace62fd89703374f6b238465914abc581b8a05613b95710247638ff60ba5ab1efad3f930c2542ddf4be25bd58f81a5a7902
6
+ metadata.gz: 695210a096216a3c91bdf43e8ff86cc3130cf8657bf5a6ef66c6f1da2ce17ca0bc26cb9f1420ba02c3e5d1c21ee03610afa9f9140a0919529ae64959b42111d4
7
+ data.tar.gz: bc72d1599263f725be624fb56c4d7ee6e488d44658f6627a2620a40b1e1eb062b14ce0048ababc968439abbd329c0c9e5a0c7b298a49db5a3c7cee693e703ee7
@@ -5,6 +5,7 @@ require 'hanoi/jane/version'
5
5
 
6
6
  require 'hanoi/jane/towers'
7
7
  require 'hanoi/jane/constrained_towers'
8
+ require 'hanoi/jane/animation'
8
9
 
9
10
  require 'hanoi/jane/formatters/matrix'
10
11
  require 'hanoi/jane/formatters/console'
@@ -0,0 +1,79 @@
1
+ module Hanoi
2
+ module Jane
3
+ class Animation
4
+ include Enumerable
5
+
6
+ attr_reader :stacks
7
+
8
+ def initialize towers
9
+ @towers = towers
10
+ @height = towers.discs
11
+ @stacks = Animation.pad towers.old_stacks, @height
12
+ @disc = towers.disc
13
+ @source = towers.source
14
+ @destination = towers.sink
15
+ @fancy = towers.fancy
16
+
17
+ @lift = true
18
+ @drop = false
19
+ @done = false
20
+ end
21
+
22
+ def animate
23
+ @drop = true unless @lift
24
+ if @lift
25
+ stack = @stacks[@source]
26
+ @position = stack.index @disc
27
+ stack[@position] = nil
28
+
29
+ if @position + 1 >= @height
30
+ @lift = false
31
+ else
32
+ stack[@position + 1] = @disc
33
+ end
34
+
35
+ @stacks[@source] = stack
36
+ end
37
+
38
+ if @drop
39
+ stack = @stacks[@destination]
40
+ @position = -1 unless @position
41
+
42
+ stack[@position] = @disc
43
+ stack[(stack.index @disc) + 1] = nil
44
+
45
+ @position -= 1
46
+
47
+ @stacks[@destination] = stack.take @height
48
+
49
+ if stack[@position] || @position == 0
50
+ @drop = false
51
+ @done = true
52
+ end
53
+ end
54
+ end
55
+
56
+ def to_s
57
+ (Formatters::Console.new @height, @stacks, @fancy).to_s
58
+ end
59
+
60
+ def matrix
61
+ m = Formatters::Matrix.new @towers
62
+ m.stacks = @stacks
63
+
64
+ m
65
+ end
66
+
67
+ def each
68
+ until @done
69
+ animate
70
+ yield self
71
+ end
72
+ end
73
+
74
+ def Animation.pad stacks, height
75
+ stacks.map { |stack| stack + Array.new(height - stack.length) }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -13,6 +13,7 @@ module Hanoi
13
13
  option :phat, type: :string, required: true
14
14
  option :constrained, type: :boolean
15
15
  option :interval, type: :numeric, default: 0.3
16
+ option :animated, type: :boolean, default: false
16
17
 
17
18
  def phat
18
19
  towers = Towers.new 5
@@ -20,7 +21,16 @@ module Hanoi
20
21
  towers = ConstrainedTowers.new 5
21
22
  end
22
23
 
24
+ towers.animated = options[:animated]
25
+
23
26
  towers.each do |state|
27
+ if state.animation
28
+ state.animation.each do |frame|
29
+ Hanoi::Jane.hit_phat frame, options[:phat]
30
+ sleep options[:interval] * 0.1
31
+ end
32
+ end
33
+
24
34
  Hanoi::Jane.hit_phat towers, options[:phat]
25
35
  sleep options[:interval]
26
36
  end
@@ -30,6 +40,9 @@ module Hanoi
30
40
  option :discs, type: :numeric, default: 3, minimum: 1
31
41
  option :constrained, type: :boolean
32
42
  option :fancy, type: :boolean, default: false
43
+ option :animated, type: :boolean, default: false
44
+ option :delay, type: :numeric, default: 0.2
45
+
33
46
  def console
34
47
  if options[:discs] < 1
35
48
  puts "Solving for %d discs makes no sense" % options[:discs]
@@ -43,14 +56,26 @@ module Hanoi
43
56
  end
44
57
 
45
58
  towers.fancy = options[:fancy]
59
+ towers.animated = options[:animated]
46
60
 
47
61
  towers.each do |state|
62
+ if state.animation
63
+ state.animation.each do |frame|
64
+ system('clear')
65
+ s = options[:fancy] ? (Formatters::Console.fancify state.rebased) : state.rebased
66
+ puts s
67
+ puts
68
+ puts frame
69
+ sleep options[:delay] * 0.2
70
+ end
71
+ end
72
+
48
73
  system('clear')
49
74
  s = options[:fancy] ? (Formatters::Console.fancify state.rebased) : state.rebased
50
75
  puts s
51
76
  puts
52
77
  puts state.console
53
- sleep 0.2
78
+ sleep options[:delay]
54
79
  end
55
80
 
56
81
  puts '%d moves to solve for %d discs' % [towers.total, options[:discs]]
@@ -18,10 +18,10 @@ module Hanoi
18
18
  horiz_divider: '🔻'
19
19
  }
20
20
 
21
- def initialize towers
22
- @discs = towers.discs
23
- @stacks = towers.stacks.clone.map { |s| s.clone }
24
- @@chars = towers.fancy ? FANCY_CHARS : CHARS
21
+ def initialize discs, stacks, fancy = false
22
+ @discs = discs
23
+ @stacks = stacks.clone.map { |s| s.clone }
24
+ @@chars = fancy ? FANCY_CHARS : CHARS
25
25
  end
26
26
 
27
27
  def to_s
@@ -43,7 +43,7 @@ module Hanoi
43
43
  end
44
44
 
45
45
  def Console.pad array, length
46
- Array.new(length + 1 - array.length) + array.reverse
46
+ Array.new(length - array.length) + array.reverse
47
47
  end
48
48
 
49
49
  def Console.make_disc width, space
@@ -6,17 +6,28 @@ module Hanoi
6
6
  @stacks = towers.stacks
7
7
  @digits = towers.rebased
8
8
 
9
- 7.times do
10
- self.push [0] * 45
9
+ populate
10
+ end
11
+
12
+ def stacks= stacks
13
+ @stacks = stacks
14
+ populate
15
+ end
16
+
17
+ def populate
18
+ 7.times do |i|
19
+ self[i] = [0] * 45
11
20
  end
12
21
 
13
22
  offset = 0
14
23
  @stacks.each do |stack|
15
24
  total = 0
16
25
  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
26
+ if disc
27
+ shim = ((5 - (disc + 1)) / 2).round
28
+ (disc + 1).times do |i|
29
+ self[6 - total][i + offset + shim] = 1
30
+ end
20
31
  end
21
32
  total += 1
22
33
  end
@@ -3,8 +3,8 @@ module Hanoi
3
3
  class Towers
4
4
  include Enumerable
5
5
 
6
- attr_reader :total, :stacks, :discs
7
- attr_accessor :fancy
6
+ attr_reader :total, :stacks, :discs, :old_stacks, :disc, :source, :sink
7
+ attr_accessor :fancy, :animated, :animation
8
8
 
9
9
  def initialize discs
10
10
  @discs = discs
@@ -12,12 +12,20 @@ module Hanoi
12
12
  @base = 2
13
13
  @stacks = [(0...discs).to_a.reverse, [], []]
14
14
  @fancy = false
15
+ @animated = false
15
16
  end
16
17
 
17
18
  def move
19
+ @old_stacks = @stacks.clone.map { |s| s.clone }
20
+
18
21
  diff
19
- find_disc
20
- @stacks[find_stack].push @stacks[@source].pop
22
+ @source = find_disc
23
+ @sink = find_stack
24
+ @stacks[@sink].push @stacks[@source].pop
25
+
26
+ if @animated
27
+ @animation = Animation.new self
28
+ end
21
29
  end
22
30
 
23
31
  def solved
@@ -29,7 +37,7 @@ module Hanoi
29
37
  end
30
38
 
31
39
  def console
32
- (Formatters::Console.new self).to_s
40
+ (Formatters::Console.new @discs, @stacks, @fancy).to_s
33
41
  end
34
42
 
35
43
  def inspect
@@ -37,7 +45,11 @@ module Hanoi
37
45
  stacks: @stacks,
38
46
  moves: @total,
39
47
  binary: rebased,
40
- moved: @disc
48
+ moved: {
49
+ disc: @disc,
50
+ from: @source,
51
+ to: @sink
52
+ }
41
53
  }
42
54
  end
43
55
 
@@ -72,11 +84,11 @@ module Hanoi
72
84
 
73
85
  def find_disc
74
86
  @stacks.each_with_index do |stack, index|
75
- @source = index if stack.index @disc
87
+ return index if stack.index @disc
76
88
  end
77
89
  end
78
90
 
79
- def find_stack #disc, source, stacks
91
+ def find_stack
80
92
  # if the next stack is empty, move there
81
93
  if @stacks[(@source + 1) % 3] == []
82
94
  return (@source + 1) % 3
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = '0.2.3'
3
+ VERSION = '0.2.5'
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.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - pikesley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-06 00:00:00.000000000 Z
11
+ date: 2018-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -146,6 +146,7 @@ files:
146
146
  - bin/setup
147
147
  - hanoi-jane.gemspec
148
148
  - lib/hanoi/jane.rb
149
+ - lib/hanoi/jane/animation.rb
149
150
  - lib/hanoi/jane/cli.rb
150
151
  - lib/hanoi/jane/constrained_towers.rb
151
152
  - lib/hanoi/jane/formatters/console.rb