Coparing within an array
-
Hi, I'm working on a project which contains drawing sprites (enemies) at a random location on a map. So that they don't get drawn in walls and stuff, I modified the collidion function from the built in tutorial, which worked fine but I also want them not to be stacked drawn twice (or more) at the same location.
So my idea was to compare the randomly generated position (monster[i].pos) at time now, with the array of generated position from start till now in this modified collision function. And only if there is no match the modified collision function gives the "Go!" to move on.
So far so good (I hope), but for some reason I do not get it coded. It always seems as if the code gets trapped in an endless loop and gets stuck.Any help appreciated.
Here my code so far:
num_mon = 6 array monster[num_mon]=[ ..., .pos = {0,0}, ... ] stat_draw = 0 mon_pos = {0,0} set = true for i = 0 to num_mon loop set = true while set loop stat_draw = i-1 //number of generated positions before i monster[i].pos = {random(value.x),random(value.y)} mon_pos = monster[i].pos if !drawitems( argument.x, argument.y) //function returns true or false set = false endif repeat . . function drawitems( x, y) (map-xy-arguments) tile_blocked = false if stat_draw ≥ 0 then //so if at least 1 position was generated before for i = 0 to stat_draw loop if monster[i].pos == mon_pos then //if there is a match tile_blocked = true endif repeat result = true if !tile_blocked and (map-x,y-arguments) then result = false endif return result
-
I found a solution, so simple and yet effective! Thanks, @toxibunny
@toxibunny said in Random but no duplicates?:
Make a 7x8 array, all filled with 0s, and mark the position as 1 if you use it. You can use that to see if a position is already filled.