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

Logic errors or my own error?

Started by British Bandit, 23 February 2015 - 08:47 PM
British Bandit #1
Posted 23 February 2015 - 09:47 PM

args = {...}
length = args[1]
width = args[2]
depth = args[3]
WidthR = width-1
LengthR = length-2
function turtle.Advance(n)
  for i=1,n do
turtle.dig()
turtle.forward()
  end
end
function turtle.Left(n)
  for i=1,n do
    turtle.turnLeft()
    turtle.dig()
    turtle.forward()
  end
end
function turtle.Right(n)
  for i=1,n do
    turtle.turnRight()
    turtle.dig()
    turtle.forward()
  end
end
function turtle.Descend(n)
  for i=1,n do
    turtle.digDown()
turtle.down()
  end
end
function turtle.Ascend(n)
  for i=1,n do
turtle.digUp()
turtle.up()
  end
end
ReTURNmine = width%2
for height=1,depth do
  turtle.Advance(1)
  for row=1,width do
if row~=width then
   DeTURNmine = row%2
else
   DeTURNmine = -1
end
turtle.Advance(LengthR)
if DeTURNmine==1 then
   turtle.Left(2)
elseif DeTURNmine==0 then
   turtle.Right(2)
end
  end
  if ReTURNmine==1 then
turtle.turnLeft()
for returnLength=1,length do
   turtle.forward()
    end
  end
  turtle.turnLeft()
  for returnWidth=1,width do
turtle.forward()
  end
  turtle.turnLeft()
  if height~=depth then
    turtle.Descend(1)
  end
end

The error occurs when the turtle gets to the final row (or column, however you see it) on a level. I tried to prevent the turtle from doing a turn on the final row, yet it still turns. Which then messes up the rest of the mining operation. Any guidance/explanation on how to fix this error (I would rather avoid having someone just give me a direct answer), thanks!
Bomb Bloke #2
Posted 23 February 2015 - 10:23 PM
It's to do with data types. The arguments passed to your script come in the form of strings. You copy args[2] directly into your "width" variable, making that a string, too.

To an extent, Lua lets you "get away" with this (eg, you can still build a "for" loop out of your number-in-a-string "width" variable), but the fact remains that your "row" variable (a number) will never be equal to your "width" variable (a string).

Best practise is to be specific about your types, eg:

width = tonumber(args[2])
British Bandit #3
Posted 23 February 2015 - 10:36 PM
Thanks a bunch man! I'm kind of ashamed that I didn't think of that. I just assumed that something had automatically stored them as an integer. Turns out I had a couple of other problems, but I already fixed them!
Thanks again!