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:37 PM
Jacob7395 #1
Posted 14 May 2012 - 03:37 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?


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)
Bossman201 #2
Posted 17 May 2012 - 03:23 AM
1. You could put
Spoiler

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

function ScreenClear()
	term.clear()
	term.setCursorPos(1, 1)
end
into a function and call that function instead and that by itself would eliminate a lot of unnecessary lines.

2. I must commend you on program formatting for your first time. It's not all that great, but really good for a beginner programmer. Typically every time I write a function, loop, or if then statement, the next line will have 4 spaces before code. This effect is cumulative when using nested loops, and if statements.
Spoiler

function a()
if x == 1 then
code code
if y >= u then
code code
end
else
while true do
code code
if x <= 27
code code code
end
end
end
end
COULD be


function a()
	if x == 1 then
		code code
		if y >= u then
			code code
		end
	else
		while true do
			code code
				if x <= 27
					code code code
				end
		end
	end
end
I've edited this several times and the formatting won't stick but I think this is enough of an example for you to understand.

3. Don't forget that the "term.setCursorPos(1, 1)" command has a use and isn't just for show. If you change the (x, y) coordinates it will start printing from where you set. Also, writing a for statement can cut down on typing excess symbols for a border. You could also just do all that in a function and call that function.
Spoiler

print("----------------------------------------------")
print("|						Door timer settings			  |")
print("|							   By: Jacob7395					|")
print("----------------------------------------------")
print("|																				  |")
print("| Set door timer to (s):										|")
print("|																								 |")
print("|																				  |")
print("|																				  |")
print("|																				  |")
print("|																				  |")
print("----------------------------------------------")
COULD be

function screen1()
	for i = 1, 18 do
		term.setCursorPos(1, i)
		term.write("|")
		term.setCursorPos(50, i)
		term.write("|")

	end

	for i = 1, 50 do
		term.setCursorPos(i, 1)
		term.write("-")
		term.setCursorPos(i, 4)
		term.write("-")
		term.setCursorPos(i, 18)
		term.write("-")
	end
	term.setCursorPos(26, 2)
	term.write("Door Timer Settings")
	term.setCursorPos(33, 5)
	term.write("By: Jacob7395")
	term.setCursorPos(2, 8)
	term.write("Set door timer to (s): ")
The computer screens dimensions are 50(x) by 18(y). It's a lot more complicated this way, but it will eliminate all need to hit —————– and use all those print commands. You could also remove the "term.setCursorPos()" and "term.write()" commands to have an empty border and add the print/term.write() commands as you hit those areas in the program.

4. You can (and definitely should) use for statements to reduce repetitive typing where possible.
Spoiler

if password == userpassword then
    term.write("Access Granted")
    sleep(1)
    term.write(".")
    sleep(1)
    term.write(".")
    sleep(1)
    term.write(".")
    sleep(1)
    break
else
    term.write("Incorrect Information")
    sleep(1)
    term.write(1)
    sleep(1)
    term.write(1)
    sleep(1)
    term.write(1)
    sleep(1)
end
COULD be

if password == userpassword then
	term.write("Access Granted")
	for i = 1, 4 do
		term.write(".")
		sleep(1)
else
	term.write("Incorrect Information")
	for i = 1, 4 do
		term.write(".")
		sleep(1)
end

5. You have a lot of repetitive commands in your program.
Spoiler

ScreenClear()
print("Welcome to the Door Control Program")
print("")
print("This program will allow you to securely control the door.")
print("")
sleep(2)
term.write("First you must set the username and password.")

sleep(2)
term.write(".")

sleep(2)
term.write(".")

sleep(2)
term.write(".")

sleep(2)
term.write(".")

sleep(2)
term.write(".")
ScreenClear()
setpassword()
print("Username and Password set.")
print("")
term.write("Processing to door control panel, please wait.")

sleep(2)
term.write(".")

sleep(2)
term.write(".")

sleep(2)
term.write(".")
sleep(2)
ScreenClear()
interface()
ScreenClear()
COULD be

ScreenClear()
for i = 1, 10 do
	if i == 1 then print("Welcome to the Door Control Program.") end
	if i == 3 then print("This program will allow you securely control the door.") end
	if i == 5 then print("First you must set the username and password.") end
	print("")
	sleep(1)  --Your sleep() commands also don't need to be so long, at a longer one at the end instead.
end
ScreenClear()
setpassword()
print("Username and Password set.")
print("")
term.write("Processing door control pannel, please wait")
for i = 1, 3
	sleep(1)
	term.write(".")
end
ScreenClear()
interface()
ScreenClear()

EDIT: Fixed minor errors in spelling and forum formatting
EDIT2: Forum is going crazy on my post.
Edited on 17 May 2012 - 01:41 PM