java开发扫雷代码 扫雷程序代码java

java扫雷游戏代码

import java.awt.*;

成都创新互联公司专注于企业成都营销网站建设、网站重做改版、平鲁网站定制设计、自适应品牌网站建设、成都h5网站建设商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为平鲁等各大城市提供网站开发制作服务。

import java.awt.event.*;

import javax.swing.*;

public class Frame

extends JFrame {

JTextField text;

JLabel nowBomb, setBomb;

int BombNum, BlockNum; // 当前雷数,当前方块数

int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数

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("当前雷数" + ":" + BombNum);

setBomb = new JLabel("设置地雷数");

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));//设置字体大小

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, "您设置的地雷数太少了,请重设!", "错误",

JOptionPane.ERROR_MESSAGE);

num=10;

BombNum = num;

}

else {

JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",

JOptionPane.ERROR_MESSAGE);

num=10;

BombNum = num;

}

}

/* 开始,布雷 */

public void startBomb() {

nowBomb.setText("当前雷数" + ":" + 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));//设置字体大小

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();

}

/* 计算方块周围雷数 */

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;

// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数

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();

}

}

/** 当选中的位置为空,则翻开周围的地图* */

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);

}

}

/* 左键点击 */

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();

}

}

}

/* 右键点击 */

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("当前雷数" + ":" + 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; // 周围雷数

boolean isBomb; // 是否为雷

boolean isClicked; // 是否被点击

int BombFlag; // 探雷标记

boolean isRight; // 是否点击右键

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);

}

}

运行在Eclipse环境下的java扫雷游戏的初级代码是什么?

import java.awt.Button;\x0d\x0aimport java.util.Set;\x0d\x0a// 每一个小方块类\x0d\x0apublic class Diamond extends Button {\x0d\x0aprivate Diamond[] diamonds;\x0d\x0a\x0d\x0a// 该小方块周围的八个方向上的小方块\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;// 产生的方块的编号\x0d\x0a\x0d\x0a// 持有所有小方块的引用,方便进行操作\x0d\x0apublic Diamond(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds;\x0d\x0a}\x0d\x0a\x0d\x0a// 按键时方块发生改变\x0d\x0apublic boolean change() {\x0d\x0athis.isChange = true;// 说明已经翻过了\x0d\x0aif(isBomb) {// 触雷\x0d\x0a//this.setBackground(Color.red);\x0d\x0areturn true;\x0d\x0a} else {// 不是雷,就显示周围雷的数目\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// 获得该小方块周围雷的数量\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是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的\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}

怎么用Java做一个扫雷程序,要原创。。。 做好了给加100

第一个JAVA文件

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

/**

* 显示所有按钮的面板

* @author Administrator

*

*/

public class AllButtonPanel extends JPanel implements ActionListener{

private int row;//行数

private int col;//列数

private int mineCount;//地雷数

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]);

}

}

}

/**

* 随机布雷的方法

*

*/

private void createMine(){

int n=0;

while(nmineCount){//随机生成mineCount个地雷

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++){//计算每个位置的周围地雷数

for(int j=0;jallButtons[i].length;j++){

if(allButtons[i][j].getCountOfSurroundMines()!=-1){

allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));

}

}

}

}

/**

* 统计(i,j)坐标周围8个位置的地雷数

* @param data

* @param i

* @param j

* @return

*/

private int getSurroundMineCount(int i,int j){

int num=0;//统计周围的雷数

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);//添加点击事件监听

allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠标右键事件监听

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) {//点击事件监听的方法

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个位置也没有地雷)

allButtons[i][j].setText("");

allButtons[i][j].setBackground(Color.CYAN);

}else{//如果该位置不是地雷,但周围8个位置中有地雷

allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");

allButtons[i][j].setBackground(Color.CYAN);

}

}

}

}else{//如果不是地雷

showEmpty(r,c);//执行排空操作

}

}

/**

* 排空方法,若(i,j)位置为空,则显示空白。然后依次递归找它周围的8个位置。

* @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);

}

}

}

第二个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;//剩余地雷数

private JLabel time;//消耗时间

private JButton reset;//重新开始

private JPanel center;

private int row,col,mine;

public CleanMine(){

text1=new JLabel("剩余地雷:");

text2=new JLabel("消耗时间:");

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("关于扫雷..."));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();

}

}

第三个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;//周围地雷数,如果本按钮是雷,则为-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;

}

}

全部编译以后就可以执行了

JAVA 扫雷的一段代码求解释

int BombNum, BlockNum; // 当前雷数,当前方块数

int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数

JButton start = new JButton(" 开始 ");

JPanel MenuPamel = new JPanel(); //新建一个区域,看名字是放菜单.但是打错字了.

JPanel bombPanel = new JPanel();//新建一个区域,雷区,由于雷是按钮,这里面应该都是按钮(JButton).

Bomb[][] bombButton; 2维组数,放地雷.

class Bomb extends JButton {

int num_x, num_y; // 第几号方块

int BombRoundCount; // 周围雷数

boolean isBomb; // 是否为雷

boolean isClicked; // 是否被点击

int BombFlag; // 探雷标记

boolean isRight; // 是否点击右键

public Bomb(int x, int y) {

num_x = x; //雷的位置 x,不解释

num_y = y; //雷的位置 y,不解释.获得是参数的值,所new Bomb的时候传入雷的位置,套嵌2个for循环.

BombFlag = 0; //是不是被插旗了

BombRoundCount = 9; //环绕数

isBomb = false; //是雷

isClicked = false; //被点

isRight = false; //是真的.( 以上都很好理解,直译^_^)

}

}

/* 计算方块周围雷数 */

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;

// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数

if (bombButton[i][j].isBomb != true) { //如果不是雷

for (int x = i - 1; x i + 2; x++) { //从左边1个,到右边1个,一共3个

for (int y = j - 1; y j + 2; y++) { //我不知道,java y坐标是上还是下,总之

//邻近的上中下.(这里会多找一个自己)

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++; //加一个

}

}

}

}

bombButton[i][j].BombRoundCount = count; //设置该Bomb环绕数的值

}

}

}

}

总之就是,建个一个Bomb类. 别外有一个方法统计那些不是雷的地方,的周围有几颗雷,到时候点开,显示出来.

用java怎么写扫雷程序

首先要写一个UI,也就是操作界面,使用java.swing.*内的东西就可以搞定;

其次写一个hander,也就是具体的按钮响应,UI的初始化(哪里有雷),怎么触发雷和其他的;

一般来说简单的扫雷模型就好了,如果需要更有意思点,可以写一些数据库的操作内容的tool类具体的就是处理历史操作记录,场均数据或多人竞技的特点。

如果你是说你没有设计思路,我可以给你个提示:递归算法是触发扫雷的方法,初始化用随机数来做。

JAVA,编一个游戏小游戏,比如扫雷,这个程序大概的代码是什么,谁能教教我?我比较笨,但是我认学。

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); //主要监听者,监听地雷面板的动作

Help helpListener = new Help(this); //辅助监听者,监听“帮助”、“关于”

JPanel landminePanel = new JPanel(); //创建地雷面板

JPanel topPanel = new JPanel(); //创建顶部面板

JPanel lowerPanel = new JPanel(); //创建底部面板

public static MyButton [][] lei; //主区按钮组

public static int numberOfUnflaged ; //剩余的雷数,显示在topPanel上,用于提示用户

public static int numberOfClicked; //已经翻开的格子数,当数字数字到"总格子数—雷数"时,即胜利

public static int usedTime; //已用时间

public static JLabel numberOfUnflagedLabel = new JLabel(); //创建剩雷数标签

public static JLabel timeLabel = new JLabel();//创建时间标签

public static Timer timer; //创建计时

Keylistener keyListener = new Keylistener(this);

public Landmine()

{

super("扫雷__1.2版__小老头"); //标题

setBounds(300,90,800,800); //设置窗口位置和大小

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、关闭按钮

BorderLayout ff = new BorderLayout(); //创建布局管理器

setLayout(ff); //关联布局管理器

setResizable(false); //禁止改变窗口大小

/*初始化一些数据*/

numberOfClicked = 0;

numberOfUnflaged = 40;

usedTime = 0;

/*设置顶部面板*/

numberOfUnflagedLabel.setText("剩余雷数:"+numberOfUnflaged);//显示剩余雷数

numberOfUnflagedLabel.setFont(resources.fontOne);//设置剩雷数标签字体

numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷数标签图标(地雷形)

topPanel.add(numberOfUnflagedLabel); //剩雷数标签加入topPanel

timeLabel.setText("用时:" + usedTime); //显示剩余时间

timeLabel.setFont(resources.fontOne); //设置时间标签字体

timeLabel.setIcon(resources.clockIcon); //设置时间标签图标

topPanel.add(timeLabel); //时间标签加入topPanel

add(topPanel,BorderLayout.NORTH); //加入主面板上部

timer = new Timer(1000,new TimerListener());//计算器注册监听者

/*设置底部面板*/

JButton aboutJB = new JButton("关于"); //创建“关于”按钮

JButton helpJB = new JButton("求救"); //创建“求救”按钮

helpJB.addActionListener(helpListener); //"求救"按钮加入监听者

aboutJB.addActionListener(helpListener);//"关于"按钮加入监听者

helpJB.addKeyListener(keyListener);

aboutJB.addKeyListener(keyListener); //注册按键监听

lowerPanel.add(aboutJB); //“关于”按钮加入lowerPanel

lowerPanel.add(helpJB); //“帮助”按钮加入lowerPanel

add(lowerPanel,BorderLayout.SOUTH);

/*设置地雷面板*/

GridLayout dd = new GridLayout(16,16);

landminePanel.setLayout(dd); //布局管理

lei = new MyButton[18][18];

for(int i=0; i18; ++i)

{//创建下标0—17的按钮,18*18矩阵

for(int j=0; j18; ++j)

{

lei[i][j] = new MyButton(i,j);

}

}

for(int i=1; i17; ++i)

{//将下标1-16的按钮,加入面板、设置图标、翻开标记为假、加入监听者

for(int j=1; j17; ++j)

{

landminePanel.add(lei[i][j]); //按钮加入地雷面板

lei[i][j].setIcon(resources.smallIcon); //设置按钮图标

lei[i][j].isClicked = false; //翻开标记设置为 假lei[i][j].setIcon(dead);

lei[i][j].addActionListener(listener); //加入监听者

lei[i][j].addMouseListener(listener); //加入鼠标事件监听者

lei[i][j].addKeyListener(keyListener); //按钮注册按键监听,当焦点在按钮上是能监听按键

}

}

add(landminePanel,BorderLayout.CENTER); //landminePanel加入主框架中央

addLandmine(); //布雷

timer.start(); //启动计时器

setVisible(true);//显示之

}

/*布雷*/

public static void addLandmine()

{//随机将40的按钮的是否为雷的标记isBomb设为真

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

{//内部类,时间监听

public void actionPerformed(ActionEvent e)

{

usedTime++;

timeLabel.setText("用时:" + usedTime);

}

}

}

//

// Playing类 执行主要游戏操作

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)

{//将所有的雷图标设为 “地雷”

for(int j=1; j17; ++j)

{

if(gui.lei[i][j].isBomb)

gui.lei[i][j].setIcon(resources.bombIcon);

}

}

receive.setIcon(resources.deadIcon);//将踩到的地雷图标设为 “衰”

gui.timer.stop(); //停止计时器

JOptionPane.showMessageDialog(null,"小朋友,你挂了…","失败!",

JOptionPane.INFORMATION_MESSAGE,

resources.deadIcon);//提示失败

int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心点错了,再来一局?" );

if(yourChose == JOptionPane.OK_OPTION)

{//点击“是”时

replay();

}

else

{//点击 “否” 或 “取消” 时退出程序

System.exit(0);

}

}

else if(receive.isClicked ==false)

{//未翻到雷

showBombNumber(receive);

}

}

public static void showBombNumber(MyButton in)

{//翻开点击的按钮

int numberOfLandmine = 0;//记录雷的个数

in.isClicked = true; //翻开标记设为真

/*检测周围8个方块是否为雷*/

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"));//根据周围的雷数显示数字图标

gui.numberOfClicked++;//翻开格子数+1

if(gui.numberOfClicked==216)

{//翻开216个格子时游戏成功,用户选择是否再来一局

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(); //终止计时器

Landmine ff = new Landmine();//重新创建一个主类的实例

//这几条语句实现了重新开始————关闭上一个窗口,重新开启一个

//但是这种方法会造成内存的浪费,一个改进的方法是不关闭当年窗口,而是将当前窗口重新初始化

}

public void mousePressed(MouseEvent e)

{//当鼠标右键点击时自动调用此函数

int mods = e.getModifiers();

MyButton receive = (MyButton)e.getSource();

if((mods InputEvent.BUTTON3_MASK) != 0)

{//鼠标右键

if(receive.isClicked == false)

{

receive.isRight = receive.isRight ? false : true;//改变receive.isRight的值

if(receive.isRight)

{//如果添加标记,则剩余雷数-1,设置标签为“旗帜”

gui.numberOfUnflaged--;

receive.setIcon(resources.flagIcon);

}

else

{//如果清除标记,则剩余雷数+1,设置标签为“未翻开”

gui.numberOfUnflaged++;

receive.setIcon(resources.smallIcon);

}

gui.numberOfUnflagedLabel.setText("剩余雷数:"+gui.numberOfUnflaged);

//更新剩余雷数标签

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

}

//

// Help类,响应“关于”、“求救”

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()=="关于")

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类,响应键盘事件

class Keylistener implements KeyListener

{

static Resources resources = new Resources();

Landmine gui;

public Keylistener(Landmine in)

{

gui = in;

}

public void keyPressed(KeyEvent e)

{//有键按下时自动执行该方法

if(e.getKeyCode() == KeyEvent.VK_UP)

{//按键为 向上 时,将所有未标记的地雷显示出

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)

{//按键为 向下 时,将所有未标记的地雷恢复为未点击的图标

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; //是否被点击

public int BombFlag; //探雷标记

public boolean isRight; //是否点击右键

public MyButton(int x, int y)

{

BombFlag = 0;

num_x = x;

num_y = y;

isBomb = false;

isClicked = true;

isRight = false;

}

}

//

// 资源类 其他类中用到的图标,字体等

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);

}

}


新闻标题:java开发扫雷代码 扫雷程序代码java
网页路径:http://scyanting.com/article/hpghoc.html