网站首页 > 文章中心 > 其它

尚学堂java扫雷代码

作者:小编 更新时间:2023-08-19 13:30:19 浏览量:37人看过

用java怎么写扫雷程序

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

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

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

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

尚学堂java扫雷代码-图1

求一个java扫雷游戏的程序源代码,尽量多点注释,要确实可用的!急急急急急急急急急急急急!!!!!

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

尚学堂java扫雷代码-图2

import java.awt.event.MouseEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.Timer;

public class ScanLei1 extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

private Container contentPane;

private JButton btn;

private JButton[] btns;

private JLabel b1;

private Timer timer;

private int bon=10;

private int[][] a;

private int b;

private int[] a1;

public ScanLei1(String title){

super(title);

contentPane=getContentPane();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

timer =new Timer(1000,(ActionListener) this);

initGUI();

}

public void initGUI(){

b=bon;

JMenuBar menuBar=new JMenuBar();

JMenu menu1=new JMenu("游戏");

JMenuItem mi1=new JMenuItem("初级");

mi1.addActionListener(this);

menu1.add(mi1);

menuBar.add(menu1);

b1=new JLabel(bon+"");

a1=new int[bon];

btn =new JButton("开始");

btn.addActionListener(this);

btns=new JButton[row*col];

p=new JPanel();

p.setLayout(new BorderLayout());

contentPane.add(p);

//combo=new JComboBox(new Object[]{"初级","中级","高级"} );

//加监听

/*combo.addItemListener(new ItemListener(){

}});*/

p1=new JPanel();

//在那个位置

//(( FlowLayout)p1.getLayout()).setAlignment( FlowLayout.RIGHT);

p1.add(b1);

p1.add(btn);

p.add(p1,BorderLayout.CENTER);

for(int i=0;irow*col;i++){

btns[i]=new JButton("");

btns[i].setMargin(new Insets(0,0,0,0));

btns[i].addActionListener(this);

btns[i].addMouseListener(new NormoreMouseEvent());

contentPane.add(p,BorderLayout.NORTH);

public void go(){

setVisible(true);

public static void main(String[] args){

new ScanLei1("扫雷").go();

public void out(int[][] a,JButton[] btns,ActionEvent e,int i,int x,int y){

int p=1;

if(a[x][y]==0){

a[x][y]=10;

for(int l=y-1;l=y+1;l++){

int m=x-1-1;

int n=l-1;

p=1;

if(n-1ncolm-1mrow)

{

for(int q=0;qrowp==1;q++){//col--row;

if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){

if(a[x-1][l]!=0a[x-1][l]!=10){

btns[n+col*q].setText(a[x-1][l]+"");

a[x-1][l]=10;

btns[n+col*q].setEnabled(false);

else if(a[x-1][l]==0){

//a[x-1][l]=10;

p=0;

m=x;

if(n-1ncolm-1mcol)

for(int q=0;qrowp==1;q++){

if(a[x+1][l]!=0a[x+1][l]!=10){

btns[n+col*q].setText(a[x+1][l]+"");

a[x+1][l]=10;

else if(a[x+1][l]==0){

int m=x-1;

int n=y-1-1;

if(a[x][y-1]!=0a[x][y-1]!=10){

btns[n+col*q].setText(a[x][y-1]+"");

a[x][y-1]=10;

else if(a[x][y-1]==0){

out(a,btns,e,n+col*q,x,y-1);

m=x-1;

n=y+1-1;

if(a[x][y+1]!=0a[x][y+1]!=10){

btns[n+col*q].setText(a[x][y+1]+"");

a[x][y+1]=10;

else if(a[x][y+1]==0){

out(a,btns,e,n+col*q,x,y+1);

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand()=="初级"){

bon=10;

timer.stop();

b1.setText("10");

btns[i]=new JButton(" ");

this.pack();

btns[i].setText(" ");

btns[i].setEnabled(true);

a[i][j]=0;

}else if(e.getActionCommand()=="中级"){

btns[i]=new JButton(" ");

btns[i].setText("");

}else if(e.getActionCommand()=="高级"){

if(e.getSource()==btn){

timer.start();

//System.out.println(bon);

//清空

//产生随机数

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

{ int p=1;

int m=(int)(Math.random()*row*col);

while(p==1){

int l=1;

int j;

for( j=0;jil==1;j++){

if(a1[j]==m){

m=(int)(Math.random()*row*col);

l=0;

if(j==i){

a1[i]=m;

b1.setText(bon+"");

//布雷

for(int i=0;ibon;i++){

int x=(a1[i]/col+1);

int y=(a1[i]%col+1);

a[x][y]=100;

尚学堂java扫雷代码-图3

if(i==0||j==0||i==row+1||j==col+1){

for(int i=1;i=row;i++){

for(int j=1;j=col;j++){

if(a[i][j]!=100){

for(int l=j-1;l=j+1;l++){

if(a[i-1][l]==100){

a[i][j]++;

if(a[i+1][l]==100){

if(a[i][j-1]==100){

if(a[i][j+1]==100){

if(e.getSource()==timer)

int t=Integer.parseInt(time);

//System.out.println(t);

}else{

t++;

for(int i=0;icol*row;i++){

if(btns[i].getText()!="★")

int x=i/col+1;

int y=i%col+1;

if(e.getSource()==btns[i]a[x][y]==100){

btns[i].setText("★");

btns[i].setEnabled(false);

for(int k=0;kcol*row;k++){

int m1=k/col+1;

int n1=k%col+1;

if(a[m1][n1]!=10btns[k].getText()=="★"){

btns[k].setText("*o*");

for(int j=0;jcol*row;j++){

int m=j/col+1;

int n=j%col+1;

if(a[m][n]==100){

btns[j].setText("★");

btns[j].setEnabled(false);

a[m][n]=10;

else if(e.getSource()==btns[i]){

out(a,btns,e,i,x,y);

if(a[x][y]!=0a[x][y]!=10){

btns[i].setText(a[x][y]+"");

}else if(btns[i].getText()=="★"){

class NormoreMouseEvent extends MouseAdapter{

public void mouseClicked(MouseEvent e) {

System.out.println(b);

int x1=i/col+1;

int y1=i%col+1;

if(e.getSource()==btns[i]btns[i].getText()!="★"a[x1][y1]!=10)

b--;

if(b==0){

int flag=0;

int x=j/col+1;

int y=j%col+1;

if(a[x][y]==100btns[j].getText()=="★"){

flag++;

if(flag==bon){

b1.setText(b+"");

}else if(e.getSource()==btns[i]btns[i].getText()=="★"a[x1][y1]!=-1){

b++;

if(bbon){

else{

悬赏100元钱.注释一个java扫雷游戏源代码,就是把每行的意思写到后面就可以了!在线等.

import java.awt.*;

import javax.swing.*;

import java.util.Random;

import java.awt.event.*;

class Min extends JPanel //雷的类

//flag = 0 不是雷 ; flag = 1是雷

private int but,count = 0; //but:哪一个鼠标键被按下去了 count:这个区域周围有多少个雷

private int mx = 0,my = 0,mw = 10; //定义雷的坐标和宽度

public Min() //构造函数

statu = 0;

public Min(int f,int x,int y,int w)

//构造函数

flag = f;

mx = x;

my = y;

mw = w;

public int getFlag(){return flag;}

public int getStatu(){return statu;}

public int getMx(){return mx;}

public int getMy(){return my;}

public int getMw(){return mw;}

public int getCount(){return count;}

public void setFlag(int f){flag = f;}

public void setCount(int c){count = c;}

public void setData(int f,int x,int y,int w,int s)

//传递值

mx = (x-1)*w;

my = (y-1)*w;

mw = w-1;

statu = s;

public int sendKey(int key)

//返回值,如果游戏结束则返回-1

int rtn = 1;

switch(statu)

case 1:

break;

case 0:

rtn = 1;

if(key == 1 statu == 0)

switch(flag)

statu = 1;

rtn = -1;

return rtn;

class DrawPanel extends JPanel

private int i,j;

private int chx = 0,chy = 0; //专门记录坐标x,y的值

public Min board[][] = {

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},

};

// 画坐标为ax,ay区域的雷的状态

public void draw(Graphics g,int ax,int ay)

int x,y,w; // 坐标x,y;和宽度:w

int s,c,flag; //状态;雷的个数;

x = board[ax][ay].getMx();

y = board[ax][ay].getMy();

w = board[ax][ay].getMw();

s = board[ax][ay].getStatu();

c = board[ax][ay].getCount();

flag= board[ax][ay].getFlag();

switch(s)

case 0: //没有打开状态

g.setColor(Color.black);

g.fillRect(x,y,w,w);

case 1: //打开状态

g.setColor(Color.blue);

if(c != 0 flag == 0) //此处没有雷

g.setColor(Color.red);

g.drawString(String.valueOf(c),x + cx,y + cy);

if(flag == 1) //此处有雷

g.drawString(" 雷",x + cx,y + cy);

g.setColor(Color.green);

g.drawString(" 旗",x + cx,y + cy);

g.drawString("?",x + cx,y + cy);

default:

// 没有图形器的绘图函数:画出坐标ax,ay的雷的状态和图形

public void draw(int ax,int ay)

Graphics g;

g = this.getGraphics();

draw(g,ax,ay);

public int openNoMin(int ax,int ay)

int i,j;

if(board[ax][ay].getStatu() != 0) return 0; //如果此区域打开了,返回

board[ax][ay].sendKey(1); //如果返回值等于-1,就说明游戏结束

draw(ax,ay);

if(board[ax][ay].getFlag() == 1)

//如果游戏结束,把所有的雷都显示出来

for(i = 1;i=bx;i++)

for(j = 1;j = by;j++)

if(board[i][j].getFlag() == 1)

board[i][j].sendKey(1);

draw(i,j);

return -1;

//如果游戏没有结束

if(board[ax][ay].getCount() 0)

ksum ++;

return 1; //周围有雷,就不用打开周围地区

if(board[ax][ay].getCount() == 0 board[ax][ay].getFlag() == 0)

//周围没有雷,打开周围地区,直到有雷的地区

openNoMin(ax-1,ay-1);openNoMin(ax,ay-1);openNoMin(ax+1,ay-1);

openNoMin(ax-1,ay ); openNoMin(ax+1,ay );

openNoMin(ax-1,ay+1);openNoMin(ax,ay+1);openNoMin(ax+1,ay+1);

return 1;

//计算坐标x,y的周围雷的个数

public int getCount(int ai,int aj)

int sum = 0;

if(board[ai][aj].getFlag() == 1)

return sum;

if(ai1aj1aibxajby)

sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+

board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag()+

board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();

if(ai==1aj==1)

sum = board[ai+1][aj ].getFlag()+

board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();

if(ai==1aj==by)

sum = board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+

board[ai+1][aj ].getFlag();

if(ai==bxaj==1)

sum = board[ai-1][aj ].getFlag()+

board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag();

if(ai==bxaj==by)

sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+

board[ai-1][aj ].getFlag();

if(ai==1aj1ajby)

board[ai+1][aj ].getFlag()+

if(ai==bxaj1ajby)

board[ai-1][aj ].getFlag()+

if(ai1aibxaj==1)

sum = board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag()+

if(ai1aibxaj==by)

board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag();

// 传入参数:几列,几行,宽度,雷数

public void initMin(int ax,int ay,int aw,int as)

int k = 1; //表明产生的第几个雷

Random r; //随机数

f = 0; //f=0表示游戏还没有结束

ksum = 0;

bx = ax;

by = ay;

bw = aw;

msum = as;

r = new Random();

//初始化底盘的值

for(i = 1;i = bx;i++)

for(j=1;j=by;j++)

board[i][j].setData(0,i,j,bw,0);

// 随机产生雷

while(k = msum)

i = r.nextInt(bx)+1;

j = r.nextInt(by)+1;

if(board[i][j].getFlag() != 1)

board[i][j].setFlag(1);

k++;

// 非雷区的周围有几个雷,初始化其值

board[i][j].setCount(getCount(i,j));

setBackground(Color.white);

repaint();

// 构造函数

public DrawPanel(int ax,int ay,int aw,int as)

initMin(ax,ay,aw,as);

addMouseListener(new MouseAdapter()

public void mousePressed(MouseEvent me)

int r;

if(f != 0) return; //如果游戏结束,返回

chx = me.getX();

chy = me.getY();

if(me.getButton() != 1)

board[chx/bw+1][chy/bw+1].sendKey(me.getButton());

draw(chx/bw+1,chy/bw+1);

else if(me.getButton() == 1)

if(openNoMin(chx/bw+1,chy/bw+1) == -1)

f = 1;

else if ( ksum + msum == bx*by )

);

// 重画所有的图形,包括一些修饰的图形

public void paint(Graphics g)

int x,y,w;

int s;

for(i=1;i=bx;i++)

draw(g,i,j);

if(f == 1)

Font fo = g.getFont();

g.setColor(Color.white);

g.setFont(f);

//g.setSize();

g.setFont(fo);

// 主类和程序的入口

public class Mine extends JFrame implements ActionListener

Container cp = getContentPane();

JButton bt = new JButton("开局");

Label l1 = new Label("列:");

DrawPanel dp = new DrawPanel(x,y,w,sum);

public Mine()

cp.setLayout(null);

cp.add(dp);

cp.add(bt);

cp.add(tf1);

cp.add(l1);

setResizable(false);

setTitle(" 扫雷");

show();

bt.addActionListener(this);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{System.exit(0);}

public void actionPerformed(ActionEvent e)

if(e.getSource() == bt)

//x = Integer.parseInt(tf1.getText());

dp.initMin(x,y,w,sum);

public static void main(String args[])

new Mine();

高分求一个运行在Eclipse环境下的java 扫雷游戏的初级代码 越小越好 越短越好 运行就好,就是初级就好了,

import java.awt.Button;

import java.util.Set;

// 每一个小方块类

public class Diamond extends Button {

private Diamond[] diamonds;

// 该小方块周围的八个方向上的小方块

private Diamond east;

private Diamond north;

private Diamond northEast;

private Diamond northWest;

private Diamond south;

private Diamond southEast;

private Diamond southWest;

private Diamond west;

private boolean isBomb;// 是否是雷

private boolean isChange;// 又没有被翻过

private int no;// 产生的方块的编号

// 持有所有小方块的引用,方便进行操作

public Diamond(Diamond[] diamonds) {

this.diamonds = diamonds;

// 按键时方块发生改变

public boolean change() {

this.isChange = true;// 说明已经翻过了

if(isBomb) {// 触雷

//this.setBackground(Color.red);

return true;

} else {// 不是雷,就显示周围雷的数目

//this.setLabel(this.getNearBombNo() + "");

this.setLabel(this.getNearBombNo() + "");

//if(this.getNearBombNo() == 0) {

//this.moveon();

//}

return false;

// 获得该小方块周围雷的数量

public int getNearBombNo() {

int no = 0;

if(this.northWest != null this.northWest.isBomb) no++;

if(this.north != null this.north.isBomb) no++;

if(this.northEast != null this.northEast.isBomb) no++;

if(this.east != null this.east.isBomb) no++;

if(this.southEast != null this.southEast.isBomb) no++;

if(this.south != null this.south.isBomb) no++;

if(this.southWest != null this.southWest.isBomb) no++;

if(this.west != null this.west.isBomb) no++;

return no;

// 获得该小方块周围的小方块

public Diamond getNearDimaond(int i) {

int index = -1;

switch (i) {

index = no - 10;

return null;

} else {

return diamonds[index];

if(index 1) {

index = no + 1;

index = no + 10;

index = no - 1;

// 递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的

public void moveon(SetDiamond set) {

set.add(this);// 先把自己加上

if(this.getNorthWest() != null this.getNorthWest().isBomb == false) {

this.getNorthWest().change();

if(this.getNorthWest().getNearBombNo() == 0) {

if(set.contains(this.getNorthWest()) == false)

this.getNorthWest().moveon(set);

set.add(this.getNorthWest());

if(this.getNorth() != null this.getNorth().isBomb == false) {

this.getNorth().change();

if(this.getNorth().getNearBombNo() == 0) {

if(set.contains(this.getNorth()) == false)

this.getNorth().moveon(set);

set.add(this.getNorth());

if(this.getNorthEast() != null this.getNorthEast().isBomb == false) {

this.getNorthEast().change();

if(this.getNorthEast().getNearBombNo() == 0) {

if(set.contains(this.getNorthEast()) == false)

this.getNorthEast().moveon(set);

set.add(this.getNorthEast());

if(this.getEast() != null this.getEast().isBomb == false) {

this.getEast().change();

if(this.getEast().getNearBombNo() == 0) {

if(set.contains(this.getEast()) == false)

this.getEast().moveon(set);

set.add(this.getEast());

if(this.getSouthEast() != null this.getSouthEast().isBomb == false) {

this.getSouthEast().change();

if(this.getSouthEast().getNearBombNo() == 0) {

if(set.contains(this.getSouthEast()) == false)

this.getSouthEast().moveon(set);

set.add(this.getSouthEast());

if(this.getSouth() != null this.getSouth().isBomb == false) {

this.getSouth().change();

if(this.getSouth().getNearBombNo() == 0) {

if(set.contains(this.getSouth()) == false)

this.getSouth().moveon(set);

set.add(this.getSouth());

if(this.getSouthWest() != null this.getSouthWest().isBomb == false) {

this.getSouthWest().change();

if(this.getSouthWest().getNearBombNo() == 0) {

if(set.contains(this.getSouthWest()) == false)

this.getSouthWest().moveon(set);

set.add(this.getSouthWest());

if(this.getWest() != null this.getWest().isBomb == false) {

this.getWest().change();

if(this.getWest().getNearBombNo() == 0) {

if(set.contains(this.getWest()) == false)

this.getWest().moveon(set);

set.add(this.getWest());

/*public Diamond[] getDiamonds() {

return diamonds;

}*/

public Diamond getEast() {

return east;

public int getNo() {

public Diamond getNorth() {

return north;

public Diamond getNorthEast() {

return northEast;

public Diamond getNorthWest() {

return northWest;

public Diamond getSouth() {

return south;

public Diamond getSouthEast() {

return southEast;

public Diamond getSouthWest() {

return southWest;

public Diamond getWest() {

return west;

public boolean isBomb() {

return isBomb;

public boolean isChange() {

return isChange;

public void setBomb(boolean isBomb) {

this.isBomb = isBomb;

public void setChange(boolean isChange) {

this.isChange = isChange;

public void setDiamonds(Diamond[] diamonds) {

public void setEast(Diamond east) {

this.east = east;

public void setNo(int no) {

this.no = no;

public void setNorth(Diamond north) {

this.north = north;

public void setNorthEast(Diamond northEast) {

this.northEast = northEast;

public void setNorthWest(Diamond northWest) {

this.northWest = northWest;

public void setSouth(Diamond south) {

this.south = south;

public void setSouthEast(Diamond southEast) {

this.southEast = southEast;

public void setSouthWest(Diamond southWest) {

this.southWest = southWest;

public void setWest(Diamond west) {

this.west = west;

java扫雷游戏代码

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

BombNum = 10;

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

BombNum = num;

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

JOptionPane.ERROR_MESSAGE);

num=10;

else {

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

/* 开始,布雷 */

public void startBomb() {

nowBomb.setText("当前雷数" + ":" + BombNum);

bombButton[i][j].isBomb = false;

bombButton[i][j].isClicked = false;

bombButton[i][j].isRight = false;

bombButton[i][j].BombFlag = 0;

bombButton[i][j].setEnabled(true);

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

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

int count = 0;

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

if (bombButton[i][j].isBomb != true) {

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

if (bombButton[i][j].isClicked == true) {

restBlock--;

if (rightBomb == BombNum || restBlock == 0) {

JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",

JOptionPane.INFORMATION_MESSAGE);

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

public void isNull(Bomb ClickedButton) {

int i, j;

i = ClickedButton.num_x;

j = ClickedButton.num_y;

if ( ( (x != i) || (y != j)) (x = 0) (y = 0)

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

isNull(ClickedButton);

if ( ( (Bomb) e.getSource()).isClicked == false

( (Bomb) e.getSource()).isRight == false) {

if ( ( (Bomb) e.getSource()).isBomb == false) {

turn( ( (Bomb) e.getSource()));

isWin();

if (bombButton[i][j].isBomb == true) {

bombButton[i][j].setText("b");

( (Bomb) e.getSource()).setForeground(Color.RED);

( (Bomb) e.getSource()).setText("X");

Bomb bombSource = (Bomb) e.getSource();

boolean right = SwingUtilities.isRightMouseButton(e);

if ( (right == true) (bombSource.isClicked == false)) {

if (bombSource.BombFlag == 1) {

if (restBomb 0) {

bombSource.setForeground(Color.RED);

bombSource.setText("F");

bombSource.isRight = true;

restBomb--;

bombSource.BombFlag = 0;

restBomb++;

bombSource.setText("Q");

bombSource.isRight = false;

bombSource.setText("");

if (bombSource.isBomb == true) {

rightBomb++;

rightBomb--;

nowBomb.setText("当前雷数" + ":" + restBomb);

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;

adaptee.start_actionPerformed(e);

////////////////////////////

class Bomb

extends JButton {

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

int BombRoundCount; // 周围雷数

boolean isBomb; // 是否为雷

int BombFlag; // 探雷标记

public Bomb(int x, int y) {

num_x = x;

num_y = y;

BombFlag = 0;

isBomb = false;

isClicked = false;

isRight = false;

class Bomb_actionAdapter

Bomb_actionAdapter(Frame adaptee) {

adaptee.actionPerformed(e);

class Bomb_mouseAdapter

extends MouseAdapter {

Bomb_mouseAdapter(Frame adaptee) {

adaptee.mouseClicked(e);

求帮忙分析java函数,conway's game of life(类似扫雷)

private?int?countNeighbours(int?row,?int?col)//参数是当前行和列

int?numNei=0;//定义numNei变量?用于在记雷数

if?(i=0?||?j=0?||?i=boardSize?||?j=boardSize)//这个是如果越界了

break;//就跳出内循环?不做检查

if(board[i][j]==true)//如果这个格子有雷?为true

numNei++;//就将雷数+1?注意:循环不会排除自身?所以自身是雷也会加?所以后面有一个判断减去

if(board[row][col]==true)//如果当前格是雷

numNei--;//还要减去一个自身

return?numNei;//最后返回

以上就是土嘎嘎小编为大家整理的尚学堂java扫雷代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章