I am once again bored at work, doing a bit of programming. I am making a little program to allow me to switch the states of a few objects from on to off and back on. As I have almost always programmed the same way for the last few years, I would like a few suggestions on improving my code.
The functional part of this code hasn't been created. I have no issues doing that. What I would like to know is: Is there a better, more compact or more correct way to build the menu I have created. Is what I have solid, or am I missing a very obvious alternative that could improve my code?
Here is my code:
--defaults
alrm=1
lck=0
light=1
function getAlrm()
if alrm==1 then
alrm=0
return
end
if alrm==0 then
alrm=1
return
end
end
function getLck()
if lck==1 then
lck=0
return
end
if lck==0 then
lck=1
return
end
end
function getLight()
if light==1 then
light=0
return
end
if light==0 then
light=1
return
end
end
while true do
--::home::
if alrm==1 then
alrmState="Disable"
end
if alrm==0 then
alrmState="Enable"
end
if lck==1 then
lckState="Disable"
end
if lck==0 then
lckState="Enable"
end
if light==1 then
lightState="Disable"
end
if light==0 then
lightState="Enable"
end
print'Welcome to Base Control'
print'Please make a selection:'
print('1: '..alrmState..' Night Alarm')
print('2: '..lckState..' Base Lockdown')
print('3: '..lightState..' Base Lighting')
print'4: Exit'
print''
write'Selection:'
rInput=read()
rInput=tonumber(rInput)
if rInput==1 then
print(''..alrmState..'ing alarm!')
getAlrm()
end
if rInput==2 then
print(''..lckState..'ing lockdown!')
getLck()
end
if rInput==3 then
print(''..lightState..'ing lights!')
getLight()
end
if rInput==4 then
print'Closing!'
return
end
if rInput~=1 and rInput~=2 and rInput~=3 and rInput~=4 then
print'Invalid choice'
end
sleep(1)
term.clear()
term.setCursorPos(1,1)
end
Alternatively, here is the pastebin: http://pastebin.com/b8HPJCKE
Again, there is nothing functionally wrong with the menu. It works as intended.