I need to get a neighboring position of a 2d array that has value “o”
To do so I use a repeat until loop so that if it choses an array position with value “l”, it will repeat, I also need it to continue if the chosen position is off the array, and to cancel if it has run 100 times so that it wont crash.
However, when the X position of neighboring position is not on the array, (5 or 0) then it makes an error as it tries to put the Z position on top of the nil. BUT this should have been taken care of when the code found that the position was off the array.
It seems as if, once continue (maybe break and return as well) is called, it runs the repeat condition. I don’t want this to happen as then the code makes an error.
Also when the Z position is off the array, it is fine as the until keyword sees that nil~=“0” and works as normal, if not intended.
Is there any way to repeat a repeat until loop without checking the repeat condition?
--Necessary excerpt from my code with IslandZoom1Layer
local IslandZoom1Layer={
{"o","o","o","l"},
{"o","o","o","o"},
{"o","l","o","o"},
{"o","o","o","o"}
}
for i, column in ipairs(IslandZoom1Layer) do
for o,part in ipairs(column) do
if part=="l" then
local Chance=math.random()
if Chance>0.25 then
local randomneighborX
local randomneighborZ
local Count=0
repeat
Count+=1
randomneighborX=((math.random(0,1)*2)-1)+i
randomneighborZ=((math.random(0,1)*2)-1)+o
if (randomneighborX>#IslandZoom1Layer or randomneighborX<1) or (randomneighborZ>#IslandZoom1Layer or randomneighborZ<1) then
print("a")
print(randomneighborX,randomneighborZ)
continue
else
print("b")
end
if Count==100 then break end
until IslandZoom1Layer[randomneighborX][randomneighborZ]=="o"
print("c")
IslandZoom1Layer[randomneighborX][randomneighborZ]="l"
local Chance=math.random()
if Chance>0.75 then --25% chance
part="o"
end
end
end
end
end
print(IslandZoom1Layer)