import java.awt.*;
成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、平魯網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為平魯?shù)雀鞔蟪鞘刑峁┚W(wǎng)站開發(fā)制作服務(wù)。
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 當(dāng)前雷數(shù),當(dāng)前方塊數(shù)
int rightBomb, restBomb, restBlock; // 找到的地雷數(shù),剩余雷數(shù),剩余方塊數(shù)
JButton start = new JButton(" 開始 ");
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb[][] bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("掃雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField("10 ", 3);
nowBomb = new JLabel("當(dāng)前雷數(shù)" + ":" + BombNum);
setBomb = new JLabel("設(shè)置地雷數(shù)");
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 開始按鈕 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num = 5 num 50) {
BombNum = num;
startBomb();
}
else if (num 5) {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太少了,請重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太多了,請重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 開始,布雷 */
public void startBomb() {
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + BombNum);
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; i BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 計(jì)算方塊周圍雷數(shù) */
public void CountRoundBomb() {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 當(dāng)需要檢測的單元格本身無地雷的情況下,統(tǒng)計(jì)周圍的地雷個(gè)數(shù)
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您勝利了!", "勝利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 當(dāng)選中的位置為空,則翻開周圍的地圖* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( ( (x != i) || (y != j)) (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
bombButton[x][y].isClicked == false
bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻開 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull(ClickedButton);
}
}
/* 左鍵點(diǎn)擊 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("b");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按確定重來", "踩到地雷", 2);
startBomb();
}
}
}
/* 右鍵點(diǎn)擊 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true) (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("Q");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第幾號方塊
int BombRoundCount; // 周圍雷數(shù)
boolean isBomb; // 是否為雷
boolean isClicked; // 是否被點(diǎn)擊
int BombFlag; // 探雷標(biāo)記
boolean isRight; // 是否點(diǎn)擊右鍵
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
import java.awt.Button;\x0d\x0aimport java.util.Set;\x0d\x0a// 每一個(gè)小方塊類\x0d\x0apublic class Diamond extends Button {\x0d\x0aprivate Diamond[] diamonds;\x0d\x0a\x0d\x0a// 該小方塊周圍的八個(gè)方向上的小方塊\x0d\x0aprivate Diamond east;\x0d\x0aprivate Diamond north;\x0d\x0aprivate Diamond northEast;\x0d\x0aprivate Diamond northWest;\x0d\x0aprivate Diamond south;\x0d\x0aprivate Diamond southEast;\x0d\x0aprivate Diamond southWest;\x0d\x0aprivate Diamond west;\x0d\x0a\x0d\x0aprivate boolean isBomb;// 是否是雷\x0d\x0aprivate boolean isChange;// 又沒有被翻過\x0d\x0aprivate int no;// 產(chǎn)生的方塊的編號\x0d\x0a\x0d\x0a// 持有所有小方塊的引用,方便進(jìn)行操作\x0d\x0apublic Diamond(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds;\x0d\x0a}\x0d\x0a\x0d\x0a// 按鍵時(shí)方塊發(fā)生改變\x0d\x0apublic boolean change() {\x0d\x0athis.isChange = true;// 說明已經(jīng)翻過了\x0d\x0aif(isBomb) {// 觸雷\x0d\x0a//this.setBackground(Color.red);\x0d\x0areturn true;\x0d\x0a} else {// 不是雷,就顯示周圍雷的數(shù)目\x0d\x0a//this.setLabel(this.getNearBombNo() + "");\x0d\x0athis.setLabel(this.getNearBombNo() + "");\x0d\x0a//if(this.getNearBombNo() == 0) {\x0d\x0a//this.moveon();\x0d\x0a//}\x0d\x0areturn false;\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a// 獲得該小方塊周圍雷的數(shù)量\x0d\x0apublic int getNearBombNo() {\x0d\x0aint no = 0;\x0d\x0aif(this.northWest != null this.northWest.isBomb) no++;\x0d\x0aif(this.north != null this.north.isBomb) no++;\x0d\x0aif(this.northEast != null this.northEast.isBomb) no++;\x0d\x0aif(this.east != null this.east.isBomb) no++;\x0d\x0aif(this.southEast != null this.southEast.isBomb) no++;\x0d\x0aif(this.south != null this.south.isBomb) no++;\x0d\x0aif(this.southWest != null this.southWest.isBomb) no++;\x0d\x0aif(this.west != null this.west.isBomb) no++;\x0d\x0a\x0d\x0areturn no;\x0d\x0a}\x0d\x0a\x0d\x0a// 獲得該小方塊周圍的小方塊\x0d\x0apublic Diamond getNearDimaond(int i) {\x0d\x0aint index = -1;\x0d\x0aswitch (i) {\x0d\x0acase 1:// 1表示西北,2,表示北,以此類推\x0d\x0aindex = no - 10;\x0d\x0aif(index return null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 2:\x0d\x0aindex = no - 9;\x0d\x0aif(index return null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 3:\x0d\x0aindex = no - 8;\x0d\x0aif(index return null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 4:\x0d\x0aindex = no + 1;\x0d\x0aif(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 5:\x0d\x0aindex = no + 10;\x0d\x0aif(index = 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 6:\x0d\x0aindex = no + 9;\x0d\x0aif(index 81) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 7:\x0d\x0aindex = no + 8;\x0d\x0aif(index = 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0acase 8:\x0d\x0aindex = no - 1;\x0d\x0aif(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {\x0d\x0areturn null;\x0d\x0a} else {\x0d\x0areturn diamonds[index];\x0d\x0a}\x0d\x0a}\x0d\x0areturn null;\x0d\x0a}\x0d\x0a\x0d\x0a// 遞歸,set是用來裝已經(jīng)翻過的小方塊的,不然會死循環(huán),為什么用set,因?yàn)閟et是不重復(fù)的\x0d\x0apublic void moveon(Set set) {\x0d\x0a\x0d\x0aset.add(this);// 先把自己加上\x0d\x0aif(this.getNorthWest() != null this.getNorthWest().isBomb == false) {\x0d\x0athis.getNorthWest().change();\x0d\x0a\x0d\x0aif(this.getNorthWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorthWest()) == false)\x0d\x0athis.getNorthWest().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorthWest());\x0d\x0a}\x0d\x0a\x0d\x0aif(this.getNorth() != null this.getNorth().isBomb == false) {\x0d\x0athis.getNorth().change();\x0d\x0aif(this.getNorth().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorth()) == false)\x0d\x0athis.getNorth().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorth());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getNorthEast() != null this.getNorthEast().isBomb == false) {\x0d\x0athis.getNorthEast().change();\x0d\x0aif(this.getNorthEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorthEast()) == false)\x0d\x0athis.getNorthEast().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorthEast());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getEast() != null this.getEast().isBomb == false) {\x0d\x0athis.getEast().change();\x0d\x0aif(this.getEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getEast()) == false)\x0d\x0athis.getEast().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getEast());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouthEast() != null this.getSouthEast().isBomb == false) {\x0d\x0athis.getSouthEast().change();\x0d\x0aif(this.getSouthEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouthEast()) == false)\x0d\x0athis.getSouthEast().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouthEast());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouth() != null this.getSouth().isBomb == false) {\x0d\x0athis.getSouth().change();\x0d\x0aif(this.getSouth().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouth()) == false)\x0d\x0athis.getSouth().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouth());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouthWest() != null this.getSouthWest().isBomb == false) {\x0d\x0athis.getSouthWest().change();\x0d\x0aif(this.getSouthWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouthWest()) == false)\x0d\x0athis.getSouthWest().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouthWest());\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getWest() != null this.getWest().isBomb == false) {\x0d\x0athis.getWest().change();\x0d\x0aif(this.getWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getWest()) == false)\x0d\x0athis.getWest().moveon(set);\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getWest());\x0d\x0a} \x0d\x0a}\x0d\x0a\x0d\x0a/*public Diamond[] getDiamonds() {\x0d\x0areturn diamonds;\x0d\x0a}*/\x0d\x0a\x0d\x0apublic Diamond getEast() {\x0d\x0areturn east;\x0d\x0a}\x0d\x0a\x0d\x0apublic int getNo() {\x0d\x0areturn no;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorth() {\x0d\x0areturn north;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorthEast() {\x0d\x0areturn northEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorthWest() {\x0d\x0areturn northWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouth() {\x0d\x0areturn south;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouthEast() {\x0d\x0areturn southEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouthWest() {\x0d\x0areturn southWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getWest() {\x0d\x0areturn west;\x0d\x0a}\x0d\x0a\x0d\x0apublic boolean isBomb() {\x0d\x0areturn isBomb;\x0d\x0a}\x0d\x0a\x0d\x0apublic boolean isChange() {\x0d\x0areturn isChange;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setBomb(boolean isBomb) {\x0d\x0athis.isBomb = isBomb;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setChange(boolean isChange) {\x0d\x0athis.isChange = isChange;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setDiamonds(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setEast(Diamond east) {\x0d\x0athis.east = east;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNo(int no) {\x0d\x0athis.no = no;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorth(Diamond north) {\x0d\x0athis.north = north;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorthEast(Diamond northEast) {\x0d\x0athis.northEast = northEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorthWest(Diamond northWest) {\x0d\x0athis.northWest = northWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouth(Diamond south) {\x0d\x0athis.south = south;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouthEast(Diamond southEast) {\x0d\x0athis.southEast = southEast;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouthWest(Diamond southWest) {\x0d\x0athis.southWest = southWest;\x0d\x0a}\x0d\x0a\x0d\x0apublic void setWest(Diamond west) {\x0d\x0athis.west = west;\x0d\x0a}\x0d\x0a\x0d\x0a}
第一個(gè)JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 顯示所有按鈕的面板
* @author Administrator
*
*/
public class AllButtonPanel extends JPanel implements ActionListener{
private int row;//行數(shù)
private int col;//列數(shù)
private int mineCount;//地雷數(shù)
private MineButton[][] allButtons;//所有按鈕
public AllButtonPanel(int row,int col,int mineCount){
this.row=row;
this.col=col;
this.mineCount=mineCount;
allButtons=new MineButton[row][col];
createButtons();
createMine();
init();
}
private void init(){
this.setLayout(new GridLayout(row,col));
for(int i=0;iallButtons.length;i++){
for(int j=0;jallButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}
/**
* 隨機(jī)布雷的方法
*
*/
private void createMine(){
int n=0;
while(nmineCount){//隨機(jī)生成mineCount個(gè)地雷
int i=(int)(Math.random()*row);
int j=(int)(Math.random()*col);
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(-1);
n++;
}
}
for(int i=0;iallButtons.length;i++){//計(jì)算每個(gè)位置的周圍地雷數(shù)
for(int j=0;jallButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));
}
}
}
}
/**
* 統(tǒng)計(jì)(i,j)坐標(biāo)周圍8個(gè)位置的地雷數(shù)
* @param data
* @param i
* @param j
* @return
*/
private int getSurroundMineCount(int i,int j){
int num=0;//統(tǒng)計(jì)周圍的雷數(shù)
if(i-1=0j-1=0){
num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i-1=0){
num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i-1=0j+1allButtons[0].length){
num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(j-1=0){
num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(j+1allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.lengthj-1=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.length){
num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.lengthj+1allButtons[0].length){
num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);
}
return num;
}
/**
* 生成按鈕
*
*/
private void createButtons(){
for(int i=0;iallButtons.length;i++){
for(int j=0;jallButtons[i].length;j++){
allButtons[i][j]=new MineButton(i,j);
allButtons[i][j].setSize(6,6);
allButtons[i][j].addActionListener(this);//添加點(diǎn)擊事件監(jiān)聽
allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠標(biāo)右鍵事件監(jiān)聽
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3){
int remain=Integer.parseInt(CleanMine.remainMine.getText());
JButton b=(JButton)e.getSource();
if(b.getText().equals("")){
remain--;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}else if(b.getText().equals("")){
remain++;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}
}
}
});
}
}
}
public void actionPerformed(ActionEvent e) {//點(diǎn)擊事件監(jiān)聽的方法
MineButton b=(MineButton)e.getSource();
int r=b.getRow();
int c=b.getCol();
if(allButtons[r][c].getCountOfSurroundMines()==-1){//如果是地雷
for(int i=0;iallButtons.length;i++){//把所有按鈕都顯示出來
for(int j=0;jallButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()==-1){//如果該位置是地雷
allButtons[i][j].setText("$");
}else if(allButtons[i][j].getCountOfSurroundMines()==0){//如果該位置為空(該位置不是地雷,周圍8個(gè)位置也沒有地雷)
allButtons[i][j].setText("");
allButtons[i][j].setBackground(Color.CYAN);
}else{//如果該位置不是地雷,但周圍8個(gè)位置中有地雷
allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");
allButtons[i][j].setBackground(Color.CYAN);
}
}
}
}else{//如果不是地雷
showEmpty(r,c);//執(zhí)行排空操作
}
}
/**
* 排空方法,若(i,j)位置為空,則顯示空白。然后依次遞歸找它周圍的8個(gè)位置。
* @param data
* @param i
* @param j
*/
private void showEmpty(int i,int j){
MineButton b=allButtons[i][j];
if(b.isCleared()){
return;
}
if(allButtons[i][j].getCountOfSurroundMines()==0){
b.setBackground(Color.CYAN);
b.setCleared(true);
if(i-1=0j-1=0){
showEmpty(i-1,j-1);
}
if(i-1=0){
showEmpty(i-1,j);
}
if(i-1=0j+1allButtons[0].length){
showEmpty(i-1,j+1);
}
if(j-1=0){
showEmpty(i,j-1);
}
if(j+1allButtons[0].length){
showEmpty(i,j+1);
}
if(i+1allButtons.lengthj-1=0){
showEmpty(i+1,j-1);
}
if(i+1allButtons.length){
showEmpty(i+1,j);
}
if(i+1allButtons.lengthj+1allButtons[0].length){
showEmpty(i+1,j+1);
}
}else if(allButtons[i][j].getCountOfSurroundMines()0){
b.setText(allButtons[i][j].getCountOfSurroundMines()+"");
b.setBackground(Color.CYAN);
b.setCleared(true);
}
}
}
第二個(gè)JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 掃雷游戲主界面
* @author tony.tang
*
*/
public class CleanMine extends JFrame implements ActionListener{
private JLabel text1,text2;
public static JLabel remainMine;//剩余地雷數(shù)
private JLabel time;//消耗時(shí)間
private JButton reset;//重新開始
private JPanel center;
private int row,col,mine;
public CleanMine(){
text1=new JLabel("剩余地雷:");
text2=new JLabel("消耗時(shí)間:");
remainMine=new JLabel("10");
time=new JLabel("0");
reset=new JButton("重新開始");
reset.addActionListener(this);
JMenuBar bar=new JMenuBar();
JMenu game=new JMenu("游戲");
JMenu help=new JMenu("幫助");
JMenuItem item;
game.add(item=new JMenuItem("開局"));item.addActionListener(this);
game.addSeparator();
ButtonGroup bg=new ButtonGroup();
game.add(item=new JCheckBoxMenuItem("初級",true));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("中級"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("高級"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("自定義..."));bg.add(item);item.addActionListener(this);
game.addSeparator();
game.add(item=new JMenuItem("退出"));item.addActionListener(this);
help.add(item=new JMenuItem("查看幫助"));item.addActionListener(this);
help.add(item=new JMenuItem("關(guān)于掃雷..."));item.addActionListener(this);
bar.add(game);
bar.add(help);
this.setJMenuBar(bar);
init();
}
private void init(){
JPanel north=new JPanel();
north.add(text1);
north.add(remainMine);
north.add(reset);
north.add(text2);
north.add(time);
this.add(north,BorderLayout.NORTH);
this.row=9;
this.col=9;
this.mine=10;
restart();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(){
public void run(){
while(Integer.parseInt(remainMine.getText())0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time.setText((Integer.parseInt(time.getText())+1)+"");
}
}
}.start();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("初級")){
this.row=9;
this.col=9;
this.mine=10;
restart();
return;
}
if(e.getActionCommand().equals("中級")){
this.row=16;
this.col=16;
this.mine=40;
restart();
return;
}
if(e.getActionCommand().equals("高級")){
this.row=16;
this.col=30;
this.mine=99;
restart();
return;
}
if(e.getActionCommand().equals("重新開始")){
restart();
return;
}
}
private void restart(){
if(center!=null){
this.remove(center);
}
center=new AllButtonPanel(row,col,mine);
this.add(center,BorderLayout.CENTER);
this.remainMine.setText(mine+"");
this.time.setText("0");
this.setSize(col*30,row*30+10);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new CleanMine();
}
}
第三個(gè)JAVA文件.
import javax.swing.JButton;
import java.awt.*;
public class MineButton extends JButton {
private int row;
private int col;
private boolean cleared=false;
private int countOfSurroundMines;//周圍地雷數(shù),如果本按鈕是雷,則為-1;
public MineButton(int row,int col){
this.row=row;
this.col=col;
this.setMargin(new Insets(0,0,0,0));
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public boolean isCleared() {
return cleared;
}
public void setCleared(boolean cleared) {
this.cleared = cleared;
}
public int getCountOfSurroundMines() {
return countOfSurroundMines;
}
public void setCountOfSurroundMines(int countOfSurroundMines) {
this.countOfSurroundMines = countOfSurroundMines;
}
}
全部編譯以后就可以執(zhí)行了
int BombNum, BlockNum; // 當(dāng)前雷數(shù),當(dāng)前方塊數(shù)
int rightBomb, restBomb, restBlock; // 找到的地雷數(shù),剩余雷數(shù),剩余方塊數(shù)
JButton start = new JButton(" 開始 ");
JPanel MenuPamel = new JPanel(); //新建一個(gè)區(qū)域,看名字是放菜單.但是打錯(cuò)字了.
JPanel bombPanel = new JPanel();//新建一個(gè)區(qū)域,雷區(qū),由于雷是按鈕,這里面應(yīng)該都是按鈕(JButton).
Bomb[][] bombButton; 2維組數(shù),放地雷.
class Bomb extends JButton {
int num_x, num_y; // 第幾號方塊
int BombRoundCount; // 周圍雷數(shù)
boolean isBomb; // 是否為雷
boolean isClicked; // 是否被點(diǎn)擊
int BombFlag; // 探雷標(biāo)記
boolean isRight; // 是否點(diǎn)擊右鍵
public Bomb(int x, int y) {
num_x = x; //雷的位置 x,不解釋
num_y = y; //雷的位置 y,不解釋.獲得是參數(shù)的值,所new Bomb的時(shí)候傳入雷的位置,套嵌2個(gè)for循環(huán).
BombFlag = 0; //是不是被插旗了
BombRoundCount = 9; //環(huán)繞數(shù)
isBomb = false; //是雷
isClicked = false; //被點(diǎn)
isRight = false; //是真的.( 以上都很好理解,直譯^_^)
}
}
/* 計(jì)算方塊周圍雷數(shù) */
public void CountRoundBomb() {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) { //開方 障礙數(shù)
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) { //同上,我判斷,設(shè)計(jì)的雷區(qū)是正方形,
//這里是找完所有的坐標(biāo).
int count = 0;
// 當(dāng)需要檢測的單元格本身無地雷的情況下,統(tǒng)計(jì)周圍的地雷個(gè)數(shù)
if (bombButton[i][j].isBomb != true) { //如果不是雷
for (int x = i - 1; x i + 2; x++) { //從左邊1個(gè),到右邊1個(gè),一共3個(gè)
for (int y = j - 1; y j + 2; y++) { //我不知道,java y坐標(biāo)是上還是下,總之
//鄰近的上中下.(這里會多找一個(gè)自己)
if ( (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) { //因邊前面 x=i-1,所以排除超出邊界
//的情況
if (bombButton[x][y].isBomb == true) { //如果是雷;
count++; //加一個(gè)
}
}
}
}
bombButton[i][j].BombRoundCount = count; //設(shè)置該Bomb環(huán)繞數(shù)的值
}
}
}
}
總之就是,建個(gè)一個(gè)Bomb類. 別外有一個(gè)方法統(tǒng)計(jì)那些不是雷的地方,的周圍有幾顆雷,到時(shí)候點(diǎn)開,顯示出來.
首先要寫一個(gè)UI,也就是操作界面,使用java.swing.*內(nèi)的東西就可以搞定;
其次寫一個(gè)hander,也就是具體的按鈕響應(yīng),UI的初始化(哪里有雷),怎么觸發(fā)雷和其他的;
一般來說簡單的掃雷模型就好了,如果需要更有意思點(diǎn),可以寫一些數(shù)據(jù)庫的操作內(nèi)容的tool類具體的就是處理歷史操作記錄,場均數(shù)據(jù)或多人競技的特點(diǎn)。
如果你是說你沒有設(shè)計(jì)思路,我可以給你個(gè)提示:遞歸算法是觸發(fā)掃雷的方法,初始化用隨機(jī)數(shù)來做。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] argus)
{
Landmine Zhang = new Landmine();
}
}
//
// Landmine類 主界面
class Landmine extends JFrame
{
static Resources resources = new Resources();
Playing listener = new Playing(this); //主要監(jiān)聽者,監(jiān)聽地雷面板的動作
Help helpListener = new Help(this); //輔助監(jiān)聽者,監(jiān)聽“幫助”、“關(guān)于”
JPanel landminePanel = new JPanel(); //創(chuàng)建地雷面板
JPanel topPanel = new JPanel(); //創(chuàng)建頂部面板
JPanel lowerPanel = new JPanel(); //創(chuàng)建底部面板
public static MyButton [][] lei; //主區(qū)按鈕組
public static int numberOfUnflaged ; //剩余的雷數(shù),顯示在topPanel上,用于提示用戶
public static int numberOfClicked; //已經(jīng)翻開的格子數(shù),當(dāng)數(shù)字?jǐn)?shù)字到"總格子數(shù)—雷數(shù)"時(shí),即勝利
public static int usedTime; //已用時(shí)間
public static JLabel numberOfUnflagedLabel = new JLabel(); //創(chuàng)建剩雷數(shù)標(biāo)簽
public static JLabel timeLabel = new JLabel();//創(chuàng)建時(shí)間標(biāo)簽
public static Timer timer; //創(chuàng)建計(jì)時(shí)
Keylistener keyListener = new Keylistener(this);
public Landmine()
{
super("掃雷__1.2版__小老頭"); //標(biāo)題
setBounds(300,90,800,800); //設(shè)置窗口位置和大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、關(guān)閉按鈕
BorderLayout ff = new BorderLayout(); //創(chuàng)建布局管理器
setLayout(ff); //關(guān)聯(lián)布局管理器
setResizable(false); //禁止改變窗口大小
/*初始化一些數(shù)據(jù)*/
numberOfClicked = 0;
numberOfUnflaged = 40;
usedTime = 0;
/*設(shè)置頂部面板*/
numberOfUnflagedLabel.setText("剩余雷數(shù):"+numberOfUnflaged);//顯示剩余雷數(shù)
numberOfUnflagedLabel.setFont(resources.fontOne);//設(shè)置剩雷數(shù)標(biāo)簽字體
numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷數(shù)標(biāo)簽圖標(biāo)(地雷形)
topPanel.add(numberOfUnflagedLabel); //剩雷數(shù)標(biāo)簽加入topPanel
timeLabel.setText("用時(shí):" + usedTime); //顯示剩余時(shí)間
timeLabel.setFont(resources.fontOne); //設(shè)置時(shí)間標(biāo)簽字體
timeLabel.setIcon(resources.clockIcon); //設(shè)置時(shí)間標(biāo)簽圖標(biāo)
topPanel.add(timeLabel); //時(shí)間標(biāo)簽加入topPanel
add(topPanel,BorderLayout.NORTH); //加入主面板上部
timer = new Timer(1000,new TimerListener());//計(jì)算器注冊監(jiān)聽者
/*設(shè)置底部面板*/
JButton aboutJB = new JButton("關(guān)于"); //創(chuàng)建“關(guān)于”按鈕
JButton helpJB = new JButton("求救"); //創(chuàng)建“求救”按鈕
helpJB.addActionListener(helpListener); //"求救"按鈕加入監(jiān)聽者
aboutJB.addActionListener(helpListener);//"關(guān)于"按鈕加入監(jiān)聽者
helpJB.addKeyListener(keyListener);
aboutJB.addKeyListener(keyListener); //注冊按鍵監(jiān)聽
lowerPanel.add(aboutJB); //“關(guān)于”按鈕加入lowerPanel
lowerPanel.add(helpJB); //“幫助”按鈕加入lowerPanel
add(lowerPanel,BorderLayout.SOUTH);
/*設(shè)置地雷面板*/
GridLayout dd = new GridLayout(16,16);
landminePanel.setLayout(dd); //布局管理
lei = new MyButton[18][18];
for(int i=0; i18; ++i)
{//創(chuàng)建下標(biāo)0—17的按鈕,18*18矩陣
for(int j=0; j18; ++j)
{
lei[i][j] = new MyButton(i,j);
}
}
for(int i=1; i17; ++i)
{//將下標(biāo)1-16的按鈕,加入面板、設(shè)置圖標(biāo)、翻開標(biāo)記為假、加入監(jiān)聽者
for(int j=1; j17; ++j)
{
landminePanel.add(lei[i][j]); //按鈕加入地雷面板
lei[i][j].setIcon(resources.smallIcon); //設(shè)置按鈕圖標(biāo)
lei[i][j].isClicked = false; //翻開標(biāo)記設(shè)置為 假lei[i][j].setIcon(dead);
lei[i][j].addActionListener(listener); //加入監(jiān)聽者
lei[i][j].addMouseListener(listener); //加入鼠標(biāo)事件監(jiān)聽者
lei[i][j].addKeyListener(keyListener); //按鈕注冊按鍵監(jiān)聽,當(dāng)焦點(diǎn)在按鈕上是能監(jiān)聽按鍵
}
}
add(landminePanel,BorderLayout.CENTER); //landminePanel加入主框架中央
addLandmine(); //布雷
timer.start(); //啟動計(jì)時(shí)器
setVisible(true);//顯示之
}
/*布雷*/
public static void addLandmine()
{//隨機(jī)將40的按鈕的是否為雷的標(biāo)記isBomb設(shè)為真
for(int count = 0; count40; /*blank*/)
{
int i = (int)(Math.random()*100 % 16 +1 ) ;
int j = (int)(Math.random()*100 % 16 +1 ) ;
if(lei[i][j].isBomb == false)
{
lei[i][j].isBomb = true;
count++;
}
}
}
class TimerListener implements ActionListener
{//內(nèi)部類,時(shí)間監(jiān)聽
public void actionPerformed(ActionEvent e)
{
usedTime++;
timeLabel.setText("用時(shí):" + usedTime);
}
}
}
//
// Playing類 執(zhí)行主要游戲操作
class Playing implements ActionListener,MouseListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Playing(Landmine in )
{
gui = in;
}
public void actionPerformed(ActionEvent event)
{
MyButton receive = (MyButton)event.getSource();
if(receive.isBomb)
{//如果翻到了雷。。
for(int i=1; i17; ++i)
{//將所有的雷圖標(biāo)設(shè)為 “地雷”
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb)
gui.lei[i][j].setIcon(resources.bombIcon);
}
}
receive.setIcon(resources.deadIcon);//將踩到的地雷圖標(biāo)設(shè)為 “衰”
gui.timer.stop(); //停止計(jì)時(shí)器
JOptionPane.showMessageDialog(null,"小朋友,你掛了…","失敗!",
JOptionPane.INFORMATION_MESSAGE,
resources.deadIcon);//提示失敗
int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心點(diǎn)錯(cuò)了,再來一局?" );
if(yourChose == JOptionPane.OK_OPTION)
{//點(diǎn)擊“是”時(shí)
replay();
}
else
{//點(diǎn)擊 “否” 或 “取消” 時(shí)退出程序
System.exit(0);
}
}
else if(receive.isClicked ==false)
{//未翻到雷
showBombNumber(receive);
}
}
public static void showBombNumber(MyButton in)
{//翻開點(diǎn)擊的按鈕
int numberOfLandmine = 0;//記錄雷的個(gè)數(shù)
in.isClicked = true; //翻開標(biāo)記設(shè)為真
/*檢測周圍8個(gè)方塊是否為雷*/
if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++;//左上
if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++; //上
if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++;//右上
if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++; //右
if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++;//右下
if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++; //下
if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++;//左下
if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++; //左
in.setIcon(new ImageIcon("images/"+numberOfLandmine+".png"));//根據(jù)周圍的雷數(shù)顯示數(shù)字圖標(biāo)
gui.numberOfClicked++;//翻開格子數(shù)+1
if(gui.numberOfClicked==216)
{//翻開216個(gè)格子時(shí)游戲成功,用戶選擇是否再來一局
int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再來一盤嗎?");
if(yourChoice == JOptionPane.OK_OPTION)
replay();
else
System.exit(0);
}
if(numberOfLandmine==0)
{//如果周圍無雷,則將周圍未翻開格子的全部翻開
if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);
if(gui.lei[in.num_x][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y]);
if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y+1]);
if(gui.lei[in.num_x][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y]);
}
}
public static void replay()
{//重新開始
gui.dispose(); //釋放框架資源
gui.timer.stop(); //終止計(jì)時(shí)器
Landmine ff = new Landmine();//重新創(chuàng)建一個(gè)主類的實(shí)例
//這幾條語句實(shí)現(xiàn)了重新開始————關(guān)閉上一個(gè)窗口,重新開啟一個(gè)
//但是這種方法會造成內(nèi)存的浪費(fèi),一個(gè)改進(jìn)的方法是不關(guān)閉當(dāng)年窗口,而是將當(dāng)前窗口重新初始化
}
public void mousePressed(MouseEvent e)
{//當(dāng)鼠標(biāo)右鍵點(diǎn)擊時(shí)自動調(diào)用此函數(shù)
int mods = e.getModifiers();
MyButton receive = (MyButton)e.getSource();
if((mods InputEvent.BUTTON3_MASK) != 0)
{//鼠標(biāo)右鍵
if(receive.isClicked == false)
{
receive.isRight = receive.isRight ? false : true;//改變r(jià)eceive.isRight的值
if(receive.isRight)
{//如果添加標(biāo)記,則剩余雷數(shù)-1,設(shè)置標(biāo)簽為“旗幟”
gui.numberOfUnflaged--;
receive.setIcon(resources.flagIcon);
}
else
{//如果清除標(biāo)記,則剩余雷數(shù)+1,設(shè)置標(biāo)簽為“未翻開”
gui.numberOfUnflaged++;
receive.setIcon(resources.smallIcon);
}
gui.numberOfUnflagedLabel.setText("剩余雷數(shù):"+gui.numberOfUnflaged);
//更新剩余雷數(shù)標(biāo)簽
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
//
// Help類,響應(yīng)“關(guān)于”、“求救”
class Help implements ActionListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Help(Landmine in)
{
gui = in ;
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand()=="關(guān)于")
JOptionPane.showMessageDialog(null,"掃雷1.2版。。小老頭出品");
if(event.getActionCommand()=="求救")
help();
}
public static void help()
{//求救
int stopNumber = (int)(Math.random() * gui.numberOfUnflaged + 1 );
int count = 0;
for(int i=1; i17;++i )
{
for(int j=1; j17; ++j)
{
if( gui.lei[i][j].isBomb !gui.lei[i][j].isClicked !gui.lei[i][j].isRight )
{
count++;
}
if(count == stopNumber)
{
gui.lei[i][j].setIcon(resources.badIcon);
return;
}
}
}
}
}
//
// Keylistener類,響應(yīng)鍵盤事件
class Keylistener implements KeyListener
{
static Resources resources = new Resources();
Landmine gui;
public Keylistener(Landmine in)
{
gui = in;
}
public void keyPressed(KeyEvent e)
{//有鍵按下時(shí)自動執(zhí)行該方法
if(e.getKeyCode() == KeyEvent.VK_UP)
{//按鍵為 向上 時(shí),將所有未標(biāo)記的地雷顯示出
for(int i=1; i17; ++i)
{
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.badIcon);
}
}
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{//按鍵為 向下 時(shí),將所有未標(biāo)記的地雷恢復(fù)為未點(diǎn)擊的圖標(biāo)
for(int i=1; i17; ++i)
{
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.smallIcon);
}
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//
// 按鈕類 MyBtton
class MyButton extends JButton
{
public int num_x,num_y; //第幾號方塊
public boolean isBomb; //是否為雷
public boolean isClicked; //是否被點(diǎn)擊
public int BombFlag; //探雷標(biāo)記
public boolean isRight; //是否點(diǎn)擊右鍵
public MyButton(int x, int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
isBomb = false;
isClicked = true;
isRight = false;
}
}
//
// 資源類 其他類中用到的圖標(biāo),字體等
class Resources
{
public static ImageIcon deadIcon;
public static ImageIcon smallIcon;
public static ImageIcon clockIcon;
public static ImageIcon bombIcon;
public static ImageIcon flagIcon;
public static ImageIcon badIcon;
public static ImageIcon bombIconForLabel;
public static Font fontOne;
public Resources()
{
deadIcon = new ImageIcon("images/dead.gif");
smallIcon = new ImageIcon("images/smallIcon.png");
clockIcon = new ImageIcon("images/clock2.png");
bombIcon = new ImageIcon("images/bomb.png");
flagIcon = new ImageIcon("images/flag_2.png");
badIcon = new ImageIcon("images/bad.gif");
bombIconForLabel = new ImageIcon("images/bombForLabel.gif");
fontOne = new Font("null",Font.BOLD,20);
}
}
網(wǎng)頁標(biāo)題:java開發(fā)掃雷代碼 掃雷程序代碼java
文章網(wǎng)址:http://www.chinadenli.net/article2/hpghoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、ChatGPT、品牌網(wǎng)站制作、微信小程序、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)