I am out of ideas halp

I am trying to write some sort of text adventure… It is in the right
folder and all that, but it won’t work… Help?

Code:
puts ‘Are you ready to play?’

answer = gets.chomp

if answer == ‘Yes’

puts ‘Okay then, we continue’

puts ‘You are in a large room, with a chest. Do you open it? Or not?’

answer1 = gets.chomp

if answer1 == ‘Yes’

puts ‘You find a large wooden stick in the shape of a sword’

else puts ‘Yeah, there is probably nothing in there anyway’

elsif answer == ‘no’

quit

elsif answer == ‘No’

quit

else

puts ‘I do not understand that answer, please say Yes, or No’

puts ‘after reloading the game’

end

There are little mistakes, if you ident your code, you will see easier.

puts ‘Are you ready to play?’

answer = gets.chomp

if answer == ‘Yes’

puts ‘Okay then, we continue’

puts ‘You are in a large room, with a chest. Do you open it? Or not?’

answer1 = gets.chomp

if answer1 == ‘Yes’

puts 'You find a large wooden stick in the shape of a sword'
#else puts 'Yeah, there is probably nothing in there anyway'
#This 'else' not must be here

elsif answer == ‘no’

quit

elsif answer == ‘No’

quit

else

puts 'I do not understand that answer, please say Yes, or No'

puts 'after reloading the game'

end
end # You forgot the final end

There is another way easier to mantain

We define question for every level

def question(level)
case level
when 1
‘Hi, do you want to play?’
when 2
‘Do you want to continue?’
else
‘Game Over’
end
end

We define posibble answer for every level and action

def action(level,answer)
case level
when 1

if say Yes go to next level (level + 1) if say No,

keep at level 1,…

case answer
when 'Yes'
puts 'You say yes to first question'
$level_gen += 1
when 'No'
puts 'You say no to first question'
else
puts 'I don\'t understand'
end

when 2

if say Yes go ahead and level + 1, if say No go back (level - 1),

if say ‘Back’ go to level 1,…

case answer
when 'Yes'
puts 'You are at level 2 and say Yes'
$level_gen += 1
when 'No'
puts 'Ok you say No at level 2'
$level_gen -= 1
when 'Back'
$level_gen = 1
puts 'We will back to start point'
else
puts 'I don\'t understand'
end

end
end

We start a global variable with our start level

$level_gen = 1

Loop forever for question and answer

while true do
puts question($level_gen)
action($level_gen,gets.chomp)
end

Thank you, I will do that next time (ie the indent thing) thanks again!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs