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

"for loop" question about turning left the first time and right the second time.

Started by Dovhian, 06 December 2012 - 10:53 PM
Dovhian #1
Posted 06 December 2012 - 11:53 PM
I have been wondering how to do this for several of my projects and now i have to ask for guidance :)/>
I'm making a turtle program that will build a flat surface depending on the input it receives through the read() command.
So far it is working well but i'm having problems with the for loop for the width of the build. When the turtle gets to the end of the lenght of the build i want it to turn right and go on to building the next lane of the length and then when that one is done turn left. This way it will zig zag up and down the build. But how do i tell it that every second time in the for loop i want it to do that?

I'm also wondering how i can make it – turtle.select() – the next slot when the 64 blocks in slot 1 have been used up?

Thanks for taking your time and helping me :)/>


term.clear()
term.setCursorPos(1,1)
function tryForward()
if not turtle.forward() then
  turtle.dig()
  turtle.forward()
end
end
function compare()
if turtle.compareDown() == false then
  turtle.digDown()
  turtle.placeDown()
end
end
function build()
tryForward()
compare()
end
print "How long do you want me to build?"
length = read()
print "How wide do yo want me to build?"
width = read()
for i=1,width do
compare()
for i=1,length - 1 do
  build()
end
turtle.turnRight()
turtle.forward()
turtle.turnRight()
compare()
for i=1,length - 1 do
  build()
end
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
compare()
end
KaoS #2
Posted 07 December 2012 - 12:00 AM
take a look at this code, it moves in a zig-zag across a 10x50 platform. it should be easy to adapt to your purposes


for i=1,50 do
  for a=1,10 do turtle.forward() end
  turtle[i%2==0 and 'turnRight' or 'turnLeft']()
  turtle.forward()
  turtle[i%2==0 and 'turnRight' or 'turnLeft']()
end

EDIT: to explain a little

number%2==0
returns true if 'number' is even and false if it is odd
ChunLing #3
Posted 07 December 2012 - 12:43 AM
And to explain the bit that's probably more puzzling, turtle.forward is an alternate notation for turtle['forward']. Thus if you put an operation inside of the brackets that yield a string matching the key to a function in the turtle table, it will access that function.

In lua, the and operator returns the first value if it is nil or false and the second value if the first value is true, while the or operator returns the first value if it isn't nil or false and the second value otherwise. The and operator has precedence over the or operator, so a and b or c is equivalent to (a and B)/> or c.

So i%2==0 and 'turnRight' or 'turnLeft' is equivalent to (i%2==0 and 'turnRight') or 'turnLeft'. Now false and 'turnRight' would evaluate to false, while true and 'turnRight' will evaluate as 'turnRight'. After that, in the expression false or 'turnLeft', the return would be 'turnLeft', while in the expression 'turnRight' or 'turnLeft', the return is 'turnRight'. And thus the entire expression is either 'turnRight' or 'turnLeft' depending on whether i%2 == 0.
Dovhian #4
Posted 07 December 2012 - 07:24 AM
Thanks a bunch guys!