Neighbour cell detection in 2D array

Page 1 of 1

1 Replies - 2420 Views - Last Post: 28 July 2013 - 04:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=325812&amp;s=7c9e1498fe83d0d3fa39741e8f4661f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Lloyd A.

Reputation: 0
• Posts: 1
• Joined: 15-June 07

Neighbour cell detection in 2D array

Posted 28 July 2013 - 03:58 PM

I have a 2D array, that I'm using for procedural generation. If I printed it out it looks like this (# = cell, and . = empty):

```
0123456789
0#########
1#.......#
2#...#...#
3#..#....#
4#.......#
5#...##..#
6#...#...#
7#.......#
8#.......#
9#########
```

I'm trying to write a method that will return the number of neighbouring cells with a # (if any). So for example I was using the above array, and if I gave the method 3,3 it would return 1. If I gave it 5,5 it would return 3.

The code I have written works, but does not take into account any cells on the edges. For example, if I try to check the neighbours of cell 0, 0 or 10, 10. I will get out of bounds errors. I'm unsure how to write code that takes this into account, without using complicated nested if statements that would be hard to understand. Any advice would be appreciated.

``` public static int NeighbourCount(int[,] array, int CellX, int CellY)
{
int count = 0;

//These if statements are for each individual direction
// 1 = #
if (array[CellX + 1, CellY] == 1)
{
count++;
}

if (array[CellX - 1, CellY] == 1)
{
count++;
}

if (array[CellX, CellY + 1] == 1)
{
count++;
}

if (array[CellX, CellY - 1] == 1)
{
count++;
}

if (array[CellX + 1, CellY + 1] == 1)
{
count++;
}

if (array[CellX + 1, CellY - 1] == 1)
{
count++;
}

if (array[CellX - 1, CellY + 1] == 1)
{
count++;
}

if (array[CellX - 1, CellY - 1] == 1)
{
count++;
}

return count; //Return 0
}
```

Thanks!

I mean, 3,4 not 3,3 for the example I gave.

Is This A Good Question/Topic? 0

Replies To: Neighbour cell detection in 2D array

#2 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

Re: Neighbour cell detection in 2D array

Posted 28 July 2013 - 04:15 PM

The easiest way to deal with 2D arrays like this is to create a 'ghost' border. Instead of declaring it 10x10, you'd declare it 12x12. Row 0 and 11, along with column 0 and 11 are the 'ghosts' and nothing can go in them. You'd then deal with your array as having bound of 1 through 10. This allows you to easily check the number of neighbors without worrying about bounds checking at the cost of slightly more memory usage.
```int NeighborCount(int x, int y) {
int count = 0;
for (int i = -1; i <=1; i++) {
for (int j = -1; j<= 1; j++) {
if (Board[x+i, y+j] == Occupied) count++;
}
}
if (Board[x,y] == Occupied) count--; // Remove the cell we are looking at, since it's not its own neighbor
return count;
}
```

This post has been edited by Momerath: 28 July 2013 - 04:16 PM