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

Is this right?

Started by ImNutzForCoal, 08 August 2012 - 12:58 AM
ImNutzForCoal #1
Posted 08 August 2012 - 02:58 AM
So, before I get onto the code, I must say, I am currently, and for a while, away from a omputer, both irl and cc. I wrote this a second ago, while reading an issue in my email that users wanted to type the password for a lock on the same line as the prompt. Would the following do that?



print("Written in DroidEditor")
term.clear()
term.setCursorPos(1,1)
print("Password: ")
term.setCursorPos(1,12)
pw = read("*")
if pw ~= "computer" then
print("Incorrect Password")
sleep(1)
shell.run("startup")
else
print("Password accepted")
sleep(2)
os.reboot()
end

I understand that it just prints depending on input, but will input still be on line 1?
Cranium #2
Posted 08 August 2012 - 03:42 AM
To start off, one thing you can do to prevent any issues is to use os.reboot() instead of shell.run("startup") Does the same thing, and refreshes anything you changed during the time you ran the program.
Second, the password you type is going to be printed on line 12, which is where you set it to(on line 5 of your program). If you want the password to be printed just after the word password, you can do this:

--add these functions at the top, and it will print the number of attempts made.
function passattempt()
  term.setCursorPos(1,18)
  write("			 Password Attempts: ")
  passnumber()
end
function passnumber()
  write(tostring(x))
end
x = 0 --I will talk about this later.
print("			 Please enter password")
  term.setCursorPos(1,8)
  write("PASSWORD: ") --use write instead, so that the cursor stays at that line when they type.
  term.setCursorPos(11,8)
  local menuinput = read("*")
   if menuinput == "PASSWORD" then --this is where you setup your correct password.
	 term.clear()
	 term.setCursorPos(1,9)
	 print("Password Accepted.")
	  sleep(1)
	 print("You may now enter.")
	  open()
	  sleep(10)
	  close()
   else
	 x = x + 1 --counts up the number of attempts.
   elseif x == 4 then  --this is what will happen if they enter the wrong password too many times.
--You can change this to do almost anything, like activate a piston on the floor dropping them into a pool of lava, or crushing them.
	 term.clear()
	 term.setCursorPos(1,9)
	 print("	  You have exceeded the maximum number")
	 print("		  of attempts for this computer")
	 os.shutdown() --turns off the computer
This is a code I like to use for my doors.
Zalerinian #3
Posted 08 August 2012 - 03:42 AM
So, before I get onto the code, I must say, I am currently, and for a while, away from a omputer, both irl and cc. I wrote this a second ago, while reading an issue in my email that users wanted to type the password for a lock on the same line as the prompt. Would the following do that?



print("Written in DroidEditor")
term.clear()
term.setCursorPos(1,1)
print("Password: ")
term.setCursorPos(1,12)
pw = read("*")
if pw ~= "computer" then
print("Incorrect Password")
sleep(1)
shell.run("startup")
else
print("Password accepted")
sleep(2)
os.reboot()
end

I understand that it just prints depending on input, but will input still be on line 1?

The input will be on line two. The output of that program, if you entered the correct password is the following:

Written in DroidEditor
Password:  -- this is due to you using print. If you want it on the same line, use write("Your text here")
********
Password Accepted
Lyqyd #4
Posted 08 August 2012 - 03:47 AM
So, before I get onto the code, I must say, I am currently, and for a while, away from a omputer, both irl and cc. I wrote this a second ago, while reading an issue in my email that users wanted to type the password for a lock on the same line as the prompt. Would the following do that?



print("Written in DroidEditor")
term.clear()
term.setCursorPos(1,1)
print("Password: ")
term.setCursorPos(1,12)
pw = read("*")
if pw ~= "computer" then
print("Incorrect Password")
sleep(1)
shell.run("startup")
else
print("Password accepted")
sleep(2)
os.reboot()
end

I understand that it just prints depending on input, but will input still be on line 1?

The input will be on line two. The output of that program, if you entered the correct password is the following:

Written in DroidEditor
Password:  -- this is due to you using print. If you want it on the same line, use write("Your text here")
********
Password Accepted

This is not correct. He sets the cursor position after the print. It should use the same line.
Zalerinian #5
Posted 08 August 2012 - 04:01 AM
So, before I get onto the code, I must say, I am currently, and for a while, away from a omputer, both irl and cc. I wrote this a second ago, while reading an issue in my email that users wanted to type the password for a lock on the same line as the prompt. Would the following do that?



print("Written in DroidEditor")
term.clear()
term.setCursorPos(1,1)
print("Password: ")
term.setCursorPos(1,12)
pw = read("*")
if pw ~= "computer" then
print("Incorrect Password")
sleep(1)
shell.run("startup")
else
print("Password accepted")
sleep(2)
os.reboot()
end

I understand that it just prints depending on input, but will input still be on line 1?

The input will be on line two. The output of that program, if you entered the correct password is the following:

Written in DroidEditor
Password:  -- this is due to you using print. If you want it on the same line, use write("Your text here")
********
Password Accepted

This is not correct. He sets the cursor position after the print. It should use the same line.

Meh, write is easier ;)/>/>
Pharap #6
Posted 08 August 2012 - 05:02 AM
use this instead:

print("Written in DroidEditor")
while true do
term.clear()
term.setCursorPos(1,1)
write("Password: ")
pw = read("*")
if pw ~= "computer" then
print("Incorrect Password")
sleep(1)
else
print("Password accepted")
sleep(2)
return
end
end

This way the same program loops until someone gets the password right or terminates with ctrl+T, ctrl+R or ctrl+S
(if you want to stop people using the shortcuts, you're going to need a more complicated program)

Also, I am assuming from what you said, this is for a lock on a door. Assuming the door is on the left of the computer and will only ever be used to open the door, you will need to use this instead:

print("Written in DroidEditor")
while true do
term.clear()
term.setCursorPos(1,1)
write("Password: ")
pw = read("*")
if pw ~= "computer" then
print("Incorrect Password")
sleep(1)
else
print("Password accepted")
rs.setOutput("left",true)
sleep(2)
rs.setOutput("left",false)
end
end
If you want anything else, quote me and ask again.
Shadow1 #7
Posted 08 August 2012 - 10:59 AM
How i delte this post?
ImNutzForCoal #8
Posted 08 August 2012 - 11:04 AM
Thanks for the help. This is for a door, but its not that part I needed assistance on, it was the lineup, so that the input would be on the same line as the prompt.

Thanks again