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

My lock program wants to cut its function half way through?!

Started by TurtlesAndStuff, 05 April 2013 - 11:54 AM
TurtlesAndStuff #1
Posted 05 April 2013 - 01:54 PM
I am new to computercraft (started on the 3rd of April this year) and I have spent about an hour making and debuging this program;

function lockUp()

   os.pullEvent = os.pullEventRaw --DO NOT CHANGE THIS!!! It protects the program from termination
   GUESSES_ALLOWED = 3 --Don't raise this too high!!!
   guessesSoFar = 0 --Keep this at 0!!!
   password = "Password1" --What you want the password to be
   lockUpTime = 60 --Upon failing password however many times max is set to, this is how long it will lock for
   lockUpTimeLeft = lockUpTime --Leave this!!!
   greeting = "Please verify that you have permission to access this ______: " --What will your computer's message be?

   while guessesSoFar < GUESSES_ALLOWED do
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint(greeting)
	  textutils.slowPrint("Attempts remaining:   "..GUESSES_ALLOWED - guessesSoFar)
	  textutils.slowPrint("Enter password:   ")
	
	  passwordGuesses = read("*")
	
	  if guess == password then
		 term.clear()
		 term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Correct!")
		 textutils.slowPrint("Access Granted!!!")
		 textutils.slowPrint("..........Loading..........")
	  end
	
	  else
		 guessesSoFar = guessesSoFar + 1
		 term.clear()
         term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Incorrect!")
		 textutils.slowPrint("Access Denied!!!")
		 textutils.slowPrint("..........Locking..........")
	  end
   end

   while lockUpTimeLeft > 0 do
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("Number of password guesses exceeded.")
	  print()
	  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
	  lockUpTimeLeft = lockUpTimeLeft - 1
	  sleep(1)
   end

   if lockUpTimeLeft == 0 then
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint("..........Rebooting program..........")
	  lockUp()
   end
end
lockUp()
but when I think im done with debugging it, I run into a problem that I don't know how to fix. When I try to run it, it tells me that it expects a end to the function at line 37, but that would cut off the function half way through and would prevent the program working. If anybody can check this out for me then that would be appreciated.

Also, just to confirm, does the " os.pullEvent = os.pullEventRaw" line prevent Ctrl + T?
Lyqyd #2
Posted 06 April 2013 - 06:34 AM
Split into new topic.

You have if / end / else / end in there. It needs to be if / else / end.
TurtlesAndStuff #3
Posted 06 April 2013 - 09:39 AM
ah thank you is there a way I can prevent Ctrl + S and Ctrl + R?
Sammich Lord #4
Posted 06 April 2013 - 09:50 AM
ah thank you is there a way I can prevent Ctrl + S and Ctrl + R?
Not really. You can keep the total number of guesses by saving to a file though.
TurtlesAndStuff #5
Posted 06 April 2013 - 10:09 AM
How do i do that?
Bubba #6
Posted 06 April 2013 - 10:21 AM
How do i do that?

Check out this link: http://www.computercraft.info/wiki/Fs.open
Khalory #7
Posted 06 April 2013 - 10:40 AM
Here's a quick explanation of how to read/write a single line file

local file = io.open("guessCounter", "r") -- puts the file "guessCounter" into the "file" variable in read mode
local guessesSoFar = files:read("*n") -- reads the first number from the file
file:close() -- after looking through the file, you need to close it
------------------ To Write to files
local file = io.open("guessCounter", "w") -- opens the file in write mode -- this will rewrite all the lines in the file
file:write(guessesSoFar) -- writes the variable "guessesSoFar" to the text file
file:close()

If you just save it to the file every time it changes then then it should work.
slango20 #8
Posted 06 April 2013 - 12:37 PM
er, you added an extra end at line 27:

function lockUp()
   os.pullEvent = os.pullEventRaw --DO NOT CHANGE THIS!!! It protects the program from termination
   GUESSES_ALLOWED = 3 --Don't raise this too high!!!
   guessesSoFar = 0 --Keep this at 0!!!
   password = "Password1" --What you want the password to be
   lockUpTime = 60 --Upon failing password however many times max is set to, this is how long it will lock for
   lockUpTimeLeft = lockUpTime --Leave this!!!
   greeting = "Please verify that you have permission to access this ______: " --What will your computer's message be?
   while guessesSoFar < GUESSES_ALLOWED do
		  term.clear()
		  term.setCursorPos(1, 1)
		  textutils.slowPrint(greeting)
		  textutils.slowPrint("Attempts remaining:   "..GUESSES_ALLOWED - guessesSoFar)
		  textutils.slowPrint("Enter password:   ")
	  
		  passwordGuesses = read("*")
	  
		  if guess == password then
				 term.clear()
				 term.setCursorPos(1, 1)
				 textutils.slowPrint("Password Correct!")
				 textutils.slowPrint("Access Granted!!!")
				 textutils.slowPrint("..........Loading..........")
		
	  
		  else
				 guessesSoFar = guessesSoFar + 1
				 term.clear()
		 term.setCursorPos(1, 1)
				 textutils.slowPrint("Password Incorrect!")
				 textutils.slowPrint("Access Denied!!!")
				 textutils.slowPrint("..........Locking..........")
		  end
   end
   while lockUpTimeLeft > 0 do
		  term.clear()
		  term.setCursorPos(1, 1)
		  print("Number of password guesses exceeded.")
		  print()
		  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
		  lockUpTimeLeft = lockUpTimeLeft - 1
		  sleep(1)
   end
   if lockUpTimeLeft == 0 then
		  term.clear()
		  term.setCursorPos(1, 1)
		  textutils.slowPrint("..........Rebooting program..........")
		  lockUp()
   end
end
lockUp()
this is the fixed code
TurtlesAndStuff #9
Posted 07 April 2013 - 12:08 PM
er, you added an extra end at line 27:

function lockUp()
   os.pullEvent = os.pullEventRaw --DO NOT CHANGE THIS!!! It protects the program from termination
   GUESSES_ALLOWED = 3 --Don't raise this too high!!!
   guessesSoFar = 0 --Keep this at 0!!!
   password = "Password1" --What you want the password to be
   lockUpTime = 60 --Upon failing password however many times max is set to, this is how long it will lock for
   lockUpTimeLeft = lockUpTime --Leave this!!!
   greeting = "Please verify that you have permission to access this ______: " --What will your computer's message be?
   while guessesSoFar < GUESSES_ALLOWED do
		  term.clear()
		  term.setCursorPos(1, 1)
		  textutils.slowPrint(greeting)
		  textutils.slowPrint("Attempts remaining:   "..GUESSES_ALLOWED - guessesSoFar)
		  textutils.slowPrint("Enter password:   ")
	  
		  passwordGuesses = read("*")
	  
		  if guess == password then
				 term.clear()
				 term.setCursorPos(1, 1)
				 textutils.slowPrint("Password Correct!")
				 textutils.slowPrint("Access Granted!!!")
				 textutils.slowPrint("..........Loading..........")
		
	  
		  else
				 guessesSoFar = guessesSoFar + 1
				 term.clear()
		 term.setCursorPos(1, 1)
				 textutils.slowPrint("Password Incorrect!")
				 textutils.slowPrint("Access Denied!!!")
				 textutils.slowPrint("..........Locking..........")
		  end
   end
   while lockUpTimeLeft > 0 do
		  term.clear()
		  term.setCursorPos(1, 1)
		  print("Number of password guesses exceeded.")
		  print()
		  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
		  lockUpTimeLeft = lockUpTimeLeft - 1
		  sleep(1)
   end
   if lockUpTimeLeft == 0 then
		  term.clear()
		  term.setCursorPos(1, 1)
		  textutils.slowPrint("..........Rebooting program..........")
		  lockUp()
   end
end
lockUp()
this is the fixed code
i already know that from some1 else but thanks anyway

Here's a quick explanation of how to read/write a single line file

local file = io.open("guessCounter", "r") -- puts the file "guessCounter" into the "file" variable in read mode
local guessesSoFar = files:read("*n") -- reads the first number from the file
file:close() -- after looking through the file, you need to close it
------------------ To Write to files
local file = io.open("guessCounter", "w") -- opens the file in write mode -- this will rewrite all the lines in the file
file:write(guessesSoFar) -- writes the variable "guessesSoFar" to the text file
file:close()

If you just save it to the file every time it changes then then it should work.
thank you both this has helped me and i think i am done here, thanks all