资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

扫雷java代码功能解释 扫雷java代码功能解释大全

怎样用JAVA实现扫雷游戏

要详细代码?还是只要启动?

创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站开发、程序开发、网站优化、微网站、微信小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的网站设计制作、网站制作、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。

java编写实现,代码如下:import Java.awt.*;

import java.awt.event.*;

import javax.Swing.*;

/*按扭类*/

class Bomb extends JButton

{

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

public int BombRoundCount; //周围雷数

public boolean isBomb; //是否为雷

public boolean isClicked; //是否被点击

public int BombFlag; //探雷标记

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

public Bomb(int x,int y)

{

BombFlag = 0;

num_x = x;

num_y = y;

BombRoundCount = 0;

isBomb = false;

isClicked = false;

isRight = false;

}

}

/*窗口及算法实现类*/

class MainBomb extends JFrame implements ActionListener,MouseListener

{

public JTextField text;

public Label nowBomb,setBomb;

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

public Icon icon_bomb = new ImageIcon("Bomb.gif"); //踩雷

public Icon icon_bomb_big = new ImageIcon("bomb_big.gif"); //踩雷标记

public Icon icon_flag = new ImageIcon("flag.gif"); //雷标记

public Icon icon_question = new ImageIcon("question.gif"); //疑惑是否有雷

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

public Panel MenuPamel = new Panel();

public Panel mainPanel = new Panel();

public Bomb[][] bombButton;

/*界面设计*/

public MainBomb()

{

super("扫雷 Aaron2004制作 2004.8 ");

BlockNum = 64;

BombNum = 10;

Container c=getContentPane();

c.setBackground(Color.gray);

c.setLayout(new BorderLayout());

text=new JTextField("10 ",3);

nowBomb = new Label("当前雷数"+" "+BombNum+"");

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

start.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

BombNum = Integer.parseInt(text.getText().trim());

if(BombNum = 10 BombNum 50 )

replay();

else

{

JOptionPane msg = new JOptionPane();

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

}

}

} );

MenuPamel.add(setBomb);

MenuPamel.add(text);

MenuPamel.add(start);

MenuPamel.add(nowBomb);

c.add(MenuPamel,"North");

mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );

bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];

for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++)

{

bombButton[ i ]=new Bomb[ (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 ].setForeground( Color.gray);

bombButton[ i ][ j ].addActionListener(this);

bombButton[ i ][ j ].addMouseListener(this);

}

for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++ )

for(int j = 0 ; j (int)Math.sqrt(BlockNum) ; j++ )

mainPanel.add(bombButton[ i ][ j ]);

c.add(mainPanel,"Center");

startBomb();

setSize(400,400);

setLocation(350,200);

setResizable(false);

}

/*布雷*/

public void startBomb()

{

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

{

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)

i--;

else

bombButton[ x ][ y ].isBomb=true ;

}

}

/*重新开始*/

public void replay()

{

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

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

bombButton[ i ][ j ].setIcon(null);

}

startBomb();

}

/*是否挖完了所有的雷*/

public void isWin()

{

int findBomb=0; //找到的地雷数

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 ].isRight == true)

findBomb++;

}

if( findBomb == Integer.parseInt(text.getText().trim()) )

{

JOptionPane msg = new JOptionPane();

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

}

}

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

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

if ( (i - 1 = 0) (j - 1 = 0)) {

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

count += 1; //检测左上方空格是否是地雷

}

}

if ( (i - 1 = 0)) {

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

count += 1; //检测上方空格是否为地雷

}

}

if ( (i - 1 = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1)) {

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

count += 1; //检测右上方是否为地雷

}

}

if ( (j - 1 = 0)) {

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

count += 1; //检测左边是否为地雷

}

}

if ( (i = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1)) {

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

count += 1; //右边

}

}

if ( (j - 1 = 0) (i + 1 = (int)Math.sqrt(BlockNum)-1)) {

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

count += 1; //左下

}

}

if ( (i + 1 = (int)Math.sqrt(BlockNum)-1)) {

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

count += 1; //下

}

}

if ( (j + 1 = (int)Math.sqrt(BlockNum)-1) (i + 1 = Math.sqrt(BlockNum)-1)) {

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

count += 1; //右下

}

}

bombButton[ i ][ j ].BombRoundCount = count;

}

}

}

}

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

public void isNull(Bomb[][] bombButton,Bomb ClickecButton)

{

int i,j;

i=ClickecButton.num_x;

j=ClickecButton.num_y;

if (ClickecButton.isBomb==true) {

}

else {

if ( (i - 1 = 0) (j - 1 = 0)) { //检测左上方空格是否是空

if (bombButton[i - 1][j - 1].isBomb == false bombButton[i - 1][j - 1].isClicked == false bombButton[i - 1][j - 1].isRight == false) {

bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount)+"");

bombButton[i - 1][j - 1].setEnabled(false);

bombButton[i - 1][j - 1].isClicked=true;

}

}

if ( (i - 1 = 0)) { //检测上方空格是否为空

if (bombButton[i - 1][ j ] .isBomb == false bombButton[i - 1][ j ].isClicked == false bombButton[i - 1][ j ].isRight == false) {

bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount)+"");

bombButton[i - 1][ j ].setEnabled(false);

bombButton[i - 1][ j ].isClicked=true;

}

}

if ( (i - 1 = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空

if (bombButton[i - 1][j + 1] .isBomb == false bombButton[i - 1][j + 1].isClicked == false bombButton[i - 1][j + 1].isRight == false) {

bombButton[i - 1][j + 1].setText((bombButton[i - 1][j + 1].BombRoundCount)+"");

bombButton[i - 1][j + 1].setEnabled(false);

bombButton[i - 1][j + 1].isClicked=true;

}

}

if ( (j - 1 = 0)) { //检测左边是否为空

if (bombButton[ i ][j - 1].isBomb == false bombButton[ i ][j - 1].isClicked == false bombButton[ i ][j - 1].isRight == false) {

bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount)+"");

bombButton[ i ][j - 1].setEnabled(false);

bombButton[ i ][j - 1].isClicked=true;

}

}

if ( (i = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空

if (bombButton[ i ][j + 1].isBomb == false bombButton[ i ][j + 1].isClicked == false bombButton[ i ][j + 1].isRight == false) {

bombButton[ i ][j + 1].setText((bombButton[ i ][j + 1].BombRoundCount)+"");

bombButton[ i ][j + 1].setEnabled(false);

bombButton[ i ][j + 1].isClicked=true;

}

}

if ( (j - 1 = 0) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空

if (bombButton[i + 1][j - 1].isBomb == false bombButton[i + 1][j - 1].isClicked == false bombButton[i + 1][j - 1].isRight == false) {

bombButton[i + 1][j - 1].setText((bombButton[i + 1][j - 1].BombRoundCount)+"");

bombButton[i + 1][j - 1].setEnabled(false);

bombButton[i + 1][j - 1].isClicked=true;

}

}

if ( (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空

if (bombButton[i + 1][ j ].isBomb == false bombButton[i + 1][ j ].isClicked == false bombButton[i + 1][ j ].isRight == false) {

bombButton[i + 1][ j ].setText((bombButton[i + 1][ j ].BombRoundCount)+"");

bombButton[i + 1][ j ].setEnabled(false);

bombButton[i + 1][ j ].isClicked=true;

}

}

if ( (j + 1 = ((int)Math.sqrt(BlockNum)-1) ) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空

if (bombButton[i + 1][j + 1].isBomb == false bombButton[i + 1][j + 1].isClicked == false bombButton[i + 1][j + 1].isRight == false) {

bombButton[i + 1][j + 1].setText((bombButton[i + 1][j + 1].BombRoundCount)+"");

bombButton[i + 1][j + 1].setEnabled(false);

bombButton[i + 1][j + 1].isClicked=true;

}

}

if ( (i - 1 = 0) (j - 1 = 0))//检测左上

isNull(bombButton,bombButton[i - 1][j - 1]);

if ( (i - 1 = 0))

isNull( bombButton,bombButton[i - 1][ j ]);//检测上方

if ( (i - 1 = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1))

isNull( bombButton,bombButton[i - 1][j + 1]);//检测右上

if ( (j - 1 = 0))

isNull(bombButton,bombButton[i][j - 1]);//检测左边

if ( (i = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) )

isNull(bombButton,bombButton[i][j + 1]);//检测右边

if ( (j - 1 = 0) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) )

isNull(bombButton,bombButton[i + 1][j - 1]); //检测左下

if ( (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测下

isNull(bombButton,bombButton[i + 1][ j ]);

if ( (j + 1 = ((int)Math.sqrt(BlockNum)-1)) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测右下

isNull(bombButton,bombButton[i + 1][j + 1]);

}

}

public void actionPerformed(ActionEvent e)

{

CountRoundBomb();

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

{

((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount )+"");

((Bomb)e.getSource()).isClicked=true;

((Bomb)e.getSource()).setIcon(null);

((Bomb)e.getSource()).setEnabled(false);

if((((Bomb)e.getSource()).BombRoundCount) == 0)

isNull(bombButton,(Bomb)e.getSource());

isWin();

}

else if(((Bomb)e.getSource()).isBomb == true)

{

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

}

((Bomb)e.getSource()).setIcon(icon_bomb_big);

JOptionPane msg = new JOptionPane();

JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);

replay();

}

}

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(BombNum 0 bombSource.isRight == false ){

bombSource.setIcon(icon_flag);

bombSource.isRight = true;

BombNum--;

}

isWin();

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

}

else if(bombSource.BombFlag == 2)

{

if( (BombNum !=0 ) ||(BombNum ==0 (bombSource.getIcon()==icon_flag)) )

BombNum++;

bombSource.setIcon(icon_question);

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

}

else if(bombSource.BombFlag == 0)

{

bombSource.setIcon(null);

bombSource.isRight = false;

}

}

}

public void mouseEntered(MouseEvent e)

{}

public void mouseReleased(MouseEvent e)

{}

public void mouseExited(MouseEvent e)

{}

public void mousePressed(MouseEvent e)

{}

}

/*主类*/

public class Main

{

public static void main(String args[])

{

(new MainBomb()).show();

}

}

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类. 别外有一个方法统计那些不是雷的地方,的周围有几颗雷,到时候点开,显示出来.

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

import java.awt.*;

import javax.swing.*;

import java.util.Random;

import java.awt.event.*;

class Min extends JPanel //雷的类

{

//备注:鼠标的左键 = 1;右键 = 3;中键 = 2

private int flag = 0,statu = 0; //定义雷的属性 0:没有打开 1:打开 2:标示为雷 3:不确定

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

//传递值

{

flag = f;

mx = (x-1)*w;

my = (y-1)*w;

mw = w-1;

statu = s;

}

//根据你点击鼠标的不同来改变雷的属性

public int sendKey(int key)

{

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

int rtn = 1;

if(key == 3)

{

switch(statu)

{

case 1:

break;

case 2:

statu = 3;

break;

case 3:

statu = 0;

break;

case 0:

statu = 2;

break;

}

rtn = 1;

}

if(key == 1 statu == 0)

{

switch(flag)

{

case 0:

statu = 1;

rtn = 2;

break;

case 1:

statu = 1;

rtn = -1;

break;

}

}

return rtn;

}

}

class DrawPanel extends JPanel

{

private int i,j;

private int f = 0; //if f = 1 then game over ,if f =2 then win

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

private int msum = 6,ksum = 0; //msum:雷的个数,ksum:标示雷的个数

private int bx = 10,by = 10,bw = 40; //bx,by:棋盘的大小,bw:棋子的大小

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()},

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

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

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

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

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

{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; //状态;雷的个数;

int cx = bw/2 - 4;

int cy = bw/2 + 4;

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

break;

}

case 1: //打开状态

{

g.setColor(Color.blue);

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

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

{

g.setColor(Color.red);

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

}

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

{

g.setColor(Color.red);

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

g.setColor(Color.blue);

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

}

break;

}

case 2: //标雷状态

{

g.setColor(Color.green);

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

g.setColor(Color.blue);

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

break;

}

case 3: //不确定状态

{

g.setColor(Color.black);

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

g.setColor(Color.red);

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

break;

}

default:

break;

}

}

// 没有图形器的绘图函数:画出坐标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(ax1||ay1||axbx||ayby) return 0; //鼠标点击的区域出界了

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

}

ksum ++;

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)

{

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

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

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

}

if(ai==bxaj1ajby)

{

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

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

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

}

if(ai1aibxaj==1)

{

sum = 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(ai1aibxaj==by)

{

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

}

return sum;

}

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

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

}

}

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

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

{

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

{

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;

repaint();

}

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

{

f = 2;

repaint();

}

}

}

}

);

}

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

public void paint(Graphics g)

{

int x,y,w;

int s;

int cx = bw/2 - 4;

int cy = bw/2 + 4;

g.clearRect(0,0,600,600);

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

{

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

{

draw(g,i,j);

}

}

if(f == 1)

{

Font f = new Font("11",1,70);

Font fo = g.getFont();

g.setColor(Color.white);

g.setFont(f);

//g.setSize();

g.drawString("Game Over",0,200);

g.setFont(fo);

}

if( f == 2 )

{

Font f = new Font("11",1,70);

Font fo = g.getFont();

g.setColor(Color.white);

g.setFont(f);

//g.setSize();

g.drawString("You win!",0,200);

g.setFont(fo);

}

}

};

// 主类和程序的入口

public class Mine extends JFrame implements ActionListener

{

Container cp = getContentPane();

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

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

Label l2 = new Label("行:");

Label l3 = new Label("宽度:");

Label l4 = new Label("雷的个数:");

TextField tf1 = new TextField("10",2); //列

TextField tf2 = new TextField("10",2); //行

TextField tf3 = new TextField("40",2); //宽度

TextField tf4 = new TextField("15",2); //雷的个数

int x=10,y=10,w=40,sum=15;

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

public Mine()

{

setBackground(Color.white);

cp.setLayout(null);

cp.add(dp);

cp.add(bt);

cp.add(tf1);

cp.add(tf2);

cp.add(tf3);

cp.add(tf4);

cp.add(l1);

cp.add(l2);

cp.add(l3);

cp.add(l4);

l1.setBounds(20 ,10,20,20);

tf1.setBounds(40,10,20,20);

l2.setBounds(70,10,20,20);

tf2.setBounds(90,10,20,20);

l3.setBounds(120,10,40,20);

tf3.setBounds(160,10,20,20);

l4.setBounds(190,10,60,20);

tf4.setBounds(250,10,20,20);

bt.setBounds(300,10,80,20);

dp.setBounds(20,40,x*w,y*w);

setResizable(false);

setSize(x*w+40,y*w+80);

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

//y = Integer.parseInt(tf2.getText());

//w = Integer.parseInt(tf3.getText());

sum = Integer.parseInt(tf4.getText());

setSize(x*w+40,y*w+80);

dp.setBounds(20,40,x*w,y*w);

show();

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

}

}

public static void main(String args[])

{

new Mine();

}

};


网站名称:扫雷java代码功能解释 扫雷java代码功能解释大全
网页路径:http://cdkjz.cn/article/doisohh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220