Emphasis on: 2-dimensional arrays and structs You’ve probably played the game Mi
ID: 3585222 • Letter: E
Question
Emphasis on: 2-dimensional arrays and structs
You’ve probably played the game Minesweeper. The minefield for the beginner level is a 9x9 grid with 10 mines seeded randomly in the grid (see screen capture below). A number in a square indicates how many mines are adjacent (up, down, and diagonally) to that square. Any squares which are not adjacent to a mine are blank.
You are to write a C++ program which will prepare the grid for a game to be played.
Note: You’re not being asked to actually play the game. No graphical interface is required or expected – displaying characters to the black console screen or to a text file is all this assignment is asking for.
Requirements:
Implement the 9x9 grid as a two-dimensional array or vector. You could choose to store either character or integer values.
Write a function to place 10 mines randomly in the grid. It should call: another function which obtains and returns two random numbers for each mine location – one for the row and another for the column. The grid position (row and column) should be implemented as a struct which the function returns.
Suggestions for representing a mine position:
the letter M
an asterisk * (which looks somewhat like the mines in the actual game)
an “at” sign @
the digit 9 (the maximum value in any other square would be 8 which would mean the square is totally surrounded by mines)
other symbol of your choice.
Assign the number values to the squares in the grid. This would be another good place for a function.
Display or print a copy of your starting grid, showing the values assigned for each square. I recommend displaying a blank space between the values in the rows to make it look more like a square in addition to making it much easier to read. And this would also be a good function. Since blank spaces (not adjacent to a mine) won’t be very visual on a console screen, display some other character such as a zero or a period.
If you want to try some ASCII art, print symbols to form boxes to contain your grid entries. To print the ASCII graphics symbols for which there is no key on the keyboard, use the put(n) function where the parameter n is an ASCII number. The function will display the character represented by the ASCII number. There is a file with all of the ASCII codes in the top-level folder in Doc Sharing. Example: cout.put(65); would print A .
If you print the boxes, it would not be necessary to print anything to represent a blank in a square.
Random Number Generator
A random number generator will be used to determine the coordinates of squares where a mine will be placed.
The standard library function rand() returns a random number such that
0 <= rand() < 32767. To convert this into a number within a specific range 1 .. n, use the algorithm
rand() % n + 1
For instance, if we were simulating rolling dice, we would want a number in the range 1 .. 6, so we could code something like: die = rand() % 6 + 1 . To obtain a number within the range 0 .. n, use the algorithm
rand() % (n + 1)
The random number generator is initialized by a call to the standard library function srand(). You can pass to srand() a specific value (good for testing purposes until you're sure your program is working correctly), or you can initialize it with a value from the system clock (for a random starting seed):
srand (237); or srand ((unsigned) time (NULL));
To use rand() and srand(), include stdlib.h, and to use the time() function, include time.h:
#include <cstdlib>
#include <ctime>
There is a sample program demonstrating the use of rand and srand in the Program Assignments folder in Doc Sharing.
srand (237); // you only call srand once at the beginning of your program
To get the coordinates for the 10 mine positions::
for (n = 1; n <= 10; n++)
{
row = rand() % 9;
col = rand () % 9;
}
Evaluation Checklist
Name, assignment #, compiler, and purpose of program provided (8)
Code has good programming style including internal comments (8)
No global variables used (pass parameters instead) (8)
Used a 2-dimensional array for the minefield (8)
Used a struct to represent a position for a mine (8)
Used a random number generator to get position for mines (8)
Placed 10 mines (20)
Placed numbers around the mine positions (20)
Output is nicely formatted and easy to read and understand (12)
Explanation / Answer
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int grid[9][9]; /* grid for game*/
struct gridPosition
{
int row; /* for row postion*/
int col; /* for column position*/
};
struct gridPosition minePosition() /* return the random position of mine*/
{
struct gridPosition p;
p.row=rand()%9;
p.col=rand()%9;
return p;
}
void placeMine() /* Place 10 mine at random location*/
{
for(int n=0;n<10;n++)
{
struct gridPosition M= minePosition();
grid[M.row][M.col]='M'; /* M for mine*/
}
}
int main()
{
srand (237);
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
grid[i][j]=0; /* Initialised the grid value*/
}
}
cout<<"Before placing the mine"<<endl;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
cout<<"| "<<grid[i][j]<<" |";
}
cout<<endl;
}
placeMine(); /* place mine*/
for(int i=0;i<9;i++) /* This nested for loop will check the grid loaction sorounded by how many mines*/
{
for(int j=0;j<9;j++)
{
if(grid[i-1][j-1]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i-1][j]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i-1][j+1]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i][j-1]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i][j+1]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i+1][j-1]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i+1][j]=='M'&& grid[i][j]!='M')
grid[i][j]++;
if(grid[i+1][j+1]=='M'&& grid[i][j]!='M')
grid[i][j]++;
}
}
cout<<"After placing the mine"<<endl;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
cout<<"| "<<(grid[i][j]<<" |";;
}
cout<<" "<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.