Search code examples
javaoptimizationtimeminesweeper

Minesweeper program exceeding time limit


I am following a course by princeton university on cousera, I wrote the following code for the following problem:

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).

Every time I submit to the grader; it says the 60 second limit was exceeded, and it stops grading.

This is the desired output

enter image description here

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]);

        boolean[][] minePositions = new boolean[m + 2][n + 2];
        int[][] grid = new int[m + 2][n + 2];

        int num = 0;
        while (num != k) {
            if ((m * n) == k) {
                for (int f = 1; f <= m; f++) {
                    for (int z = 1; z <= n; z++) {
                        minePositions[f][z] = true;
                    }
                }
                break;
            }
            int r = (int) (Math.random() * (m * n - 1));
            int q = r / n;
            int rem = r % n;
            if (q == 0) {
                q = 1;
            }
            else if (q > (m - 1)) {
                q = m - 1;
            }
            if (rem == 0) {
                rem = 1;
            }
            else if (rem > (n - 1)) {
                rem = n - 1;
            }
            
            if (!minePositions[q][rem]) {
                minePositions[q][rem] = true;
                num++;
            }
        }

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (minePositions[i - 1][j + 1]) grid[i][j]++;
                if (minePositions[i][j + 1]) grid[i][j]++;
                if (minePositions[i + 1][j + 1]) grid[i][j]++;
                if (minePositions[i - 1][j]) grid[i][j]++;
                if (minePositions[i + 1][j]) grid[i][j]++;
                if (minePositions[i - 1][j - 1]) grid[i][j]++;
                if (minePositions[i][j - 1]) grid[i][j]++;
                if (minePositions[i + 1][j - 1]) grid[i][j]++;
            }
            for (int j = 1; j <= n; j++) {
                if (minePositions[i][j]) System.out.print("*  ");
                else System.out.print(grid[i][j] + "  ");
            }
            System.out.print("\n");
        }

    }
}

Solution

  • I think using arrays of size (m + 2, n + 2) confused your math here. I'll continue on your work, always use m + 2 and n + 2 in your calculations to avoid any confusion.

    int r = (int) (Math.random() * ((m + 2) * (n + 2)));

    Generate a random number than can span all your array cells.

    int q = r / (n + 2);
    int rem = r % (n + 2);
    
    // Increment num only if q and rem are in the ranges and the position does not have a mine on it
    if (q > 0 && q <= m && rem > 0 && rem <= n && !minePositions[q][rem]) {
        minePositions[q][rem] = true;
        num++;
    }
    

    The full code

    int m = Integer.parseInt(args[0]);
    int n = Integer.parseInt(args[1]);
    int k = Integer.parseInt(args[2]);
    
    boolean[][] minePositions = new boolean[m + 2][n + 2];
    int[][] grid = new int[m + 2][n + 2];
    
    int num = 0;
    while (num != k) {
        int r = (int) (Math.random() * ((m + 2) * (n + 2)));
        int q = r / (n + 2);
        int rem = r % (n + 2);
    
        if (q > 0 && q <= m && rem > 0 && rem <= n && !minePositions[q][rem]) {
            minePositions[q][rem] = true;
            num++;
        }
    }
    
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (minePositions[i - 1][j + 1]) grid[i][j]++;
            if (minePositions[i][j + 1]) grid[i][j]++;
            if (minePositions[i + 1][j + 1]) grid[i][j]++;
            if (minePositions[i - 1][j]) grid[i][j]++;
            if (minePositions[i + 1][j]) grid[i][j]++;
            if (minePositions[i - 1][j - 1]) grid[i][j]++;
            if (minePositions[i][j - 1]) grid[i][j]++;
            if (minePositions[i + 1][j - 1]) grid[i][j]++;
        }
        for (int j = 1; j <= n; j++) {
            if (minePositions[i][j]) System.out.print("*  ");
            else System.out.print(grid[i][j] + "  ");
        }
        System.out.print("\n");
    }