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

help creating program

Started by riley5678, 23 June 2013 - 03:05 PM
riley5678 #1
Posted 23 June 2013 - 05:05 PM
hi i am making a program for a hotel me and friends have built but its not working right and im not sure why am i doing something wrong
ive put the code in the spoiler sorry its a bit long

Spoiler

diskSide = 'right'
bcaSide = "back"
bcbSide = "bottom"
room1 = redstone.testBundledInput(bcaSide, colors.white)
room2 = redstone.testBundledInput(bcaSide, colors.orange)
room3 = redstone.testBundledInput(bcaSide, colors.magenta)
room4 = redstone.testBundledInput(bcaSide, colors.yellow)
room5 = redstone.testBundledInput(bcaSide, colors.lime)
room6 = redstone.testBundledInput(bcaSide, colors.green)
roomempty = redstone.testBundledInput(bcaSide, colors.black)
print("Please insert your ticket into the machine")
print("and wait for it to load")
e = os.pullEvent('disk')
label = disk.getLabel(diskSide)
disk.eject(diskSide)
term.clear()
term.setCursorPos(1, 1)
if label == "Themepark Ticket Diamond" then
if roomempty == false then
print("Sorry all rooms are full")
print("Thank You for visiting the hotel")
sleep(3)
os.shutdown()
else
print("Your ticket includes a free room!")
print("would you like to book a room y/n")
inputd = read()
if inputd == "yes" or "y" then
term.clear()
term.setCursorPos(1, 1)
if room1 == true then
print("Room 1 is available")
end
if room2 == true then
print("Room 2 is available")
end
if room3 == true then
print("Room 3 is available")
end
if room4 == true then
print("Room 4 is available")
end
if room5 == true then
print("Room 5 is available")
end
if room6 == true then
print("Room 6 is available")
end
print("Please select a room...")
input = read()
if  input == '1' or "room 1" or "Room 1" or "room1" or "Room1" then
rs.setBundledOutput(bcbSide, colors.white)
rs.setBundledOutput(bcaSide, colors.pink)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  input == '2' or "room 2" or "Room 2" or "room2" or "Room2" then
rs.setBundledOutput(bcbSide, colors.orange)
rs.setBundledOutput(bcaSide, colors.gray)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  input == '3' or "room 3" or "Room 3" or "room3" or "Room3" then
rs.setBundledOutput(bcbSide, colors.magenta)
rs.setBundledOutput(bcaSide, colors.cyan)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  input == '4' or "room 4" or "Room 4" or "room4" or "Room4" then
rs.setBundledOutput(bcbSide, colors.yellow)
rs.setBundledOutput(bcaSide, colors.purple)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  input == '5' or "room 5" or "Room 5" or "room5" or "Room5" then
rs.setBundledOutput(bcbSide, colors.lime)
rs.setBundledOutput(bcaSide, colors.blue)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  input == '6' or "room 6" or "Room 6" or "room6" or "Room6" then
rs.setBundledOutput(bcbSide, colors.blue)
rs.setBundledOutput(bcaSide, colors.red)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
print("Please wait while we prepare your room card")
e = os.pullEvent("redstone")
if rs.testBundledInput(bcbSide, colors.pink) == true then
term.clear()
term.setCursorPos(1, 1)
print("Your card has arrived")
print("Enjoy your stay")
sleep(3)
os.shutdown()
end
end
if inputd == "no" or "No" or "n" or "N" then
print("Thank you for visiting the hotel")
sleep(2)
os.shutdown()
end
end
else
print("Your ticket does not include a free")
if roomfull == false then
print("Sorry all rooms are full")
print("Thank You for visiting the hotel")
sleep(3)
os.shutdown()
else
print("would you like to book a room y/n")
print("for 1 gold ingot")
inputb = read()
if inputb == "Yes" or "yes" or "y" or "Y" then
print("Please insert one Gold Ingot")
print("into the card return shoot")
e = os.pullEvent("redstone")
if rs.testBundledInput(bcaSide, colors.brown) == true then
if room1 == true then
print("Room 1 is available")
end
if room2 == true then
print("Room 2 is available")
end
if room3 == true then
print("Room 3 is available")
end
if room4 == true then
print("Room 4 is available")
end
if room5 == true then
print("Room 5 is available")
end
if room6 == true then
print("Room 6 is available")
end
print("Please select a room...")
inputc = read()
if  inputc == '1' or "room 1" or "Room 1" or "room1" or "Room1" then
rs.setBundledOutput(bcbSide, colors.white)
rs.setBundledOutput(bcaSide, colors.pink)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  inputc == '2' or "room 2" or "Room 2" or "room2" or "Room2" then
rs.setBundledOutput(bcbSide, colors.orange)
rs.setBundledOutput(bcaSide, colors.gray)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  inputc == '3' or "room 3" or "Room 3" or "room3" or "Room3" then
rs.setBundledOutput(bcbSide, colors.magenta)
rs.setBundledOutput(bcaSide, colors.cyan)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  inputc == '4' or "room 4" or "Room 4" or "room4" or "Room4" then
rs.setBundledOutput(bcbSide, colors.yellow)
rs.setBundledOutput(bcaSide, colors.purple)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  inputc == '5' or "room 5" or "Room 5" or "room5" or "Room5" then
rs.setBundledOutput(bcbSide, colors.lime)
rs.setBundledOutput(bcaSide, colors.blue)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
if  inputc == '6' or "room 6" or "Room 6" or "room6" or "Room6" then
rs.setBundledOutput(bcbSide, colors.blue)
rs.setBundledOutput(bcaSide, colors.red)
sleep(1)
rs.setBundledOutput(bcbSide, 0)
rs.setBundledOutput(bcaSide, 0)
end
print("Please wait while we prepare your room card")
e = os.pullEvent("redstone")
if rs.testBundledInput(bcbSide, colors.pink) == true then
term.clear()
term.setCursorPos(1, 1)
print("Your card has arrived")
print("Enjoy your stay")
sleep(3)
os.shutdown()
end
end
if inputc == "no" or "No" or "N" or "n" then
print("Thank you for visiting the hotel")
sleep(2)
os.shutdown()
end
end
end
end
Edited by
Lyqyd #2
Posted 23 June 2013 - 08:59 PM
Split into new topic.
theoriginalbit #3
Posted 23 June 2013 - 09:15 PM
hello riley5678,

In the future, please post the error that you're getting from the computer, if you're getting one, they are very useful and tell you a great deal of information about your problem. That information helps us, help you, much quicker.

In this case you obviously are not getting an error (based on what I can see is wrong), so in that case please post what you think the program should be doing, and what it actually is doing. Even point us to a specific section if you think the problem is there.

The immediate problem that I can see is with all your if conditionals


if inputc == "no" or "No" or "N" or "n" then


In Lua a value will resolve to true, and nil will resolve to false. As such even when your conditionals are being evaluated this is the result


true/false or true or true or true

And by the rules of boolean logic the outcome of this condition, irrelevant of the first value whether it is true or false, the result will be true, meaning that the if statement will run. Why? This is boolean logic.

OR

true or true = true
true or false = true
false or true = true
false or false = false

AND

true and true = true
true and false = false
false and true = false
false and false = false

NOT

not true = false
not false = true

As such we must change the if statements so that they accurately check the variable, to do this we add "inputc == " before each value, resulting in


if inputc == "no" or inputc == "No" or inputc == "N" or inputc == "n" then

However we can further improve this logic by doing the following


local inputc = string.lower(read())
if inputc == "no" or inputc == "n" then

Notice how just by making the input lowercase we dramatically shorten the conditional? Much better isn't it.

Lastly, another thing that is not so much a bug, or a problem, but definitely something that can be improved is your if statement structures. Currently you're doing this


if (condition) then
  -- code
end


if (condition) then
  -- code
end


if (condition) then
  -- code
end


if (condition) then
  -- code
end

The more efficient way to do this, when checking the same variable over and over, is to use the `elseif` keyword like so



if (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code
end

This way if it matches one, it will perform it, and not bother checking the rest, because we know it won't match, and if it doesn't match it will just move on and check the next one. Defining one final `else` will also give a way of saying, "well it was none of these"



if (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code

elseif (condition) then
  -- code
else
  print("Unknown command")
end

I hope this helps and if you have any questions just ask :)/>

— BIT