I've been working on my minesweeper project and I seem to be running into a little trouble. My program compiles, but my problem is setting the mines on my grid. I have used a random generator to set the positions, but I don't know how to implement it on my grid. What I want the program to do is when a block with a bomb is hit, its visibilty should change and a black square should show up under. Here's my code so far:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Minesweeper extends JFrame implements ActionListener {
public int gridRows; //
public int gridColumns; //
int mines;
private int numMines;
private int row, col;
boolean secondTime;
JMenuItem Expert, Intermediate, Easy;
JButton[][] easyGrid; // 2d array for button grid.
boolean[][] setMine = new boolean[row][col];
int numberMine[][];
public static void main(String[] args) {
// creates frame
Minesweeper gameWindow = new Minesweeper();
gameWindow.setSize(400, 400);
gameWindow.setVisible(true); // sets the visibility of the game board to true.
}
public Minesweeper() {
super("Minesweeper");
}
// class constructor.
public void init() {
// variables set the size of the playing board
int rowsX = 9;
int columnsY = 9;
gridRows = rowsX;
gridColumns = columnsY;
setLayout(new GridLayout(rowsX, columnsY)); // creates and sets a grid format.
easyGrid = new JButton[rowsX][columnsY]; // sets the grid size from the parameters given
// loops through rows and columns to draw grid.
for (int i = 0; i < columnsY; i++) {
for (int j = 0; j < rowsX; j++) {
row = i;
col = j;
easyGrid[i][j] = new JButton(""); //creates new button
easyGrid[i][j].addActionListener(this); // assigns an action to the grid buttons
add(easyGrid[i][j]); // adds buttons to the game board
}
}
JMenu gameMenu = new JMenu("GameOptions"); // creates menu option
// creating each option under the JMenuBar (toolBar)
JMenuItem easyChoice = new JMenuItem("Beginner(9x9)");
easyChoice.addActionListener(this);
gameMenu.add(easyChoice); // adds choice option to the drop down menu
Easy = easyChoice;
// button selects a 16 by 16 grid for intermediate players
JMenuItem mediumChoice = new JMenuItem("Intermediate(16x16)");
mediumChoice.addActionListener(this);
gameMenu.add(mediumChoice); // adds choice option to the drop down menu
Intermediate = mediumChoice;
// button selects a 16 by 30 grid for more advanced players
JMenuItem hardChoice = new JMenuItem("Advanced(16x30)");
hardChoice.addActionListener(this);
gameMenu.add(hardChoice); // adds choice option to the drop down menu
Expert = hardChoice;
JMenuBar toolBar = new JMenuBar();
toolBar.add(gameMenu);
setJMenuBar(toolBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // sets the button for exiting the program.
pack(); // set frame size.
}
// lays out the mines on the grid.
public void createMines() {
java.util.Random rand = new java.util.Random(); // Random class
int rndm1;
int rndm2; // gives the co-ordinates of the mine
for (int countBomb = 0; countBomb <= mines; countBomb++) {
rndm1 = rand.nextInt(8) + 1; // pick a row (+1 for buffer compensation)
rndm2 = rand.nextInt(8) + 1; // pick a col
if (!easyGrid[gridRows][gridColumns].equals(setMine[rndm1][rndm2])) {
setMine[rndm1][rndm2] = true;
easyGrid[rndm1][rndm2].setVisible(true);
// checks if there is no bomb present already.
}
}
}
// this method redirects the user based on a selection made from the drop down menu
public void actionPerformed(ActionEvent event) {
if (event.getSource() instanceof JButton) {
((JButton) event.getSource()).setVisible(false);
}
if (event.getSource() == Easy) {
gridRows = 30;
gridColumns = 16;
mines = 99;
secondTime = true;
init();
} else if (event.getSource() == Intermediate) {
gridColumns = gridRows = 16;
mines = 40;
secondTime = true;
init();
} else if (event.getSource() == Expert) {
gridRows = gridColumns = 8;
mines = 10;
secondTime = true;
init();
}
}
}