Skip to content

Instantly share code, notes, and snippets.

@malpinder
Created March 18, 2014 11:07
Show Gist options
  • Save malpinder/9617992 to your computer and use it in GitHub Desktop.
Save malpinder/9617992 to your computer and use it in GitHub Desktop.
Refactoring excercise. Make this thrashing leviathan of code into something a bit more understandable, before it devours us all.
# I'm so, so sorry.
class Adventure
def run
@location = :north
puts "You are in a maze of twisty little passages, all alike"
puts "What do you do?"
result = gets.chomp
if result == "HELP"
puts "Here are the accepted commands:"
puts "NORTH to go NORTH"
puts "SOUTH to go SOUTH"
puts "EAST to go EAST"
puts "WEST to go WEST"
puts "INVENTORY to see what you are carrying"
puts "LOOK to see what you can see"
puts "PICK UP to pick something up"
elsif result == "NORTH" || result == "EAST" || result == "WEST"
puts "You can't go that way!"
elsif result == "SOUTH"
@location = :south
puts "It is dark here. You are likely to be eaten by a grue."
elsif result == "INVENTORY"
puts "You have a lantern. It has no oil in it."
if @hat_on_head
puts "A jaunty hat rests upon your head."
end
elsif result == "LOOK" && !@hat_on_head
puts "You can see a hat on the ground."
else
puts "I don't understand that."
end
puts "What do you do?"
result = gets.chomp
if result == "HELP"
puts "Here are the accepted commands:"
puts "NORTH to go NORTH"
puts "SOUTH to go SOUTH"
puts "EAST to go EAST"
puts "WEST to go WEST"
puts "INVENTORY to see what you are carrying"
puts "LOOK to see what you can see"
puts "PICK UP to pick something up"
elsif result == "SOUTH" && @location == :south
puts "You can't go that way!"
elsif result == "SOUTH"
@location = :south
puts "It is dark here. You are likely to be eaten by a grue."
elsif result == "NORTH" && @location == :south
@location = :north
puts "You are in a maze of twisty little passages, all alike"
elsif result == "EAST" && @location == :south
puts "You have been eaten by a grue."
return false
elsif result == "EAST" || result == "WEST"
puts "You can't go that way!"
elsif result == "INVENTORY"
puts "You have a lantern. It has no oil in it."
if @hat_on_head
puts "A jaunty hat rests upon your head."
end
elsif result == "LOOK" && !@hat_on_head
puts "You can see a hat on the ground."
elsif result == "PICK UP"
@hat_on_head = true
puts "You pick up the hat."
else
puts "I don't understand that."
end
puts "What do you do?"
result = gets.chomp
if result == "HELP"
puts "Here are the accepted commands:"
puts "NORTH to go NORTH"
puts "SOUTH to go SOUTH"
puts "EAST to go EAST"
puts "WEST to go WEST"
puts "INVENTORY to see what you are carrying"
puts "LOOK to see what you can see"
puts "PICK UP to pick something up"
elsif result == "SOUTH" && @location == :south
puts "You can't go that way!"
elsif result == "SOUTH"
@location = :south
puts "It is dark here. You are likely to be eaten by a grue."
elsif result == "NORTH" && @location == :south
@location = :north
puts "You are in a maze of twisty little passages, all alike"
elsif result == "EAST" && @location == :south
puts "You have been eaten by a grue."
return false
elsif result == "EAST" || result == "WEST"
puts "You can't go that way!"
elsif result == "INVENTORY"
puts "You have a lantern. It has no oil in it."
if @hat_on_head
puts "A jaunty hat rests upon your head."
end
elsif result == "LOOK" && !@hat_on_head
puts "You can see a hat on the ground."
elsif result == "LOOK"
puts "You can't see anything around here."
elsif result == "PICK UP"
@hat_on_head = true
puts "You pick up the hat."
else
puts "I don't understand that."
end
puts "What do you do?"
result = gets.chomp
if result == "HELP"
puts "Here are the accepted commands:"
puts "NORTH to go NORTH"
puts "SOUTH to go SOUTH"
puts "EAST to go EAST"
puts "WEST to go WEST"
puts "INVENTORY to see what you are carrying"
puts "LOOK to see what you can see"
puts "PICK UP to pick something up"
elsif result == "SOUTH" && @location == :south
puts "You can't go that way!"
elsif result == "SOUTH"
@location = :south
puts "It is dark here. You are likely to be eaten by a grue."
elsif result == "NORTH" && @location == :south
@location = :north
puts "You are in a maze of twisty little passages, all alike"
elsif result == "EAST" && @location == :south
puts "You have been eaten by a grue."
return false
elsif result == "EAST" || result == "WEST"
puts "You can't go that way!"
elsif result == "INVENTORY"
puts "You have a lantern. It has no oil in it."
if @hat_on_head
puts "A jaunty hat rests upon your head."
end
elsif result == "LOOK" && !@hat_on_head
puts "You can see a hat on the ground."
elsif result == "LOOK"
puts "You can't see anything around here."
elsif result == "PICK UP"
@hat_on_head = true
puts "You pick up the hat."
else
puts "I don't understand that."
end
puts "What do you do?"
result = gets.chomp
if result == "HELP"
puts "Here are the accepted commands:"
puts "NORTH to go NORTH"
puts "SOUTH to go SOUTH"
puts "EAST to go EAST"
puts "WEST to go WEST"
puts "INVENTORY to see what you are carrying"
puts "LOOK to see what you can see"
puts "PICK UP to pick something up"
elsif result == "SOUTH" && @location == :south
puts "You can't go that way!"
elsif result == "SOUTH"
@location = :south
puts "It is dark here. You are likely to be eaten by a grue."
elsif result == "NORTH" && @location == :south
@location = :north
puts "You are in a maze of twisty little passages, all alike"
elsif result == "EAST" && @location == :south
puts "You have been eaten by a grue."
return false
elsif result == "EAST" || result == "WEST"
puts "You can't go that way!"
elsif result == "INVENTORY"
puts "You have a lantern. It has no oil in it."
if @hat_on_head
puts "A jaunty hat rests upon your head."
end
elsif result == "LOOK" && !@hat_on_head
puts "You can see a hat on the ground."
elsif result == "LOOK"
puts "You can't see anything around here."
elsif result == "PICK UP"
@hat_on_head = true
puts "You pick up the hat."
else
puts "I don't understand that."
end
puts "What do you do?"
result = gets.chomp
if result == "HELP"
puts "Here are the accepted commands:"
puts "NORTH to go NORTH"
puts "SOUTH to go SOUTH"
puts "EAST to go EAST"
puts "WEST to go WEST"
puts "INVENTORY to see what you are carrying"
puts "LOOK to see what you can see"
puts "PICK UP to pick something up"
elsif result == "SOUTH" && @location == :south
puts "You can't go that way!"
elsif result == "SOUTH"
@location = :south
puts "It is dark here. You are likely to be eaten by a grue."
elsif result == "NORTH" && @location == :south
@location = :north
puts "You are in a maze of twisty little passages, all alike"
elsif result == "EAST" && @location == :south
puts "You have been eaten by a grue."
return false
elsif result == "EAST" || result == "WEST"
puts "You can't go that way!"
elsif result == "INVENTORY"
puts "You have a lantern. It has no oil in it."
if @hat_on_head
puts "A jaunty hat rests upon your head."
end
elsif result == "LOOK" && !@hat_on_head
puts "You can see a hat on the ground."
elsif result == "LOOK"
puts "You can't see anything around here."
elsif result == "PICK UP"
@hat_on_head = true
puts "You pick up the hat."
else
puts "I don't understand that."
end
puts "You have died of starvation."
end
end
Adventure.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment