Skip to content

Instantly share code, notes, and snippets.

@thedanotto
Created January 13, 2015 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedanotto/7183c0a8e30d4364a675 to your computer and use it in GitHub Desktop.
Save thedanotto/7183c0a8e30d4364a675 to your computer and use it in GitHub Desktop.
# christmas tree is 7 layers tall
# it is 11 asterix wide
# how many spaces is an asterix? Each asterix is one space
# Special features
# number of rows 1 - 10
# if a tree is 3 rows...
# *
# ***
# *****
# formula for number of asterix.... is ... rows * 2 - 1
# Grab the number of rows the user would like for their christmas tree
def get_user_input(question)
puts question
gets.chomp
end
# This calculates the maximum number asterix of the lowest level
def number_of_asterix(row)
row * 2 - 1
end
# number of asterix in the row given row
# we need to middle-fy the tree
def spaces_needed(row_number, max_tree_width)
# center row one of a 3 layer tree
# max_width - number of asterix in the row / 2
spaces = (max_tree_width - number_of_asterix(row_number)) / 2
#puts spaces
return spaces.to_i
end
def spacer(number_of_spacers)
case number_of_spacers
when 0 then ""
when 1 then " "
when 2 then " "
when 3 then " "
when 4 then " "
when 5 then " "
when 6 then " "
when 7 then " "
when 8 then " "
when 9 then " "
when 10 then " "
when 11 then " "
when 12 then " "
when 13 then " "
when 14 then " "
when 15 then " "
#else ""
end
end
def asterixer(num_of_asterix)
case num_of_asterix
when 0 then ""
when 1 then "*"
when 2 then "**"
when 3 then "***"
when 4 then "****"
when 5 then "*****"
when 6 then "******"
when 7 then "*******"
when 8 then "********"
when 9 then "*********"
when 10 then "**********"
when 11 then "***********"
when 12 then "************"
when 13 then "*************"
when 14 then "**************"
when 15 then "***************"
when 16 then "****************"
when 17 then "*****************"
when 18 then "******************"
when 19 then "*******************"
when 20 then "********************"
when 21 then "*********************"
when 22 then "**********************"
when 23 then "***********************"
when 24 then "************************"
when 25 then "*************************"
when 26 then "**************************"
when 27 then "***************************"
when 28 then "****************************"
when 29 then "*****************************"
when 30 then "******************************"
when 31 then "*******************************"
when 32 then "********************************"
#else ""
end
end
def tree_too_big(maximum_tree_height, requested_tree_height)
requested_tree_height > maximum_tree_height
end
def christmas_tree
tree_height = get_user_input("How tall would you like your tree?").to_i
tree_offset = get_user_input("How many spaces would you like to move your tree to the right?") .to_i
invert_tree = get_user_input("Would you like to invert the tree? 'Yes' or 'No'")
# Figure out if the requested tree is too big...
maximum_tree_height = 15
tree_is_too_big = tree_too_big(maximum_tree_height, tree_height)
# if the tree is too big ... set the max tree height of 15
tree_height = maximum_tree_height if tree_is_too_big
# if the tree is too big... tell the user
puts "You entered a tree that was too large. You get a 15 row tree. It's as good as we can do!" if tree_is_too_big
offset_is_too_big = tree_too_big(maximum_tree_height, tree_offset)
# if the requested offset is too big, set the maximum offset to 15
tree_offset = maximum_tree_height if offset_is_too_big
# if the requested offset it too big, let the user know.
puts "You wanted to offset too large of an amount. You get a tree moved 15 spaces to the right. Be happy with that." if offset_is_too_big
# A variable to determine the max width of the tree. Necessary for spaces.
max_tree_width = number_of_asterix(tree_height)
begin
if invert_tree == "No"
puts spacer(tree_offset) << spacer(spaces_needed(1, max_tree_width)) << asterixer(number_of_asterix(1)) if tree_height > 1
puts spacer(tree_offset) << spacer(spaces_needed(2, max_tree_width)) << asterixer(number_of_asterix(2)) if tree_height > 2
puts spacer(tree_offset) << spacer(spaces_needed(3, max_tree_width)) << asterixer(number_of_asterix(3)) if tree_height > 3
puts spacer(tree_offset) << spacer(spaces_needed(4, max_tree_width)) << asterixer(number_of_asterix(4)) if tree_height > 4
puts spacer(tree_offset) << spacer(spaces_needed(5, max_tree_width)) << asterixer(number_of_asterix(5)) if tree_height > 5
puts spacer(tree_offset) << spacer(spaces_needed(6, max_tree_width)) << asterixer(number_of_asterix(6)) if tree_height > 6
puts spacer(tree_offset) << spacer(spaces_needed(7, max_tree_width)) << asterixer(number_of_asterix(7)) if tree_height > 7
puts spacer(tree_offset) << spacer(spaces_needed(8, max_tree_width)) << asterixer(number_of_asterix(8)) if tree_height > 8
puts spacer(tree_offset) << spacer(spaces_needed(9, max_tree_width)) << asterixer(number_of_asterix(9)) if tree_height > 9
puts spacer(tree_offset) << spacer(spaces_needed(10, max_tree_width)) << asterixer(number_of_asterix(10)) if tree_height > 10
puts spacer(tree_offset) << spacer(spaces_needed(11, max_tree_width)) << asterixer(number_of_asterix(11)) if tree_height > 11
puts spacer(tree_offset) << spacer(spaces_needed(12, max_tree_width)) << asterixer(number_of_asterix(12)) if tree_height > 12
puts spacer(tree_offset) << spacer(spaces_needed(13, max_tree_width)) << asterixer(number_of_asterix(13)) if tree_height > 13
puts spacer(tree_offset) << spacer(spaces_needed(14, max_tree_width)) << asterixer(number_of_asterix(14)) if tree_height > 14
puts spacer(tree_offset) << spacer(spaces_needed(15, max_tree_width)) << asterixer(number_of_asterix(15)) if tree_height > 15
# this will generate the trunk, kinda like a boss.
puts spacer(tree_offset) << spacer(spaces_needed(1, max_tree_width)) << asterixer(number_of_asterix(1))
else
# code to invert tree
# this will generate the trunk, kinda like a boss.
puts spacer(tree_offset) << spacer(spaces_needed(1, max_tree_width)) << asterixer(number_of_asterix(1))
#this will generate the tree...definitely like a boss.
puts spacer(tree_offset) << spacer(spaces_needed(15, max_tree_width)) << asterixer(number_of_asterix(15)) if tree_height > 15
puts spacer(tree_offset) << spacer(spaces_needed(14, max_tree_width)) << asterixer(number_of_asterix(14)) if tree_height > 14
puts spacer(tree_offset) << spacer(spaces_needed(13, max_tree_width)) << asterixer(number_of_asterix(13)) if tree_height > 13
puts spacer(tree_offset) << spacer(spaces_needed(12, max_tree_width)) << asterixer(number_of_asterix(12)) if tree_height > 12
puts spacer(tree_offset) << spacer(spaces_needed(11, max_tree_width)) << asterixer(number_of_asterix(11)) if tree_height > 11
puts spacer(tree_offset) << spacer(spaces_needed(10, max_tree_width)) << asterixer(number_of_asterix(10)) if tree_height > 10
puts spacer(tree_offset) << spacer(spaces_needed(9, max_tree_width)) << asterixer(number_of_asterix(9)) if tree_height > 9
puts spacer(tree_offset) << spacer(spaces_needed(8, max_tree_width)) << asterixer(number_of_asterix(8)) if tree_height > 8
puts spacer(tree_offset) << spacer(spaces_needed(7, max_tree_width)) << asterixer(number_of_asterix(7)) if tree_height > 7
puts spacer(tree_offset) << spacer(spaces_needed(6, max_tree_width)) << asterixer(number_of_asterix(6)) if tree_height > 6
puts spacer(tree_offset) << spacer(spaces_needed(5, max_tree_width)) << asterixer(number_of_asterix(5)) if tree_height > 5
puts spacer(tree_offset) << spacer(spaces_needed(4, max_tree_width)) << asterixer(number_of_asterix(4)) if tree_height > 4
puts spacer(tree_offset) << spacer(spaces_needed(3, max_tree_width)) << asterixer(number_of_asterix(3)) if tree_height > 3
puts spacer(tree_offset) << spacer(spaces_needed(2, max_tree_width)) << asterixer(number_of_asterix(2)) if tree_height > 2
puts spacer(tree_offset) << spacer(spaces_needed(1, max_tree_width)) << asterixer(number_of_asterix(1)) if tree_height > 1
puts "YUMMY ICE CREAM CONE!"
end
rescue
puts "You asked for a tree that we cannot create. MERRY CHRISTMAS AND A HAPPY NEW YEAR."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment