This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GooseQuack's profile picture

Need help with a simple text based adventure

Started by GooseQuack, 18 February 2015 - 09:04 PM
GooseQuack #1
Posted 18 February 2015 - 10:04 PM
Hello, I'm having some trouble with this test program I was writing. I don't really know much about CC so I would appreciate some help :)/>

Whenever I try to run it it works a little bit. It runs the first if statement then if I write enter it runs the "if input == "enter"" code and then ends the whole program. If I type leave it just ends the program without any text. Help is appreciated thank you in advance.


term.clear()
term.setCursorPos(1,1)
print("You have found yourself in the middle of the woods. There is a small house in front of you. What will you do?")
print("Enter or leave")
  input = io.read()
 
  if input == "enter" then
    print("You come closer to the old shack and enter the door, inside you see a chair. What will you do?")
    print("Sit in the chair or leave")
    if input == "sit" then
	  print("You have sat down in the chair. You feel comfortable until you start noticing that once you try to get out of the chair, YOU CAN'T!")
	  print("What will you do now? Scream or try and escape?")
	  if input == "scream" then
	    print("You try to scream but nobody hears you")
	  if input == "escape" then
	    print("You wiggle and waggle and after minutes of shuffling about you are unstuck from the chair! You can see that the door is still open. What will you do?")
	    print("Leave or stay in the shack?")
	 
	    if input == "stay" then
		  print("You stay in the shack but soon die of starvation. The end. (Ending 2 of 2)")
	    if input == "stay" then
			

  if input == leave then
    print("You have left the house and will die in the woods alone")
    print("(Ending 1 of 2)")
end
Cycomantis #2
Posted 18 February 2015 - 10:37 PM
Your missing quotes around leave in your if statement so it is checking for a variable names leave which does not exist.

this
if input == leave then

should be
if input == "leave" then

Also the way you are capturing and checking for the input could be causing issues as well.

Its best to capture the input into a var then check it using 1 if statement.

You also need to capture the user input more then once. If statements are also missing some ends i'm surprised this runs.

Here's an example as how to capture and check user input

local input = read()
if input == "enter" then
--do enter stuff
elseif input == "leave" then
--do the leave stuff
end

Since after asking the first question you ask additional question you will want to nest this same type of structure within the appropriate part of the first if statement

local input = read() --captures first answer
if input == "enter" then
	print("ask your question?")
	input = read() --capture second answer
	if input == "answer a" then
	   --do that stuff
	elseif input == "answer b" then
	   --do other stuff
	end --end 2nd if statement that is nested
elseif input == "leave" then
--do the leave stuff
end --end first if statement

You can also keep the code cleaner and easier to read by using different variables for the different inputs, answer1, answer2, or something that makes more sense to the specific code you are writing. That also allows you to make the vars local to specific part of the code and have less chances of conficts.


local answer1 = read() --captures first answer
if answer1 == "enter" then
	print("ask your question?")
    local answer2 = read() --capture second answer
	if answer2 == "answer a" then
	   --do that stuff
	elseif answer2 == "answer b" then
	   --do other stuff
	end --end 2nd if statement that is nested
elseif answer1 == "leave" then
--do the leave stuff
end --end first if statement
Edited on 18 February 2015 - 09:48 PM
GooseQuack #3
Posted 19 February 2015 - 12:02 AM
Thank you so much!
I may not know much about this but I'm learning, again thank you :)/>