33 posts
Posted 12 June 2013 - 12:45 PM
Hi,
i am learning computercraft programming and i decided to try and modify the password lock doors program. I want it to read out failed attempt(number of attempts go here) then once 3 wrong attempts have a curred it will send a redstone signal to a tnt block thus destroying the house and the person trying to enter the password. However i'm not sure how to make it print out the attempts the rest I can do my self, i think.
8543 posts
Posted 12 June 2013 - 01:50 PM
Split into new topic.
You are in the Members group and should have been able to post your own topic.
645 posts
Location
'Merica
Posted 12 June 2013 - 03:57 PM
well you'll need to do something like this. So when the password is wrong it adds 1 to itself, and when it reaches 3, with your code, it will make the house and the person trying to get in blow up. And when the password is correct it resets the attempts to 0, so you wont blow up your house if the password fails three times, three different times.
attempts = 0
while true do
write('Password: ')
local pass = read('*')
if pass == '123' then
attempts = attempts - attempts
--do stuff when correct.
elseif pass ~= '123' then
attempts = attempts + 1
elseif attempts >= 3 then
--do house blow up stuff
end
end
7083 posts
Location
Tasmania (AU)
Posted 12 June 2013 - 07:47 PM
Might want to edit that - the "blow up stuff" bit will never get called unless you completely end the previous if/elseif block and then start a new one.
49 posts
Posted 12 June 2013 - 09:19 PM
Here is the fixed code:
attempts = 0
pass = "CorrectPassword"
while true do
term.clear()
term.setCursorPos(1,1)
print("Attempts: "..attempts.."/3") --# The ..attempts.. concatenate the string with the string the variable contains
write("Password: ")
local input = read('*')
if input == pass then
attempts = 0
--open door
elseif attempts >= 3 then
--boom boom
else
attempts = attemps + 1
end
end
Useful link:
http://computercraft.info/wiki/Concatenation
33 posts
Posted 13 June 2013 - 05:06 PM
Thanks so much for the help, i've managed to get it to work perfectly heres the finished product in action and the full finished code.
https://www.youtube.com/watch?v=7dgMvh6KYfMand the code which could probably be cleaned up slightly but I'm rubbish at coding:
local atm = 0
while true do
term.clear()
term.setCursorPos(1, 1)
write("enter password: ")
ps = read()
if ps == "123" then
atm = atm - atm
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", false)
else
atm = atm + 1
print(atm.." attempts")
sleep(1)
if atm >=3 then
redstone.setOutput("back", true)
end
end
end