This article describes the mine-sweeping game and improved version implemented by Java swing. Share it for your reference, as follows:
Version 1:
package awtDemo;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;/** * This is a simple mine-sweeping example, written by swing, suitable for beginners to practice* This program uses setBounds (x,y,w,h) to layout the control* Method refers to the mine-sweeping built into win xp. Of course, the writing function has not been made, * Some of the functions made have bugs * * @author Ping_QC */public class test extends JFrame implements ActionListener, Runnable, MouseListener { private static final long serialVersionUID = -2417758397965039613L; private final int EMPTY = 0; private final int MINE = 1; private final int CHECKED = 2; private final int MINE_COUNT = 10; // The number of thunder private final int BUTTON_BORDER = 50; // Size of each point private final int MINE_SIZE = 10; // Interface specification, 20x20 private final int START_X = 20; // Start position x private final int START_Y = 50; // Start position y private boolean flag; private JButton[][] jb; private JLabel jl; private JLabel showTime; private int[][] map; /** * Detect whether there is lightning around a point. The coordinates of the surrounding points can be calculated from this array*/ private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { -1, -1 } }; /** * Randomly generates a set number of thunder*/ public void makeMine() { int i = 0, tx, ty; for (; i < MINE_COUNT;) { tx = (int) (Math.random() * MINE_SIZE); ty = (int) (Math.random() * MINE_SIZE); if (map[tx][ty] == EMPTY) { map[tx][ty] = MINE; i++; // Don't remember the repeated lightning} } } } /** * Put the button array on the frame and correspond to the map[][] array*/ public void makeButton() { for (int i = 0; i < MINE_SIZE; i++) { for (int j = 0; j < MINE_SIZE; j++) { jb[i][j] = new JButton(); // if (map[i][j] == MINE) // jb[i][j].setText(i+","+j); // listener add jb[i][j].addActionListener(this); jb[i][j].addMouseListener(this); jb[i][j].setName(i + "_" + j); // convenient clicking is to determine which button was clicked// Font font = new Font(Font.SERIF, Font.BOLD, 10); // jb[i][j].setFont(font); // jb[i][j].setText(i+","+j); jb[i][j].setBounds(j * BUTTON_BORDER + START_X, i * BUTTON_BORDER + START_Y, BUTTON_BORDER, BUTTON_BORDER); this.add(jb[i][j]); } } } public void init() { flag = false; jl.setText("Welcome to test, there are a total of " + MINE_COUNT + "thunder"); jl.setVisible(true); jl.setBounds(20, 20, 500, 30); this.add(jl); showTime.setText("Elapsed: 0 seconds"); showTime.setBounds(400, 20, 100, 30); this.add(showTime); makeMine(); makeButton(); this.setSize(550, 600); this.setLocation(700, 100); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public test(String title) { super(title); this.setLayout(null); //Not using the layout manager, set the position of each control with setBounds jb = new JButton[MINE_SIZE][MINE_SIZE]; jl = new JLabel(); showTime = new JLabel(); map = new int[MINE_SIZE][MINE_SIZE]; // Map the buttons into an array} public static void main(String[] args) { test test = new test("Wulin.com-Mine Sweeping Game Test 1"); test.init(); test.run(); } @Override public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); int x, y; if ((obj instanceof JButton) == false) { showMessage("Error", "Internal Error"); return; } String[] tmp_str = ((JButton) obj).getName().split("_"); x = Integer.parseInt(tmp_str[0]); y = Integer.parseInt(tmp_str[1]); if (map[x][y] == MINE) { showMessage("Death", "You stepped on a mine~~~"); flag = true; showMine(); return; } dfs(x, y, 0); checkSuccess(); } /** * After each click, determine whether all the lightnings have been found by calculating the number of buttons with enabled status */ private void checkSuccess() { int cnt = 0; for (int i = 0; i < MINE_SIZE; i++) { for (int j = 0; j < MINE_SIZE; j++) { if (jb[i][j].isEnabled()) { cnt++; } } } if (cnt == MINE_COUNT) { String tmp_str = showTime.getText(); tmp_str = tmp_str.replaceAll("[^0-9]", ""); showMessage("Victory", "When this minesweep is shared: " + tmp_str + "seconds"); flag = true; showMine(); } } private int dfs(int x, int y, int d) { map[x][y] = CHECKED; int i, tx, ty, cnt = 0; for (i = 0; i < 8; i++) { tx = x + mv[i][0]; ty = y + mv[i][1]; if (tx >= 0 && tx < MINE_SIZE && ty >= 0 && ty < MINE_SIZE) { if (map[tx][ty] == MINE) { cnt++;// Statistics of the thunder number near this point} else if (map[tx][ty] == EMPTY) { ; } else if (map[tx][ty] == CHECKED) { ; } } } if (cnt == 0) { for (i = 0; i < 8; i++) { tx = x + mv[i][0]; ty = y + mv[i][1]; if (tx >= 0 && tx < MINE_SIZE && ty >= 0 && ty < MINE_SIZE && map[tx][ty] != CHECKED) { dfs(tx, ty, d + 1); } } } else { jb[x][y].setText(cnt + ""); } jb[x][y].setEnabled(false); return cnt; } /** * Show some information on the jl tag* * @param title * @param info */ private void showMessage(String title, String info) { jl.setText(info); System.out.println("in functino showMessage() : " + info); } public void run() { int t = 0; while (true) { if (flag) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t++; showTime.setText("Elapsed:" + t + "seconds"); } // showMine(); } private void showMine() {// Icon iconMine = new ImageIcon("e:/mine.jpg"); for (int i = 0; i < MINE_SIZE; i++) { for (int j = 0; j < MINE_SIZE; j++) { if (map[i][j] == MINE) { jb[i][j].setText("#");// jb[i][j].setIcon(iconMine); } } } } @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == 3) { Object obj = e.getSource(); if ((obj instanceof JButton) == false) { showMessage("Error", "Internal Error"); return; } String[] tmp_str = ((JButton) obj).getName().split("_"); int x = Integer.parseInt(tmp_str[0]); int y = Integer.parseInt(tmp_str[1]); if ("{1}".equals(jb[x][y].getText()))) { jb[x][y].setText(""); } else { jb[x][y].setText("{1}"); } /* if(jb[x][y].getIcon() == null){ jb[x][y].setIcon(new ImageIcon("e:/flag.jpg")); }else{ jb[x][y].setIcon(null); }*/ } } @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 }}Running effect:
Version 2 is an improvement to the above version 1 program, and the right-click marking function and the independent selection difficulty function are added on the basis of the unchanged basis.
package awtDemo;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;@SuppressWarnings("serial")public class saolei extends JFrame implements ActionListener, Runnable, MouseListener { private final int loEMPTY = 0; private final int loMINE = 1; private final int loCHECKED = 2; private final int loMINE_COUNT = 10; private final int loBUTTON_BORDER = 50; private final int loMINE_SIZE = 10; private final int loSTART_X = 20; private final int loSTART_Y = 50; private boolean flag; private JButton[][] jb; private JLabel jl; private JLabel showTime; private int[][] map; private int[][] mv = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } }; public void makeloMINE() { int i = 0, tx, ty; for (; i < loMINE_COUNT;) { tx = (int) (Math.random() * loMINE_SIZE); ty = (int) (Math.random() * loMINE_SIZE); if (map[tx][ty] == loEMPTY) { map[tx][ty] = loMINE; i++; } } } public void makeButton() { for (int i = 0; i < loMINE_SIZE; i++) { for (int j = 0; j < loMINE_SIZE; j++) { jb[i][j] = new JButton(); jb[i][j].addActionListener(this); jb[i][j].addMouseListener(this); jb[i][j].setName(i + "_" + j); jb[i][j].setBounds(j * loBUTTON_BORDER + loSTART_X, i * loBUTTON_BORDER + loSTART_Y, loBUTTON_BORDER, loBUTTON_BORDER); this.add(jb[i][j]); } } } public void init() { flag = false; jl.setText("Test welcome, there are a total of " + loMINE_COUNT + "thunder"); jl.setVisible(true); jl.setBounds(20, 20, 500, 30); this.add(jl); showTime.setText("Elapsed: 0 seconds"); showTime.setBounds(400, 20, 100, 30); this.add(showTime); makeloMINE(); makeButton(); this.setSize(550, 600); this.setLocation(700, 100); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public saolei(String title) { super(title); this.setLayout(null); //No layout manager is used, setBounds for each control's position jb = new JButton[loMINE_SIZE][loMINE_SIZE]; jl = new JLabel(); showTime = new JLabel(); map = new int[loMINE_SIZE][loMINE_SIZE]; // Map the buttons into an array} public static void main(String[] args) { saolei test = new saolei("Wulin.com-Mine Sweeping Game Test 2"); test.init(); test.run(); } @Override public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); int x, y; if ((obj instanceof JButton) == false) { showMessage("Error", "Internal Error"); return; } String[] tmp_str = ((JButton) obj).getName().split("_"); x = Integer.parseInt(tmp_str[0]); y = Integer.parseInt(tmp_str[1]); if (map[x][y] == loMINE) { showMessage("Death", "You stepped on a mine~~~"); flag = true; showloMINE(); return; } dfs(x, y, 0); checkSuccess(); } private void checkSuccess() { int cnt = 0; for (int i = 0; i < loMINE_SIZE; i++) { for (int j = 0; j < loMINE_SIZE; j++) { if (jb[i][j].isEnabled()) { cnt++; } } } if (cnt == loMINE_COUNT) { String tmp_str = showTime.getText(); tmp_str = tmp_str.replaceAll("[^0-9]", ""); showMessage("Victory", "When this minesweep is shared: " + tmp_str + "seconds"); flag = true; showloMINE(); } } private int dfs(int x, int y, int d) { map[x][y] = loCHECKED; int i, tx, ty, cnt = 0; for (i = 0; i < 8; i++) { tx = x + mv[i][0]; ty = y + mv[i][1]; if (tx >= 0 && tx < loMINE_SIZE && ty >= 0 && ty < loMINE_SIZE) { if (map[tx][ty] == loMINE) { cnt++; } else if (map[tx][ty] == loEMPTY) { ; } else if (map[tx][ty] == loCHECKED) { ; } } } if (cnt == 0) { for (i = 0; i < 8; i++) { tx = x + mv[i][0]; ty = y + mv[i][1]; if (tx >= 0 && tx < loMINE_SIZE && ty >= 0 && ty < loMINE_SIZE && map[tx][ty] != loCHECKED) { dfs(tx, ty, d + 1); } } } else { jb[x][y].setText(cnt + ""); } jb[x][y].setEnabled(false); return cnt; } private void showMessage(String title, String info) { jl.setText(info); System.out.println("in functino showMessage() : " + info); } public void run() { int t = 0; while (true) { if (flag) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t++; showTime.setText("Elapsed time: " + t + " seconds"); } } private void showloMINE() { for (int i = 0; i < loMINE_SIZE; i++) { for (int j = 0; j < loMINE_SIZE; j++) { if (map[i][j] == loMINE) { jb[i][j].setText("Thunder"); } } } } public void mouseClicked(MouseEvent e) { if (e.getButton() == 3) { Object obj = e.getSource(); if ((obj instanceof JButton) == false) { showMessage("Error", "Internal Error"); return; } String[] tmp_str = ((JButton) obj).getName().split("_"); int x = Integer.parseInt(tmp_str[0]); int y = Integer.parseInt(tmp_str[1]); if ("{1}quot".equals(jb[x][y].getText())) { jb[x][y].setText(""); } else { jb[x][y].setText("{1}quot"); } } } public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { }}Running effect:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.