Skip to content

Commit

Permalink
Merge pull request #5 from pikesley/fancy-console
Browse files Browse the repository at this point in the history
Emoji on the console
  • Loading branch information
pikesley committed Jan 6, 2018
2 parents a51995e + 1153781 commit 805df1e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
7 changes: 6 additions & 1 deletion lib/hanoi/jane/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ def phat
desc 'console', 'Solve the towers on the console'
option :discs, type: :numeric, default: 3, minimum: 1
option :constrained, type: :boolean
option :fancy, type: :boolean, default: false
def console
if options[:discs] < 1
puts "Solving for %d discs makes no sense" % options[:discs]
exit 1
end

towers = Towers.new options[:discs]

if options[:constrained]
towers = ConstrainedTowers.new options[:discs]
end

towers.fancy = options[:fancy]

towers.each do |state|
system('clear')
puts state.rebased
s = options[:fancy] ? (Formatters::Console.fancify state.rebased) : state.rebased
puts s
puts
puts state.console
sleep 0.2
Expand Down
54 changes: 41 additions & 13 deletions lib/hanoi/jane/formatters/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,64 @@ module Hanoi
module Jane
module Formatters
class Console
CHARS = {
space: ' ',
disc: 'o',
pole: '|',
vert_divider: '|',
horiz_divider: '-'
}

FANCY_CHARS = {
space: ' ',
disc: '🎾',
pole: '💈',
vert_divider: '🔺',
horiz_divider: '🔻'
}

def initialize towers
@discs = towers.discs
@stacks = towers.stacks.clone.map { |s| s.clone }
@@chars = towers.fancy ? FANCY_CHARS : CHARS
end

def to_s
s = ''
(Console.rotate @stacks.map { |s| (Console.pad s, @discs).reverse }).each do |stack|
s += stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join ' '
s += "\n"
joiner = @@chars[:space]

(Console.rotate @stacks.map { |s| (Console.pad s, @discs).reverse }).each_with_index do |stack, i|
joiner = @@chars[:vert_divider] if i == @discs

s += "%s%s%s\n" % [
joiner,
stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join(joiner),
joiner
]

end

s
s += "%s\n" % [@@chars[:horiz_divider] * (4 + (Console.scale @discs) * 3)]
end

def Console.pad array, length
until array.length == length
array.push nil
end

array.reverse
Array.new(length + 1 - array.length) + array.reverse
end

def Console.make_disc width, space, char = 'o'
def Console.make_disc width, space
char = @@chars[:disc]
unless width
return ' ' * space
width = 0
char = @@chars[:pole]
end

count = Console.scale width
padding = (space - count) / 2

'%s%s%s' % [
' ' * padding,
@@chars[:space] * padding,
char * count,
' ' * padding
@@chars[:space] * padding
]
end

Expand All @@ -47,6 +70,11 @@ def Console.scale size
def Console.rotate stacks
stacks.map { |s| s.clone }.transpose.reverse
end

def Console.fancify number
fancy_digits = '012'
number.chars.map { |d| fancy_digits[d] }.join ' '
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/hanoi/jane/towers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ class Towers
include Enumerable

attr_reader :total, :stacks, :discs
attr_accessor :fancy

def initialize discs
@discs = discs
@total = 0
@base = 2
@stacks = [(0...discs).to_a.reverse, [], []]
@fancy = false
end

def move
Expand Down
2 changes: 1 addition & 1 deletion lib/hanoi/jane/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Hanoi
module Jane
VERSION = '0.2.2'
VERSION = '0.2.3'
end
end
10 changes: 5 additions & 5 deletions spec/hanoi/jane/formatters/console_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module Jane
towers = Towers.new 3

it 'has the correct initial content' do
expect(towers.console).to eq " o \n ooo \n ooooo \n"
expect(towers.console).to eq " | | | \n o | | \n ooo | | \n| ooooo | | | | |\n-------------------------\n"
end

it 'has the correct first-state content' do
towers.move
expect(towers.console).to eq " \n ooo \n ooooo o \n"
expect(towers.console).to eq " | | | \n | | | \n ooo | | \n| ooooo | o | | |\n-------------------------\n"
end

it 'has the correct second-state content' do
towers.move
expect(towers.console).to eq " \n \n ooooo o ooo \n"
expect(towers.console).to eq " | | | \n | | | \n | | | \n| ooooo | o | ooo |\n-------------------------\n"
end
end

Expand Down Expand Up @@ -45,12 +45,12 @@ module Formatters
end

it 'pads an array' do
expect(Console.pad [0], 3).to eq [nil, nil, 0]
expect(Console.pad [0], 3).to eq [nil, nil, nil, 0]
end

it 'makes a disc' do
expect(Console.make_disc 0, 5).to eq ' o '
expect(Console.make_disc nil, 5). to eq ' '
expect(Console.make_disc nil, 5). to eq ' | '
end

it 'scales a size' do
Expand Down

0 comments on commit 805df1e

Please sign in to comment.