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

My 'and' is acting like an 'or'

Started by CCJJSax, 29 May 2013 - 03:22 PM
CCJJSax #1
Posted 29 May 2013 - 05:22 PM
This line is not functioning like I'd expect.

while math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and turtle.getFuelLevel() - FuelNeeded < 0 do

Basically all this means (or at least what I'm going for) is that it needs a certain amount of items in slot 16, and enough fuel to complete the operation. The amount of items (torches) in slot 16 changes depending on the distance you need to go, and so does fuel.

But when I run the code with either one true, even if only one of those are fulfilled, the code goes on…… Even though both A and B isn't. This isn't the first time I ran into this though. Is it some bug with computercraft?

I'm still learning Lua, so be gentle with your criticism ;)/> jk

Spoiler


-- todo section
-- enter to terminate/pause
-- Fuel Need before it starts. distanceX5


local tArgs={...}
if tArgs[1] == nil then
  term.clear()
  term.setCursorPos(1, 1)
  print("How far do you want to go?")
  tArgs[1] = read()
end

local function clear()
    term.clear()
    term.setCursorPos(1, 1)
end

local function timesfuelcheck()
  term.clear()
  term.setCursorPos(1, 1)
  print("turtle will stop when this number reaches zero: "..m)
  print("fuel remaining: "..turtle.getFuelLevel())
  print("blocks dug: "..dignumb)
  if extrainfonumb ~= 0 then
    print(extrainfo)
  elseif extrainfonumb == 0 then
  end  
  term.setCursorPos(1, 12)
  print("Bottom right slot needs to have torches")
end

local function checkdig()
  while turtle.detect() do
    turtle.dig()
    dignumb=dignumb+1
    timesfuelcheck()
    sleep(.5)
  end
  while turtle.suck() do
  end
end

local function checkdigUp()
  while turtle.suckUp() do
  end
  while turtle.detectUp() do
    turtle.digUp()
    dignumb=dignumb+1
    timesfuelcheck()
    sleep(.5)
  end
end

local function checkdigDown()
  while turtle.suckDown() do
  end
  while turtle.detectDown() do
    turtle.digDown()
    dignumb=dignumb+1
    timesfuelcheck()
    sleep(.5)
  end
end

local function checkdigLeft()
  turtle.turnLeft()
  checkdig()
  turtle.turnRight()
end

local function checkdigRight()
  turtle.turnRight()
  checkdig()
  turtle.turnLeft()
end

local function checkplaceDown()
turtle.select(1)
  while not turtle.detectDown() do
    if turtle.placeDown() == true then
      turtle.placeDown()
    elseif turtle.placeDown() == false then
      x = 1
      repeat
        print("Something failed. Attempting to fix it.")
        x=x+1
        turtle.select(x)
        sleep(.3)
      until 
        x==15 or turtle.placeDown() == true
          if x==15 then
            term.clear()
            term.setCursorPos(1, 1)
            print("no placeable items in any of the slots")
            print("rechecking in four seconds.")
            print("4")
            sleep(1)
            print("3")
            sleep(1)
            print("2")
            sleep(1)
            print("1")
            sleep(1)
            print("rechecking")
            sleep(.5)
            checkplaceDown()
          elseif turtle.placeDown() == true then
            sleep(.3)
            print("continuing")
          end
    end
  end
end


BlocksNeeded = 10
TorchesNeeded = tArgs[1] / 5
FuelNeeded = tArgs[1] * 5

local i = 0
local timer = os.startTimer(1)
clear()
while math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and turtle.getFuelLevel() - FuelNeeded < 0 do
clear()
if BlocksNeeded-turtle.getItemCount(1)-1 >= 0 then
      print("I recommend "..BlocksNeeded-turtle.getItemCount(1).." more blocks.")
end
    if math.ceil(TorchesNeeded - turtle.getItemCount(16)) >= 0 then
      print("Need "..math.ceil(TorchesNeeded-turtle.getItemCount(16)).." more torches in the bottom right slot.")
    else
      print("Enough torches")
    end
if turtle.getFuelLevel() - FuelNeeded < 0 then
print("I need "..turtle.getFuelLevel() - FuelNeeded/(-1).." more fuel")
else
print("Enough Fuel")
end
print()
    print("       Press any letter to skip")

    local evt, arg = os.pullEvent()
    if evt == "timer" then
        if arg == timer then
            i = i + 1
            print(i)
            timer = os.startTimer(1)
        end
    elseif evt == "char" then
            break
    end
end

clear()
turtle.select(1)
dignumb = 0
extrainfonumb = 0
m = tArgs[1]
torch = 5
for i = 1, tArgs[1] do
    timesfuelcheck()
    checkdig()
    turtle.forward()
    checkdigLeft()
    checkdigRight()
    checkdigUp()
    turtle.up()
    checkdigLeft()
    if torch == 5 then
turtle.turnRight()
checkdig()
turtle.select(16)
        if turtle.place() == true then
extrainfonumb = 3
extrainfo = "placing torch"
timesfuelcheck()
turtle.place()
torch = 0
        else
          extrainfonumb = 3
          extrainfo = "unable to place"
          timesfuelcheck()
          torch = 0
        end
        turtle.turnLeft()
turtle.select(1)
else
checkdigRight()
    end
    checkdigDown()
    turtle.down()
    checkdigDown()
    turtle.down()
    checkplaceDown()
    checkdigLeft()
    checkdigRight()
    turtle.up()
    torch = torch+1
      if extrainfonumb ~= 0 then
        extrainfonumb = extrainfonumb-1
      end
    m = m-1
    timesfuelcheck()
end


^this looks better in Notepad++ lol
James0x57 #2
Posted 29 May 2013 - 05:32 PM
Try this:
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and (turtle.getFuelLevel() - FuelNeeded) < 0

Possibly it was doing this:
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and turtle.getFuelLevel() - (FuelNeeded < 0)
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and (turtle.getFuelLevel() - 0)
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and turtle.getFuelLevel()
CCJJSax #3
Posted 29 May 2013 - 06:16 PM
Try this:
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and (turtle.getFuelLevel() - FuelNeeded) < 0

Possibly it was doing this:
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and turtle.getFuelLevel() - (FuelNeeded < 0)
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and (turtle.getFuelLevel() - 0)
math.ceil(TorchesNeeded - turtle.getItemCount(16)-1) >= 0 and turtle.getFuelLevel()

I was hoping you were right, but nope :(/>. It continued still because it had enough fuel. :(/>
James0x57 #4
Posted 29 May 2013 - 06:32 PM
Weird.

Basically, this is what you're doing there:
while ((turtle.getItemCount(16) > math.ceil(TorchesNeeded)) and (turtle.getFuelLevel() > FuelNeeded)) do

Which seems fine to me… Mayhaps your problem is in another castle…
CCJJSax #5
Posted 29 May 2013 - 07:15 PM
Weird.

Basically, this is what you're doing there:
while ((turtle.getItemCount(16) > math.ceil(TorchesNeeded)) and (turtle.getFuelLevel() > FuelNeeded)) do

Which seems fine to me… Mayhaps your problem is in another castle…

Well, I have had really bad luck with "and"s and ors and such. I think it's a little bugged. Then again, I am pretty new to coding. So it's probably me.
GopherAtl #6
Posted 29 May 2013 - 07:24 PM
If I'm reading your variable names correctly…

TorchesNeeded - turtle.getItemCount(16)-1) >= 0

Torches I need minus the torches I have equals how many more torches I need. If you have more than needed, will be negative. It will be greater than or equal to zero only if you have less torches than you need. Unless I'm misinterpreting the intent somehow, you meant to have <=, not >=.
Bomb Bloke #7
Posted 29 May 2013 - 07:27 PM
Your fuel check condition has the exact same problem.

The way I see it, the line in James0x57's last post should work (because it is not "basically what you're doing", but rather what you want to be doing).
CCJJSax #8
Posted 29 May 2013 - 07:55 PM
If I'm reading your variable names correctly…

TorchesNeeded - turtle.getItemCount(16)-1) >= 0

Torches I need minus the torches I have equals how many more torches I need. If you have more than needed, will be negative. It will be greater than or equal to zero only if you have less torches than you need. Unless I'm misinterpreting the intent somehow, you meant to have <=, not >=.

It had been working with the torches. The torches part worked perfectly until I made it an and with the fuel requirement. And the use of the <= instead of < was probably just a blonde moment lol
CCJJSax #9
Posted 29 May 2013 - 08:00 PM
Your fuel check condition has the exact same problem.

The way I see it, the line in James0x57's last post should work (because it is not "basically what you're doing", but rather what you want to be doing).

Unfortunately it continued still. It had plenty of fuel, but it didn't have enough torches. So it should have waited, but it didn't .. :(/>
theoriginalbit #10
Posted 29 May 2013 - 08:16 PM
For your reference…

Logical Operators
SpoilerAND

true AND true = true
true AND false = false
false AND true = false
false AND false = false

OR

true OR true = true
true OR false = true
false OR true = true
false OR false = false

NOT

NOT true = false
NOT false = true

And all programming languages perform what is in brackets first. So that can sometimes effect your logic.
Also it should be noted that most programming language will exit conditionals when the result cannot be changed, for example if a conditional is all AND's and a false comes up, it will not check any more as there is no way an AND can change that false to anything else, if there is an OR in the conditional it will skip to that and continue from there. If the conditional is OR, it will do the same thing as with AND, if a true comes up, it will exit or skip to an AND, as there is no way to change that true with an OR (this is all assuming it has done the brackets first).

Ok I've just tested this one myself, and it works fine. Give it a try.

while ((TorchesNeeded - turtle.getItemCount(16)-1) >= 0) and ((FuelNeeded - turtle.getFuelLevel()) >= 0) do
NOTE: All i actually did was modify the statement for your fuel checking to match the same as the torches. The extra brackets are not needed, they are just there as its my habit, and also it was just making sure it was evaluating it all correctly.

EDIT: If that doesn't work, then the problem is definitely somewhere else.
Edited on 29 May 2013 - 06:18 PM
CCJJSax #11
Posted 29 May 2013 - 09:35 PM
For your reference…

Logical Operators
SpoilerAND

true AND true = true
true AND false = false
false AND true = false
false AND false = false

OR

true OR true = true
true OR false = true
false OR true = true
false OR false = false

NOT

NOT true = false
NOT false = true

And all programming languages perform what is in brackets first. So that can sometimes effect your logic.
Also it should be noted that most programming language will exit conditionals when the result cannot be changed, for example if a conditional is all AND's and a false comes up, it will not check any more as there is no way an AND can change that false to anything else, if there is an OR in the conditional it will skip to that and continue from there. If the conditional is OR, it will do the same thing as with AND, if a true comes up, it will exit or skip to an AND, as there is no way to change that true with an OR (this is all assuming it has done the brackets first).

Ok I've just tested this one myself, and it works fine. Give it a try.

while ((TorchesNeeded - turtle.getItemCount(16)-1) >= 0) and ((FuelNeeded - turtle.getFuelLevel()) >= 0) do
NOTE: All i actually did was modify the statement for your fuel checking to match the same as the torches. The extra brackets are not needed, they are just there as its my habit, and also it was just making sure it was evaluating it all correctly.

EDIT: If that doesn't work, then the problem is definitely somewhere else.

What did it do that worked? It didn't do what I wanted it to do.
theoriginalbit #12
Posted 29 May 2013 - 09:37 PM
What did it do that worked? It didn't do what I wanted it to do.
It did not continue until it had the required amount of torches and fuel.
CCJJSax #13
Posted 29 May 2013 - 09:39 PM
What did it do that worked? It didn't do what I wanted it to do.
It did not continue until it had the required amount of torches and fuel.

Really? wow. And that was the only line you changed? Perhaps it has something to do with the math.ceil()
theoriginalbit #14
Posted 29 May 2013 - 09:42 PM
Really? wow. And that was the only line you changed? Perhaps it has something to do with the math.ceil()
It is all that I changed. I thought about that, but it shouldn't have effected anything.