# CECS 277 – Lab 3 – 2D Arrays solution

\$30.00

Original Work ?

5/5 - (1 vote)

## Minesweeper Board Creator

Generate an RxC array of integers, where R is the number of rows and C is the number of
columns, both inputted by the user and in the range of 5-10. Then prompt the user to
enter the number of mines that will be randomly scattered throughout the grid, also in the
range of 5-10.

Use the value 9 to represent the mines on the grid. Choose random
locations in the array to represent your mines. Fill the remaining array locations by
counting the number of mines in any adjacent squares (diagonals included). Display the
filled array. Error check all user inputs and Javadoc all methods.

Create the following methods:
1. placeMines( int [][] grid, int mines ) – for each mine, generate a
random row and a random column and check that location, if it is empty, then
place a 9 there, otherwise repeat the loop. Make sure that it cannot overwrite a
previously written 9, otherwise you’ll end up with the incorrect number of mines.

2. fillGrid( int [][] grid ) – for each spot in the array (except places with a
mine), count any 9’s in the surrounding eight spaces, then place the value of the
counter in that spot. Use a nested loop to iterate through the surrounding eight
spaces. Be careful not to overwrite any mines with a count.

3. displayGrid( int [][] grid ) – display the contents of the grid using a set of
nested loops.
The main method should prompt the user for the three inputs, then call each of the three
methods in the above order.
Example Outputs:
Minesweeper Maker
Enter number of Rows (5-10): 5
Enter number of Columns (5-10): 5
Enter number of Mines (5-10): 5
9 2 2 1 1
2 9 2 9 1
2 2 2 1 1
9 2 1 1 0
1 2 9 1 0
Minesweeper Maker
Enter number of Rows (5-10): 6
Enter number of Columns (5-10): 8
Enter number of Mines (5-10): 10
0 0 1 1 1 0 0 0
1 1 1 9 3 2 2 1
9 3 3 4 9 9 3 9
9 3 9 9 3 3 4 9
1 2 2 2 1 1 9 2
0 0 0 0 0 1 1 1