class Boom < Exception
end
def enterTheDungeon!
puts 'You are entering the dungeon.'
end
def surpriseEncounter!
encounters = ['goblin horde', 'huge ogre', 'stone golem']
puts 'A ' + encounters.sample + ' appears.'
end
def attack!
puts 'You draw your weapon and attack.'
end
def run!
puts 'You decide to run.'
end
def stepOnTrap!
puts 'You steppend on a trap!'
raise Boom
end
def play
begin
enterTheDungeon!
surpriseEncounter!
attack!
run!
stepOnTrap!
rescue
puts 'Game Over!'
end
end
play
We're testing a preliminary set-up for a text-based dungeon crawling game. Certain events (like stepping on a trap) are supposed to raise an exception, which leads to 'Game Over!'. When running the above sample, why do you get the exception instead of 'Game Over!'?
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.