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

Need Help With Debugging Program.

Started by TheeIntrepidGamer, 20 November 2013 - 02:57 PM
TheeIntrepidGamer #1
Posted 20 November 2013 - 03:57 PM
This program I've been working on for a day or two now has been going smooth until one, or two bugs came about. It's supposed to be a configurable locking program for doors/computers alike. The whole program works fine if I try to lock a computer, but if I try to lock a door the program won't pick up that I'm trying to lock the door. It won't say that it's a locked door, instead I've put in an 'else' where it says 'debug menu', which means it isn't recognizing that I've selected it to lock a 'door' which is specified in the 'PassLockFiles/DoT' file. I've tried changing variable names, and have added 'else' statements to the ends of all my 'if' statements to try, and work out where the bug is, but I've had no luck. I've overlooked this code for 2 days now trying to find these bugs. It would be of great help if someone could assist me in finding them. :)/>

I will post some screenshots, and the code below.

Pastebin:
http://pastebin.com/umqCpkmH
(Some of the spacing near the bottom got messed up due to me copying the code over
from Notepad++ to pastebin.)

Spoiler

--[[Variables]]--
w,h = term.getSize()
x = 0
y = 0
z = 0
f2 = 0
f = 0
g = 0
--[[Functions]]--
function clear()
  term.clear()
end
function write(str,x2,y2)
  term.setCursorPos(x2,y2)
  term.write(str)
end
function cwrite(str,y2)
  term.setCursorPos(w/2 - #str/2,y2)
  term.write(str)
end
function title(str)
  cwrite(string.rep("-",w),1)
  cwrite(str,2)
  cwrite(string.rep("-",w),3)
end
function firstBootTitle()
  cwrite(string.rep("-",w),1)
  cwrite("<Installer Setup>",2)
  cwrite(string.rep("-",w),3)
end
function optionsTitle()
  cwrite(string.rep("-",w),1)
  cwrite("<Options>",2)
  cwrite(string.rep("-",w),3)
end
function DoTOptions()
  cwrite("What will you be locking today?",5)
  cwrite("Press 'A' for 'Computer'.",7)
  cwrite("Press 'B' for 'Door'.",8)
  while x == 0 do
	eva,p1a = os.pullEvent("char")
	if p1a == "a" then
	  hel = fs.open("PassLockFiles/DoT","w")
	  hel.write("term")
	  hel.close()
	  x = x + 1
	elseif p1a == "b" then
	  hel = fs.open("PassLockFiles/DoT","w")
	  hel.write("door")
	  hel.close()
	  x = x + 1
	end
  end
end
function doorSideOption()
  cwrite("What side is your door on?",5)
  cwrite("Press 'A' for 'Left'.",7)
  cwrite("Press 'B' for 'Right'.",8)
  cwrite("Press 'C' for 'Top'.",9)
  cwrite("Press 'D' for 'Bottom'.",10)
  hbl = fs.open("PassLockFiles/doorSide","w")
  while y == 0 do
	ev,p1 = os.pullEvent("char")
	if p1 == "a" then
	  hbl.write("left")
	  y = y + 1
	elseif p1 == "b" then
	  hbl.write("right")
	  y = y + 1
	elseif p1 == "c" then
	  hbl.write("top")
	  y = y + 1
	elseif p1 == "d" then
	  hbl.write("bottom")
	  y = y + 1
	end
  end
  hbl.close()
end
function passwordOption()
  while z == 0 do
	clear()
	title("<PASSWORD CENTER>")
	term.setCursorPos(3,7)
	term.write("NEW PASSWORD: ")
	chpass = read("*")
	term.setCursorPos(3,9)
	term.write("RETYPE PASSWORD: ")
	ch2pass = read("*")
	if chpass == ch2pass then
	  clear()
	  title("<PASSWORD SET!>")
	  hb = fs.open("PassLockFiles/PWFile","w")
	  hb.write(ch2pass)
	  hb.close()
	  sleep(2)
	  clear()
	  z = z + 1
	else
	  clear()
	  title("<PASSWORDS DON'T MATCH. TRY AGAIN.>")
	  sleep(2)
	  clear()
	end
  end
  z = z - 1
end
function optionMenu()
  while f2 == 0 do
	clear()
	title("<OPTIONS>")
	cwrite("Press 'A' to change password.",7)
	cwrite("Press 'E' to exit.",9)
	ev,p1 = os.pullEvent("char")
	if p1 == "a" then
	  passwordOption()
	  clear()
	elseif p1 == "e" then
   f2 = f2 + 1
	  clear()
	end
  end
  f2 = f2 - 1
end
function openDoor(side)
  rs.setOutput(side,true)
  sleep(2)
  rs.setOutput(side,false)
end
--[[Startup Checks]]--
if fs.exists("PassLockBasic") == false then
  fs.copy(shell.getRunningProgram(),"PassLockBasic")
  su = fs.open("startup","w")
  su.write("shell.run(\"PassLockBasic\")")
  su.close()
  un = fs.open("uninstallPLB","w")
  un.writeLine("--[[Variables]]--")
  un.writeLine("w,h = term.getSize()")
  un.writeLine("--[[Functions]]--")
  un.writeLine("function cwrite(str,y)")
  un.writeLine("  term.setCursorPos(w/2 - #str/2,y)")
  un.writeLine("  term.write(str)")
  un.writeLine("end")
  un.writeLine("term.clear()")
  un.writeLine("cwrite(\"Uninstalling...\",8)")
  un.writeLine("fs.delete(\"PassLockBasic\")")
  un.writeLine("fs.delete(\"PassLockFiles\")")
  un.writeLine("fs.delete(\"startup\")")
  un.writeLine("sleep(3)")
  un.writeLine("term.clear()")
  un.writeLine("cwrite(\"Uninstalled!\",8)")
  un.writeLine("sleep(1.5)")
  un.writeLine("term.clear()")
  un.writeLine("term.setCursorPos(1,1)")
  un.close()
end
if fs.exists("PassLockFiles") == false then
  fs.makeDir("PassLockFiles")
end
if fs.exists("PassLockFiles/DoT") == false then
  h15 = fs.open("PassLockFiles/DoT","w")
  h15.close()
end
if fs.exists("PassLockFiles/firstBoot") == false then
  h = fs.open("PassLockFiles/firstBoot","w")
  h.write("1")
  h.close()
end
if fs.exists("PassLockFiles/doorSide") == false then
  h13 = fs.open("PassLockFiles/doorSide","w")
  h13.close()
end
if fs.exists("PassLockFiles/PWFile") == false then
  h14 = fs.open("PassLockFiles/PWFile","w")
  h14.close()
end
ha = fs.open("PassLockFiles/firstBoot","r")
if ha.readAll() == "1" then
  ha.close()
  ha2 = fs.open("PassLockFiles/firstBoot","w")
  ha2.write("0")
  ha2.close()
  clear()
  firstBootTitle()
  DoTOptions()
  clear()
  ha3 = fs.open("PassLockFiles/DoT","r")
  if ha3.readAll() == "door" then
	ha3.close()
	firstBootTitle()
	doorSideOption()
  end
  passwordOption()
end
clear()
term.setCursorPos(1,1)
--[[Locking Code]]--

while f == 0 do
  d3 = fs.open("PassLockFiles/DoT","r")
  h3 = fs.open("PassLockFiles/PWFile","r")
  if d3.readAll() == "term" then
	title("<LOCKED TERMINAL>")
  elseif d3.readAll() == "door" then
	title("<LOCKED DOOR>")
  else
	title("<DEBUG MENU>")
  end
  d3.close()
  term.setCursorPos(3,7)
  term.write("PASSWORD: ")
  pass = read("*")
  if pass == h3.readAll() then
h3.close()
	while g == 0 do
	  clear()
	  title("<PASS LOCK MENU>")
	  cwrite("Press 'U' to unlock.",7)
	  cwrite("Press 'O' to goto options.",8)
	  ev,p1 = os.pullEvent("char")
	  if p1 == "u" then
g = g + 1
k2 = fs.open("PassLockFiles/DoT","r")
ds2 = fs.open("PassLockFiles/doorSide","r")
		if k2.readAll() == "term" then
		  f = f + 1
		  clear()
		  title("<TERMINAL UNLOCKED>")
		  sleep(2)
		  clear()
		  term.setCursorPos(1,1)
		elseif k2.readAll() == "door" then
		  title("<DOOR UNLOCKED>")
		  if ds2.readAll() == "left" then
			rs.setOutput("left",true)
	 sleep(2)
			rs.setOutput("left",false)
		  elseif ds2.readAll() == "right" then
			rs.setOutput("right",true)
			sleep(2)
	 rs.setOutput("right",false)
		  elseif ds2.readAll() == "top" then
			rs.setOutput("top",true)
	 sleep(2)
	 rs.setOutput("top",false)
		  elseif ds2.readAll() == "bottom" then
			rs.setOutput("bottom",true)
	 sleep(2)
	 rs.setOutput("bottom",false)
		  else
			cwrite("DOORSIDE ERROR.",7)
		  end
		  ds2.close()
		end
		k2.close()
	  clear()
	  elseif p1 == "o" then
		optionMenu()
	  end
	end
  else
	clear()
	cwrite("[ERROR]",9)
  end
end
clear()
title("<Booting Up CraftOS...>")
sleep(1.5)
cwrite("Welcome, User!",8)
sleep(1)
clear()
term.setCursorPos(1,1)

Screenshots


Spoiler
Door Mode Screenshots






Computer Mode Screenshots









Lyqyd #2
Posted 20 November 2013 - 04:21 PM
readAll is a one-shot thing, you can't check it again without closing the file handle and opening it again. Instead, you should put the contents of the file into a variable and check that variable to see if it contains "door" or "term".
TheeIntrepidGamer #3
Posted 20 November 2013 - 08:19 PM
Thanks, I'll get to work on fixing that.

EDIT: It's working great now, thanks. :D/>
Edited on 20 November 2013 - 07:57 PM