Skip to content

Instantly share code, notes, and snippets.

@RainMonster
Created September 3, 2013 20:37
Show Gist options
  • Save RainMonster/6429229 to your computer and use it in GitHub Desktop.
Save RainMonster/6429229 to your computer and use it in GitHub Desktop.
Ruby class interaction and inheritance exercise
FIRST_NAME = ['Hal', 'Claire', 'Nancy', 'Hermione', 'Willis', 'Robert', 'Ned', 'Rhaegar']
LAST_NAME = ['Wilde', 'Mercer', 'Jenkins', 'Lewis']
class HauntedHouse
attr_reader :storyteller, :someone_is_awake, :house_family
attr_accessor :ghost, :exorcise, :very_scary
def initialize(number_of_family_members)
@house_family = []
@ghost = Ghost.new
@very_scary = 2
@exorcise = 0
fill_the_house(number_of_family_members) # family could be a separate class with some of the methods here.
end
def make_spooky_sounds
@very_scary += 1
someone_is_awake
puts "#{@someone_is_awake.first_name} has woken up and is wandering the house!"
end
def ghost_scares!
if @ghost.exorcised
puts "This ghost can't scare anybody anymore!"
elsif (@ghost.scare + @very_scary) > @someone_is_awake.brave
@someone_is_awake.dead = true
puts "Oh no! #{@ghost} appeared and frightened #{@someone_is_awake.first_name} to death!"
else
puts "#{@ghost} appeared and tried to frighten #{@someone_is_awake.first_name}."
puts "Luckily, they were so brave that they escaped!"
@someone_is_awake.brave -= 2
@exorcise += 1
end
end
def call_an_exorcist?
if @exorcise < 2
puts "That seems kind of silly, no one really believes that stuff."
else
puts "You'd better call a priest right now!"
@ghost.exorcise
counting_the_dead
end
end
def tell_a_story?
choose_a_storyteller
puts "Would you like #{@storyteller.first_name} to tell you a funny or scary story?"
choice = gets.chomp
if choice == 'funny'
@storyteller.tell_funny_story
@very_scary -= 2
elsif choice == 'scary'
@storyteller.tell_scary_story
@very_scary += 2
else
puts "Please choose funny or scary for your story"
end
end
def counting_the_dead
@house_family.each do |person|
if person.dead == true
puts "#{person.first_name} has sadly died at the age of #{person.age}."
end
end
end
private
def fill_the_house(number_of_family_members)
number_of_children = number_of_family_members - 2
2.times do
@house_family << Parent.new
end
number_of_children.times do
@house_family << Child.new
end
end
def someone_is_awake
@someone_is_awake = @house_family.shift
shuffle_family(@someone_is_awake)
end
def choose_a_storyteller
@storyteller = @house_family.shift
if @storyteller.dead == true
puts "You notice something a little eerie about #{@storyteller.first_name}"
puts "They look really pale, and their voice sounds raspy and hollow."
@very_scary += 3
end
shuffle_family(@storyteller)
end
def shuffle_family(family_member)
@house_family << family_member
@house_family.shuffle!
end
end
class Parent
@@Last_Name = LAST_NAME.sample # This will not work if more than one house exists
attr_reader :first_name, :age, :last_name
attr_accessor :dead, :brave
def initialize
@first_name = FIRST_NAME.sample
@dead = false
@age = (18..50).to_a.sample
@brave = [15, 15, 20].sample
end
def to_s
"#{@first_name} #{@@Last_Name}"
end
def tell_scary_story
puts "#{self.first_name} told a pretty scary story, and now everyone's feeling creeped out!"
end
def tell_funny_story
puts "#{self.first_name} told a pretty funny story and now this house doesn't seem so bad."
end
end
class Child < Parent
def initialize
super
@age = (8..17).to_a.sample
@brave = [10, 10, 15].sample
end
end
class Ghost
attr_reader :last_name, :exorcised, :scare, :very_scary
def initialize
@gender = ['Man', 'Lady'].sample
@last_name = LAST_NAME.sample
@scare = [3, 5, 5, 7].sample
@exorcised = false
end
def exorcise
@exorcised = true
puts "The ghost is exorcised! Now everyone is safe!"
end
def to_s
"Old #{@gender} #{@last_name}"
end
end
# Creates instance of haunted house
scary_house = HauntedHouse.new(5)
# # Creating ghost should create its own name
p scary_house.ghost != ''
# # User will choose what type of story to hear from the storyteller
# # Telling a funny story decreases the @very_scary variable and
# # gives these guys a chance
scary_house.tell_a_story?
puts
# Wakes up a member of the family, who begins wandering the house
scary_house.make_spooky_sounds
puts
# The ghost is going to scare this person
scary_house.ghost_scares!
puts
# If the @exorcism variable is high enough, the family will call an
# exorcist to get rid of the ghost.
scary_house.call_an_exorcist?
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment