Created
October 3, 2017 19:10
-
-
Save therod/c742e39683fee7943e2236ee6e75a314 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
class OrangeTree | |
def initialize | |
@height = 0 | |
@age = 0 | |
@oranges = 0 | |
@alive = true | |
end | |
def one_year_passes | |
@alive = false if @height > 10 && rand(2) == 1 | |
return puts 'A dead tree cannot grow! :(' unless @alive | |
@age += 1 | |
@height += rand(2) + 0.5 | |
@oranges = 3 * @age if can_produce_oranges? | |
print 'Your Tree survived for another year! ' | |
print "It's now #{@age} years old and #{@height}m high. \n" | |
end | |
def pick_an_orange | |
return puts 'You cannot pick oranges from a dead tree!' unless @alive | |
return puts 'There are no oranges to pick!' if @oranges.zero? | |
@oranges -= 1 | |
puts 'You pick a delicious orange and put it in your mouth. Yummy!' | |
end | |
def count_the_oranges | |
return puts 'You cannot count oranges, the tree is dead!' unless @alive | |
puts "I'm counting and there are #{@oranges} oranges on the tree!" | |
end | |
private | |
def can_produce_oranges? | |
@age >= 3 | |
end | |
end | |
# ------------------------------------------------- | |
mytree = OrangeTree.new | |
20.times do | |
mytree.one_year_passes | |
mytree.count_the_oranges | |
mytree.pick_an_orange | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment