Question

In: Computer Science

Minesweeper. Minesweeper is a 1960s era video game played on an m-by-n grid of cells. The...

Minesweeper. Minesweeper is a 1960s era video game played on an m-by-n grid of cells. The goal is to deduce which cells contain hidden mines using clues about the number of mines in neighboring cells. Write a program Minesweeper.java that takes three integer command-line arguments m, n, and k and prints an m-by-n grid of cells with k mines, using asterisks for mines and integers for the neighboring mine counts (with two space characters between each cell). To do so,

  • Generate an m-by-n grid of cells, with exactly k of the mn cells containing mines, uniformly at random.
  • For each cell not containing a mine, count the number of neighboring mines (above, below, left, right, or diagonal).

Submission. Submit a .zip file containing DiscreteDistribution.java, ThueMorse.java, Birthday.java, and Minesweeper.java. You may not call library functions except those in the java.lang (such as Integer.parseInt() and Math.sqrt()). Use only Java features that have already been introduced in the course (e.g., loops and arrays, but not functions).

Solutions

Expert Solution

Hello! Here the answer to the anbove problem. Hope it helps. If you are satisfied with the answer, do consider upvoting. Stay stafe and keep learning!

Minesweeper.java

public class Minesweeper {

   public static void main(String[] args) {

       int m = Integer.parseInt(args[0]);
       int n = Integer.parseInt(args[1]);
       int k = Integer.parseInt(args[2]);

       //defining 2-d matrix arr[1-m][1-n];
       char arr[][] = new char[m+1][n+1];
       int tmp = k;
       int row_num = (int) ((Math.random()*(m)) + 1);
       int column_num = (int)((Math.random() *(n)) + 1);
       //generating k mines randomly.
       while(tmp>0)
       {
           //keep generating row and column numbers till the time we have
           //a position which does not contain '*'.
           while(arr[row_num][column_num]=='*')
           {
               //generates random number between [1-m].
               row_num = (int) (Math.random()*(m) + 1);
               //generates random number between [1-n].
               column_num = (int)(Math.random()*(n) + 1);
           }
           //setting generated position as '*'.
           arr[row_num][column_num] = '*';
           tmp--;
       }
       for(int i=1;i<=m;i++)
       {
           for(int j=1;j<=n;j++)
           {
               if(arr[i][j]!='*')
               {
                   //count the number of neighbours which are '*';
                   int count = 0;
                   if(i-1>=1 && i-1<=m && j>=1 && j<=n && arr[i-1][j] == '*')
                       count++;
                   if(i-1>=1 && i-1<=m && j+1>=1 && j+1<=n && arr[i-1][j+1] == '*')
                       count++;
                   if(i>=1 && i<=m && j+1>=1 && j+1<=n && arr[i][j+1] == '*')
                       count++;
                   if(i+1>=1 && i+1<=m && j+1>=1 && j+1<=n && arr[i+1][j+1] == '*')
                       count++;
                   if(i+1>=1 && i+1<=m && j>=1 && j<=n && arr[i+1][j] == '*')
                       count++;
                   if(i+1>=1 && i+1<=m && j-1>=1 && j-1<=n && arr[i+1][j-1] == '*')
                       count++;
                   if(i>=1 && i<=m && j-1>=1 && j-1<=n && arr[i][j-1] == '*')
                       count++;
                   if(i-1>=1 && i-1<=m && j-1>=1 && j-1<=n && arr[i-1][j-1] == '*')
                       count++;
                   //converts the number to char value. Ex: 2 is converted to '2';
                   arr[i][j] = (char)(count+48);
               }
           }
       }

       //printing the resulting 2-d matrix.
       System.out.println("Final result:");
       for(int i=1;i<=m;i++)
       {
           for(int j=1;j<=n;j++)
           {
               System.out.print(arr[i][j]+" ");
           }
           System.out.println();
       }
   }

}

Output:


Related Solutions

In this project we will implement the Minesweeper game. Minesweeper is played on a rectangle grid....
In this project we will implement the Minesweeper game. Minesweeper is played on a rectangle grid. When the game starts, a number of bombs are hidden on random positions on the field. In every round, the player "touches" a cell of the field. If the cell contains a bomb, it explodes, the game ends, and the player loses. Otherwise, the cell is uncovered to show the number of bombs in the vicinity, that is, the number of neighboring cells that...
Consider the m by n grid graph: n vertices in each of m rows, and m...
Consider the m by n grid graph: n vertices in each of m rows, and m vertices in each of n columns arranged as a grid, and edges between neighboring vertices on rows and columns (excluding the wrap-around edges in the toric mesh). There are m n vertices in total. a)What is the diameter of this graph? b) From the top left vertex to the bottom right vertex, how many shortest paths are there? Please explain.
An m × n grid graph has m rows of n vertices with vertices closest to...
An m × n grid graph has m rows of n vertices with vertices closest to each other connected by an edge. Find the greatest length of any path in such a graph, and provide a brief explanation as to why it is maximum. You can assume m, n ≥ 2. Please provide an explanation without using Hamilton Graph Theory.
3.The “Guess-the Mean Game” is played as follows: Each of the N players writes on a...
3.The “Guess-the Mean Game” is played as follows: Each of the N players writes on a piece of paper an integer between 1 and 10 without showing it to any other players. The players then reveal the numbers they have written down. The winner is that person whose number comes the closest to equaling half the average of the number submitted by the other players. For example, if there are four players and they write down the numbers 1, 3,...
Monshimout is a game from the Cheyenne people and played by women. It could be played...
Monshimout is a game from the Cheyenne people and played by women. It could be played by two or more players, and if played by more than two, then the players divided into two equal teams. Game equipment consisted of five plum stones and a basket made of woven grass or willow twigs. The basket measured 3-4 inches deep, 8 inches across at the top, and almost 1/2 inch thick. The plum stones were left plain on one side, but...
Develop a Java application to simulate a game played in an elementary classroom. In this game,...
Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his...
Select a country as a possible new market for a new video game system, the M-Box...
Select a country as a possible new market for a new video game system, the M-Box by MES-Sim Corporation. Research and analyze that country. Recommend whether the market should be developed, or not, by MES-Sim Corporation The MIR requires teams to gather current, or the most recently available, data on the market’s people, economy, government, and technological status from online sources.
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
What would be the equilibrium of the classic game “Chicken” if it was played as a...
What would be the equilibrium of the classic game “Chicken” if it was played as a Stackleberg game in which one player gets to decide first?
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with...
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with a group of 3 persons. Each person chooses one number and it is assigned to either 'snap', 'crackle', or 'pop'. Then the number of rounds is decided (e.g. 12). The players play the game for the predetermined number of rounds (here 12). In each round: ● if the number of the round is a multiple of the snap number, the person who chose the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT