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 dont 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. heres my code so far:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AdvancedLevel 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 AdvancedLevel()
{
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… // 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]…
{
setMine[rndm1][rndm2] = true;
easyGrid[rndm1][rndm2].setVisible(…
// 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()).setVis…
}
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();
}
}
}