2016-04-12 7 views
-1

ここで私が取り組んでいるコードです。ruby​​変数スコープのエラーがテキストの冒険

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 
> 

私はプログラムは「オープンドア」で入力した後、金の部屋に私を取ると予想、代わりにbarfメッセージに行き、whileループを再起動します。

私はスコープで何かを台無しにしてしまったと思いますが、研究を行いましたが、なぜ変数が操作されなかったのか、なぜ私が期待した結果が得られないのかまだ分かりません。

+0

へようこそbear_moved elsif choice == "taunt bear" && !bear_moved変化に比較演算子を削除する必要があります。 「[mcve]」をお読みください。問題のより良い説明が必要です。何が正しくやっていないのですか?あなたのコード例はその出力を与えません。 –

+0

最後の3つの条件では、 "elsif"構文が異なることに注意してください。2つの括弧があり、評価の順序が変わる可能性があります。それを確認してください。また、「オープンドア」の入力に空白が含まれていないことを確認してください:** puts "|"、choice、 "|" **など、単純な** puts **にこれを表示する必要があります。 プログラムロジックに問題がある場合は、少なくとも、データをトレースし、いくつかの印刷ステートメントでフローを制御してから、ヘルプを探してください。デバッグは非常に便利なスキルです。 – Prune

答えて

1

bear_moved = trueの代わりにbear_moved == trueを書いた可能性があります。

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 
+0

母、私はとても愚かな気分です、ありがとう –

0

bear_moved変数ではなく、メソッドの14行目でtrueとの比較、ブール値を受け取る必要があります。

0

クマが罵倒されて動かないとクマが動くはずですが、クマが動いたらもう一度チェックしないようにしたいと思います。 ソリューションは=スタックオーバーフローのない==

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 
関連する問題