以下是我正在处理的代码。
def gold_room
puts "This room is full of gold. How much do you take?"
print "> "
choice = $stdin.gets.chomp
# this line has a bug, so fix it
if choice.include?("0") || choice.include?("1")
how_much = choice.to_i
else
dead("Man, learn to type a number.")
end
if how_much < 50
puts "Nice, you're not greedy, you win!"
exit(0)
else
dead("You greedy bastard!")
end
end
def bear_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 = false
while true
print "> "
choice = $stdin.gets.chomp
if choice == "take honey"
dead("The bear looks at you then slaps your face off.")
elsif choice == "taunt bear" && !bear_moved
puts "The bear has moved from the door. You can go through it now."
bear_moved == true
elsif choice == "taunt bear" && bear_moved
dead("The bear gets pissed off and chews your leg off.")
elsif (choice == "open door") && bear_moved
gold_room
else
puts "I got no idea what that means."
end
end
end
def cthulhu_room
puts "Here you see the great evil Cthulhu."
puts "He, it, whatever stares at you and you go insane."
puts "Do you flee for your life or eat your head?"
print "> "
choice = $stdin.gets.chomp
if choice.include? "flee"
start
elsif choice.include? "head"
dead("Well that was tasty!")
else
cthulhu_room
end
end
def dead(why)
puts why, "Good job!"
exit(0)
end
def start
puts "You are in a dark room."
puts "There is a door to your right and left."
puts "Which one do you take?"
print "> "
choice = $stdin.gets.chomp
if choice == "left"
bear_room
elsif choice == "right"
cthulhu_room
else
dead("You stumble around the room until you starve.")
end
end
start
当我尝试在OS X的终端上运行它时,我得到了这个:
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
I got no idea what that means. # should go to gold_room, new area
>
我希望程序在输入&#34;打开门&#34;后把我带到黄金房间,而是转到barf消息并重新启动while循环。
我认为我已经弄乱了范围,但我做了研究,仍然不明白为什么变量没有被操纵,或者为什么我没有得到我期望的结果。< / p>
答案 0 :(得分:1)
您的问题可能是您写的是bear_moved == true
而不是bear_moved = true
。
答案 1 :(得分:1)
它bear_moved = true
而非bear_moved == true
elsif choice == "taunt bear" && !bear_moved
puts "The bear has moved from the door. You can go through it now."
bear_moved = true
elsif choice == "taunt bear" && bear_moved
答案 2 :(得分:0)
bear_moved
变量应该接收布尔值,而不是在方法的第14行将其与true
进行比较。
答案 3 :(得分:0)
我认为你要确保当熊被嘲弄并且它不动时它应该移动熊,如果熊移动则不再检查。
解决方案应该是删除elsif choice == "taunt bear" && !bear_moved
上的比较运算符,将bear_moved更改为=
而不是==
while true
print "> "
choice = $stdin.gets.chomp
if choice == "take honey"
dead("The bear looks at you then slaps your face off.")
elsif choice == "taunt bear" && !bear_moved
puts "The bear has moved from the door. You can go through it now."
bear_moved = true
elsif choice == "taunt bear" && bear_moved
dead("The bear gets pissed off and chews your leg off.")
elsif (choice == "open door") && bear_moved
gold_room
else
puts "I got no idea what that means."
end
end