Posted 03 May 2013 - 03:16 PM
Title: Having trouble with changing code from recursive infinite loop to another infinite loop.
I am having trouble trying to get my program to work. I spent a while trying to create a program to get a Felling Turtle to chop down rows of trees that I have another turtle planting. So I created this program but go the Error message java.lang.ArrayIndexOutOfBoundsException:256. I know this means that I have a recursive infinite loop but I don't know how to adapt the code for a 'while true do'. Any help would be gratefully appreciated.
Here is the code:
function start()
x = 0
y = 0
z = 0
if turtle.detect() == true
then tree()
else oneforward()
end
end
function oneforward()
turtle.forward()
start()
end
function tree()
turtle.dig()
turtle.forward()
digup()
end
function digup()
turtle.digUp()
turtle.up()
if turtle.detectUp() == true
then digup()
else down()
end
end
function down()
turtle.down()
if turtle.detectDown() == true
then forward()
else down()
end
end
function forward()
x = x + 1
turtle.forward()
turtle.forward()
turtle.forward()
if x == 22
then turncounter()
else tree()
end
end
function turncounter()
z = z + 1
if z == 5
then print("Done")
else turncheck()
end
end
function turncheck()
if y == 0
then turnright()
else turnleft()
end
end
function turnright()
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.forward()
y = 1
tree()
end
function turnleft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
y = 0
tree()
end
start()
I am having trouble trying to get my program to work. I spent a while trying to create a program to get a Felling Turtle to chop down rows of trees that I have another turtle planting. So I created this program but go the Error message java.lang.ArrayIndexOutOfBoundsException:256. I know this means that I have a recursive infinite loop but I don't know how to adapt the code for a 'while true do'. Any help would be gratefully appreciated.
Here is the code:
function start()
x = 0
y = 0
z = 0
if turtle.detect() == true
then tree()
else oneforward()
end
end
function oneforward()
turtle.forward()
start()
end
function tree()
turtle.dig()
turtle.forward()
digup()
end
function digup()
turtle.digUp()
turtle.up()
if turtle.detectUp() == true
then digup()
else down()
end
end
function down()
turtle.down()
if turtle.detectDown() == true
then forward()
else down()
end
end
function forward()
x = x + 1
turtle.forward()
turtle.forward()
turtle.forward()
if x == 22
then turncounter()
else tree()
end
end
function turncounter()
z = z + 1
if z == 5
then print("Done")
else turncheck()
end
end
function turncheck()
if y == 0
then turnright()
else turnleft()
end
end
function turnright()
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.forward()
y = 1
tree()
end
function turnleft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
y = 0
tree()
end
start()