Question #47549

Minesweeper : This question is expected to be quite time-consuming. You may consider skipping it if you don’t have enough time.
Write a program that let you play customized minesweeper. The program will first let user enter a number N, and then a map in N * N size will be created. Also, N mines will be randomly spreaded on the map.

Expert's answer

Answer on Question #47549, Programming, Java | JSP | JSF

Problem.

Minesweeper: This question is expected to be quite time-consuming. You may consider skipping it if you don't have enough time.

Write a program that let you play customized minesweeper. The program will first let user enter a number N, and then a map in N * N size will be created. Also, N mines will be randomly spread on the map.

Solution.

Code (Minesweeper.java)


import java.util.Random;
public class Minesweeper {
    /**
     * The size of the board
     */
    private int size;
    /**
     * The number of bombs in adjacent cells.
     */
    public int[][] bombsNumber;
    /**
     * The bombs positions.
     */
    public int[][] bombsPosition;
    /**
     * Create game.
     *
     * @param size the size of board
     */
    public Minesweeper(int size) {
        this.size = size;
        bombsNumber = new int[size + 2][size + 2];
        bombsPosition = new int[size + 2][size + 2];
        for (int i = 0; i < size + 2; i++) {
            for (int j = 0; j < size + 2; j++) {
                bombsNumber[i][j] = 0;
                bombsPosition[i][j] = 0;
            }
        }
        for (int i = 0; i < size; i++) {
            Random rand = new Random();
            boolean isNew = false;
            while (!isNew) {
                int number = rand.nextInt(size * size) + 1;
                int x = number / size + 1;
                int y = number - size * (x - 1);
                if (bombsPosition[x][y] == 0) {
                    bombsPosition[x][y] = 1;
                    isNew = true;
                }
            }
        }
        for (int i = 1; i < size + 1; i++) {
            for (int j = 1; j < size + 1; j++) {
                bombsNumber[i][j] = bombsPosition[i][j]
                    + bombsPosition[i - 1][j] + bombsPosition[i + 1][j]
                    + bombsPosition[i][j - 1] + bombsPosition[i][j + 1]
                    + bombsPosition[i + 1][j + 1] + bombsPosition[i - 1][j - 1] + bombsPosition[i + 1][j - 1];
            }
        }
    }
    /**
     * Print the puzzle.
     */
    public void print() {
        for (int i = 1; i < size + 1; i++) {
            for (int j = 1; j < size + 1; j++) {
                System.out.format(" %3d", bombsNumber[i][j]);
            }
            System.out.println();
        }
    }
}


Code (Main.java)


import java.util.Scanner;
public class Main {
    private static Scanner myScanner = new Scanner(System.in);
    public static void main(String[] args) {
        int size = myScanner.nextInt();
        Minesweeper game = new Minesweeper(size);
        game.print();
    }
}


Result

```

5

2 3 2 1 0

2 3 2 1 0

2 3 2 1 0

2 2 2 0 0

2 2 2 0 0

```

http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS