hanoi-jane 0.2.2 → 0.2.3

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: c83c6e60d1e0f4428dd46e92914cc1d5d0f5d30a
4
- data.tar.gz: f3d23854bebd0539e22a78252c7e1837291b6d7f
3
+ metadata.gz: 6784220653ef2903d69fca3be4290dffb8b02c10
4
+ data.tar.gz: 9da04a7d95acdd741ec597ecb7dfa49cac7dd263
5
5
  SHA512:
6
- metadata.gz: 00f5f0260b30c83b267b424e5d7ee3509ae4399cfe990aa61c3ae10c3586138eae73db046208296619e20b08927673f3093aaadea712283d79f74f8b893924f9
7
- data.tar.gz: 26ec239964715266ddc6f320b0d1d8150e52b3e9813205612a5f2285026c21cdc6da60d3187bf23ac79cc0492ba1ad960616b699f3f400e998e6414d83e64eae
6
+ metadata.gz: 5ff0d22b64d36b613ed87957586fb24a1282443787b2ea632285f46382c02984b8e18c394a9fa8570bbc03548052528821cad8f076048f003e851c5ea0c8940e
7
+ data.tar.gz: b4732d47a93152b5939490fffebddace62fd89703374f6b238465914abc581b8a05613b95710247638ff60ba5ab1efad3f930c2542ddf4be25bd58f81a5a7902
@@ -29,6 +29,7 @@ module Hanoi
29
29
  desc 'console', 'Solve the towers on the console'
30
30
  option :discs, type: :numeric, default: 3, minimum: 1
31
31
  option :constrained, type: :boolean
32
+ option :fancy, type: :boolean, default: false
32
33
  def console
33
34
  if options[:discs] < 1
34
35
  puts "Solving for %d discs makes no sense" % options[:discs]
@@ -36,13 +37,17 @@ module Hanoi
36
37
  end
37
38
 
38
39
  towers = Towers.new options[:discs]
40
+
39
41
  if options[:constrained]
40
42
  towers = ConstrainedTowers.new options[:discs]
41
43
  end
42
44
 
45
+ towers.fancy = options[:fancy]
46
+
43
47
  towers.each do |state|
44
48
  system('clear')
45
- puts state.rebased
49
+ s = options[:fancy] ? (Formatters::Console.fancify state.rebased) : state.rebased
50
+ puts s
46
51
  puts
47
52
  puts state.console
48
53
  sleep 0.2
@@ -2,41 +2,64 @@ module Hanoi
2
2
  module Jane
3
3
  module Formatters
4
4
  class Console
5
+ CHARS = {
6
+ space: ' ',
7
+ disc: 'o',
8
+ pole: '|',
9
+ vert_divider: '|',
10
+ horiz_divider: '-'
11
+ }
12
+
13
+ FANCY_CHARS = {
14
+ space: ' ',
15
+ disc: '🎾',
16
+ pole: '💈',
17
+ vert_divider: '🔺',
18
+ horiz_divider: '🔻'
19
+ }
20
+
5
21
  def initialize towers
6
22
  @discs = towers.discs
7
23
  @stacks = towers.stacks.clone.map { |s| s.clone }
24
+ @@chars = towers.fancy ? FANCY_CHARS : CHARS
8
25
  end
9
26
 
10
27
  def to_s
11
28
  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"
29
+ joiner = @@chars[:space]
30
+
31
+ (Console.rotate @stacks.map { |s| (Console.pad s, @discs).reverse }).each_with_index do |stack, i|
32
+ joiner = @@chars[:vert_divider] if i == @discs
33
+
34
+ s += "%s%s%s\n" % [
35
+ joiner,
36
+ stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join(joiner),
37
+ joiner
38
+ ]
39
+
15
40
  end
16
41
 
17
- s
42
+ s += "%s\n" % [@@chars[:horiz_divider] * (4 + (Console.scale @discs) * 3)]
18
43
  end
19
44
 
20
45
  def Console.pad array, length
21
- until array.length == length
22
- array.push nil
23
- end
24
-
25
- array.reverse
46
+ Array.new(length + 1 - array.length) + array.reverse
26
47
  end
27
48
 
28
- def Console.make_disc width, space, char = 'o'
49
+ def Console.make_disc width, space
50
+ char = @@chars[:disc]
29
51
  unless width
30
- return ' ' * space
52
+ width = 0
53
+ char = @@chars[:pole]
31
54
  end
32
55
 
33
56
  count = Console.scale width
34
57
  padding = (space - count) / 2
35
58
 
36
59
  '%s%s%s' % [
37
- ' ' * padding,
60
+ @@chars[:space] * padding,
38
61
  char * count,
39
- ' ' * padding
62
+ @@chars[:space] * padding
40
63
  ]
41
64
  end
42
65
 
@@ -47,6 +70,11 @@ module Hanoi
47
70
  def Console.rotate stacks
48
71
  stacks.map { |s| s.clone }.transpose.reverse
49
72
  end
73
+
74
+ def Console.fancify number
75
+ fancy_digits = '012'
76
+ number.chars.map { |d| fancy_digits[d] }.join ' '
77
+ end
50
78
  end
51
79
  end
52
80
  end
@@ -4,12 +4,14 @@ module Hanoi
4
4
  include Enumerable
5
5
 
6
6
  attr_reader :total, :stacks, :discs
7
+ attr_accessor :fancy
7
8
 
8
9
  def initialize discs
9
10
  @discs = discs
10
11
  @total = 0
11
12
  @base = 2
12
13
  @stacks = [(0...discs).to_a.reverse, [], []]
14
+ @fancy = false
13
15
  end
14
16
 
15
17
  def move
@@ -1,5 +1,5 @@
1
1
  module Hanoi
2
2
  module Jane
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
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.2
4
+ version: 0.2.3
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-02 00:00:00.000000000 Z
11
+ date: 2018-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler