Skip to content

Instantly share code, notes, and snippets.

@chadjemmett
Created June 28, 2012 21:02
Show Gist options
  • Save chadjemmett/3013877 to your computer and use it in GitHub Desktop.
Save chadjemmett/3013877 to your computer and use it in GitHub Desktop.
A Dragon class.
class Dragon
def initialize name
@name = name
@asleep = false
@stuff_in_belly = 10
@stuff_in_guts = 0
puts @name + " is born!"
end
def feed
if @asleep == true
puts "You can't feed #{@name}. He's asleep right now."
else
puts "You feed " + @name + "."
@stuff_in_belly = 10
passage_of_time
end #end of checking if the dragon is asleep.
end
def walk
puts "You walk " + @name + "."
@stuff_in_guts = 0
passage_of_time
end
def put_to_bed
puts "You put " + @name + " to bed."
@asleep = true
3.times do
if @asleep
passage_of_time
end
if @asleep
puts @name + "snores, filling the room with smoke."
end
end
if @asleep == false
puts @name + " wakes up slowly."
end
end
def toss
puts "You toss " + @name + " up in the air. \n He giggles, which singes your eyebrows."
passage_of_time
end
def rock
puts "You rock " + @name + " gently."
@asleep = true
puts "He briefly dozes off..."
passage_of_time
if @asleep == false
puts "...but wakes up when you stop."
end
end
#added a diaper change method.
def change
if @stuff_in_guts >= 7
puts " you change " + @name + "'s diaper"
@stuff_in_guts = 0
else
puts "diaper is clean."
end
passage_of_time
end
def status
puts "alseep?"
puts @asleep
puts "number of stuff in guts"
puts @stuff_in_guts
puts "number of stuff in belly"
puts @stuff_in_belly
end #end of status
private
def hungry?
@stuff_in_belly <= 2
end
def poopy?
@stuff_in_guts >= 8
end
def passage_of_time
if @stuff_in_belly > 0
@stuff_in_belly = @stuff_in_belly - 1
@stuff_in_guts = @stuff_in_guts + 1
else
if @asleep
@asleep = false
puts "He wakes up suddenly."
end
puts @name + " is starving! In desparation , he ate you!"
exit
end
if @stuff_in_guts >= 10
@stuff_in_guts = 0
puts "Whoops! " + @name + " had an accident..."
end
if hungry?
if @asleep
@asleep = false
puts "He wakes up suddenly."
end
puts @name + "'s stomach grumbles..."
end
if poopy?
if @asleep == false
puts "He wakes up suddenly!"
end
puts @name + " does the potty dance."
end
end #end of passage of time.
end #this is the end of the class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment