Created
June 20, 2013 23:53
-
-
Save billgathen/5827806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bill Gathen (@epicpoodle) | |
# | |
class MyGame | |
def initialize | |
@teleported = false | |
@took_gold = false | |
@room = :entry | |
end | |
def play | |
loop do | |
describe(@room) | |
act_on(choice) | |
end | |
end | |
def describe room | |
rooms = { | |
entry: "You are in a room with a blue door and a red door.", | |
blue: "You are in a blue room with #{@teleported ? 'a gold door' : 'a teleporter in the center. There are no exits' }.", | |
red: "You are in a red room with #{@teleported ? 'a gold door' : 'a teleporter in the center. There are no exits' }.", | |
gold: "You are in a gold room with #{@took_gold ? 'no exits' : 'a door leading to safety and a pile of gold on the floor' }." | |
} | |
puts rooms[room] | |
end | |
def choice | |
print '> ' | |
gets.chomp | |
end | |
def act_on choice | |
if choice.include? "look" | |
# no op | |
elsif @room == :entry | |
if choice.include? "red" | |
@room = :red | |
elsif choice.include? "blue" | |
@room = :blue | |
else | |
complain | |
end | |
elsif @room == :blue or @room == :red | |
if @teleported | |
if choice.include? "gold" or choice.include? "door" | |
@room = :gold | |
else | |
complain | |
end | |
else | |
if choice.include? "teleport" | |
@teleported = true | |
puts "The world goes swimmy. This must be what teleporting feels like..." | |
if @room == :red | |
@room = :blue | |
else | |
@room = :red | |
end | |
else | |
complain | |
end | |
end | |
elsif @room == :gold | |
if @took_gold | |
dead("You search in vain for escape but eventually starve, cuddling your pile of gold.") | |
elsif choice.include? "door" | |
puts "Prudence wins out over greed. The sun shines on your face and you know you made the right decision. You win." | |
Process.exit(0) | |
elsif choice.include? "gold" | |
puts "You take the gold. You're rich. You're a happy miser." | |
@took_gold = true | |
else | |
complain | |
end | |
else | |
dead("Interesting choice.") | |
end | |
end | |
def dead why | |
puts "#{why} You're dead." | |
Process.exit(0) | |
end | |
def complain | |
puts "I don't understand you." | |
end | |
end | |
if $0 == __FILE__ | |
MyGame.new.play | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment