Objective:
Create a graphical game of minesweeper IN JAVA. The board should consist of 10x10 buttons.
Of the 100 spaces there should be at least 20 randomly placed mines. If the button is clicked and
it is not a mine then it clears itself. If a space has been cleared then it should indicate how many
of its eight neighbors are mines. If a space is clicked and it is a mine then the game is over and
the player is asked if they want to play again. Finally, if all the non-mine spaces have been
clicked then the player is prompted that they won.
Solution
2 files need to be made.
First Game.Java which will have the gaming logic and User Interface and Main.Java which will
have have the main method and object of Game.java Number of rows and cols is customizable so
user can change that as and when needed.
import java.awt.*;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Game extends JFrame implements ActionListener, ContainerListener {
int fw, fh, blockr, blockc, var1, var2, num_of_mine, detectedmine = 0, savedlevel = 1,
savedblockr, savedblockc, savednum_of_mine = 10;
int[] r = {-1, -1, -1, 0, 1, 1, 1, 0};
int[] c = {-1, 0, 1, 1, 1, 0, -1, -1};
JButton[][] blocks;
int[][] countmine;
int[][] colour;
ImageIcon[] ic = new ImageIcon[14];
JPanel panelb = new JPanel();
JPanel panelmt = new JPanel();
JTextField tf_mine, tf_time;
JButton reset = new JButton(\"\");
Random ranr = new Random();
Random ranc = new Random();
boolean check = true, starttime = false;
Point framelocation;
Stopwatch sw;
MouseHendeler mh;
Point p;
Game() {
super(\"Game\");
setLocation(400, 300);
setic();
setpanel(1, 0, 0, 0);
setmanue();
sw = new Stopwatch();
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
sw.stop();
setpanel(savedlevel, savedblockr, savedblockc, savednum_of_mine);
} catch (Exception ex) {
setpanel(savedlevel, savedblockr, savedblockc, savednum_of_mine);
}
reset();
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
}
public void reset() {
check = true;
starttime = false;
for (int i = 0; i < blockr; i++) {
for (int j = 0; j < blockc; j++) {
colour[i][j] = \'w\';
}
}
}
public void setpanel(int level, int setr, int setc, int setm) {
if (level == 1) {
fw = 200;
fh = 300;
blockr = 10;
blockc = 10;
num_of_mine = 10;
} else if (level == 2) {
fw = 320;
fh = 416;
blockr = 16;
blockc = 16;
num_of_mine = 70;
} else if (level == 3) {
fw = 400;
fh = 520;
blockr = 20;
blockc = 20;
num_of_mine = 150;
} else if (level == 4) {
fw = (20 * setc);
fh = (24 * setr);
blockr = setr;
blockc = setc;
num_of_mine = setm;
}
savedblockr = blockr;
savedblockc = blockc;
savednum_of_mine = num_of_mine;
setSize(fw, fh);
setResizable(false);
detectedmine = num_of_mine;
p = this.getLocation();
blocks = new JButton[blockr][blockc];
countmine = new int[blockr][blockc];
colour = new int[blockr][blockc];
mh = new MouseHendeler();
getContentPane().removeAll();
panelb.removeAll();
tf_mine = new JTe.
Presiding Officer Training module 2024 lok sabha elections
Create a 10x10 Minesweeper Game in Java
1. Objective:
Create a graphical game of minesweeper IN JAVA. The board should consist of 10x10 buttons.
Of the 100 spaces there should be at least 20 randomly placed mines. If the button is clicked and
it is not a mine then it clears itself. If a space has been cleared then it should indicate how many
of its eight neighbors are mines. If a space is clicked and it is a mine then the game is over and
the player is asked if they want to play again. Finally, if all the non-mine spaces have been
clicked then the player is prompted that they won.
Solution
2 files need to be made.
First Game.Java which will have the gaming logic and User Interface and Main.Java which will
have have the main method and object of Game.java Number of rows and cols is customizable so
user can change that as and when needed.
import java.awt.*;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Game extends JFrame implements ActionListener, ContainerListener {
int fw, fh, blockr, blockc, var1, var2, num_of_mine, detectedmine = 0, savedlevel = 1,
savedblockr, savedblockc, savednum_of_mine = 10;
int[] r = {-1, -1, -1, 0, 1, 1, 1, 0};
int[] c = {-1, 0, 1, 1, 1, 0, -1, -1};
JButton[][] blocks;
int[][] countmine;
int[][] colour;
ImageIcon[] ic = new ImageIcon[14];
JPanel panelb = new JPanel();
JPanel panelmt = new JPanel();
JTextField tf_mine, tf_time;
JButton reset = new JButton("");
Random ranr = new Random();
Random ranc = new Random();
boolean check = true, starttime = false;
2. Point framelocation;
Stopwatch sw;
MouseHendeler mh;
Point p;
Game() {
super("Game");
setLocation(400, 300);
setic();
setpanel(1, 0, 0, 0);
setmanue();
sw = new Stopwatch();
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
sw.stop();
setpanel(savedlevel, savedblockr, savedblockc, savednum_of_mine);
} catch (Exception ex) {
setpanel(savedlevel, savedblockr, savedblockc, savednum_of_mine);
}
reset();
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
show();
}
public void reset() {
check = true;
starttime = false;
for (int i = 0; i < blockr; i++) {
for (int j = 0; j < blockc; j++) {
colour[i][j] = 'w';
}
}
}
public void setpanel(int level, int setr, int setc, int setm) {
if (level == 1) {
5. panelb.repaint();
//getcontentpane().setOpaque(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().addContainerListener(this);
//getContentPane().revalidate();
getContentPane().repaint();
getContentPane().add(panelb, BorderLayout.CENTER);
getContentPane().add(panelmt, BorderLayout.NORTH);
setVisible(true);
}
public void setmanue() {
JMenuBar bar = new JMenuBar();
JMenu game = new JMenu("GAME");
JMenuItem menuitem = new JMenuItem("new game");
final JCheckBoxMenuItem beginner = new JCheckBoxMenuItem("Begineer");
final JCheckBoxMenuItem intermediate = new JCheckBoxMenuItem("Intermediate");
final JCheckBoxMenuItem expart = new JCheckBoxMenuItem("Expart");
final JCheckBoxMenuItem custom = new JCheckBoxMenuItem("Custom");
final JMenuItem exit = new JMenuItem("Exit");
final JMenu help = new JMenu("Help");
final JMenuItem helpitem = new JMenuItem("Help");
ButtonGroup status = new ButtonGroup();
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
//panelb.removeAll();
//reset();
setpanel(1, 0, 0, 0);
//panelb.revalidate();
//panelb.repaint();
}
});
beginner.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelb.removeAll();