导航:首页 > 源码编译 > 扫雷算法

扫雷算法

发布时间:2022-02-07 05:39:01

1. 扫雷算法

我做了一个扫雷游戏。不过是C#的。如果你觉得有用的话可以给我发邮箱一个信息。我会给把程序给你回过去,包含源码。不过算法可能不是很优话
因为都是在大学的时候写的。你要是觉得有用就发[email protected]
呵呵

2. 扫雷怎么推理又怎么计算

1、推理方法:

扫雷中的1,2,3,4代表在这个数字周围的8个方块里有地雷的数量,如是一就代表有一个;二就代表有2个。

如:对一条未挖开的方块组成的边,如果它旁边的数字为“232”,则表示这三个数字旁边的三个方块都是地雷。

2、通常玩法是先乱点,点出一块较大的无雷区域,再根据无雷区域边上的数字判断地雷的位置测出去,在你确定有地雷的方块上点右键插上红棋。

把全部的地雷上都正确的插上红旗就可以赢。

3、计算方法就根据上述规则在游戏中自行尝试。由于每次开始游戏后的雷的位置不同,所以需要多进行游戏,多摸索,只要掌握游戏方法就可以找出所有雷了。

(2)扫雷算法扩展阅读:

扫雷口诀

一:基本定式不要忘。

二:鼠标点击不要快。

三:就近猜雷把心横。

四:猜雷猜错不要悔。

五:碰上好局不要慌。

3. 扫雷的程序

参照以下步骤就行了:
1.启动“扫雷”小游戏。
2.在键盘上输入“xyzzy”。
3.然后按住”shift“键大约一秒钟。
4.现在雷区里哪个是雷哪个不是雷,一目了然,当鼠标移到雷区的某个方块时,注意看电脑屏幕的最左上角,如果左上角有一个小亮点显示,证明不是雷,反之则是雷。

注意:在输入xyzzy时并没有提供输出的窗口什么的,只要打开扫雷游戏,再直接在键盘上输字母,然后按shift键就可以了。

不一定非用那个
也可以点双键
在已扫出的数字上
同时点两个鼠标键
周围可能是雷的就出现啦!!

呵呵,只能帮你这么多了!!!!!
多多谅解!!!!!!
请参考

4. 扫雷点到空格时的算法 跪求

从手工点开的这个空格进行处理,按上右下左或你自己定义的一个顺序来判断相应位置的格式是否是空格且未被点开,如果不是,则跳过,如果是,则将其自动点开,同时把这几个位置加入队列后续处理。
简单的流程图示意:

当前位置是空白位置?----否--->非空白的处理
|
|是
|
V
加入队列
|
V
+--->队列为空?-------->是--->结束
||
||否
||
|V
|第一个元素出队
||
|V
|点开该元素所指的位置
||
|V
|上左下右的位置如果是空白且未点开则入队
||
--------+


上面是非递归的方案,递归方案则更容易了:
伪代码算法描述如下:
Click(pos)//点开pos这个位置
{
//IsClicked()判断是否是已经点开的格子
if(IsClicked(pos))
return;

//IsBlank()判断是否是空白格子
if(!IsBlank(pos))
{
点开非空白格子的处理
}

//下面是点开空白格子的处理
ClickBlank(pos);
}

ClickBlank(pos)
{
if(!IsBlank(pos))
rerurn;

if(IsClicked(pos))
return;

//下面对四个方向的格子进行自动点开
//你需要计算四向的格子位置,无效的直接返回

ClickBlank(pos上面的格子);
ClickBlank(pos右面的格子);
ClickBlank(pos下面的格子);
ClickBlank(pos左面的格子);
}

5. J2ME扫雷算法

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

}
}

6. 关于编程,扫雷的算法问题,求教~~~

点下去空白自动出现可以根据递归来算,找每个格子周围是否有空白,有就递归。。。

7. 游戏扫雷有一种算法能分析出哪个是雷,是怎么算的

呵呵
这个要自己玩多次后就慢慢会算了比如2就代表旁边的格子有两个,谁也不知道在哪,只有依据周围的数字来定个大概

8. java扫雷递归算法

在扫雷游戏中,如何实现使Field处于打开状态。如果它是地雷,打开所有Field;如果它不是地雷,并且它四周也没有地雷,将其四周Field也打开。

//雷的数目如果是0,可以打开当前Field四周的Field
if (getField(x, y).getMineValue() == 0) {
List<Field> arroundList = getAround(x, y);
for (Field field : arroundList) {
//递归调用open方法,
open(field.getX(), field.getY());
}
}
Open方法指的就是你指定的打开方法,这里通过0或者数字1来定义是否为雷,如果是1则是
雷。希望能提示到你。主要就是使用到啦增强型for循环

9. 扫雷算法时间复杂度是多少

算法复杂度,可分为时间复杂度,以及空间复杂度,
时间复杂度根据执行程序的时间,时间越长复杂度越高,
空间复杂度是根据所占内存的大小来确定,占用空间越多,空间复杂度越高
所以你这问题问的太笼统,扫雷算法又不止一种,有好的算法,也有不好的

10. 扫雷中布雷的算法

首先,你要先定义一个n*n的二维数组,该数组的i-1到i+1,j-1到j+1除去i,j本身。值为周围有几个雷。如果该数组的值为10则本身是雷。
接着,你自动生成m个雷,让这m个雷分布在界面上。被选中的值i,j点的值为10,从i-1到i+1,从j-1到j+1的值都+1;
第三,在界面上想办法区分出n*n个格子,然后把格子的坐标和二维数组关联起来

阅读全文

与扫雷算法相关的资料

热点内容
兵器pdf 浏览:920
云服务器怎么限制cpu 浏览:164
学信网用的什么app 浏览:876
linux重启命令apache 浏览:751
半夜解压有什么坏处 浏览:424
linux代理命令 浏览:637
调用tasking的编译器编译 浏览:292
青柠app是什么 浏览:866
linuxapachephp56 浏览:397
安卓手机如何打开eng文件 浏览:24
看拉丁电视都用什么app好 浏览:780
什么是哲学pdf 浏览:509
hdfs的三个下载命令 浏览:525
java常用的排序算法 浏览:359
51单片机连接adc 浏览:861
python命名变量报错 浏览:122
安卓手机如何换windows系统 浏览:614
python中的类是什么 浏览:632
我的英雄学院用哪个app可以看 浏览:37
excel插入选项卡对象命令 浏览:695