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

Tree Chop program

Started by Lectavison, 02 April 2013 - 09:14 AM
Lectavison #1
Posted 02 April 2013 - 11:14 AM
Hello I seem to have a problem with my code.

Error:
bios:124: vm error: java.lang.ArrayIndexOutOfBoundsException: 256


function tree()
  if turtle.compare() == false then
    turtle.dig()
    turtle.forward()
    while turtle.detectUp() do
	  turtle.digUp()
	  turtle.up()
    end
    while not turtle.detectDown() do
	  turtle.down()
    end
    turtle.back()
    turtle.place()
  end
  tree()
end
tree()
Engineer #2
Posted 02 April 2013 - 11:17 AM
By doing this:

function x()
x()
end

x()

with this code you have recursion, afunction that calls it selfs. After ~ 256 times you get that error. Try:

while true do 
--code
End

Edit: my phone decided to post an unfinished post :/
Kingdaro #3
Posted 02 April 2013 - 11:17 AM
A function calling itself is a bad way of looping a program. Use a while loop.


while true do
  if turtle.compare() == false then
    turtle.dig()
    turtle.forward()
    while turtle.detectUp() do
      turtle.digUp()
      turtle.up()
    end
    while not turtle.detectDown() do
      turtle.down()
    end
    turtle.back()
    turtle.place()
  end
end
TheArchitect #4
Posted 02 April 2013 - 11:18 AM
You are causing a stack overflow by calling the function from itself:


function tree()
  --stuff

  tree()
end

You should remove that function call from inside the function. You're calling it after that.

Edit: If you want the turtle to stay on a loop, do this:


function tree()
  --do stuff
end

while true do
  tree();
end

Also, I think the trees may not grow if you are parking the turtle immediately adjacent to the sapling, the turtle might count as an occupied block.
Lectavison #5
Posted 02 April 2013 - 04:48 PM
I ended up just puting a while true loop to enclose the entire function. though thanks for the help now i partialy under stand what is the problem when i see an error like that.

Also, I think the trees may not grow if you are parking the turtle immediately adjacent to the sapling, the turtle might count as an occupied block.

If you look in the mincraft wiki about tree farming they have a simple tree farm with dirt on all sides of the sapling
Ray_Anor #6
Posted 02 April 2013 - 07:15 PM
recursion - not bad solution, but not so many times… if you do recursion you must think twicely… or more time
theoriginalbit #7
Posted 02 April 2013 - 07:17 PM
recursion - not bad solution, but not so many times… if you do recursion you must think twicely… or more time
It really is a bad solution… there are no circumstances ever where you can and should use it as an infinite loop… there must always be a way out of the recursion!
TheArchitect #8
Posted 02 April 2013 - 07:23 PM
You can probably count in one hand the cases in which recursion is an acceptable way to solve a problem. In a great majority of the cases, if you use recursion, you're doing it wrong.
theoriginalbit #9
Posted 02 April 2013 - 07:27 PM
You can probably count in one hand the cases in which recursion is an acceptable way to solve a problem.
Yep. I can only count 3.
Kingdaro #10
Posted 02 April 2013 - 07:35 PM
I remember a recursive factorial function being listed in Programming in Lua 5.2. Something like this, except with if..else.


function fact(n)
  return n * (n > 1 and fact(n - 1) or 1)
end

But that would overflow with larger numbers. Though it's nice and small.

EDIT: I just tested, and a stack overflow happens at about 20000. Waaay before that point, though, it just starts printing 'inf'
Ray_Anor #11
Posted 02 April 2013 - 07:44 PM
200000!
Man, it's really big number and greater than longInt