Forum: Ruby I am out of ideas halp

2b2cb3c6e72a6dd85581489990272f56?d=identicon&s=25 Joo J. (joo_j)
on 2014-06-24 19:21
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
D1b31c2021bd98b2ba7c2773225011eb?d=identicon&s=25 Sleeth :!: (sleeth)
on 2014-06-24 22:03
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
D1b31c2021bd98b2ba7c2773225011eb?d=identicon&s=25 Sleeth :!: (sleeth)
on 2014-06-24 22:30
# 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
2b2cb3c6e72a6dd85581489990272f56?d=identicon&s=25 Joo J. (joo_j)
on 2014-06-24 22:46
Thank you, I will do that next time (ie the indent thing) thanks again!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.