Martin DeMello
8/5/2008 6:09:00 AM
On Mon, Aug 4, 2008 at 10:34 PM, Tim Mcd <tmcdowell@gmail.com> wrote:
> So, I am currently working on an Interactive Fiction like game in Ruby,
> and have stumbled across a bit of a problem. The 'move' function makes a
> call to the room's 'exits' variable (exits[:north], for instance) but I
> want it to catch if the variable :north doesn't exist to display
> something like "You can't go taht way!". I can't figure out how to do
> this with my meager skills. :(
>
> Any help would be greatly appreciated!
This should illustrate the basic approach:
exits = {:north => 'front of building', :east => 'fork in road'}
print "what now? > "
move = gets.chomp.downcase
dir = case move
when "n", "north"
:north
when "s", "south"
:south
when "e", "east"
:east
when "w", "west"
:west
else
nil
end
if dir
if exits[dir]
puts "moving to #{exits[dir]}"
else
puts "you can't go that way!"
end
else
puts "no such direction. please enter one of [n]orth, [s]outh, [e]ast
or [w]est"
end
martin