# Pick nearest matching value in array

0 favourites
• 12 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
• I have an array with width 10 and height 10, filed with 0 and 1 on random locations.

And I want to specify the XY - for example X=3, Y=5, and if there is a 0 at this coordinates then change the value to "X" but if there is a 1 then find nearest 0 to X=3, Y=5 and then change that location to "X".

Is there a nice way to do that?

• Seriously people...

If you can help please do. But do not send me any PMs with links to your youtube channels (with videos that have nothing in common to my question) and don't tell me "I want to help you but first send me your capx file"... You just made fool out of yourselves.

• What about equality in the distance for the nearest 0 ?

• What about equality in the distance for the nearest 0 ?

Hmm, I don't think it matters in my case. It can be any 0 nearest to the initial 0 (which is now 1).

• the initial 0 (which is now 1).

You lost me there !

I think it would be easier for me to understand if you made a few examples with different scenarios.

• I'm afraid I can't understand exactly what you are after...

But...

Would for each x element

with a condition searching 0 do the trick? (as it loops it picks up first instance)

Would for each x element

pick nth instance

Once you locate the 0, then you can change it with your 3,5

You might have to use a local variable to keep track of where it is, and maybe pass that back into another loop to pin point.

But, a function passing params it should neaten up and simplify nicely...

But, you lost me, as I can't visualize what you are doing.

I would use 0 height, 4 deep and push data onto x

x

0, 0,x,y

1, 0,x,y

2, 1,x,y

3, 0,x,y

etc

easier to search/locate/change

• hehe yeah, just read what I wrote and lost myself as well

Let's try one more time

White = value 0

Red = value 1

Blue = value "X"

1. There is an array of some width and some height randomly filled with 0 and 1.

2. I want to set value "X" at X=6 and Y=4. If that XY 6,4 is 0 then I can set it to "X" just fine.

3. But array can change after some time .

4. And now value at XY 6,4 is 1 and I can't set it to "X" anymore.

5. I need to find the nearest available cell to XY 6,4 with value = 0 (any direction or distance).

6. So I can safely set it to "X".

If that grid would be made of sprites then I could simply use "Pick nearest" action to pick nearest sprite with variable = 0... But it's a bit more complicated with arrays.

• Ok, that was quite thorough. It shouldn't be too hard for me to come up with something.

• Just from the top of my head. Not tested.

X=6, Y=4 are the cords of the field according to which you are searching for the nearest field with a value of 0.

``````set searchOffset = 0;

while not fieldFound (or whatever loop, this one is not important to specify)
{
for "loopNameX" (X-searchOffset) to (X+searchOffset)
{
for "loopNameY" (Y-searchOffset) to (Y+searchOffset)
{
if Array(loopindex("loopNameX"),loopindex("loopNameY")) == 0;
//Do whatever you need
stop loop
}
}
}[/code:56q4jfsh]``````
• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Breadth-first search. Starting at some cell like (6,4) you mark it as checked* and add it's neighbours to a queue. While the queue is not empty you take the first cell from the front of the queue and check if it's 0. If it is you are done. If not you mark it as checked and add it's unchecked neighbours to the end of the queue. Repeat until you find a 0 or the queue is empty.

* using a second array or the z-axis

• There you go, in the form of a recursive function that check in a circular manner around the provided point. It returns the first found zero scanning from top to bottom and left to right.

https://dl.dropboxusercontent.com/u/700 ... Array.capx

ramones' answer is better, since my method will possibly revisit already checked Array cells if no suitable candidate was found in the first recursion.

edit: Just updated the capx with a travesty of a Breadth-First algorithm. It's surprisingly simple, yet, better than my previous version.

• Works like a charm! Took me awhile to understand what is going on there, but already implemented it and modified a bit to fit my game needs.

Much appreciated!

• 12 posts