Created
September 5, 2013 21:02
-
-
Save waneka/6456124 to your computer and use it in GitHub Desktop.
This file contains 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 Disasters | |
attr_reader :name, :yield_destroyed, :probability | |
def initialize(name, yield_destroyed, probability) | |
@probability = probability | |
@name = name | |
@yield_destroyed = yield_destroyed | |
end | |
end |
This file contains 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
require_relative 'other_fruit' | |
tree = OrangeTree.new | |
tree.age! until tree.any_fruit? | |
puts "Tree is #{tree.age} years old and #{tree.height} feet tall" | |
until tree.dead? | |
sleep(0.2) | |
basket = [] | |
# It places the oranges in the basket | |
# IT PLACES THE ORANGES IN THE BASKET | |
while tree.any_fruit? | |
basket << tree.pick_a_fruit! | |
end | |
diameter_sum = basket.inject(0) {|sum, v| sum + v.diameter} | |
avg_diameter = (diameter_sum/basket.length).round(2) | |
puts "Year #{tree.age} Report" | |
if tree.disaster == nil | |
puts "Tree height: #{tree.height} feet" | |
puts "Weather: #{tree.weather.name}" | |
puts "Harvest: #{basket.size} #{tree.fruit_class.to_s.downcase}#{'s' unless basket.size == 1} with an average diameter of #{avg_diameter} inches" | |
puts "" | |
else | |
puts "AHHHHHHHHH" | |
puts "DISTASTAAAHHH" | |
puts "#{tree.disaster.name} You managed to scrounge up #{basket.size} oranges" | |
puts "" | |
end | |
# Age the tree another year | |
tree.age! | |
end | |
puts "Awwwwwww sh!t, ze tree - she is dead!" |
This file contains 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
require_relative 'weather' | |
require_relative 'disasters' | |
class FruitTree | |
attr_reader :age, :height, :dead, :fruit, :fruit_class, :weather, :disaster | |
WEATHER = [Weather.new('Drought', (0.5), (0.15)), | |
Weather.new('Wicked Gorgeous', (1.25), (0.20)), | |
Weather.new('Flood', (0.25), (0.15)), | |
Weather.new('Early Frost', (0.75), (0.15)), | |
Weather.new('Great weather!', (1.0), (0.30)), | |
Weather.new('UFO sighting - Best Harvest Ever!', (30.0), (0.05))] | |
DISASTERS = [Disasters.new('Tornado!', (0.85), (0.1)), | |
Disasters.new('Earthquake!', (0.90), (0.05)), | |
Disasters.new('GODZILLA!!!', (1.00), (0.05)), | |
Disasters.new('Plague of Locusts!', (0.75), (0.10)), | |
Disasters.new('Volcano Erupts!', (0.99), (0.08)), | |
Disasters.new('Hurricane!', (0.95), (0.12))] | |
def initialize | |
@fruit_class = Fruit | |
@age = 0 | |
@height = 0 | |
@dead = false | |
@fruit = [] | |
@avg_max_height = 0 | |
@tree_dies_at = 0 | |
@produces_fruit_at = 0 | |
end | |
def age! | |
choose_weather | |
@height = (@height + grow).round(2) | |
@dead = true if @age >= @tree_dies_at | |
@age += 1 | |
produce_fruit | |
disaster! | |
end | |
def grow | |
((@avg_max_height - @height) * 0.1).round(2) * (weather == WEATHER[5] ? 1 : @weather.growth) | |
end | |
def disaster! | |
@disaster = DISASTERS.sample | |
num = (rand() < @disaster.probability ? @disaster.yield_destroyed : 0) | |
num == 0 ? @disaster = nil : (num * @fruit.length).to_i.times {@fruit.pop} | |
end | |
def choose_weather | |
weather_c = 0 | |
until weather_c > 0 | |
@weather = WEATHER.sample | |
weather_c = weather.growth if rand() < weather.probability | |
end | |
end | |
def produce_fruit | |
(@weather.growth*@height*(3*rand()+1)).to_i.times {@fruit << @fruit_class.new} if @age >= @produces_fruit_at | |
if @weather == WEATHER[5] | |
@fruit.map {|i| i.diameter *= 5} | |
end | |
end | |
def dead? | |
@dead | |
end | |
def any_fruit? | |
@fruit.any? | |
end | |
def pick_a_fruit! | |
raise NoFruitError, "This tree is barren" unless self.any_fruit? | |
return @fruit.slice!(rand(@fruit.length)) | |
end | |
end | |
class Fruit | |
attr_reader :ripe | |
attr_accessor :diameter | |
def initialize | |
@ripe = false | |
end | |
end | |
# growth factor = (a*c*e^((c+x)/b))/(b*(e^(c/b)+e^(x/b)))^2 | |
This file contains 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
require_relative 'fruit_tree' | |
class Orange < Fruit | |
def initialize | |
super | |
@min_diameter = 2 | |
@max_diameter = 4 | |
@diameter = ((@max_diameter - @min_diameter) * rand() + @min_diameter).round(2) | |
end | |
end | |
class OrangeTree < FruitTree | |
def initialize | |
super | |
@fruit_class = Orange | |
@avg_max_height = 30 | |
@tree_dies_at = 50 | |
@produces_fruit_at = 4 | |
end | |
end | |
class Apple < Fruit | |
def initialize | |
super | |
@min_diameter = 1 | |
@max_diameter = 3 | |
@diameter = ((@max_diameter - @min_diameter) * rand() + @min_diameter).round(2) | |
end | |
end | |
class AppleTree < FruitTree | |
def initialize | |
super | |
@fruit_class = Apple | |
@avg_max_height = 15 | |
@tree_dies_at = 20 | |
@produces_fruit_at = 5 | |
end | |
end |
This file contains 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 Weather | |
attr_reader :name, :growth, :probability | |
def initialize(name, growth, probability) | |
@probability = probability | |
@name = name | |
@growth = growth | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment