# Pick nearest matching value in array

Get help using Construct 2

### » Wed Sep 30, 2015 5:58 pm

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?
B
160
S
68
G
43
Posts: 2,603
Reputation: 36,168

### » Wed Sep 30, 2015 6:59 pm

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.
B
160
S
68
G
43
Posts: 2,603
Reputation: 36,168

### » Wed Sep 30, 2015 7:18 pm

What about equality in the distance for the nearest 0 ?
B
78
S
32
G
27
Posts: 1,021
Reputation: 20,952

### » Wed Sep 30, 2015 7:34 pm

Magistross wrote: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).
B
160
S
68
G
43
Posts: 2,603
Reputation: 36,168

### » Wed Sep 30, 2015 8:14 pm

shinkan wrote: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.
B
78
S
32
G
27
Posts: 1,021
Reputation: 20,952

### » Wed Sep 30, 2015 8:24 pm

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
You think you can do these things, but you can't, Nemo!
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,738

### » Wed Sep 30, 2015 8:59 pm

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.
B
160
S
68
G
43
Posts: 2,603
Reputation: 36,168

### » Wed Sep 30, 2015 9:13 pm

Ok, that was quite thorough. It shouldn't be too hard for me to come up with something.
B
78
S
32
G
27
Posts: 1,021
Reputation: 20,952

### » Wed Sep 30, 2015 9:42 pm

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.

Code: Select all
`set searchOffset = 0;while not fieldFound (or whatever loop, this one is not important to specify){   add 1 to searchOffset;   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       }   }}`
B
35
S
20
G
85
Posts: 1,060
Reputation: 48,004

### » Wed Sep 30, 2015 9:49 pm

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
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,730

Next