1 posts
Posted 10 October 2012 - 09:07 PM
Greetings, I have a code for a password protected door, the code follows:
os.PullEvent = os.PullEventRaw
while true do
term.clear()
term.setCursorPos(1, 1)
print ("Password:")
input = read ("*")
if input == "pass" then
print ("Password Correct")
rs.setOutput("left", true)
os.sleep(2)
rs.setOutput("left", false)
else
print ("Password Incorrect")
os.sleep(2)
end
end
Would there be anything I could do to shorten or improve this code?
521 posts
Location
Stockholm, Sweden
Posted 10 October 2012 - 09:25 PM
You could do
Spoiler
os.pullEvent = os.pullEventRaw
local password = "myPersonalPassword"
local inputPass = ""
while true do
while inputPass ~= password do
term.setCursorPos(1,1)
term.clearLine()
write("Password: ")
inputPass = read("*")
end
rs.setOutput("left", true)
os.sleep(2)
rs.setOutput("left", false)
inputPass = ""
end
And if you want that "Incorrect password" functionality you can just add it like this
Spoiler
os.pullEvent = os.pullEventRaw
local password = "myPersonalPassword"
local inputPass = "1983274569sdfhb3825698sdfh2544051976134985rifghs"
while true do
while inputPass ~= password do
if inputPass ~= "1983274569sdfhb3825698sdfh2544051976134985rifghs" then
term.setCursorPos(1,1)
term.clearLine()
print("Incorrect password!")
sleep(2)
end
term.setCursorPos(1,1)
term.clearLine()
write("Password: ")
inputPass = read("*")
end
rs.setOutput("left", true)
os.sleep(2)
rs.setOutput("left", false)
inputPass = "1983274569sdfhb3825698sdfh2544051976134985rifghs"
end
Why I'm setting the variable inputPass to 1983274569sdfhb3825698sdfh2544051976134985rifghs is because so that no-one will guess it, unless they're incredibly lucky.
I'm using it to check if the input is NOT equal to ↑ then print "Incorrect password!".
136 posts
Posted 10 October 2012 - 09:35 PM
rant
There is always something you can do to improve your code, never call it 'complete', there are always more features, more robust encryption, efficiency increases…
/rant
To be fair, there isn't much you can do to improve this code. Change print("password") to write("password") is the only thing I can see and it's only a personal choice. Other than that, start sticking features into it, use bit shifting to encrypt your password and store the encryption into a file, so when you enter a password encrypt it in the same way, if it matched the stored encryption it is correct (only need it if anyone else can get access to the code). Tie passwords to user names. Lock it out after 3 failures. You get the idea…
8543 posts
Posted 10 October 2012 - 11:15 PM
Adding unnecessary features is not improving code.
To OP: You've got some incorrect capitalization on that first line. The P in pullEvent (and -Raw) should be lowercase.