Search code examples
javamultidimensional-arrayminesweeper

Minesweeper Nearby Mine Counting


I'm trying to create a 10x10 Minesweeper-esque board made of _'s, with 10 mines randomly placed as *'s. No actual gameplay is involved, just the making of the board. I've been able to (somewhat) successfully place the mines randomly but I can't get the nearby mine counting aspect to work. I've tried many different things but this is what I've come up with so far.

import java.util.Random;
public class Mines {

public static final int BOARD_SIZE = 10;
enum Space {Empty, Mine, MineCount};
public static void main(String[] args) {
    Random rand = new Random();

    //Creates board
    Space[][] board = new Space[BOARD_SIZE][BOARD_SIZE];
    for (int y=0;y<board.length;y++)
    {
        for (int x=0;x<board.length;x++)
        {
            board[x][y] = Space.Empty;
        }
    }

    System.out.println("Creating empty board");
    //Draws the board
    for(int y=0;y<board.length;y++)
    {
        for(int x=0;x<board.length;x++)
        {
            switch(board[y][x])
            {
            case Empty:
                System.out.print("_");
                break;
                default:
                    System.out.println("?");
            }
        }
        System.out.println();
    }

    System.out.println("Placing mines");
    //Sets mines
    for(int i=0;i<board.length;i++)
    {
        int mX = rand.nextInt(BOARD_SIZE);
        int mY = rand.nextInt(BOARD_SIZE);
        if(board[mX][mY] == Space.Empty)
        { 
            board[mX][mY] = Space.Mine;
        }
    }
    for(int y=0;y<board.length;y++)
    {
        for(int x=0;x<board.length;x++)
        {
            switch(board[y][x])
            {
            case Empty:
                System.out.print("_");
                break;
            case Mine:
                System.out.print("*");
                break;
            }
        }
        System.out.println();
    }

    //Count mines
    System.out.println("Counting the mines");
    //Prints board again
    for(int y=0;y<board.length;y++)
    {
        for(int x=0;x<board.length;x++)
        {
            switch(board[y][x])
            {
            case Empty:
                System.out.print("_");
                break;
            case Mine:
                System.out.print("*");
                break;
            case MineCount:
                int mineCount = 0;
                if(board[x-1][y-1] == Space.Mine)
                {
                    mineCount++;
                    board[y][x] = Space.MineCount;
                    System.out.print(mineCount);
                }
            }
        }
        System.out.println();
    }
}

}


Solution

  • Try this code (Not an object oriented approach), but is easily understandable:

    import java.util.Random;
    public class Mines {
    
        public static final int BOARD_SIZE = 5;
        enum Space {
            Empty, Mine
        };
    
        public static void main(String[] args) {
            Random rand = new Random();
    
            // Creates board
            System.out.println("Empty board");
            Space[][] board = new Space[BOARD_SIZE][BOARD_SIZE];
            for (int y = 0; y < board.length; y++) {
                for (int x = 0; x < board.length; x++) {
                    board[x][y] = Space.Empty;
                    System.out.print("_");
                }
                System.out.println();
            }
    
            // Sets mines
            for (int i = 0; i < BOARD_SIZE; i++) {
                int mX = rand.nextInt(BOARD_SIZE);
                int mY = rand.nextInt(BOARD_SIZE);
                // Condition if random number combination [mX, mY] generated previously. Guarantees BOARD_SIZE mines always.
                if(Space.Mine.equals(board[mX][mY])) {
                    i--;
                    continue;
                }
                board[mX][mY] = Space.Mine;
            }
    
            System.out.println("\nPlacing mines");
            for (int y = 0; y < board.length; y++) {
                for (int x = 0; x < board.length; x++) {
                    switch (board[y][x]) {
                        case Empty :
                            System.out.print("_");
                            break;
                        case Mine :
                            System.out.print("*");
                            break;
                    }
                }
                System.out.println();
            }
    
            // Count mines
            System.out.println("\nCounting mines");
            for (int y = 0; y < board.length; y++) {
                for (int x = 0; x < board.length; x++) {
                    if(Space.Mine.equals(board[y][x])) {
                        System.out.print("*");
                    } else {
                        System.out.print(findAdjCount(y, x, board));
                    }
                }
                System.out.println();
            }
        }
    
        private static int findAdjCount(int row, int col, Space[][] board) {
            int cnt = 0;
            // Check 8 adjacent positions
            for (int i = row - 1; i <= row + 1; i++) {
                for (int j = col - 1; j <= col + 1; j++) {
                    if(i >= 0 && i < BOARD_SIZE && j >= 0 && j < BOARD_SIZE) {
                        if(Space.Mine.equals(board[i][j])) {
                            cnt++;
                        }
                    }
                }
            }
            return cnt;
        }
    }