0

This is the code that create the map variable array of 0 and 1:

public static int[,] CreateArray(int numberOfRows, int numberOfCols)
        {
            int[,] map = new int[numberOfRows, numberOfCols];
            for (int i = 0; i < numberOfRows; ++i)
            {
                Random rnd = new Random();
                for (int j = 0; j < numberOfCols; ++j)
                {
                    map[i, j] = rnd.Next(0, 2);
                }
            }

            return map;
        }

And this is how i use it call the method:

int[,] map = CreateArray(8, 8);

But each time i'm running the game i'm getting a different map like this: This is an image of two random maps i merged them to one image:

Maps

But i want to get random path the blue color which is the number 1 in the array if im not mistake i want to create like randon path with it. For example this is a map if i make the array of 0 and 1 manualy:

int[,] map = new int[,] 
{
    {0,0,1,0,0,0,0,0,},
    {0,0,1,1,0,0,0,0,},
    {0,0,0,1,1,0,0,0,},
    {0,0,0,0,1,0,0,0,},
    {0,0,0,1,1,0,0,0,},
    {0,0,1,1,0,0,0,0,},
    {0,0,1,0,0,0,0,0,},
    {0,0,1,1,1,1,1,1,},
};

And the result is:

Map with path

I'm using xna with two images to draw the green and blue. But the problem is to create the random map each time with a random path and not just blue color and green.

Shiran Yeyni
  • 35
  • 2
  • 9

3 Answers3

3

You have 2 problems here. The first will fix the striping you are seeing and is pretty easy to fix. For each row you are making a new pseudo random number generator. Since those are typically seeded with the current time, on a small enough map you will get the same seed for each row and thus the same "random" output. Move the initialization outside the outer loop so you end up with this:

        Random rnd = new Random();
        for (int i = 0; i < numberOfRows; ++i)
        {
            for (int j = 0; j < numberOfCols; ++j)
            {
                map[i, j] = rnd.Next(0, 2);
            }
        }

The second problem you will have now is that there is no guarantee that the "ones" in your matrix will be connected. That's a bit tougher of a problem to solve and depends on what you are ultimately going for.

Becuzz
  • 6,846
  • 26
  • 39
  • The real problem is #2, creating a random river path through the map. Using the double iterative loop won't fix this – Jon Aug 11 '14 at 17:29
1

If you are trying to draw a random river through your map, maybe take a different approach. Start with the river at a random EDGE coordinate such as (0,4). Then have a do/while loop that looks at which adjacent tiles are green. Pick an adjacent tile using Random.Next().

E.g.

currentRiverTile = GetRandomEdgeTile();
do
{
  map[currentRiverTile.X, currentRiverTile.Y] = 1;

  var adjacentTiles = GetAdjacentTiles(currentRiverTile);
  var rnd = new Random();

  currentRiverTile = adjacentTiles(rnd.GetNext(0,adjacentTiles.Length));

} while (!IsEdgeTile(currentRiverTile));

This will create a random river through your map. Fill out the code for checking whether a tile is an edge (check X = 0 || x == 8 || y == 0 || y == 8).

Jon
  • 3,230
  • 1
  • 16
  • 28
  • I just realized that this algorithm could spiral the river into itself and never reach another edge. You'll want to adjust it to make sure this can't happen (maybe if you start on one edge, only look at adjacent tiles pointing away from that edge (or sideways), that way the river will never curl back around into the middle. – Jon Aug 11 '14 at 17:31
0

The problem is that you are creating a new Random object every iteration through your loop. This causes issues because the Random is seeded by the time. If you create many in a very short period of time, then it will use the same seed, and therefore be the same random.

As AlexD said, pull your random code out of the loop. That way, it will create it only once. This problem has led me to the end result. Try to use only 1 Random object for the entire program. Doing it this way eliminates the repeating that might occur when using multiple random objects.

So, your code should look like:

public static int[,] CreateArray(int numberOfRows, int numberOfCols)
    {
        int[,] map = new int[numberOfRows, numberOfCols];
        Random rnd = new Random();

        for (int i = 0; i < numberOfRows; ++i)
        {
            for (int j = 0; j < numberOfCols; ++j)
            {
                map[i, j] = rnd.Next(0, 2);
            }
        }

        return map;
    }

Take a look at this StackOverflow describing a similar issue. Also, for some more about Random, look at DotNetPerls.com and the MSDN. The MDSN in particular is a resource I feel that every programmer should learn to love, as many of the issues can be solved by looking through it.

Community
  • 1
  • 1
Gilbrilthor
  • 432
  • 4
  • 14