Last active
August 29, 2015 14:13
-
-
Save maximedelpit/2c77df22e67355cd3f6c to your computer and use it in GitHub Desktop.
Livecode_oranger.rb
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 'oranger' | |
oranger = Oranger.new | |
98.times do | |
oranger.one_year_passes | |
oranger.pick_a_fruit | |
puts "L'oranger est #{oranger.dead? ? 'mort' : 'vivant'}, il a #{oranger.age} ans, mesure #{oranger.height}cm et a #{oranger.fruits} fruits" | |
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 Oranger | |
attr_reader :age, :height, :fruits | |
def initialize | |
@age = 0 | |
@height = 0 | |
@fruits = 0 | |
@alive = true | |
end | |
def one_year_passes | |
@age +=1 | |
@fruits = 0 | |
@alive = compute_health | |
if dead? == false | |
@height += 1 if @age <= 10 | |
grow_fruit | |
end | |
end | |
def pick_a_fruit | |
if @fruits > 0 | |
@fruits -= 1 | |
return 1 | |
else | |
return 0 | |
end | |
end | |
def dead? | |
!@alive | |
end | |
private | |
def compute_health | |
return false unless @alive | |
if @age <= 50 | |
true | |
elsif 50 < @age && @age <= 100 | |
return [true, false].sample | |
else | |
false | |
end | |
end | |
def grow_fruit | |
if @age > 5 && @age <= 10 | |
@fruits = 100 | |
elsif @age > 10 && @age <= 15 | |
@fruits = 200 | |
else | |
@fruits = 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment