Struggling with C++ Minesweeper Game

Pages: 1234
I have been trying to get this game of minesweeper working for ages and for the life of me cannot figure out how to get it to correct display the board, the game is supposed to have three levels but I am only doing one and then I can use the code to do the rest.
Last edited on
I think you can use this to display the game board:

1
2
3
4
5
6
for (rows=0; rows<8; rows++) {
    for (columns=0; columns<8; columns++) {
         cout<<columns<<rows<<' ';
    }
    cout<<endl;
}


What do you need these for?

string rows, columns;
Their names are identical to the intergers used in the beginner function.

You need to assign the bombs to the board before the player starts moving, otherwise you can't tell if player hits a bomb.
Hi thanks for the swift reply, to be 100% honest with you I don't have a clue where to go from the above as C++ isnt a strong point of mine.

I can see what you mean about me repeating it so I will remove what you have said. How do you suggest I go about making this a working game?

Such as how would I randomise the placement of bombs, looking for 10 bombs on the beginner level.

Thank you very much for this help as I have been racking my brains about this for ages.
You can randomly assign bombs pretty easily in a loop. Once I get to my computer, I can show you an example if need be
That would be a great help. Thanks

Should of signed up sooner lol
Right I have managed to get it to show a board with the help from AbR, the only problem is that the board is filled in with loads of random numbers.

How do I go about changing all these numbers to say all X's?

How do I get mines on the board?

How do I get it to allow a move to be played?

Sorry for all the questions, I can for once see light at the end of the tunnel with the help from this forum.
Right I have managed to get it to show a board with the help from AbR, the only problem is that the board is filled in with loads of random numbers.

Supposedly these aren't random numbers, but the number of columns and rows (ie column 4, row 3 is displayed as '32' (because the first position og both columns and rows is 0 (zero)))

How do I go about changing all these numbers to say all X's?

My suggestion (and there may be smarter ways to do this) is to make two two-dimensional arrays: gameboard[width][height] (which shows what the player can see) and masterboard[width][height] (which holds the actual positions of the bombs. I use width and height instead of specific values, so you can use the same code, no matter what difficulty the game is played at (width, height and number of mines can be passed from the menu to the game function as parameters). I'm making some example code for you, but it is at home on my computer and right now I'm at work.

Using a slightly modified version of the double loop I posted earlier, you can fill the arrays with dots or something:

1
2
3
4
5
6
for (rows=0; rows<height; rows++) {
    for (columns=0; columns<width; columns++) {
         gameboard[colums][rows]=(char)0xFA; //fills gameboard with '·'
         masterboard[colums][rows]=(char)0xFA; //fills masterboard with '·'
    }
}


How do I get mines on the board?

You can do this:
1
2
3
4
5
6
7
8
9
int minecounter = 0;
do {
    coordx = rand()%width;
    coordy = rand()%height;
    if (masterboard[coordx][coordy]==0xFA; { //if coordinates is empty (equals a dot)
        masterboard[coordx][coordy]='b';     //place a bomb
        minecounter++;
    }
} while (minecounter < mines); //do this loop until all mines are placed 


How do I get it to allow a move to be played?

You'll need to check the users input (two coordinates) against the masterboard to see what the player finds. Each time the user makes a move, you'll need to update the gameboard.

Before you start implementing these snippets, you might want to consider if your program has the most beneficial layout. Much of this code could go into small functions, which could be called from the main game loop, otherwise you may end up with a really long code.
The way I have laid it out is the only way I know how to, have you got any tips on how I can change it to make the layout better?

Thanks
Right, I am now lost, I have tried to incorporate the mines and change the numbers to dots, as well as create a loop but after a good 2-3 hours I cannot get it to run, so I have gone back to my partly working version.

So in simple terms, how do I make this functional?

Sorry for being so thick a this, but once done hopefully I can learn from it.

Thanks
closed account (o3hC5Di1)
Hi there,

How about you post your updated code and we'll go from there? :)
That way we can tell you what to change in order for the numbers to become dots, etc.

All the best,
NwN
Looks as if someone posted some code on how to get bombs into your board. I havent had time to get to a computer until now.

Anyway, are you familiar with terms front end and back end? You don't want to mix them. You should have a backend representation of your board. As well as a graphical front end. The front end gets all of its information from the back end. If you keep these separated, you shouldn't have a problem in getting this to work
Deleted to make sapce
Last edited on
closed account (o3hC5Di1)
Hi there,

I don't think you need two boards here.
Just use one board array, put some mines into it and register the guesses into it.
If you fill in the mines in your beginnerboard[][] array manually for now (worry about randomizing it later), i.e.:

1
2
beginnerboard[2][3] = 'b';  //b marks a bomb
beginnerboard[4][1] = 'b';


Now everytime the player guesses you can check his guess:

1
2
3
4
5
6
if (userchoice=='M' && beginnerboard[X][Y] == 'b') 
{cout << "Boom!";}
else if (userchoice=='B' && beginnerboard[X][Y] == 'b') 
{ beginnerboard[X][Y] == 'x' }  //x marks a guessed bomb
else
{ beginnerboard[X][Y] == 'g' }  //g is for guessed empty 


As for displaying the board, you don't need to output the variables columns and rows, that's what's displaying the numbers.

After the previous, you would do something like:

1
2
3
4
5
6
7
8
9
10
11
for (rows=0; rows<9; rows++) {
    for (columns=0; columns<9; columns++) {
         if (beginnerboard[rows][columns] == 'g')
        { cout<<'- ';} //dash if guessed and no bomb
        else if (beginnerboard[rows][columns] == 'x')
        {cout<<'X '} // X marks a marked bomb
        else
        { cout<<'. '; } //point if not guessed, or if bomb
    }
    cout<<endl;
}


You would have to add some validation still, if a marked bomb field actually contains a bomb or not, but first let's get this going.
Hope that makes sense to you?

All the best,
NwN

Ps: the reason I'm saying he won't need two boards is that displaying the board can be done by reading the contents of the existing array, which for this simple game can contain the guesses imho. Please do correct me if I'm wrong.
Last edited on
AbR I kinda understand where you are coming from,

the problem that I have is that when ever I change the number to a word or a int, it doesn't accept it?

Thanks for all this help

1
2
	char gameboard [8][8] = {0};
	char masterboard [width][height] = {0};
Deleted to make space
Last edited on
1
2
char beginnerboard [9][9] = {0};
char guesses[8][8] = {0};

The arrays are not the same size - a problem will occour here:

1
2
3
4
5
...
for (rows=0; rows<9; rows++) {
    for (columns=0; columns<9; columns++) {
         if (beginnerboard[rows][columns] == 'g
... 

When colums = 8 the values of beginnerboard is [0][8] this position is off the end of your array.
beginnerboard is declared like this [0-7][0-7].

Just make them the same size and the first problem will go away.

edit: But your code still needs some trimming to work properly.
You have braces {} missing several places, making your loops fail or misfunction.
To display the map correctly line 28 should go after the end brace and have an extra end brace after it (to close the two for-loops).
From line 31 your initial code starts to conflict with your edits, userchoice has not been declared at this point.

Consider removing lines 31-71, and then rewrite the code, starting by taking user input.
Last edited on
Ps: the reason I'm saying he won't need two boards is that displaying the board can be done by reading the contents of the existing array, which for this simple game can contain the guesses imho. Please do correct me if I'm wrong.


Theres nothing really wrong with using one board in this situation as it's relatively simple. But it is good practice to learn how to split your code into front end and back end. It makes life much easier on larger projects.

Where exactly is your code failing, and what is it doing? Sorry, but the lack of indenting and formatting is making it tough to follow. Also would do some help to break your functions up more. But that can be for another day.

1
2
3
4
if (beginnerboard[rows][columns] == 'g')
        { cout<<'- ';} //dash if guessed and no bomb
        else if (beginnerboard[rows][columns] == 'x')
        {cout<<'X ';} // X marks a marked bomb 


This could be a culprit. From what I see, nowhere in your array are the characters 'g' or 'x'.
You also have places like this
if (userchoice=='M' line 31, which dont make much sense. You never actually get input for userchoice
right, you lot must have the patients of a saint. lol

Sorry about the indentations still a newbie as you can tell.

The message now states that userchoice is not being initialized, which i suspect means that there is no input for user choice?

so would that then mean somewhere I have to put userchoice = [x][y]??

I am starting to understand this a little bit more, I think.

Cant seem to work out what the G is for? the X is mentioned as an int at the top?

Thanks
Well, userchoice sounds like input to me. Is that so? If so, at some point before using it you need to something like

std::cin >> userchoice

Well, the g and x are both literals, not variables. Random note,
'X ' on line 25 is not correct. It also happens on another place that I see. Anyway, single quotes is used for chars. Chars are just single characters, and you have a single character plus a whitespace, which also counts as a character. So if you want to do something like that, you need double quotes.

Is this program even compiling right now?
Pages: 1234