1

I'm making a Minesweeper clone and I am currently writing the genMines method. How would I generate the mines, so they are stored into memory, but not shown on the map? Would I need a separate variable for each new mine, or would I make a separate 2d array with the mines and nothing else? My current code for the method is as follows:

    public void genMines(){
    int numMines = 0;
    Random random = new Random();
    while(numMines<7){
        int row = random.nextInt(5); 
        int column = random.nextInt(5);
        if(board[row][column] != '-'){ // no need to make a bomb on a bomb :P
            board[row][column] = 'B';
        }
    }
}

For context, - is the uncovered symbol in this clone, and B would represent bomb once uncovered.

Zach Mcgregor
  • 33
  • 1
  • 7
  • 1
    You would typically have 1 2D array for the board state, and 1 2D array for the mine locations. – Compass May 29 '18 at 16:15
  • 1
    Have separate arrays. One that stores the mines, one what you want to display. Or you have a mine-class with `boolean` attributes and your draw-method only draws the ones with the correct flag. There are a lot of ways to do this. – Zabuzard May 29 '18 at 16:15

0 Answers0