java扫雷布雷算法_java 扫雷 如何随机布雷?

importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;/*按扭类*/classBombextendsJButton{publicintnum_x,num_y;//第几号方块publicintBombRoundCount;//周围雷数publicboo...

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("red_flag.gif"); //雷标记

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

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

public Panel MenuPamel = new Panel();

public Panel mainPanel = new Panel();

public Bomb[][] bombButton;

/*界面设计*/

public MainBomb()

{

super("扫雷 ");

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

}

代码没全拷过来 想问一下 怎么能让一个格周围的布雷不为零??

展开

你可能感兴趣的:(java扫雷布雷算法)