Minesweeper Random Array

Hi, Im trying to make Minesweeper in C++.

Everything is working, Win / loss detection, already selected / flagged square detection, square flagging, menu, board etc.

But, Im using fixed arrays.

Ive been trying to make a Random Array but just cant get it to work. Here is my Test code for random array for a 24 x 24 grid (I want mines set to 99 but cant work out how to do that either so just set srand to 6 as that gives around 100 mines.

Here is my code, The Random mine placement is working fine its just increasing the surrounding squares count that I cant get working. any help would be great Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string> 
#include <cmath>
#include <stdio.h>
#include <crtdbg.h>
#include <limits>
#include <time.h>
#include <iomanip>
using namespace std;   

int main()
{
   int mines=0;
 int boardm[24][24];
 srand((unsigned)time(0));
 
//------------------------------- Random mine placement, seems to work.
 
  { for(int X=0; X<24; X++){
   for(int Y=0; Y<24; Y++){
        boardm[X][Y] = (rand ()%6);
      if (boardm[X][Y] == 0)               // Sets all zeros to -1 that indicate mines
         boardm[X][Y] = -1;
      if (boardm[X][Y] == -1)              //used for mine counter
         mines++;
      if (boardm[X][Y] != -1)            //Sets all other numbers to 0 
         boardm[X][Y] =0;
   
          cout << boardm[X][Y] << endl;
      
 } 
 }
  }
   
cout << "There is "  << mines << " Mines\n\n";

//-------------------- My attempt at increasing the surround squares count.

{
   for(int X=0; X<16; X++){
   for(int Y=0; Y<16; Y++){
      {if (boardm[X][Y] == -1)
      boardm[X+1][Y] ++;
	  if (boardm[X][Y] == -1)
      boardm[X+1][Y+1] ++;
	  if (boardm[X][Y] == -1)
      boardm[X+1][Y-1] ++;
	  if (boardm[X][Y] == -1)
      boardm[X-1][Y] ++;
	  if (boardm[X][Y] == -1)
      boardm[X-1][Y+1] ++;
	  if (boardm[X][Y] == -1)
		  boardm[X-1][Y-1] ++;}
	 

   cout << boardm[X][Y];
   }}
}

}


Thanks again
Last edited on
@steve 2012

Here is your program, with the array, boardm, zeroed out, meaning no mines. Then, 99 mines are placed in random locations on the board. Wasn't really sure what the last part of your program above, did, so I left it out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Mine Sweeper.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string> 
#include <cmath>
#include <stdio.h>
#include <crtdbg.h>
#include <limits>
#include <time.h>
#include <iomanip>

using namespace std;   

int main()
{
	int mines=0, X, Y;
	int boardm[24][24] = {0}; // Set whole array to zeroes
	srand((unsigned)time(0));

	//------------------------------- Random mine placement, seems to work.

	cout << "Placing Mines.." << endl;
	do
	{
		do
		{
			X = rand()%24; // get a random row
			Y = rand()%24; // get a random column
		}while (boardm[X][Y] !=0 ); // do again if a mine is located there already
		boardm[X][Y] = 1; // place a mine at location
		mines++; // increase mine count
	} while (mines < 99); // Keep doing this until we have 99 mines

	for(X=0; X<24; X++)
	{
			for( Y=0; Y<24; Y++)
			{
				cout << boardm[X][Y] << " "; // Print out Mine Sweeper board
			}
			cout << endl; // After print a row, do a new line, and start a new row
	}
	cout << "There are "  << mines << " Mines\n\n";
}
Last edited on
if you want the numbers to work with whitenite's code you could put
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(X=0; X<24; X++)
	{
			for( Y=0; Y<24; Y++)
			{
					if (boardm[X][Y] == -1)
					{
						boardm[X+1][Y] ++;
						boardm[X+1][Y+1] ++;
						boardm[X][Y+1] ++;
						boardm[X-1][Y+1] ++;
						boardm[X-1][Y] ++;
						boardm[X-1][Y-1] ++;
						boardm[X][Y-1] ++;
						boardm[X+1][Y-1] ++;
     					}
			}
	}


between his do while loop and his for loop (line 35).

The reason that it wasn't working was probably because you were changing some of the spaces AFTER printing them. It would shown up wrong during at least that print.

As for the random placement, the reason why rand%6 was giving you ~100 mines is because you were giving each of 576 spaces a 1/6 chance of having a mine. This gives you about 96 mines per generation (576/6). In order to get exactly 100 mines in random locations you need to take 100 mines and randomly determine a position for them, rather than giving each spot a chance to randomly have a mine. This is what whitenite1's code does.


[Edit] oops, his code makes mines as "1"s. For my code to work you have to make the mines as -1 by making line 32 of whitenite's code =-1 instead of =1
Last edited on
thanks all I will give that a go. The bottom section was an attempt to increase each surrounding square that was next to a mine by one.

Is that the correct method for doing it?

Thanks
Thanks again, that random generation of the mines worked but I dont think my method of increasing each surrounding mined square is working :(

Am I on the right tracks using that "if" statement to set the numbers for the squares surrounding the mines or should I be doing something else?

Thanks
Ok guys I think I have got it now after doing some researching :D

Thanks for the help
Topic archived. No new replies allowed.