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 | | |
--------------------
| | | | |
--------------------
| | | | |
--------------------
|