This is in fact a common approach and should work, so there probably is an error on the code somewhere.
You can try using the available list method - compile a list of available places, randomly select one and immediately remove it from the list. Because it's no longer on the list, it will never get selected again.
for i = 1, #Objects do
for e = 1, #Objects do
if Objects[i][1] == Objects[e][1] and Objects[i][2] == Objects[e][2] then
Objects[i][1] = math.random(1, 25)
Objects[i][2] = math.random (1, 25)
end
end
end
Last edited by TheHistoricApple on Fri Jan 20, 2017 4:36 am, edited 1 time in total.
I see your problem - because it runs over all planets in both loops, at one point it will compare with itself, which will always return true and trigger reposition. There is also nothing preventing planets from getting repositioned into another planet or even forcing it to move at all (with 0.16% chance). But it's not the best approach in general.
What you should do is check for space being occupied when you create each planet, and shift it around until it's place is free, only then put it to the planet list.
for i = 1, #Objects do
for e = 1, #Objects do
if e ~= i then
if Objects[i][1] == Objects[e][1] and Objects[i][2] == Objects[e][2] then
Objects[i][1] = math.random(1, 25)
Objects[i][2] = math.random (1, 25)
end
end
end
end
You could do this when creating the planets in order to avoid looping through all objects but I don't know, I wouldn't do this at all
for i, person inipairs(everybody) do [tab]ifnot person.obey then person:setObey(true) end end
love.system.openURL(github.com/pablomayobre)
No it wouldn't. It doesn't reposition the planet itself for the second time but it still doesn't ensure that repositioned planet can't move into another plant thus defeating the purpose, or even move at all.
local finished = false
while not finished do
finished = true
planet[ 1 ] = math.random ( 1, 25 )
planet[ 2 ] = math.random ( 1, 25 )
for i = 1, #objects do
if planet[ 1 ] == objects[ i ][ 1 ] and planet[ 2 ] == objects[ i ][ 2 ] then
finished = false
break
end
end
end
objects[ #objects + 1 ] = planet
Yeah I knew, I just keeped the code he was using, I still consider there should be a better way to do this, something like a table with all valid positions and removing the already used items or something like that... I think that performing this loop every single time to add a planet to the map would be a real pain...
for i, person inipairs(everybody) do [tab]ifnot person.obey then person:setObey(true) end end
love.system.openURL(github.com/pablomayobre)
That depends on how many planets are there and how big is the field. Odds of running into another planet randomly are N / W × H, to reach 50:50 odds on 25x25 map you'll need 63 planets in it, and even then there's still 50% chance it'll make it into a free spot on the first try, 75% on second try, 87.5% on third try and so on. It really only stops working well when available space is almost completely exhausted.