1

Here is my code to create a grid with randomly generate mines. Problem is the mines is so diffuse, so when I count the mines for non-mine cells, it mainly have value 1, 2 and nearly don't have value 4, 5 , 6, 7. How to improve this algorithm?

Assume that number of columns, rows and mines are constant.

var r = new Random();
int columns, rows, TotalMine;
int[,] grid = new int[columns, rows];

int MineCount = 0;
int X = 0;
int Y = 0;

// Add Mines (This is so simple, it cause the problem)
while (MineCount++ < TotalMine)
{
    do
    {
        X = r.Next(columns);
        Y = r.Next(rows);
    }
    while (grid[X, Y] == -1);

    grid[X, Y] = -1; // -1 = have mine
}
NoName
  • 7,940
  • 13
  • 56
  • 108
  • I'm not sure where you're getting the values for the mine from? – Jack hardcastle Jan 07 '15 at 08:43
  • 1
    There is nothing inherently wrong with your code, but I guess you need to spread more mines to create more free cells surrounded by more than 2 mines. – Erich Kitzmueller Jan 07 '15 at 08:48
  • @ammoQ: yes, I did, but when I increase the number of mine, it cause there is too few cells with value = 0. – NoName Jan 07 '15 at 08:50
  • 1
    You could compare your mine fields with those of http://minesweeperonline.com/ (you can create a custom sized boards there) and check if it looks similar or not. – Erich Kitzmueller Jan 07 '15 at 08:52
  • 3
    You'd need some kind of clumping to do what you want I think, which is sort of anti-random. Create a random number of clumps, for each clump create a random radius, add a random number of mines to each clump within that radius based on random center. Now distribute the rest of the mines randomly. You'll need to tweak the parameters to that a few times to get it right but you should end up with mostly open space and the mines forming tighter groups where the clumps have been placed. – James Jan 07 '15 at 08:58
  • Your algorithm is inefficient, but it should work and should give you a good, random distribution. IMHO, in a game like Minesweeper, you should stick with true randomness, in spite of your apparent concerns. You can balance the relative distribution of mined cells (i.e. affecting the nearby-mine counts for empty cells) by adjusting the total number of mines (give the user an option), but doing anything behind that, you're basically giving the player extra information (i.e. once they see a little of the distribution, they can anticipate the rest), which completely changes the game. – Peter Duniho Jan 07 '15 at 17:53

1 Answers1

4

Your algorithm is perfectly fine and will create randomly spread mines (assuming the RNG is good enough).

One way improving I could imagine would be using a Game of Life algorithm to remove extremes, for examle cluttered fields where one field is surrounded by 7 or 8 mines.

Just iterate over all fields and count the surrounding mines (i.e. calculate the fields' numbers). If it's 7 or 8, remove one random mine next to it.

As an alternative, you could use perlin noise to create "clouds" and then only place mines randomly in areas where you've got at least a given "density". This way you can rather easily create bigger areas with nothing in between them.

You could also mix both ideas a bit:

  • Create a table or board as big as your playing field, with values being randomly distributed (true or false).
  • Do several iterations with a Game of Life algorithm to create some pattern (you should end up with "islands" or random structures).
  • Place your mines only within areas that are set to false (or true - whatever you choose).
Mario
  • 35,726
  • 5
  • 62
  • 78