kharmaculpepper
26.02.2020 •
Computers and Technology
Minesweeper is a single-player game in which the player continuously selects different cells of an "m × n grid. each cell of the grid is either occupied by a bomb or is a safe cell. if a cell is occupied and the player selects the cell, the player loses. otherwise, the selected cell shows the number of bombs in the neighboring cells. a neighbor cell is a cell next to the current cell in the horizontal, vertical, and diagonal direction write a 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.
Solved
Show answers
More tips
- S Style and Beauty Unbelievable Hairstyles for Long Hair - 2021 Trends...
- H Health and Medicine How to Whiten Teeth and Get the Perfect Smile...
- F Food and Cooking How to Make Thin Pancakes: Recipe and Tips...
- S Style and Beauty Is Hot Scissor Haircutting Beneficial or Dangerous?...
- S Style and Beauty How to Get Rid of Under Eye Bruises?...
- F Food and Cooking Is Bacon Good for You?...
- S Style and Beauty Discover the Art of Nail Design: How Do You Paint Your Nails?...
- P Philosophy How to Develop Extrasensory Abilities?...
- O Other Everything You Need to Know About Kudyabliks...
- C Computers and Internet The Twitter Phenomenon: What it is and How to Use it...
Answers on questions: Computers and Technology
- C Computers and Technology Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer...
- C Computers and Technology Where does the word FILM come from when referring to movies? 15 p the glazed look of people watching a movie the development process when creating a movie the semi-transparent cellulose...
- C Computers and Technology Kahnacademy sucksi am so mad i want to k .. ill myself...
- C Computers and Technology How to tell if your cell phone is being tracked tapped or monitored by spy software?...
- H Health Anew patient comes to your clinic, and you are taking his medical history. he reports, i take insulin shots for that disease that gives you high blood sugars. the correct medical...
- H History Why did chamberlain believe there would be peace in our time...
- M Mathematics How many trees are there whose complement is also a tree?...
- H History Why do some people believe it was a mistake for the united nations and the united states not to occupy iraq after the persian gulf war?...
- M Mathematics Am i correct for 10? ( only correct answer lots of )...
- H Health Tracing the cause of parkinson s disease is difficult because pd is...
Ответ:
//importing Scanner class to take input from the user
import java.util.Scanner;
public class MineSweeper {
public static void main(String[] args) {
// TODO Auto-generated method stub
// creating an object to the scanner class
Scanner sc = new Scanner(System.in);
int m,n;
System.out.print("Enter the number of rows:");
m = sc.nextInt();
System.out.print("Enter the number of Columns:");
n = sc.nextInt();
//creating grid with all spaces
char grid[][] = new char[m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
grid[i][j]=' ';
}
}
//taking number of bombs as input from the user
System.out.print("Enter the number of bombs:");
int b = sc.nextInt();
//putting the bombs in grid
for(int i=1;i<=b;i++) {
System.out.println("Enter the bomb position:");
System.out.print("row:");
int row = sc.nextInt();
System.out.print("Column:");
int col = sc.nextInt();
if(row>=0 && row<m && col>=0 && col<n)
grid[row][col]='*';
else {
System.out.println("Invalid row or column....");
i-=1;
}
}
int count ;
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(grid[i][j]!='*') {
count = numberOfBombs(grid,i,j,m,n);
char ch = intToChar(count);
grid[i][j] = ch;
}
}
}
// displaying the output of grid
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
System.out.print(grid[i][j]+" ");
}
System.out.println();
}
}
// converting digit ot char
public static char intToChar(int i) {
switch(i) {
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
default :
return '-';
}
}
//finding the number of bombs in neighbourhood of position in grid
public static int numberOfBombs(char grid[][], int i , int j, int m, int n) {
int count = 0;
if(i+1<m) {
if(grid[i+1][j]=='*')
count +=1;
if(j+1<n) {
if(grid[i+1][j+1]=='*')
count+=1;
}
if(j-1>=0) {
if(grid[i+1][j-1]=='*')
count+=1;
}
}
if(i-1>=0) {
if(grid[i-1][j]=='*')
count +=1;
if(j+1<n) {
if(grid[i-1][j+1]=='*')
count+=1;
}
if(j-1>=0) {
if(grid[i-1][j-1]=='*')
count+=1;
}
}
if(j+1<n) {
if(grid[i][j+1]=='*')
count +=1;
}
if(j-1>=0) {
if(grid[i][j-1]=='*')
count +=1;
}
return count;
}
}
Ответ:
I,dk and im sorry i just needed some points sorry
Explanation: