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

Help Wanted: Bug in a mining program but can't find my mistake

Started by Snowy99, 24 April 2015 - 09:42 PM
Snowy99 #1
Posted 24 April 2015 - 11:42 PM
Hey guys, I am rather new to coding and wrote a program that I am having issues fixing. I have one mistake but can't find where it is. It is a strip mine program for a mining turtle that allows me to create a main tunnel and branches going off to the side. The only issue I have found is that after I get my branch on the right and it turns and does the one on the left it doesn't stop mining. The right goes perfectly. 50 blocks, and comes back. Then it starts mining the left side, it goes down to the 50th block and just keeps going.

local i = 1
repeat
repeat
turtle.dig()
turtle.forward()
turtle.turnRight()
turtle.digUp()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.turnRight()
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.turnRight()
  turtle.forward()
  turtle.digUp()
  turtle.dig()
  turtle.forward()
  turtle.dig()
  turtle.digUp()
i = i + 1
until i == 2
repeat
  turtle.dig()
  turtle.forward()
  turtle.digUp()
i = i + 1
until i == 50
turtle.turnRight()
turtle.turnRight()
repeat
  turtle.forward()
i = i + 1
until i == 53
repeat
  turtle.dig()
  turtle.forward()
  turtle.digUp()
i = i + 1
until i == 50
turtle.turnRight()
turtle.turnRight()
repeat
  turtle.forward()
i = i + 1
until i == 51
turtle.turnLeft()
i = i + 1
until i == 50
Dragon53535 #2
Posted 25 April 2015 - 04:47 AM
You're using the same damn variable for EVERY loop inside, so i is CONTINUING to increment up. so at some point it will hit that 50 point, but then it will go up more, so 51, 52 and so on and so forth FOREVER. It will never be reset to 0… I will tell you however, either chance your variable names so that you don't have just one variable, or swap it to a few for loops.

for i = 1, 50 do
  --#Stuff
end
This will do stuff 50 times. and the variable names DONT MATTER.

for i = 1, 50 do
  for i = 1, 50 do
    for i = 1, 50 do
	  --#Stuff
    end
  end
end
These will each run 50 times, however the inner one will do it 50 times per the middle one, and the middle will do it 50 times per the outer one. This is probably what you would want.
Snowy99 #3
Posted 25 April 2015 - 06:14 PM
*Facepalm*
Thank you, I totally missed that.