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

Program simplafacation

Started by Jacob7395, 14 May 2012 - 01:39 PM
Jacob7395 #1
Posted 14 May 2012 - 03:39 PM
I've made my first ever program on Computercraft and in any programing language. Its allows you to open a door for a spsific and lets you set a user name and pasword to lock the commputer. apart from the bad splling the program works how i want it to but I know it can be smplafied and improved. So i guess my question is how?

This is my code apart form the interfaces sqrewing up a litte it looks fine :P/>/>


term.clear()
term.setCursorPos(1,1)
x = 10
function resetpassword()
while true do

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

  write ('Set User Name: ')
  rename = io.read()
  write ('set Password : ')
  repassword = io.read()
  print (' ')
  print ('Youre User Name is '..rename)
  print ('Youre password is '..repassword)
  print (' ')
  write ('Do you conferm this? ')
  passwordconferm = io.read()
   if passwordconferm == 'yes' then
	print (' ')
	print ('Infomation confermed')
	rename = username
	repassword = userpassword
	sleep (2.5)
	term.clear()
	term.setCursorPos(1,1)
	break
   else
	print (' ')
	print ('Infomation is incorrect')
	print (' ')
	write ('Do you wish to repeat reset? ')
	repeatpasswordset = io.read()
	if repeatpasswordset == 'yes' then
	 print (' ')
	 else
	 break
	end
  
   end
end
end
function opendoor()
term.clear()
term.setCursorPos(1,1)
  sleep(0.5)
  redstone.setOutput('left', true)
   while x >-1 do
	print (x)
	sleep(1)
	x = x - 1
   end
  redstone.setOutput('left', false)
  sleep(1)
term.clear()
term.setCursorPos(1,1)
sleep(1)
end
function interface()
while true do
  term.clear()
  term.setCursorPos(1,1)
  print "----------------------------------------------"
  print "|			  Door conrtrol panel		   |"
  print "|				 By: Jacob7395			  |"
  print "----------------------------------------------"
  print "|											|"
  print "| 1. open door							   |"
  print "| 2. Lock panel							  |"
  print "| 3. Settings								|"
  print "| 4. exit									|"
  print "|											|"
  print "|											|"
  print "----------------------------------------------"
  event, param1, param2, param3 = os.pullEvent()
  if event == "char" and param1 == "1" then opendoor()  end
  if event == "char" and param1 == "2" then password()  end
  if event == "char" and param1 == "3" then interfacesettings()  end
  if event == "char" and param1 == "4" then break end
end
end
function setpassword()
while true do

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

  write ('Set User Name: ')
  setname = io.read()
  write ('set Password : ')
  setpassword = io.read()
  print (' ')
  print ('Youre User Name is '..setname)
  print ('Youre password is '..setpassword)
  print (' ')
  write ('Do you conferm this? ')
  passwordconferm = io.read()
   if passwordconferm == 'yes' then
	print (' ')
	print ('Infomation confermed')
	username = setname
	userpassword = setpassword
	sleep (2.5)
	term.clear()
	term.setCursorPos(1,1)
	break
   else
	print (' ')
	print ('Infomation is incorrect')
	print (' ')
	print ('User name and password must be seet ')
				sleep(2)
   end
  
end
end
function interfacesettings()
while true do
   term.clear()
   term.setCursorPos(1,1)
   print "----------------------------------------------"
   print "|		 Door conrtrol panel settings	   |"
   print "|				 By: Jacob7395			  |"
   print "----------------------------------------------"
   print "|											|"
   print "| 1. open door timer						 |"
   print "| 2. reset username and password			 |"
   print "| 3. exit									|"
   print "|											|"
   print "|											|"
   print "|											|"
   print "----------------------------------------------"
   event, param1, param2, param3 = os.pullEvent()
   if event == "char" and param1 == "1" then
	setdoortimer()
   end
   if event == "char" and param1 == "2" then
	resetpassword()
   end
   if event == "char" and param1 == "3" then break end
  end
end
function setdoortimer()
  term.clear()
  term.setCursorPos(1,1)
  print "----------------------------------------------"
  print "|			  Door timer settings		   |"
  print "|				 By: Jacob7395			  |"
  print "----------------------------------------------"
  print "|														|"
  print "| Set door timer to (s):					 |"
  print "|															   |"
  print "|											|"
  print "|											|"
  print "|											|"
  print "|											|"
  print "----------------------------------------------"
  term.setCursorPos(26,6)
  write ('')
  x = io.read()
  x = tonumber(x)
end
function password()
while true do
term.clear()
term.setCursorPos(1,1)
  write ('User Name: ')
  name = io.read()
   if name == username then
	write ('Password: ')
	password = io.read()
	 if password == userpassword then
	  write ('access granted')
	  sleep(1)
	  write ('.')
	  sleep(1)
	  write ('.')
	  sleep(1)
	  print ('.')
	  sleep(1)
	  break
	 else
	  write ('incorrect information')
	  sleep(1)
	  write ('.')
	  sleep(1)
	  write ('.')
	  sleep(1)
	  print ('.')
	  sleep(1)
	 end
   else
	write ('incorrect information')
	sleep(1)
	write ('.')
	sleep(1)
	write ('.')
	sleep(1)
	print ('.')
	sleep(1)
   end
end
term.clear()
term.setCursorPos(1,1)
end
term.clear()
term.setCursorPos(1,1)
print ('Welcome to the door control program')
print (' ')
sleep(2)
print ('This program will allow you to securly control the door')
print (' ')
sleep(2)
write ('Fisrt you must set the User name and password')
sleep(2)
write ('.')
sleep(2)
write ('.')
sleep(2)
write ('.')
sleep(2)
write ('.')
sleep(2)
write ('.')
term.clear()
term.setCursorPos(1,1)
setpassword()
print ('User name and Password set')
print (' ')
write ('Processing to door controle pannle pleace wait')
sleep(2)
write ('.')
sleep(2)
write ('.')
sleep(2)
write ('.')
sleep(2)
term.clear()
term.setCursorPos(1,1)
interface()
term.clear()
term.setCursorPos(1,1)
Grim Reaper #2
Posted 15 May 2012 - 04:45 AM
Perhaps instead of repeatedly writing sleep(2) and writing ('-') to simulate loading, you could write it as a function.

Maybe something like this:

function drawWait(timesDrawn, time, character) -- how many times you want to draw the character, time to wait and the character written
for i = 0, timesDrawn do
  sleep(time); write(tostring(character));
end
end
drawWait(3, 2, '-') -- Output being: -(slept 2)-(slept 2)-

And maybe add a clear() function instead of term.clear(); term.setCursorPos(1,1)

function clear() term.clear(); term.setCursorPos(1,1) end

Also, you could try printing your sentence and space on the next line in the same statement

--[[
print("SENTENCE GOES HERE")
print(" ")
-- ]] --Replacing this!
print("SENTENCE GOES HEREn ")
-- The n will tell LUA to jump to the next line and then print whatever is after it. This is the
-- reason for having to write 2 '' to print one on the screen. There is a whole collection of
-- '+character' to create easy text manipulations. An example being 't' which will give an indent.
Luanub #3
Posted 15 May 2012 - 06:00 AM
Instead of doing this..


function clear() term.clear(); term.setCursorPos(1,1) end

Just do

shell.run("clear")
Jacob7395 #4
Posted 15 May 2012 - 07:11 AM
Thank ill try all of this stuff :P/>/>
MysticT #5
Posted 15 May 2012 - 03:38 PM
Instead of doing this..


function clear() term.clear(); term.setCursorPos(1,1) end

Just do

shell.run("clear")
I don't think that's better. Adding just 4 lines of code at the top of the program allows you to write just clear() (only 7 characters), instead of shell.run("clear") (18 characters). I know you could just copy it, but even if it were longer, it's better to use the function. Just check this post for some explanations.