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

I'm trying to make a password program that only gives you a few tries and then you die.

Started by Silent_Devil, 06 February 2016 - 08:59 AM
Silent_Devil #1
Posted 06 February 2016 - 09:59 AM
I'm trying to make a password program that only gives you a few tries and then you die, but I can't seem to get a counter to work!? Pleas Help! Here's my code

term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)

function welcome()

term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
rednet.open(left)

local i = 3

term.setCursorPos(3,1)
print("Welome, please enter the password")

end

function correct()

term.setCursorPos(5,1)
term.setTextColor(colors.lime)
print("Correct! Opening Door!")
rs.setOutput("back", true)
sleep(2)
rs.setOutput("back", false)
welcome()
end

function incorrect()

term.setCursorPos(5,1)
term.setTextColor(color.red)
print(" Incorrect! Try Again!") then
local i= i-1

welcome()

if local i == 0 then

term.setCursorPos(7,1)
term.setTextSize(2)

print("YOU HAVE FAILED! YOU WILL NOW DIE!")
rednet.broadcast("die")
os.shutdown()

end

welcome()

input = read()

if input == "Illuminati" then

correct() else

incorrect()


What am I doing wrong?
Edited on 06 February 2016 - 09:00 AM
Luca_S #2
Posted 06 February 2016 - 10:11 AM
Please put every code in a code block or pastebin( I did that now, click here ). Also saving the password in plain text is not a good idea. This is not protected against CTRL + T. But now to the code fixing(I intended the code, so it is easier to understand):


term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
function welcome()
  term.clear()
  term.setCursorPos(1,1)
  term.setTextColor(colors.white)
  rednet.open(left)
This won't work, the correct way would be

rednet.open("left")
Let's go on.

  local i = 3
  term.setCursorPos(3,1)
  print("Welome, please enter the password")
end
function correct()
  term.setCursorPos(5,1)
  term.setTextColor(colors.lime)
  print("Correct! Opening Door!")
  rs.setOutput("back", true)
  sleep(2)
  rs.setOutput("back", false)
  welcome()
end
function incorrect()
  term.setCursorPos(5,1)
  term.setTextColor(color.red)
Also it is colors.red

  print(" Incorrect! Try Again!") then
Edit: WHAT? Why did you put a "then" there?
Just remove that.

  local i= i-1
This will also have problems, you defined i LOCAL in welcome(), that means it is only accessible in welcome(), you should put the local i = 3 at the beginning of your code and then use

i = i -1
here, you will also need to remove "local i = 3" from welcome

  welcome()
  if local i == 0 then
Ok, here you put a local before the i, you only need to use local when you first create the variable,

  if i == 0 then
would be correct here

  term.setCursorPos(7,1)
  term.setTextSize(2)
Textsize only exists for monitors, remove that.

  print("YOU HAVE FAILED! YOU WILL NOW DIE!")
  rednet.broadcast("die")
  os.shutdown() --Just a little notice, this will shutdown the computer before you saw the YOU HAVE FAILED! Message, consider adding a sleep()
end
welcome()
input = read()
if input == "Illuminati" then
  correct() else
  incorrect()
you are missing an

end
here, you need to add that.
Also around the last bit you might want to add this:

while true do
  welcome()
  input = read()
  if input == "Illuminati" then
	correct() else
	incorrect()
  end
end

I am for several reasons not giving you the whole code fixed, apply what I told you to your code, then it will work.
Edited on 06 February 2016 - 09:24 AM
Silent_Devil #3
Posted 07 February 2016 - 11:25 PM
Thanks for the help man! As you can probably gather, I'm fairly new to organising code and using "local" variables!
Mr_Programmer #4
Posted 08 February 2016 - 01:22 PM
Thanks for the help man! As you can probably gather, I'm fairly new to organising code and using "local" variables!

There is not much difference between a local and a global variable, "local" variables or for the program its writen in and global variables can be used across programs
Lupus590 #5
Posted 08 February 2016 - 01:44 PM
Thanks for the help man! As you can probably gather, I'm fairly new to organising code and using "local" variables!

There is not much difference between a local and a global variable, "local" variables or for the program its writen in and global variables can be used across programs

it's a bit more complex than that:

iAmAGlobal = 1 --#usable across programs
local iAmLocalToProgram = 1 --#is nil outside of this file
while true do
  local iAmLocalToLoop = 1 --#is nil outside of loop and file
  break
end
print(iAmLocalToLoop) --# this outputs nil

you may want to read about scope: http://lua-users.org/wiki/ScopeTutorial
Edited on 08 February 2016 - 12:45 PM
TheOddByte #6
Posted 08 February 2016 - 03:10 PM
Thanks for the help man! As you can probably gather, I'm fairly new to organising code and using "local" variables!

There is not much difference between a local and a global variable, "local" variables or for the program its writen in and global variables can be used across programs

it's a bit more complex than that:

iAmAGlobal = 1 --#usable across programs
local iAmLocalToProgram = 1 --#is nil outside of this file
while true do
  local iAmLocalToLoop = 1 --#is nil outside of loop and file
  break
end
print(iAmLocalToLoop) --# this outputs nil

you may want to read about scope: http://lua-users.org...i/ScopeTutorial
The last line would not output nil, it would output what you declared the local variable as outside of the loop

local str = "bar"
while true do
	local str = "foo"
	print( str ) --# output: foo
	break
end
print( str ) --# output: bar

Edit: Nevermind, noticed that you had different names for the variables.
Edited on 08 February 2016 - 02:11 PM
Lupus590 #7
Posted 08 February 2016 - 04:03 PM

local str = "bar"
while true do
	local str = "foo"
	print( str ) --# output: foo
	break
end
print( str ) --# output: bar

that code is still a valid example, defining a new local variable with the same name doesn't overwrite the outer one
however this would overwrite the outer local
local str = "bar"
while true do
   str = "foo" --# note the lack of local
	  --# this means that we are using whatever is up/out in scope
	  --# if we didn't define one then it defaults to global
	print( str ) --# output: foo
	break
end
print( str ) --# output: foo

another example this time with globals in loops

while true do
  str = "foo"
  break
end
print(str) --#output: foo not nil 

it should be noted that loops are not the only form of scope, if statements and functions are also 'scope boundaries' (may not be the correct term), there may be more too, check the link I gave two posts up
Edited on 08 February 2016 - 03:08 PM