Created
December 14, 2009 12:28
-
-
Save willbailey/256012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# ================================== | |
# = SOLUTION TO RUBY QUIZ NUMBER 2 = | |
# ================================== | |
# by Will & Stef | |
require 'optparse' | |
# map of positions to characters for each number | |
Patterns = { | |
"0" => { :t => "-", :tl => "|", :tr => "|", :m => " ", :bl => "|", :br => "|", :b => "-"}, | |
"1" => { :t => " ", :tl => " ", :tr => "|", :m => " ", :bl => " ", :br => "|", :b => " "}, | |
"2" => { :t => "-", :tl => " ", :tr => "|", :m => "-", :bl => "|", :br => " ", :b => "-"}, | |
"3" => { :t => "-", :tl => " ", :tr => "|", :m => "-", :bl => " ", :br => "|", :b => "-"}, | |
"4" => { :t => " ", :tl => "|", :tr => "|", :m => "-", :bl => " ", :br => "|", :b => " "}, | |
"5" => { :t => "-", :tl => "|", :tr => " ", :m => "-", :bl => " ", :br => "|", :b => "-"}, | |
"6" => { :t => "-", :tl => "|", :tr => " ", :m => "-", :bl => "|", :br => "|", :b => "-"}, | |
"7" => { :t => "-", :tl => " ", :tr => "|", :m => " ", :bl => " ", :br => "|", :b => " "}, | |
"8" => { :t => "-", :tl => "|", :tr => "|", :m => "-", :bl => "|", :br => "|", :b => "-"}, | |
"9" => { :t => "-", :tl => "|", :tr => "|", :m => "-", :bl => " ", :br => "|", :b => "-"} | |
} | |
class LcdNumberPrinter | |
# constructor | |
def initialize(numbers, size) | |
@numbers = numbers.split(//) | |
@size = size.to_i | |
end | |
# total height of the lcd display | |
def height | |
(2 * @size) + 3 | |
end | |
# width of each digit in the display | |
def digit_width | |
@size + 2 | |
end | |
# total width of the display | |
def total_width | |
(digit_width * @numbers.size) | |
end | |
# print each line in the display | |
def print_lines | |
0.upto(height - 1).each do |row| | |
print_columns(row) | |
end | |
end | |
# print the column list and add a line break | |
def print_columns row | |
0.upto(total_width - 1).each do |column| | |
print_character_for_coordinates(row, column) | |
end | |
print "\n" | |
end | |
# print a character based on the position in the number slot | |
# the current number and spacing | |
def print_character_for_coordinates(row, column) | |
print " " if is_right_side_of_digit?(column - 1) | |
slot = get_digit_slot(row, column) | |
character = slot ? Patterns[get_number(column)][slot] : " " | |
character = " " if [:t,:b,:m].include?(slot) && !is_in_the_middle?(column) | |
print character | |
end | |
# determine the location within the number slot map to print | |
def get_digit_slot(row, column) | |
return :t if row == 0 | |
return :b if row == height - 1 | |
return :m if row == 1 + @size | |
return :tl if is_top_half_of_digit?(row) && is_left_side_of_digit?(column) | |
return :tr if is_top_half_of_digit?(row) && is_right_side_of_digit?(column) | |
return :bl if is_bottom_half_of_digit?(row) && is_left_side_of_digit?(column) | |
return :br if is_bottom_half_of_digit?(row) && is_right_side_of_digit?(column) | |
return nil | |
end | |
# true if we are in the middle section of a number slot | |
def is_in_the_middle?(column) | |
!is_left_side_of_digit?(column) && !is_right_side_of_digit?(column) | |
end | |
# true if we are on the top half of a number slot | |
def is_top_half_of_digit?(row) | |
row <= @size | |
end | |
# true if we are on the bottom half of a number slot | |
def is_bottom_half_of_digit?(row) | |
row > @size | |
end | |
# true if we are on the the left boundary of a number slot | |
def is_left_side_of_digit?(column) | |
(column==0 || column % digit_width == 0) | |
end | |
# true if we are on the the right boundary of a number slot | |
def is_right_side_of_digit?(column) | |
(((number_position(column) + 1) * digit_width) -1 == column) | |
end | |
# get the ordinal position of the current number in the array of numbers | |
def number_position(column) | |
(column)/digit_width | |
end | |
# get the number for the current column | |
def get_number column | |
@numbers[number_position(column)] | |
end | |
end | |
if __FILE__ == $0 | |
params = ARGV.getopts("s:") | |
size = params["s"] || 2 | |
numbers = ARGV[0] | |
unless numbers && numbers.match(/^\d+$/) | |
puts "usage you must provide a list of numbers" | |
exit | |
end | |
LcdNumberPrinter.new(numbers, size).print_lines | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment