views:

78

answers:

1

Basically I'm creating a forest fire program that depends on the wind / dryness of the surround pieces. I have an array var Trees [,] that is 20 x 20. The middle square is set on "fire" what needs to be done once you click button1: Evaluate each square around the one that is set on fire to determine the probability for the others to catch fire.

  Color[,] map = new Color[WIDTH, HEIGHT];
        for (int x = 0; x < WIDTH; x++)
            for (int y = 0; y < HEIGHT; y++)
            {
                if (x == WIDTH / 2 && y == HEIGHT / 2)
                    map[x, y] = Color.Red;
                else
                    map[x, y] = Color.Green;
            }

        fireBox1.box = map;

This is the array that i have setup 20 x 20 with the middle square set on fire. I just have no idea how to get the squares around the one that is currently on fire.

+1  A: 

You can start with a simple loop.

for (int i = 0; i < 20; i++)
{
    for (int j = 0; j < 20; j++)
    {
        var tree = Trees[i, j];
        // ...
    }
}

After you have built your matrix the center should look like this.

[G][G][G]
[G][R][G]
[G][G][G]

Then we can loop through only the points that touch the center point.

int centerX = 9;
int centerY = 9;
int beginX = centerX - 1; 
int endX = centerX + 1; 
int beginY = centerY - 1; 
int endY = centerY + 1; 

for (int y = beginY; y <= endY; y++)
{    
    for (int x = beginX ; x <= endX; x++)
    {
        //Skip the center
        if (x == centerX && y == centerY)
            continue;       
        // Calculate the chance of catching on fire.
        if (IsWindyPoint(x, y) || IsDryPoint(x, y))
            map[x, y] = Color.Yellow;
    }
}

So assuming we have wind blowing east we should see this as the matrix.

[G][G][G]
[G][R][Y]
[G][G][G]

And eventually it will expand out like this.

[G][G][G][G]
[G][G][Y][Y]
[G][R][R][Y]
[G][G][Y][Y]
[G][G][G][G]
ChaosPandion
You could probably further improve this by using the `Length` property of the array instead of hardcoding 20 :-)
Darin Dimitrov
So use a for each loop that goes through each position in the array and evaluate the probability from there?
Watch out for going out of the array's bounds! What happens if you use this with `Tree[40,40]` and you had `beginX = 39` and `beginY = 39`? Not good.
Callum Rogers
@Callum - Surprises make coding fun. :)
ChaosPandion
Thank you very much, i really appreciate it. I went with something along the lines of creating another array from the original array and automated it so if there was Green point on one then green on the other and if it was on fire then calculated the probability from there. Ty for the help chaos.
@lo3 - No problem, good luck out there.
ChaosPandion