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

How can I make this program better

Started by umbcorp, 19 August 2012 - 02:18 PM
umbcorp #1
Posted 19 August 2012 - 04:18 PM
I'm writing a program to control the systems in my warehouse. I guess my code is sloppy, is there any way that I can make it more organized ? like using functions and such ? help or examples would be great!


force = "1"
power = "2"
gate = "3"
four = "4"
five = "5"
resource = "4"
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("--------------------------------------------------")
  print("WAREHOUSE CONTROL SYSTEM")
  print("--------------------------------------------------")
  print("Choose an option")
  print("")
  print("[1] Force Shield")
  print("[2] Power")
  print("[3] Gate Systems")
  print("[4] Resource Management")
  print("")
  print("--------------------------------------------------")
  print("--------------------------------------------------")
  term.setCursorPos(1,13)
  input = read()
   if input == force then
    term.clear()
    term.setCursorPos(1,1)
    print("--------------------------------------------------")
    print("FORCEFIELD SYSTEM")
    print("--------------------------------------------------")
    print("")
    print("[1] Turn On EU Injection")
    print("[2] Turn OFF EU injection")
    print("[3] Turn On FORCESHIELD")
    print("[4] Turn OFF FORCESHIELD")
    print("[5] Main Menu")
    print("")
    print("--------------------------------------------------")
    print("--------------------------------------------------")
    term.setCursorPos(1,13)
    input = read()
	 if input == force then
	  ---- code here
	  elseif input == power then
	  ----- code here
	  elseif input == gate then
	  ----- code here
	  elseif input == four then
	  ----- code here
	  elseif input == five then
	  ----- code here
	 end
   elseif input == power then
    term.clear()
    term.setCursorPos(1,1)
    print("--------------------------------------------------")
    print("POWER CONTROL SYSTEM")
    print("--------------------------------------------------")
    print("Choose an option")
    print("")
    print("[1] Turn On Reactor")
    print("[2] Turn Off Reactor")
    print("[3] Block Energy Transfer")
    print("[4] Unblock Energy Transfer")
    print("[5] Exit")
    print("")
    print("--------------------------------------------------")
    print("--------------------------------------------------")
    term.setCursorPos(1,14)
    input = read()
	 if input == force then
	  ---- code here
	  elseif input == power then
	  ----- code here
	  elseif input == gate then
	  ----- code here
	  elseif input == four then
	  ----- code here
	  elseif input == five then
	  ----- code here
	 end
   elseif input == gate then
    term.clear()
    term.setCursorPos(1,1)
    print("--------------------------------------------------")
    print("GATE CONTROL SYSTEM")
    print("--------------------------------------------------")
    print("Choose an option")
    print("")
    print("[1] Open all gates")
    print("[2] Close all gates")
    print("[3] Seal all gates")
    print("[4] Unseal all gates")
    print("[5] Exit")
    print("")
    print("--------------------------------------------------")
    print("--------------------------------------------------")
    term.setCursorPos(1,14)
    input = read()
	 if input == force then
	  ---- code here
	  elseif input == power then
	  ----- code here
	  elseif input == gate then
	  ----- code here
	  elseif input == four then
	  ----- code here
	  elseif input == five then
	  ----- code here
	 end
   elseif input == four then
    term.clear()
    term.setCursorPos(1,1)
    print("--------------------------------------------------")
    print("RESOURCE CONTROL SYSTEM")
    print("--------------------------------------------------")
    print("Choose an option")
    print("")
    print("[1] Turn On sorting Machine")
    print("[2] Turn Off Sorting Machine")
    print("[5] Exit")
    print("")
    print("--------------------------------------------------")
    print("--------------------------------------------------")
    term.setCursorPos(1,12)
    input = read()
	 if input == force then
	  ---- code here
	  elseif input == power then
	  ----- code here
	  elseif input == five then
	  ----- code here
	 end 
   end  
end
Noodle #2
Posted 19 August 2012 - 04:48 PM
You could put functions around the designs so you just have to put resCtrl() for the resource control, and do so for the other functions.
Mmmmmmmm #3
Posted 19 August 2012 - 05:05 PM
Put quotes around the strings when checking input. (e.g. input == "five").

You could also make it function regardless of resolution, so that the lines of dashes cover the entire screen no matter the width. This could be done like so:
string.rep("-",w)
Where w is the width.