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

I need help on something

Started by CardingiSFun, 27 September 2012 - 09:39 PM
CardingiSFun #1
Posted 27 September 2012 - 11:39 PM
So im trying to make something
basicly when u open the pc it asks you if you want to start and closes a iron door
then it starts asking questions and then u awnser them one by one
after u finished it opens a door for 5 seconds
then restarts
Lettuce #2
Posted 27 September 2012 - 11:47 PM
Good. Now why don't you tell us the problem, and give us your code. This community is pretty awesome, but we haven't achieved psychic abilities yet.
CardingiSFun #3
Posted 27 September 2012 - 11:49 PM
Well i dont know where to start im new to cc and i know a little bit thats all
Fatal_Exception #4
Posted 27 September 2012 - 11:49 PM
Yeah. The purpose of this forum is to help people with their code, not to write entire programs for them with no effort on the OP's part.
Cranium #5
Posted 27 September 2012 - 11:50 PM
That's actually a fairly simple script to write, considering it doesn't sound like you need major security on it. I assume this is for something like an adventure map?
Take a look at some of the password doors in the programs section, as well as the tutorials section in regards to menu options. We're not going to write the code for you, but if you come back with something, we can give you tips and pointers on how to improve it.
ChaddJackson12 #6
Posted 29 September 2012 - 01:02 AM
Since I am bored I think I could write some code for you on what I think you want… But you will have to edit the code to your specifications beyond that. Also, there is a max/min of 5 questions… Meaning that you can't change that number unless you know how to. :P/>/> By the way, the answers to the questions are not related to me in any way… So don't get your hopes up. :D/>/>

You may change the questions and the answers! Just make sure that question1 goes with answer1, ect. Then make sure to put the answer or question in quotes, if it isn't…

Save this file as "startup" on the computer you want to use this for.


-- Question and Answer configuration --
local question1 = "What color hair do you have?" -- The first question that is asked.
local answer1 = "Black" -- Answer to question 1, and is case sensitive.

local question2 = "What color teeth do you have?" -- The second question that is asked.
local answer2 = "White" -- Answer to question 2, and is case sensitive.

local question3 = "What is your friend's name?" -- The third question that is asked.
local answer3 = "Bob" -- Answer to question 3, and is case sensitive.

local question4 = "What is your mother's name?" -- The fourth question that is asked.
local answer4 = "Rebecca" -- Answer to question 4, and is case sensitive

local question5 = "What is your dad's name?" -- The fifth and last question that is asked.
local answer5 = "Dave" -- Answer to question 5, and is case sensitive.

local doorside = "right" -- Change this to one of the following sides: right, left, top, bottom, back, or front. This side will open the iron doors.

-- A function (Don't mess with it!) --
function usedoor()
rs.setOutput(doorside, true)
print("Door Opened!")
os.sleep(1)
o, p = term.getCursorPos()
write("Remaining Time: 8")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 7")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 6")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 5")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 4")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 3")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 2")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 1")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 0")
os.sleep(.5)
print("nDoor Closed!")
rs.setOutput(doorside, false)
os.sleep(1)
os.shutdown()
end

-- The code --
term.clear()
term.setCursorPos(1,1)
print("Would you like to start?")
write("n> ")
local input = read()
if input == "Yes" or input == "yes" then
print("nThen please answer the following questions.")
os.sleep(1.5)
print("nQuestion 1:")
os.sleep(1.5)
write(question1 .. ": ")
Answerone = read()
print("nQuestion 2:")
os.sleep(1,5)
write(question2 .. ": ")
Answertwo = read()
print("nQuestion 3:")
os.sleep(1.5)
write(question3 .. ": ")
Answerthree = read()
print("nQuestion 4:")
os.sleep(1.5)
write(question4 .. ": ")
Answerfour = read()
print("nQuestion 5:")
os.sleep(1.5)
write(question5 .. ": ")
Answerfive = read()
if Answerone == answer1 and Answertwo == answer2 and Answerthree == answer3 and Answerfour == answer4 and Answerfive == answer5 then
print("nAll questions answered were correct! Congradulations!")
os.sleep(1.5)
usedoor()
else
print("nYou missed the answer to a question! Not opening the door!")
os.sleep(3)
os.shutdown()
end
elseif input == "No" or input == "no" then
print("nOk!")
os.sleep(1.5)
os.shutdown()
end
CardingiSFun #7
Posted 07 October 2012 - 05:29 AM
Since I am bored I think I could write some code for you on what I think you want… But you will have to edit the code to your specifications beyond that. Also, there is a max/min of 5 questions… Meaning that you can't change that number unless you know how to. :D/>/> By the way, the answers to the questions are not related to me in any way… So don't get your hopes up. ;)/>/>

You may change the questions and the answers! Just make sure that question1 goes with answer1, ect. Then make sure to put the answer or question in quotes, if it isn't…

Save this file as "startup" on the computer you want to use this for.


-- Question and Answer configuration --
local question1 = "What color hair do you have?" -- The first question that is asked.
local answer1 = "Black" -- Answer to question 1, and is case sensitive.

local question2 = "What color teeth do you have?" -- The second question that is asked.
local answer2 = "White" -- Answer to question 2, and is case sensitive.

local question3 = "What is your friend's name?" -- The third question that is asked.
local answer3 = "Bob" -- Answer to question 3, and is case sensitive.

local question4 = "What is your mother's name?" -- The fourth question that is asked.
local answer4 = "Rebecca" -- Answer to question 4, and is case sensitive

local question5 = "What is your dad's name?" -- The fifth and last question that is asked.
local answer5 = "Dave" -- Answer to question 5, and is case sensitive.

local doorside = "right" -- Change this to one of the following sides: right, left, top, bottom, back, or front. This side will open the iron doors.

-- A function (Don't mess with it!) --
function usedoor()
rs.setOutput(doorside, true)
print("Door Opened!")
os.sleep(1)
o, p = term.getCursorPos()
write("Remaining Time: 8")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 7")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 6")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 5")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 4")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 3")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 2")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 1")
term.setCursorPos(o, p)
os.sleep(1)
write("Remaining Time: 0")
os.sleep(.5)
print("nDoor Closed!")
rs.setOutput(doorside, false)
os.sleep(1)
os.shutdown()
end

-- The code --
term.clear()
term.setCursorPos(1,1)
print("Would you like to start?")
write("n> ")
local input = read()
if input == "Yes" or input == "yes" then
print("nThen please answer the following questions.")
os.sleep(1.5)
print("nQuestion 1:")
os.sleep(1.5)
write(question1 .. ": ")
Answerone = read()
print("nQuestion 2:")
os.sleep(1,5)
write(question2 .. ": ")
Answertwo = read()
print("nQuestion 3:")
os.sleep(1.5)
write(question3 .. ": ")
Answerthree = read()
print("nQuestion 4:")
os.sleep(1.5)
write(question4 .. ": ")
Answerfour = read()
print("nQuestion 5:")
os.sleep(1.5)
write(question5 .. ": ")
Answerfive = read()
if Answerone == answer1 and Answertwo == answer2 and Answerthree == answer3 and Answerfour == answer4 and Answerfive == answer5 then
print("nAll questions answered were correct! Congradulations!")
os.sleep(1.5)
usedoor()
else
print("nYou missed the answer to a question! Not opening the door!")
os.sleep(3)
os.shutdown()
end
elseif input == "No" or input == "no" then
print("nOk!")
os.sleep(1.5)
os.shutdown()
end

Your my fucking hero i cant tell me how much i like you right now (Not in the gay way)
Kingdaro #8
Posted 07 October 2012 - 06:02 AM


-- your questions
local questions = {
  "What color hair do you have?",
  "What color teeth do you have?",
  "What is your friend's name?",
  "What is your mother's name?",
  "What is your dad's name?"
}

-- your corresponding answers
-- the first question will have the first answer.
local answers = {
  "Black",
  "White",
  "Bob",
  "Rebecca",
  "Dave"
}

-- asks the user if they wanna start.
print 'Would you like to begin?'

-- this is the complicated part. read() returns whatever the user types.
-- it then makes all of the letters in the input lowercase, and checks for the first letter only.
-- it will continue if the user has input "YES", "yes", or "Yes please"
if read():lower():sub(1,1) == 'y' then
  print 'Let us begin then.'
  sleep(1)
else
  print 'Alright, never mind.'
  sleep(1)
  os.reboot()
end

-- start the loop of questioning
local num = 1
local result = true

-- keep going over this loop while the question number is less than or equal to
-- the number of questions.
-- in this case, it stops going at 6.
while num <= #question do
  -- clear the screen in preparation for the next question.
  term.clear()

  -- using the parallel API to count down and take user input at the same time.
  parallel.waitForAny(
-- this targets the first line of the console, and prints the question and time there
        function()
          for time=8, 1, -1 do
                term.setCursorPos(1,1)
                print(questions[num]..' (Time: '..time..')')
                sleep(1)
          end
          result = false
        end
-- at the end of this counting cycle, it'll skip the user input
-- because of "wait for any"
        ,
-- this targets the second line, and takes user input there
-- so that it doesn't interfere with the first line.
        function()
          term.setCursorPos(1,2)
 -- checking for case insensative answers
 -- lowercasing both all of the answer and all of the input
          local input = read():lower()
          if input ~= answers[num]:lower() then
                result = false
          end
        end
-- at the end of this, it skips the timer because of "wait for any"
  )

  -- our result variable is what tells us if the user has ran out of time,
  -- or has answered incorrectly. if it is false, get us out of the loop and
  -- don't ask any more questions.
  if not result then break end

  -- otherwise, keep doing to the next question.
  num = num + 1
end

-- clear the screen again to print the result of the questioning
term.clear()
term.setCursorPos(1,1)

-- if nothing went wrong and the result variable was never set to false
-- open the door for us :D/>/>
if result then
  print 'Good job.'
  rs.setOutput('right', true) -- change this to the side your door is on, of course.
  sleep(5)
  rs.setOutput('right', false) -- same here

-- if the variable is false, just reboot the system.
else
  print 'Wrong.'
  sleep(1)
  os.reboot()
end

If someone brings up the point of how "newbies aren't looking for efficiency", we're writing the program for him anyway so it doesn't really matter.

EDIT: In retrospect, I didn't really shorten it that much :S
Luanub #9
Posted 07 October 2012 - 07:08 AM
Should add some comments to the code so he can hopefully learn something from it.
Kingdaro #10
Posted 07 October 2012 - 07:21 AM
Done.

Though it doesn't make it look any less daunting :/
ChaddJackson12 #11
Posted 08 October 2012 - 10:47 PM


-- your questions
local questions = {
  "What color hair do you have?",
  "What color teeth do you have?",
  "What is your friend's name?",
  "What is your mother's name?",
  "What is your dad's name?"
}

-- your corresponding answers
-- the first question will have the first answer.
local answers = {
  "Black",
  "White",
  "Bob",
  "Rebecca",
  "Dave"
}

-- asks the user if they wanna start.
print 'Would you like to begin?'

-- this is the complicated part. read() returns whatever the user types.
-- it then makes all of the letters in the input lowercase, and checks for the first letter only.
-- it will continue if the user has input "YES", "yes", or "Yes please"
if read():lower():sub(1,1) == 'y' then
  print 'Let us begin then.'
  sleep(1)
else
  print 'Alright, never mind.'
  sleep(1)
  os.reboot()
end

-- start the loop of questioning
local num = 1
local result = true

-- keep going over this loop while the question number is less than or equal to
-- the number of questions.
-- in this case, it stops going at 6.
while num <= #question do
  -- clear the screen in preparation for the next question.
  term.clear()

  -- using the parallel API to count down and take user input at the same time.
  parallel.waitForAny(
-- this targets the first line of the console, and prints the question and time there
		function()
		  for time=8, 1, -1 do
				term.setCursorPos(1,1)
				print(questions[num]..' (Time: '..time..')')
				sleep(1)
		  end
		  result = false
		end
-- at the end of this counting cycle, it'll skip the user input
-- because of "wait for any"
		,
-- this targets the second line, and takes user input there
-- so that it doesn't interfere with the first line.
		function()
		  term.setCursorPos(1,2)
-- checking for case insensative answers
-- lowercasing both all of the answer and all of the input
		  local input = read():lower()
		  if input ~= answers[num]:lower() then
				result = false
		  end
		end
-- at the end of this, it skips the timer because of "wait for any"
  )

  -- our result variable is what tells us if the user has ran out of time,
  -- or has answered incorrectly. if it is false, get us out of the loop and
  -- don't ask any more questions.
  if not result then break end

  -- otherwise, keep doing to the next question.
  num = num + 1
end

-- clear the screen again to print the result of the questioning
term.clear()
term.setCursorPos(1,1)

-- if nothing went wrong and the result variable was never set to false
-- open the door for us ;)/>/>
if result then
  print 'Good job.'
  rs.setOutput('right', true) -- change this to the side your door is on, of course.
  sleep(5)
  rs.setOutput('right', false) -- same here

-- if the variable is false, just reboot the system.
else
  print 'Wrong.'
  sleep(1)
  os.reboot()
end

If someone brings up the point of how "newbies aren't looking for efficiency", we're writing the program for him anyway so it doesn't really matter.

EDIT: In retrospect, I didn't really shorten it that much :S
I don't know who this was directed to, but I could use this quite a bit to learn about tables. Because I am pretty stupid when it comes to tables. :D/>/>