我发现有些人平常闲着的时候会玩window自带的游戏,其中最常见的就是扫雷和纸牌。本来想用matlab编写全自动扫雷程序用来作弊,可是后来发现扫雷问题是NP完全问题(正如:旅行商NP难问题一样不能被解决),便放弃了。于是编写了类似扫雷游戏(没有经过大量测试,可能有bug,效率也不高,作弊:在命令窗口输入minefield 其中,值为1的地方为雷区)。大致规则和原来一样,只是做了些改进:加入了音乐和语音提示。具体代码如下(下面有两个文件:一个脚本文件,一个函数文件,只需运行第一个文件即可):

1.脚本文件:

clear all;
row=10;
col=10;
num=30;
jieshu=0;
%global flag;
flag=zeros(row,col);
flag1=ones(row,col);
minenum=zeros(row,col);
minefield=rand(row,col);
[temp,index]=sort(minefield(:));
minefield=(minefield<=minefield(index(num)));
count=0;
for i=1:row
    for j=1:col
      x1=i-1;y1=j-1;
      x2=i-1;y2=j;
      x3=i-1;y3=j+1;
      x4=i;  y4=j-1;
      x5=i;  y5=j+1;
      x6=i+1;y6=j-1;
      x7=i+1;y7=j;
      x8=i+1;y8=j+1;
      if x1>0&&y1>0
          if minefield(x1,y1)==1
              count=count+1;
          end
      end
      if x2>0
          if minefield(x2,y2)==1
              count=count+1;
          end
      end
      if x3>0&&y3<11
          if minefield(x3,y3)==1
              count=count+1;
          end
      end
      if y4>0
          if minefield(x4,y4)==1
              count=count+1;
          end
      end
      if y5<11
          if minefield(x5,y5)==1
              count=count+1;
          end
      end
      if x6<11&&y6>0
          if minefield(x6,y6)==1
              count=count+1;
          end
      end
      if x7<11
          if minefield(x7,y7)==1
              count=count+1;
          end
      end
      if x8<11&&y8<11
          if minefield(x8,y8)==1
              count=count+1;
          end
      end
    minenum(i,j)=count;
    count=0;
    end
end

hf=figure('NumberTitle','off','Name','扫雷','menubar','none');
uh1=uimenu('label','游戏');
uimenu(uh1,'label','背景颜色选择','callback','c=uisetcolor([0 0 1],''选择颜色'');set(hf,''color'',c);');
uh2=uimenu('label','帮助');
uimenu(uh2,'label','游戏规则','callback',['text(-0.05,0,''与window自带的扫雷不同的是:雷用黑色标记,右击时用红色作记号,'',''fontsize'',12,''fontname'',''宋体'');',...
       'hold on; text(-0.12,-0.07,''输了后,会有音乐和语音提示,赢了后,会有语音提示!'',''fontsize'',12,''fontname'',''宋体'') ; axis off ']);
uimenu(uh2,'label','制作信息','callback','msgbox(''copyright:Wteng  Thanks for using!'')');
for m=1:row;
    for n=1:col;
       h(m,n)=uicontrol(gcf,'style','push',...
            'foregroundColor',0.7*[1,1,1],...
            'string',strcat(num2str(m),num2str(n)),...
            'unit','normalized','position',[0.16+0.053*n,0.9-0.073*m,0.05,0.07],...
            'BackgroundColor',0.7*[1,1,1],'fontsize',17,...
            'fontname','times new roman',...
            'ButtonDownFcn',['if isequal(get(gcf,''SelectionType''),''alt'')',...
            ' if ~get(gco,''Value'') if isequal(get(gco,''Tag''),''y'') ',...
            'set(gco,''style'',''push'',''string'','''',''backgroundcolor'',0.7*[1 1 1]);',...
            'set(gco,''Tag'',''n''); else set(gco,''style'',''text'',''string'','''',''backgroundcolor'',[1 0 0]);',...
            'set(gco,''Tag'',''y'');end;end;end'],...
            'Callback',['h1=gcbo;[mf,nf]=find(h==h1);search(mf,nf,minenum,h,minefield,flag,jieshu);'...
            'for i=1:10 for j=1:10  hcomp(i,j)=get(h(i,j),''value'');  end;end;comp=(~hcomp==minefield);',...
            'if  all(comp(:))  mh=msgbox(''你好厉害哟!!'',''提示'');sp=actxserver(''SAPI.SpVoice'');sp.Speak(''你好厉害哟!''); end;']);
   end
end

2.搜索蔓延函数如下:

function search(mf,nf,minenum,h,minefield,flag,jieshu)
if flag==minefield
    mh=msgbox('你好厉害哟!','提示');
end
if minefield(mf,nf)==1
    set(gco,'style','text','string','','backgroundcolor',[0 0 0]);
    load handel;
    sound(y,Fs)
    pause(10);
    mh=msgbox('您输了!请再接再厉!','提示');
    sp=actxserver('SAPI.SpVoice');
    sp.Speak('您输了!请再接再厉!')
    pause(2)
    close all;
    delete(hf);
else
if minenum(mf,nf)==0
    flag(mf,nf)=1;
    set(h(mf,nf),'string','');
    set(h(mf,nf),'value',1);
    mf1=mf-1;nf1=nf-1;
    mf2=mf-1;nf2=nf;
    mf3=mf-1;nf3=nf+1;
    mf4=mf;  nf4=nf-1;
    mf5=mf;  nf5=nf+1;
    mf6=mf+1;nf6=nf-1;
    mf7=mf+1;nf7=nf;
    mf8=mf+1;nf8=nf+1;
if mf1>0&&nf1>0 && flag(mf1,nf1)==0
    flag(mf1,nf1)=1;
    if minenum(mf1,nf1)==0
        set(h(mf1,nf1),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf1,nf1),'string',num2str(minenum(mf1,nf1)));
    set(h(mf1,nf1), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf1,nf1),'style','text','backgroundcolor',[1 1 1]);
    end
    if minenum(mf1,nf1)==0
        search(mf1,nf1,minenum,h,minefield,flag,jieshu);

    end
    set(h(mf1,nf1),'value',1);
end
if mf2>0 && flag(mf2,nf2)==0
    flag(mf2,nf2)=1;
    if minenum(mf2,nf2)==0
        set(h(mf2,nf2),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf2,nf2),'string',num2str(minenum(mf2,nf2)));
    end
    set(h(mf2,nf2), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf2,nf2),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf2,nf2)==0
        search(mf2,nf2,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf2,nf2),'value',1);
end
if mf3>0&&nf3<11 && flag(mf3,nf3)==0
    flag(mf3,nf3)=1;
    if minenum(mf3,nf3)==0
        set(h(mf3,nf3),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf3,nf3),'string',num2str(minenum(mf3,nf3)));
    end
    set(h(mf3,nf3), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf3,nf3),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf3,nf3)==0
        search(mf3,nf3,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf3,nf3),'value',1);
end
if nf4>0 && flag(mf4,nf4)==0
    flag(mf4,nf4)=1;
    if minenum(mf4,nf4)==0
        set(h(mf4,nf4),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf4,nf4),'string',num2str(minenum(mf4,nf4)));
    end
    set(h(mf4,nf4), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf4,nf4),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf4,nf4)==0
        search(mf4,nf4,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf4,nf4),'value',1);
end
if nf5<11 && flag(mf5,nf5)==0
    flag(mf5,nf5)=1;
    if minenum(mf5,nf5)==0
        set(h(mf5,nf5),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf5,nf5),'string',num2str(minenum(mf5,nf5)));
    end
    set(h(mf5,nf5), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf5,nf5),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf5,nf5)==0
        search(mf5,nf5,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf5,nf5),'value',1);
end
if mf6<11&&nf6>0 && flag(mf6,nf6)==0
    flag(mf6,nf6)=1;
    if minenum(mf6,nf6)==0
        set(h(mf6,nf6),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf6,nf6),'string',num2str(minenum(mf6,nf6)));
    end
    set(h(mf6,nf6), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf6,nf6),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf6,nf6)==0
        search(mf6,nf6,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf6,nf6),'value',1);
end
if mf7<11 && flag(mf7,nf7)==0
    flag(mf7,nf7)=1;
    if minenum(mf7,nf7)==0
        set(h(mf7,nf7),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf7,nf7),'string',num2str(minenum(mf7,nf7)));
    end
    set(h(mf7,nf7), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf7,nf7),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf7,nf7)==0
        search(mf7,nf7,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf7,nf7),'value',1);
end
if mf8<11&&nf8<11 && flag(mf8,nf8)==0
    flag(mf8,nf8)=1;
    if minenum(mf8,nf8)==0
        set(h(mf8,nf8),'style','text','string','','backgroundcolor',[0 0 0]);
    else
    set(h(mf8,nf8),'string',num2str(minenum(mf8,nf8)));
    end
    set(h(mf8,nf8), 'foregroundColor',0.1*[1,1,1]);
    set(h(mf8,nf8),'style','text','backgroundcolor',[1 1 1]);

    if minenum(mf8,nf8)==0
        search(mf8,nf8,minenum,h,minefield,flag,jieshu);
    end
    set(h(mf8,nf8),'value',1);
end
    else
    set(h(mf,nf),'string',num2str(minenum(mf,nf)));
end
  set(h(mf,nf), 'foregroundColor',0.1*[1,1,1]);
  set(h(mf,nf),'style','text','backgroundcolor',[1 1 1]);
end
end

1.开始界面

2.输了的界面

3.赢了的界面

原文:http://blog.csdn.net/tengweitw/article/details/20228303

作者:nineheadedbird

【matlab编程】Matlab版扫雷的更多相关文章

  1. 一些matlab教程资源收藏,使用matlab编程的人还是挺多的

    Matlab教程专题资源免费下载整理合集收藏 <MATLAB从入门到精通>高清文字版[PDF] 103.9MB 简体中文 <矩阵实验室>(Mathworks.Matlab.R2 ...

  2. Matlab编程基础

    平台:Win7 64 bit,Matlab R2014a(8.3) “Matlab”是“Matrix Laboratory” 的缩写,中文“矩阵实验室”,是强大的数学工具.本文侧重于Matlab的编程 ...

  3. MATLAB 编程风格指南及注意事项

    MATLAB编程风格指南Richard Johnson 著Genial 译MATLAB 编程风格指南Richard JohnsonVersion 1.5,Oct. 2002版权: Datatool 所 ...

  4. 【Matlab编程】哈夫曼编码的Matlab实现

    在前年暑假的时候,用C实现了哈夫曼编译码的功能,见文章<哈夫曼树及编译码>.不过在通信仿真中,经常要使用到Matlab编程,所以为了方便起见,这里用Matlab实现的哈夫曼编码的功能.至于 ...

  5. Matlab 2016b 正式版下载

    Matlab 2016b  正式版下载 这两天为完成最优化大作业,zyy又开始鼓捣matlab了.之前我装的是matlab 2013a,发现不支持求解整数规划,遂更新了一下. 下载地址 链接:http ...

  6. matlab和c++混合编程---matlab和vs的环境配置问题及方法和步骤(转载)

    matlab和c++混合编程---方法和步骤 matlab和c++混合编程---matlab和vs的环境配置问题 摘要:Matlab具有很强的数值计算和分析等能力,而C/C++是目前最为流行的高级程序 ...

  7. MATLAB编程技巧

    [摘要] MATLAB是一种科学计算语言,和C.Fortran等高级语言相类似,能方便的实现程序控制.以下介绍一点matlab编程的技巧. 嵌套计算 程序执行的速度取决于调用的子程序的个数和算法实现. ...

  8. PID各环节的意义和功能,自带PID的matlab编程实例

    这是PID的标准形式包括比例/积分/微分三部分,e为偏差 下面我们分析三个环节的作用,设:当前系统状态A,目标状态B, e=B-A,初始状态e>0 (以下是个人的理解,欢迎读者评论) 1 比例环 ...

  9. Windows | Ubuntu18.04分别安装Matlab 2017b破解版

    首先下载好Windows和Ubuntu 版本的MATLAB 2017b 安装包, 1.Windows上安装,解压文件R2017b_win64_dvd1.iso和R2017b_win64_dvd2.is ...

  10. 58 matlab 编程

    0 引言 matlab中有些东西记录一下 1 matlab coder matlab命令行窗口输入: coder 回车即可打开matlab coder 窗口.接着,matlab将引导你把matlab格 ...

随机推荐

  1. NLP系列(3)_用朴素贝叶斯进行文本分类(下)

    作者: 龙心尘 && 寒小阳 时间:2016年2月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50629110 ...

  2. RxJava(十一)defer操作符实现代码支持链式调用

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52597643 本文出自:[余志强的博客] 一.前言 现在越来越多An ...

  3. spark运算结果写入hbase及优化

    在Spark中利用map-reduce或者spark sql分析了数据之后,我们需要将结果写入外部文件系统. 本文,以向Hbase中写数据,为例,说一下,Spark怎么向Hbase中写数据. 首先,需 ...

  4. 关于JQuery中的ajax请求或者post请求的回调方法中的操作执行或者变量修改没反映的问题

    前段时间做一个项目,而项目中所有的请求都要用jquery 中的ajax请求或者post请求,但是开始处理一些简单操作还好,但是自己写了一些验证就出现问题了,比如表单提交的时候,要验证帐号的唯一性,所以 ...

  5. Java 单元测试 JUnit4 快速入门

    JUnit最佳实践 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class CardServiceTest {     /**      * 最佳 ...

  6. FFmpeg的HEVC解码器源代码简单分析:CTU解码(CTU Decode)部分-TU

    ===================================================== HEVC源代码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpe ...

  7. 验证码程序Demo

    小伙伴都有这样的经历,册各种网站,总是输不对验证码,双十一那天狂买的小伙伴是不是对输入验证码有着不一样的感触呢,以前觉得验证码真是个麻烦鬼,一个不小心,一个眼拙,哎呦,没有输入正确,又是一阵子大眼瞪小 ...

  8. Xcode一种涉及到多桌面的调试技巧

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Mac本身是支持多桌面功能的,以下是本猫OS界面的截图: 可以 ...

  9. ubuntu16.04主题美化和软件推荐

    前几天把ubuntu从15.10更新到了16.10,在网上看到有很多直接更新出问题的,正好赶上换SSD,于是采用全新安装,之前用ubuntu的时候装软件最让人头疼了,这回又得头疼一次了!! 索性把他记 ...

  10. 1091. Acute Stroke (30)

    题目如下: One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given ...