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

Simple door with admin Login and riddle

Started by Carnivorious, 24 December 2012 - 10:08 AM
Carnivorious #1
Posted 24 December 2012 - 11:08 AM
Well basically I wanted to make a program that asks you a password… but then I thought: "Hey that's way to boring for me and my friends!" So I inserted a little riddle to decide who is worthy enough to enter. I also added a little part in the beginning, so that if you are an admin (I chose to name it Benjamin after me) then you can chose to login and skip the riddle. The code will be in the post for I am sure you people can make it a lot easier!


term.clear()
term.setCursorPos(1, 1)

print("Are you a guest? Press 1. Are you Benjamin? Press 3 to Login.")
visit = io.read()
if visit == "1" then
  term.clear()
  term.setCursorPos(1, 1)
  correctpass = "time"
  print("This thing all things devours:")
  print("Birds, beasts, trees, flowers;")
  print("Gnaws iron, bites steel;")
  print("Grinds hard stones to meal;")
  print("Slays king, ruins town,")
  print("And beats high mountain down.")
  print("What am I?")
  pass = io.read()
  term.clear()
  term.setCursorPos(1, 1)
    if pass == (correctpass) then
    write("You are worthy, you may pass.")
    redstone.setOutput("right",true)
    sleep(3)
    os.shutdown()
    else
    write("You are not worthy to enter. BEGONE!")
    sleep(2)
    os.shutdown()
    end
 
else
  if visit == "3" then
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("Username: ")
	  Username = io.read()
	  print("Password: ")
	  Password = io.read()
	 
	    if Username == "Benjamin" and Password == "test" then
	    term.clear()
	    term.setCursorPos(1, 1)
	    print("Welcome admin.")
	    redstone.setOutput("right",true)
	    sleep(6)
	    os.shutdown()
	    else
	    shell.run('startup')
	    end
	  else
	  end
end
immibis #2
Posted 24 December 2012 - 01:32 PM
And if I type 2, or 7, or X, or DERP then I get to log in without knowing the answer or the password?
NDFJay #3
Posted 24 December 2012 - 01:45 PM
And if I type 2, or 7, or X, or DERP then I get to log in without knowing the answer or the password?

by the look of the code it will just return shell…
Grim Reaper #4
Posted 24 December 2012 - 01:54 PM
It doesn't return to the shell, but it does keep adding recursive calls onto the stack. If you try to log in as an admin and fail about 100 times or so, the program will crash due to a stack overflow.

Check this tutorial out. It will help you understand why your program will crash eventually. It's okay; it takes most people who are beginners (including myself) a while to understand and consider the flow of control and the state of the stack when designing a program, so welcome to the club!
Carnivorious #5
Posted 24 December 2012 - 07:58 PM
It doesn't return to the shell, but it does keep adding recursive calls onto the stack. If you try to log in as an admin and fail about 100 times or so, the program will crash due to a stack overflow.

Check this tutorial out. It will help you understand why your program will crash eventually. It's okay; it takes most people who are beginners (including myself) a while to understand and consider the flow of control and the state of the stack when designing a program, so welcome to the club!
I realise it will crash eventually, yet I didn't think anybody would be brave enough to try it 178 times. (Max number of tries iirc) At first I made it say that you chose an input that was not an option, but that gave too many bugs so I deleted it. I could however adjust the code like this: add another shell.run('startup') after the last else, but it would stack like the other one did. Thanks anyway for the advice and the opinions and I''ll definitely check out the tutorial!