Minesweeper Help

I have a grid, and now I need to put bombs in some places randomly, I've tried looking at other peoples version, but I can't figure it out. I would like the grid to be empty and when choosing coordinates if the square is empty then I want it to show an O and if it's a bomb to reveal an X, any help would be great.

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
63
64
65
66
67
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;





int main()

{
	//void printgrid(int Rows = 8, int Columns = 8);
	int Rows = 8;
	int Columns = 8;



// Top Row Numbers
int i = 1, j;
for (j = 0; j <= 4 * Columns; j++) {
	if (j % 4 == 2)
		cout << i++;
	else cout << " ";
}
cout << endl;

//Left Side Numbers
for (i = 0; i <= 2 * Rows; i++) {
	if (i % 2 != 0)
		cout << (char)(i / 2 + '1');
	for (j = 0; j <= 2 * Columns; j++) {
		if (i % 2 == 0)
		{
			if (j == 0)
				cout << " ";
			if (j % 2 == 0)
				cout << " ";
			else cout << "---";
		}
		else {
			if (j % 2 == 0)
				cout << "|";
			else cout << "   ";
		}
	} //Right Side Numbers
	if (i % 2 != 0)
		cout << (char)(i / 2 + '1');
	cout << endl;
}


//Bottom Numbers
cout << " ";
for (j = 0, i = 1; j <= 4 * Columns; j++) {
	if (j % 4 == 2)
		cout << i++;
	else cout << " ";
}
cout << endl;


	cout <<Rows << Columns;

	return 0; }
you *draw* a grid but you don't seem to logically *store* a grid.
I think youll want a construct that represents the board, maybe
bool board[8][8];
and then set say 10 of them to true at random.
then when you print the board refer to this structure on whether to print an X or not when you draw the board. (this is typical -- you often in games keep the drawing of something and the data for it totally separated).
Could you show me how I'm very new to this
work this into your code.
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
#include <random>
int main()
{
    const int boardsize = 8;  //constants belong in a single place, with a name, 
                                        //not in code as 8 100 times.
    const int nummines = 10;
     //declare your board structure. 
    bool board[boardsize][boardsize]; //this should be vectors if you know them 
    int r,c; //rows and columns
    for(r = 0; r< boardsize; r++) //explicit assignment, there are shortcuts you could take
     for(c = 0; c< boardsize; c++)
           board[r][c] = false; 

     int count = 0; 
     std::default_random_engine generator; //set up random numbers
     std::uniform_int_distribution<int> distribution(0,boardsize-1)
     while (count < nummines)
     {
            r =  distribution(generator); //get a random row and column
            c =  distribution(generator);
             if(!board[r][c])   //just in case we get duplicates at random
                 {
                    board[r][c] = true; 
                    count ++;
                 }
     }      
}


see if you can get that working. as nummines approaches the total number of locations in the board, the loop may become a problem. for only 10 out of 64, its fine. if it were 60 out of 64, it would sit there getting repeats for a long time potentially, see? If that is an issue we can talk about a better way.

Topic archived. No new replies allowed.