Tordur
Tordur

Reputation: 15

Difficulty with simple bitmap manipulation

This is the first time I'm asking in here, so bear with me :) Well basically I have a problem with my code, and I cannot figure out what it is. It's a city generator for a game that I'm developing. It creates a 20 x 20bitmap with the ground in brown, and a river in blue. Now I need it to generate a 3x3 block in pink, and then it should check if there is any overlap, if yes, genereate a new random position, and proceed to check if there is a blue color. My problem is that it generates the river, and the 3x3 pink block regardless of if its overlapping the blue part.

According to the code, it shouldn't be possible.. And the function that generates the city block gets called after the river is:

private void CreateCityBlock(string name, Color col) {

        //This variable stops the loop
        bool canGenerate = false;

        //Create a loop that checks if it can generate the 3x3 block
        while (!canGenerate)
        {
            //Create a random and generate two positions for "x" and "y"
            Random rnd = new Random();
            int x = rnd.Next(1, 19);
            int y = rnd.Next(1, 19);



            //Check if it overlaps with the river
            if (!city.GetPixel(x, y).Equals(Color.Blue)||
                !city.GetPixel(x - 1, y + 1).Equals(Color.Blue) ||
                !city.GetPixel(x, y + 1).Equals(Color.Blue) ||
                !city.GetPixel(x + 1, y + 1).Equals(Color.Blue) ||
                !city.GetPixel(x - 1, y).Equals(Color.Blue) ||
                !city.GetPixel(x + 1, y).Equals(Color.Blue) ||
                !city.GetPixel(x - 1, y - 1).Equals(Color.Blue) ||
                !city.GetPixel(x, y - 1).Equals(Color.Blue) ||
                !city.GetPixel(x + 1, y - 1).Equals(Color.Blue))
            {
                //set the color to pink
                city.SetPixel(x - 1, y + 1, col);
                city.SetPixel(x, y + 1, col);
                city.SetPixel(x + 1, y + 1, col);
                city.SetPixel(x - 1, y, col);
                city.SetPixel(x, y, col);
                city.SetPixel(x + 1, y, col);
                city.SetPixel(x - 1, y - 1, col);
                city.SetPixel(x, y - 1, col);
                city.SetPixel(x + 1, y - 1, col);
                canGenerate = true;

            }





        }
    }

Upvotes: 1

Views: 87

Answers (1)

John Willemse
John Willemse

Reputation: 6698

The problem lies in the fact that the || (conditional-OR) operator does not evaluate any expressions if the first expression is True.

So, if the first pixel is not blue, then the rest is not evaluated, since !False equals True.

In this case, I would write a separate "checking" method to evaluate all pixels and return the result accordingly, e.g.:

// Returns true if the area overlaps a river pixel, false otherwise
private bool Overlaps(Bitmap city, int x, int y)
{
    for (int cx = x - 1; cx < x + 2; cx++)
    {
        for (int cy = y - 1; cy < y + 2; cy++)
        {
            if (city.GetPixel(cx, cy).Equals(Color.Blue))
                return true;
        }
    }

    return false;
}

Upvotes: 1

Related Questions