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

Java - Out of bounds!

Started by PROdotes, 07 September 2012 - 12:06 PM
PROdotes #1
Posted 07 September 2012 - 02:06 PM
So… hiya :D/>/>
figured out already it's a recursion error but no idea how to solve it… here's what the problem is…

my shaft mining turtle uses recursion to check for ore and mine it…

i have something like this:

mineOre()
check top, if there's ore, mine it, go up, call mineOre() go down
check 4 sides around you 1 by 1, if there's ore, mine it, go to the place you mined, call mineOre() and go back
check bottom and you guessed it, if there's ore, mine it, go down, call mineOre() and go up
end mineOre()

this works fine 99% of the time but, from time to time, the turtle will hit a large pocket of ores that are together and after calling the function X times (not sure how many unless I add an output txt for error handing) it will just break the code and "die"… while i'm waiting for it to return…

How would one fix that?
should I count how many times I have called the recursion? Is there another way of doing it outside of a recursion? (i suppose i could put it in a do while ore = true loop or something)… any ideas?
pseudo-pseudo code will do… but real code is a bonus :D/>/>

tyvm in advance ^^
sjele #2
Posted 07 September 2012 - 02:11 PM
I belive calling the same function inside the same function only works x times.
KaoS #3
Posted 07 September 2012 - 02:17 PM
a while true loop is probably a better idea

replace

thefunction(params)
--code
end

with

while true do
while true do
if (check for ore on all sides) then
params=side
break
end
end
--code
end
PROdotes #4
Posted 07 September 2012 - 02:37 PM
the "problem" with this is that I would prolly have to keep track of my path so i can retrace it…

i would have to do this…



function detectOre()
  look for ore on all 6 sides and return 0 for no ore or 1-6 for the side where you found ore
end

while direction ~= 0 and log lenght > 0
  direction = detectOre()
  if ore ~= 0 then
    mine in direction "direction"
    log = log + tostring(direction)
  elseif ore == 0 and length of log > 0 then
    move 1 back depending on log and remove last char from log
  end
end

is this correct? just writing without testing atm since i'm at work… and I don't really "speak lua" so I have no clue how string operations like len, add, crop and similar work…
not sure if this will properly orient the turtle when it's backtracking… and if I missed a "special case"…
PROdotes #5
Posted 07 September 2012 - 02:54 PM
edit: just realized 2 errors in the code…
for 1, direction = detectOre() has to go at the end of the while loop or it will break before it checks for new ore
and second is, it needs another direction = detectOre() before the while loop or else it won't even go into the loop :D/>/>
KaoS #6
Posted 07 September 2012 - 03:00 PM
yes exactly, I would recommend using a table through as it is easier to separate the log entries and access a specific one


local tSides={'left','front','right','back','bottom','top'}
function detectOre()
for k,v in pairs(tSides) do
  if turtle.detect(v) do
   return v
  end
end
end --don't forget to add a function to detect to the left, right and back
while true do
local log={}
  local direction = detectOre()
  if not direction then
   break
  end
   table.insert(log,direction)
   --dig function
end
end

I'm at work too –HighFive! :D/>/>
Edited on 07 September 2012 - 01:01 PM
PROdotes #7
Posted 07 September 2012 - 03:21 PM
i should add a break into detect ore so it doesn't check all 6 sides if it finds something…
also, when the turtle returns it does a table.remove(log, elements_in_table)… technical question but is there a way to chceck the number of elements in the table or do i have do do

mine(direction), table.insert(log,direction), elements = elements + 1

working on fun programming stuff at work, ftw! ;P
KaoS #8
Posted 07 September 2012 - 03:32 PM
#table returns the number of consecutive values in a table or length of a string


mytable={'one','two','three'}
print(#mytable)