Posted 27 September 2014 - 08:56 PM
I am relatively new to programming, so this problem has an easy solution, I'm sure. I'm making a task list program that displays the current task on an attached monitor and lets you cycle through the tasks by clicking a button. I was tinkering with the program and suddenly it quits after running the main loop once and I can't figure out why. Nothing I added would seem to cause it to do that. If anyone is willing to look at the code and see if they can figure out why, I'd be very grateful.
-- Task List program
-- Requires an advanced monitor attached to the right side. Keeps a task list. Displays one task at a time on the monitor. Can click
-- "Next" on the monitor to cycle to the next task or "Done" to mark that task as done and delete it from the list. On the computer
-- terminal, can type in a new task or quit the program.
-- Global Variables:
-- currentTask
-- taskList
-- runNextLoop
-- monitor
-- Return the number of the last task in the list
function LastTask()
local iteration = 1
while taskList[iteration] ~= nil do
iteration = iteration + 1
end
return iteration
end
-- Check for and attach monitor
function AttachMonitor()
monitor = peripheral.wrap("right")
return true
end
-- Check for and attach disk drive (not implemented yet)
-- Load task list from disk drive (not implemented yet)
-- Display menu on computer terminal
function DrawComputerMenu()
-- Computer terminal screen is 51 x 19
term.clear()
term.setCursorPos(1, 2)
write("***************************************************")
write("* Jerry's Task List *")
write("***************************************************")
write("\n\n")
write("A)dd a task\n")
write("S)ave the task list\n")
write("Q)uit this program\n")
write("\n")
write("Please select a menu choice.")
end
-- Draw the banner at the top of the monitor that says "CURRENT JOB"
function DrawBanner()
monitor.setCursorPos(1, 1)
monitor.setBackgroundColor(colors.red)
monitor.write(" CURRENT JOB ")
monitor.setBackgroundColor(colors.black)
end
-- Draw the buttons at the bottom of the monitor that say "NEXT" and "DONE"
function DrawButtons()
monitor.setBackgroundColor(colors.green)
monitor.setCursorPos(2, 8)
monitor.write(" NEXT ")
monitor.setCursorPos(13, 8)
monitor.write(" DONE ")
monitor.setBackgroundColor(colors.black)
end
-- Write the current task in the center of the monitor
function DrawTask()
monitor.setCursorPos(2, 3)
monitor.write(taskList[currentTask])
end
-- Refresh the monitor with banner, task, and buttons
function DrawMonitor()
-- Monitor screen at 1.5 text scale is 19 x 8
monitor.setTextScale(1.5)
DrawBanner()
DrawButtons()
DrawTask()
end
-- Add task
function AddTask()
currentTask = lastTask()
term.setCursorPos(1, 12)
write("Please type in the task you wish to add.")
term.setCursorPos(1, 14)
taskList[currentTask] = read()
term.setCursorPos(1, 16)
write("Got it! The task was added to the end of the list!")
os.sleep(2)
end
-- Save task list
function SaveTaskList()
-- Not implemented yet
end
-- Delete task from list
function DeleteTask()
local endOfTasks = LastTask()
for i = currentTask, endOfTasks do
taskList[i] = taskList[i + 1]
end
taskList[endOfTasks] = nil
currentTask = currentTask - 1
if currentTask < 1 then
currentTask = 1
end
end
runNextLoop = true
local isMonitorAttached = AttachMonitor()
if isMonitorAttached == false then
print("Please ensure that an advanced monitor is attached!")
runNextLoop = false
end
taskList = {}
currentTask = 1
-- Main loop
while runNextLoop == true do
DrawComputerMenu()
DrawMonitor()
local event, param1, param2, param3 = os.pullEvent()
if event == "key" then
if param1 == 30 then
AddTask()
elseif param1 == 31 then
SaveTaskList()
elseif param1 == 16 then
runNextLoop = false
else
term.setCursorPos(1, 12)
write("Please press a key corresponding to a menu choice or click a monitor button!")
os.sleep(2)
end
elseif (event == "mouse_click") and (param1 == 1) then
if (param2 == 8) then
if (param3 > 1) and (param3 < 8) then
currentTask = currentTask + 1
if currentTask > LastTask() then currentTask = 1 end
elseif (param3 > 12) and (param3 < 19) then
DeleteTask()
end
end
end
end