2016-04-12 9 views
-1

Hier ist der Code, an dem ich arbeite.Ruby Variable Scoping Fehler in Text-Abenteuer

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 

Wenn ich versuche, es in meinem Terminal auf OS X zu laufen, bekomme ich diese:

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 
> 

ich das Programm erwartet, dass ich nach der Eingabe in der „offenen Tür“, um die Gold-Zimmer zu nehmen, aber stattdessen geht es zu den barf-nachrichten und startet die while-schleife neu.

Ich nehme an, ich habe etwas mit dem Umfang vermasselt, aber ich habe recherchiert und verstehe immer noch nicht, warum die Variable nicht manipuliert wurde, oder warum ich nicht das Ergebnis bekomme, das ich erwarte.

+0

Willkommen bei Stack-Überlauf auf elsif choice == "taunt bear" && !bear_moved Änderung zu entfernen. Bitte lesen Sie "[mcve]". Wir brauchen eine bessere Beschreibung des Problems. Was macht es nicht richtig? Ihr Codebeispiel gibt uns diese Ausgabe nicht. –

+0

Ich bemerke, dass Ihre "elsif" -Syntax für die letzten drei Bedingungen unterschiedlich ist: zwei haben Klammern, die die Bewertungsreihenfolge ändern können. Bitte überprüfe das. Bitte überprüfen Sie auch, dass Ihr Eintrag von "open door" keine Leerzeichen enthält: Ein einfaches ** puts ** sollte dies anzeigen, wie ** puts "|", choice, "|" **. Wenn Sie Probleme mit Ihrer Programmlogik haben, sollten Sie die Daten zumindest verfolgen und die Abläufe mit einigen Druckanweisungen steuern, bevor Sie sich anderweitig um Hilfe kümmern. Debugging ist eine sehr nützliche Fähigkeit. – Prune

Antwort

1

Ihr Problem könnte sein, dass Sie bear_moved == true anstelle von geschrieben haben.

1

es ist bear_moved = true nicht 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

Haha, ich fühle mich so albern, danke –

0

Die bear_moved Variable den boolean erhalten sollte, anstatt sie mit true auf der 14. Zeile der Methode zu vergleichen.

0

Ich denke, dass Sie sicherstellen möchten, dass, wenn Bär verspottet wird und es sich nicht bewegt, sollte es Bär bewegen, überprüfen Sie nicht noch einmal, ob Bär sich bewegte. Die Lösung sollte sein, den Vergleichsoperator der bear_moved zu = nicht ==

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