0

I've been trying to learn Ruby so I can build a website and was told that Learning Ruby the Hard Way was a good place to start. I've been doing the exercises, but got stuck at ex36. We are supposed to write a mini text based decision game (pick your adventure style). I wrote mine and thought it made sense, I'm trying a couple of new features though.

I tried making some variables that change as the player goes on in the story, and the ending depend on these variables (soul, life, riches, debt).

I took away the narrator speaking but left the double quotes

soul = 1 
life = 1
riches = 0
debt = 0


def score
    if soul == 1 && life == 1
        puts ""
        gold_score
    elsif soul <= 0 && life == 1
        puts ""
        puts ""
        gold_score
    elsif soul > 1 && life == 1
        puts ""
        gold_score
    else
        puts ""
        exit(0)
    end
end

def gold_score
    if riches == 0 || debt == 0
        puts ""
        exit(0)
    elsif riches == 0 || debt == -1
        puts ""
        exit(0)

    elsif riches == 1 || debt == 0
        puts ""
        exit(0)
    else
        puts ""
        exit(0)
    end
end

def dead(why)
    puts ""
    life = 0
    score
end

def start 
    puts ""
    puts ""

    print "> "
    choice = $stdin.gets.chomp

    if choice == "left" || choice == "Left"
        pirates_room
    elsif choice == "right" || choice == "Right"
        trex_room
    else
        dead("")
    end

end

def gold_room
    puts ""
    puts ""
    puts ""
    puts "."
    puts ""
    puts ""

    print "> "
    choice = $stdin.gets.chomp

    if choice == "1" 
        puts ""
        soul = soul + 1
        score
    elsif choice == "2"
        puts ""
        riches = riches + 1
        score
    else
        puts ""
        puts ""
        score
    end
end

def pirates_room
    puts ""
    puts ""
    puts ""
    puts ""

    print "> "
    choice = $stdin.gets.chomp

    if choice.include? ""
        puts ""
        dead("")
    elsif choice.include?("beer") || choice.include?("keg") 
        puts ""
        puts ""
        puts ""
        soul = soul - 1
        gold_room
    elsif choice.include?("Cthulhu") || choice.include?("bible")
        puts ""
        puts ""
        puts ""
        puts ""
        debt = -1
        puts ""
        gold_room
    else
        pirates_room
    end
end

def trex_room
    puts ""
    puts ""
    puts ""

    print "> "
    choice = $stdin.gets.chomp

    if choice.include?("stay") || choice.include?("Stay")
        dead("")
    elsif choice.include?("pray") || choice.include?("Pray")
        puts ""
        puts ""
        puts ""
        puts ""
        puts ""
        gold_room
    else
        trex_room
    end
end

start

I ran it, and the problem seems to be with the variables I put on the beggining. Everytime I try to "handle" them ( add a number, or see if they are greater or less than a number) there's an error.

I've been looking for the situations where this happens and I've got a few:

ex36.rb:106:in `pirates_room': undefined method `-' for nil:NilClass (NoMethodError)
            from ex36.rb:55:in `start'
            from ex36.rb:143:in `<main>'

ex36.rb:81:in `gold_room': undefined method `+' for nil:NilClass (NoMethodError)
            from ex36.rb:115:in `pirates_room'
            from ex36.rb:55:in `start'
            from ex36.rb:143:in `<main>'

ex36.rb:8:in `score': undefined local variable or method `soul' for main:Object (NameError)
            from ex36.rb:44:in `dead'
            from ex36.rb:59:in `start'
            from ex36.rb:122:in `<main>'

What am I doing wrong? I have zero experience with coding so I'm probably just doing some basic error, I've tried to find an answer here, but the one with similar errors the responses don't seem to apply to this case or at least I don't understand how they apply. Can't I define variables and then change them along the way with the methods?

Thanks for the help.

Zero
  • 1
  • 2
  • simply put, it looks like you have a scoping issue. For understanding variable scope types there are a [few good reads available](http://www.techotopia.com/index.php/Ruby_Variable_Scope). Try experimenting with turning `soul` into an instance variable. – Rots Aug 13 '16 at 11:36
  • @Rots I knew it was going to be something simple :p I've tried bot with instance variable and global variable, and both work. Now I'll read the website to understand what the differences are. Thanks a lot for your help! – Zero Aug 13 '16 at 11:50

0 Answers0