defprompt()print"> "enddefgold_room()puts"This room is full of gold. How much do you take?"prompt;next_move=gets.chompifnext_move.include?"0"ornext_move.include?"1"how_much=next_move.to_i()elsedead("Man, learn to type a number.")endifhow_much<50puts"Nice, you're not greedy, you win!"Process.exit(0)elsedead("You greedy bastard!")endenddefbear_room()puts"There is a bear here."puts"The bear has a bunch of honey."puts"The fat bear is in front of another door."puts"How are you going to move the bear?"bear_moved=falsewhiletrueprompt;next_move=gets.chompifnext_move=="take honey"dead("The bear looks at you then slaps your face off.")elsifnext_move=="taunt bear"andnotbear_movedputs"The bear has moved from the door. You can go through it now."bear_moved=trueelsifnext_move=="taunt bear"andbear_moveddead("The bear gets pissed off and chews your leg off.")elsifnext_move=="open door"andbear_movedgold_room()elseputs"I got no idea what that means."endendenddefcthulu_room()puts"Here you see the great evil Cthulu."puts"He, it, whatever stares at you and you go insane."puts"Do you flee for your life or eat your head?"prompt;next_move=gets.chompifnext_move.include?"flee"start()elsifnext_move.include?"head"dead("Well that was tasty!")elsecthulu_room()endenddefdead(why)puts"#{why} Good job!"Process.exit(0)enddefstart()puts"You are in a dark room."puts"There is a door to your right and left."puts"Which one do you take?"prompt;next_move=gets.chompifnext_move=="left"bear_room()elsifnext_move=="right"cthulu_room()elsedead("You stumble around the room until you starve.")endendstart()
你應該看到的結果
你可以結果:
$ ruby ex35.rb
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
> taunt bear
The bear has moved from the door. You can go through it now.
> open door
This room is full of gold. How much do you take?
> asf
Man, learn to type a number. Good job!
$