[SOLUTION] Ruby Quiz #14 LCD Numbers

Discussion in 'Ruby' started by Nathaniel Talbott, Jan 9, 2005.

  1. --Apple-Mail-9-295837381
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    format=flowed

    This was the first quiz I participated in (I've been waiting for one I
    could whip out real quick), and it made a nice programming warm-up
    after being away over the holidays. I added one small bit of
    functionality: a colon, so I could do time displays. However, the colon
    looked ugly when expanded, so I made it so it only expands vertically,
    not horizontally.

    The template-based approach I used could probably be used (with a bit
    of tweaking) to allow prettier displays, but I didn't want to show off
    how much I stink at ASCII art by trying myself :)

    Thanks for the quiz!


    Nathaniel
    Terralien, Inc.

    <:((><


    --Apple-Mail-9-295837381
    Content-Transfer-Encoding: 7bit
    Content-Type: application/octet-stream;
    x-unix-mode=0744;
    name="lcd"
    Content-Disposition: attachment;
    filename=lcd

    #!/usr/bin/env ruby

    module LCD
    class Template
    def self.height(size)
    3 + (2 * size)
    end

    def initialize(string, option)
    case option
    when "h"
    @horizontal_stable = true
    end
    @lines = string.gsub(/\./, ' ').split("\n")
    end

    def actual_line(line, size)
    case
    when line == 0
    0
    when line == (self.class.height(size) - 1)
    4
    when line < (size + 1)
    1
    when line == (size + 1)
    2
    else
    3
    end
    end

    def display(line, size)
    l = @lines[actual_line(line, size)].dup
    l[1, 1] *= size unless(@horizontal_stable)
    l
    end
    end

    TEMPLATES = {}
    DATA.read.scan(/=([^\n])([a-z]?)\n([^=]+)/m){|c, option, t| TEMPLATES[c] = Template.new(t, option)}

    def self.display(string, size=1)
    output = ""
    input = string.split(//)
    Template.height(size).times do |i|
    line = []
    input.each do |character|
    line << TEMPLATES[character].display(i, size)
    end
    output << "#{line.join(" " * size)}\n"
    end
    output
    end
    end

    if(__FILE__ == $0)
    require 'optparse'

    size = 1
    ARGV.options do |o|
    o.on("-sSIZE", "--size", Integer){|size|}
    end.parse!
    puts LCD::display(ARGV.first, size)
    end

    __END__
    =0
    -.
    |.|
    ...
    |.|
    -.
    =1
    ...
    ..|
    ...
    ..|
    ...
    =2
    -.
    ..|
    -.
    |..
    -.
    =3
    -.
    ..|
    -.
    ..|
    -.
    =4
    ...
    |.|
    -.
    ..|
    ...
    =5
    -.
    |..
    -.
    ..|
    -.
    =6
    -.
    |..
    -.
    |.|
    -.
    =7
    -.
    ..|
    ...
    ..|
    ...
    =8
    -.
    |.|
    -.
    |.|
    -.
    =9
    -.
    |.|
    -.
    ..|
    -.
    =:h
    ...
    ...
    -.
    ...
    -.
    --Apple-Mail-9-295837381--
     
    Nathaniel Talbott, Jan 9, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruby Quiz

    [QUIZ] LCD Numbers (#14)

    Ruby Quiz, Jan 7, 2005, in forum: Ruby
    Replies:
    26
    Views:
    267
    Zach Dennis
    Jan 14, 2005
  2. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    8
    Views:
    200
    Dave Burt
    Jan 10, 2005
  3. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    16
    Views:
    295
    David Tran
    Jan 10, 2005
  4. Vance A Heron

    [SOLUTION] LCD Numbers (#14)

    Vance A Heron, Jan 10, 2005, in forum: Ruby
    Replies:
    0
    Views:
    111
    Vance A Heron
    Jan 10, 2005
  5. David Tran
    Replies:
    9
    Views:
    217
    David Tran
    Jan 21, 2005
Loading...

Share This Page