The example of this article is shared for you java The specific code of mine sweeping game , For your reference , The details are as follows
import java.awt.BorderLayout; import java.awt.Color; import
java.awt.Container; import java.awt.GridLayout; import java.awt.Insets; import
java.awt.Label; import java.awt.event.ActionEvent; import
java.awt.event.ActionListener; import java.awt.event.ItemEvent; import
java.awt.event.ItemListener; import java.awt.event.MouseEvent; import
java.awt.event.MouseListener; import java.util.Random; import
javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame;
import javax.swing.JOptionPane; import javax.swing.JPanel; public class SaoLei
implements MouseListener,ActionListener{ JPanel p=new JPanel(); JFrame frame =
new JFrame(" mine clearance "); @SuppressWarnings("rawtypes") JComboBox combobox = new
JComboBox(); JButton reset = new JButton(" restart "); Container container = new
Container(); // Game data structure SaoLeiConstant constant = new SaoLeiConstant();
JButton[][] buttons = new JButton[constant.row][constant.col];// Define button int[][]
counts = new int [constant.row][constant.col];// Define the number of Mines under the integer array save button // Create construction method public
SaoLei() { // Display window frame.setSize(600,700);//600*700 frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new
BorderLayout()); // Add redo , Select difficulty button addtopButton(); // Add minefield button addButtons(); // Mine burying
addLei(); // Add mine count calcNeiboLei(); frame.setVisible(true); } void
addtopButton() { p.removeAll(); p.add(reset); reset.setBackground(Color.green);
reset.setOpaque(true); reset.addActionListener(this);
//combobox.addItem(" Selection difficulty "); combobox.addItem(" Novice difficulty "); combobox.addItem(" Elementary difficulty ");
combobox.addItem(" Intermediate difficulty "); combobox.addItem(" Advanced difficulty "); combobox.addItem(" Master difficulty ");
combobox.setBackground(Color.GREEN); combobox.setOpaque(true);
combobox.addItemListener(new ItemListener(){ @Override public void
itemStateChanged(ItemEvent e) { String item = e.getItem().toString(); if(item
== " Novice difficulty ") { constant.leiCount = 20; ResetGame(); } else if(item == " Elementary difficulty ") {
constant.leiCount = 43; ResetGame(); } else if(item == " Intermediate difficulty "){
constant.leiCount = 63; ResetGame(); } else if(item == " Advanced difficulty "){
constant.leiCount = 99; ResetGame(); } else if(item == " Master difficulty ") {
constant.leiCount = 119; ResetGame(); } } }); p.add(combobox);
frame.add(p,BorderLayout.NORTH); //p.add(new
Label(" Total lightning number :"+constant.leiCount,Label.CENTER)); //p.add(new
Label(" Total lightning number :"+constant.leiCount,Label.RIGHT)); } /* void addnanduButton() {
nandu.setBackground(Color.green); nandu.setOpaque(true);
nandu.addActionListener(this); frame.add(nandu,BorderLayout.WEST); } void
addResetButton() { reset.setBackground(Color.green); reset.setOpaque(true);
reset.addActionListener(this); //reset.addMouseListener(this);
frame.add(reset,BorderLayout.NORTH); } */ void addLei() { Random rand = new
Random(); int randRow,randCol; for(int i=0; i<constant.leiCount; i++) { randRow
= rand.nextInt(constant.row); randCol = rand.nextInt(constant.col);
if(counts[randRow][randCol] == constant.LEICODE) { i--; } else {
counts[randRow][randCol] = constant.LEICODE;
//buttons[randRow][randCol].setText("X"); } } } void addButtons() {
frame.add(container,BorderLayout.CENTER); container.setLayout(new
GridLayout(constant.row,constant.col)); for(int i=0;i<constant.row;i++) {
for(int j=0;j<constant.col;j++) { JButton button = new JButton();
button.setBackground(Color.white); button.setOpaque(true);
button.addActionListener(this); button.addMouseListener((MouseListener) this);
buttons[i][j] = button; container.add(button); } } } void calcNeiboLei() { int
count; for(int i=0;i<constant.row;i++) { for(int j=0;j<constant.col;j++) {
count =0; if(counts[i][j] == constant.LEICODE) continue; if(i>0 && j>0 &&
counts[i-1][j-1] == constant.LEICODE) count++; if(i>0 && counts[i-1][j] ==
constant.LEICODE) count++; if(i>0 && j<19 &&counts[i-1][j+1] ==
constant.LEICODE) count++; if(j>0 && counts[i][j-1] == constant.LEICODE)
count++; if(j<19 && counts[i][j+1] == constant.LEICODE) count++; if(i<19 && j>0
&& counts[i+1][j-1] == constant.LEICODE) count++; if(i<19 && counts[i+1][j] ==
constant.LEICODE) count++; if(i<19 && j<19 && counts[i+1][j+1] ==
constant.LEICODE) count++; counts[i][j] = count; buttons[i][j].setMargin(new
Insets(0,0,0,0));// Make the button change with the pattern on the button //buttons[i][j].setText(counts[i][j] + ""); } }
} @Override public void actionPerformed(ActionEvent e) { JButton button =
(JButton)e.getSource(); if(button.equals(reset)) { ResetGame();// Restart the game } else
{ int count = 0; for(int i=0;i<constant.row;i++) { for(int
j=0;j<constant.col;j++) { if(button.equals(buttons[i][j])) { count =
counts[i][j]; if(count == constant.LEICODE) { loseGame(); } else {
openCell(i,j); checkWin(); } return; } } } } } public void
mouseClicked(MouseEvent e) { JButton button = (JButton)e.getSource(); if
(e.getButton() == MouseEvent.BUTTON3) {// Judge right click action for(int
i=0;i<constant.row;i++) { for(int j=0;j<constant.col;j++) {
if(button.equals(buttons[i][j])) { if((buttons[i][j].isEnabled() == true)) {
//buttons[i][j].setEnabled(false); buttons[i][j].setMargin(new
Insets(0,0,0,0));// Make the button change with the pattern on the button buttons[i][j].setText("?"); return; } } } } } }
void ResetGame() { for(int i=0;i<constant.row;i++) { for(int
j=0;j<constant.col;j++) { buttons[i][j].setText("");
buttons[i][j].setEnabled(true); buttons[i][j].setBackground(Color.white);
counts[i][j] = 0; } } addLei(); calcNeiboLei(); } void checkWin() { for(int
i=0;i<constant.row;i++) { for(int j=0;j<constant.col;j++) {
if(buttons[i][j].isEnabled() == true && counts[i][j] != constant.LEICODE )
return; } } JOptionPane.showMessageDialog(frame,"Yeah, You won !"); } // Open lattice using recursive method
void openCell(int i, int j) { if(buttons[i][j].isEnabled() == false) return;
buttons[i][j].setBackground(Color.yellow); buttons[i][j].setOpaque(true);
buttons[i][j].setEnabled(false); if(counts[i][j] == 0) { if(i>0 && j>0 &&
counts[i-1][j-1] != constant.LEICODE) openCell(i-1,j-1); if(i>0 && j<19 &&
counts[i-1][j] != constant.LEICODE) openCell(i-1,j); if(i>0 && j<19
&&counts[i-1][j+1] != constant.LEICODE) openCell(i-1,j+1); if(j>0 &&
counts[i][j-1] != constant.LEICODE) openCell(i,j-1); if(j<19 && counts[i][j+1]
!= constant.LEICODE) openCell(i,j+1); if(i<19 && j>0 && counts[i+1][j-1] !=
constant.LEICODE) openCell(i+1,j-1); if(i<19 && counts[i+1][j] !=
constant.LEICODE) openCell(i+1,j); if(i<19 && j<19 && counts[i+1][j+1] !=
constant.LEICODE) openCell(i+1,j+1); } else { buttons[i][j].setMargin(new
Insets(0,0,0,0)); buttons[i][j].setText(counts[i][j] + ""); } } void loseGame()
{ for(int i=0;i<constant.row;i++) { for(int j=0;j<constant.col;j++) { int count
= counts[i][j]; if(count == constant.LEICODE) { buttons[i][j].setMargin(new
Insets(0,0,0,0)); buttons[i][j].setText(" thunder ");
buttons[i][j].setBackground(Color.red); buttons[i][j].setEnabled(false); } else
{ buttons[i][j].setMargin(new Insets(0,0,0,0)); buttons[i][j].setText(count +
""); buttons[i][j].setEnabled(false); } } }
JOptionPane.showMessageDialog(frame,"error, You lost !"); } public static void
main(String[] args) { new SaoLei(); } @Override public void
mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override
public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub }
@Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated
method stub } @Override public void mouseExited(MouseEvent e) { // TODO
Auto-generated method stub } }
Constant class
public class SaoLeiConstant { final int row = 20;// Number of rows 30 final int col =
20;// Number of columns 30 final int LEICODE = 10;// Define the number below the mine protected int temp = 20; protected
int leiCount = temp;// Thunder number 30 }
design sketch

Technology
Daily Recommendation
views 13