Question: Write a java program that receives m and n as the number of rows and columns of the grid, respectively. In the second line, the
Write a java program that receives m and n as the number of rows and columns of the grid, respectively. In the second line, the user inputs an integer, b, showing the number of bombs placed in the grid. It then follows by b lines of input from the user, each referring to the row and column index of each one of the bombs.
After receiving grid dimensions, the number of bombs, and bomb locations, you will need to display (print) the completed grid showing the bomb cells with * and safe cells with the number of neighboring bombs to that cell.
This is the code i have but i am getting an error
package assignmentTwo;
import java.util.Scanner;
public class minesweeper {
static boolean isValid(int row, int col, int[][]grid){
if (row >= 0 && row= 0 && col
return true;
}
return false;
}
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int grid[][] = new int[m][n];
for (int i=0; i for (int j=0; j grid[i][j]=0; } } int b = sc.nextInt(); for (int i =0; i int row = sc.nextInt(); int col = sc.nextInt(); grid[row][col]=-1; } if (isValid (row -1, col -1, grid)) { if(grid[row -1][col -1]!=-1) { grid[row -1][col -1]++; } } if (isValid(row -1, col, grid)) { if(grid[row -1][col]!=-1) { grid[row -1][col]++; } } if (isValid(row -1, col +1, grid)) { if(grid[row -1][col +1]!=-1) { grid[row -1][col +1]++; } } if (isValid(row, col -1, grid)) { if(grid[row][col -1]!=-1) { grid[row][col -1]++; } } if (isValid(row, col +1, grid)) { if(grid[row][col +1]!=-1) { grid[row][col +1]++; } } if (isValid(row +1, col -1, grid)) { if(grid[row +1][col -1]!=-1) { grid[row +1][col -1]++; } } if (isValid(row +1, col, grid)) { if(grid[row +1][col]!=-1) { grid[row +1][col]++; } } if (isValid(row +1, col +1, grid)) { if(grid[row +1][col +1]!=-1) { grid[row +1][col +1]++; } } System.out.println(); for (int i = 0; i for (int j = 0; j if (grid[i][j]== -1) { System.out.print("* "); } else { System.out.print(grid[i][j]+" "); } } System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
