Mine Sweeper Assignment - saving random grid

Here is the assignment: http://acc6.its.brooklyn.cuny.edu/~cisc3110my2/project/minesweeper.html

I've made the code so that a random grid (made from 2D vector) will be created every time the program is run. The grid is valid every time (i.e., the correct number of mines are always adjacent to the number).


However, right now I need to figure out how to 'save' this randomly created grid so that I can reprint it within the same run.

The idea is that I will ask the user for the row and column #, and then reprint the exact same grid each time, after the question. This way the use won't have to keep scrolling up to see the grid.

Here is the snippet of code I used to make the random grid:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
     for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            int randN = (rand()%10)+1; // make random #

            // print blanks in random grid boxes (these blanks may be -1's (which are mines), or valid numbers. 
            if(grid[i][j] == -1 || randN==1 || randN==2 || randN==5 || randN==9 || randN==10)
cout << setw(3) << "| " << cgrid[i][j]; // cgrid vector is just filled with blanks
             


            else // rest of the boxes will be valid numbers
           cout << setw(3) << "| " << grid[i][j]; // this is the full valid grid. But only some numbers //are printed
        }

        cout << endl;
        cout << " --------------------";
        cout << endl;
    }


Mines are represented by -1's.

As you can see, I used 2 separate 2D vectors to make this one grid. First 2D vector is with the numbers (-1 as mines, or numbers 1-8). The other is a 2D vector of blanks (i.e. empty strings), which I used to make blank boxes.

Any idea how I can save this randomly created grid, possibly put it into a new function so I can call that function every time I need to reprint it?

The output when I run the program may look like this:

[The full grid is printed on top for my own reference. The actual grid the user will see is the 'GAME GRID']


  1 -1 -1  2 -1
  1  2  3  3  2
  2  3  4 -1  2
 -1 -1 -1 -1  3
 -1 -1 -1  4 -1


GAME GRID

 | 1 |   |   | 2 |
 --------------------
 | 1 | 2 | 3 |   |
 --------------------
 |   | 3 |   |   |
 --------------------
 |   |   |   |   |
 --------------------
 |   |   |   |   |
 --------------------

Last edited on
Here is the snippet of code I used to make the random grid:

No, it isn't.

Can you not return the grid from the function that creates it?
No, it isn't.


?

Can you not return the grid from the function that creates it?


No I can't. I tried calling the function that creates the grid from the second function im working with, but (as expected), it creates a different grid. The problem is that im using the rand function to place the -1's in random boxes, then im also using rand() to place numbers and mines (-1's) in random boxes while leaving others blank. This creates a different grid every time.

And I can't get rid of rand() because I need to create a random grid for every run (else its not really a game, as players can just memorize where the mines are).

Here was my output when I called the Create_Grid function:



GAME GRID

 |   |   |   |   | 1
 --------------------
 | 3 |   |   | 4 |
 --------------------
 |   | 3 |   |   |
 --------------------
 |   | 1 | 1 | 1 | 1
 --------------------
 | 1 | 1 | 0 |   | 0
 --------------------

How many mines do you think there are? 5

Enter row and column # (separated by space) for mine #1: 3 4
Correct! +1 points.



GAME GRID

 |   |   |   |   |
 --------------------
 | 3 |   |   | 4 |
 --------------------
 |   | 3 | 3 |   |
 --------------------
 |   |   |   |   | 1
 --------------------
 | 1 |   |   | 0 |
 --------------------

Enter row and column # (separated by space) for mine #2:


As you can see the two grids are different. I also printed the full grids just to make sure, and indeed the full grids are completely different.

What complicates this more is that im using TWO 2D vectors to make this one grid (one with the numbers, and the other simply holds empty strings). I use a combination of both to make the grid. I cout the numbered vector for numbers and mines and cout the empty strings vector for empty boxes (this is all done 'randomly').

I know this may be difficult to figure out, but any help in this will be appreciated.
Last edited on
Arslan7041 wrote:
Here is the snippet of code I used to make the random grid:
cire wrote:
No, it isn't.
Arslan7041 wrote:
?

That snippet is displaying cells (randomly) that aren't bombs. There is no creation here. There are no grids being made.


No I can't. I tried calling the function that creates the grid from the second function im working with, but (as expected), it creates a different grid.

Well, of course if you call a function that creates a grid, it creates a different grid. Save the grid created from the first call. Use that grid until you're finished with it.


What complicates this more is that im using TWO 2D vectors to make this one grid (one with the numbers, and the other simply holds empty strings)

From what I can see, your 2nd grid is completely superfluous. Just output a space instead of looking up which space to output. One is as good as another.
Aha!

I figured it out. I simply made a third 2D vector and saved each element of the original onto it. I made a function which prints this saved vector. It works.

From what I can see, your 2nd grid is completely superfluous. Just output a space instead of looking up which space to output. One is as good as another.


I tried that but for some reason it doesn't work. I replaced all my "cgrid" with spaces (of same length as were in the vector), but it messes up the spacing every time.

Here's what my output looks like with spaces instead of the vector:

  2 -1  2 -1  1
 -1  2  2  1  1
  2  2  0  1  1
 -1  2  0  1 -1
 -1  2  0  1  1


GAME GRID

 ---------------------
|   |    | 2|    | 1 |
 ---------------------
|    | 2|   |    | 1 |
 ---------------------
 | 2 | 2|   |    | 1 |
 ---------------------
|    | 2 | 0|   |    |
 ---------------------
|    | 2|    | 1|    |
 ---------------------

How many mines do you think there are?


Notice my spacing is messed up.

I had expected it to work, but for some reason it doesn't.
I think I'll be coming back to this thread for more questions. But thank you cire for your help.
Topic archived. No new replies allowed.