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);
}
}
} );
【java扫雷布雷算法_java 扫雷 如何随机布雷()】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设计模式面试题及其答案汇总
- Java|【数据结构与算法】——必知必会的排序算法你会几种
- 数据结构与算法|数据结构与算法 | 用Java语言实现顺序表真的不难
- Java|Java基础——面向基础模块
- Java|阿里云——Java实现手机短信验证码功能
- Java|Java实现短信验证码
- 进阶之路|大聪明教你学Java | 面试官(请你说说 Redis 为什么这么快())
- spring整合mybatis bug解决java.lang.IllegalArgumentException: Mapped Statements collection does not conta
- 安卓网络编程学习——java原生网络编程
- Java Applet基础——输出HelloWorld