matlab 扫雷,matlab 扫雷
我发现有些人平常闲着的时候会玩window自带的游戏,其中最常见的就是扫雷和纸牌。本来想用matlab编写全自动扫雷程序用来作弊,可是后来发现扫雷问题是NP完全问题(正如:旅行商NP难问题一样不能被解决),便放弃了。于是编写了类似扫雷游戏(没有经过大量测试,可能有bug,作弊:在命令窗口输入minefield 其中,值为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
matlab 扫雷,matlab 扫雷相关推荐
- c++引用matlab类,matlab调用C++函数浅谈(一)
由于在下才疏学浅,在网上看各高手指南时亦觉云里雾里,遂决定一切说明从最基础说起,一是方便自己(记性奇差),二是方便似我的小白.以下部分是我从各网站论坛等摘抄.重组.改写过的,以求更加详实明朗,由于参考 ...
- 磁盘驱动读取系统MATLAB仿真,matlab读写..doc
matlab读写. MATLAB二进制数据文件的读写 (2011-06-04 19:44:27) 转载▼ 标签: easleyhux matlab 二进制读写 杂谈分类: MATLAB 所谓二进制格式 ...
- 【 MATLAB 】MATLAB 实现模拟信号采样后的重建(三)一阶保持(FOH)内插
上篇博文采用了零阶保持(ZOH)的方式进行了重构:[ MATLAB ]MATLAB 实现模拟信号采样后的重建(二)零阶保持(ZOH) 这篇博文我们使用一阶保持(FOH)内插来重建信号,采用的案例依然是 ...
- 【 MATLAB 】MATLAB 实现模拟信号采样后的重建(二)零阶保持(ZOH)
上篇博文采样sinc函数内插的方式实现了模拟信号的重建:[ MATLAB ]MATLAB 实现模拟信号采样后的重建(一) 这篇博文我们使用零阶保持器(ZOH)来重建信号,采用的案例依然是上篇博文中的案 ...
- Matlab:Matlab中常用的函数、案例详细攻略
Matlab:Matlab中常用的函数.案例详细攻略 目录 常用函数 1.与文件相关 2.MATLAB GUI不同控件函数间变量传递方法 常用函数 Matlab中的bwmorph函数解释 bwmorp ...
- vb调用matlab工具箱,Matlab与VB集成 - 关于VB的经验之谈 - VB爱好者乐园(VBGood) - 关于VB的经验,电子教程,代码,控件,论坛,博客,微博等....
工业生产中经常遇到复杂的数据信息处理问题,需要大运算量的矩阵计算及对分析结果进行实时.直观的图形化显示.Matlab是集数值分析.矩阵运算.信号处理和图形显示于一体的高性能数学软件,将其强大的计算功能 ...
- ccs matlab联调,MATLAB与CCS联调中idelink_ert_tlc和ert_tlc选择
同问.希望高工给解释一下.我在用idelink_ert_tlc的时候就能通过2014B给DSP直接下载程序.但是用ert_tlc就不行,提示文件路径不对.问题描述如下,请高工帮忙解决一下,谢谢! ## ...
- matlab转向语句,MATLAB控制语句
MATLAB控制语句 目的:研究控制结构(用于,是否,切换,中断,继续,输入/输出功能,读取和存储数据). 如果:如果计算逻辑表达式,并根据表达式的值执行一组语句. If语句的语法 例子 输出: Sw ...
- opencv matlab配置,Matlab下运行c++程序的配置(包含opencv的c++程序)
本文使用的是matlab2012b.VS2010和OpenCV249 1.打开matlab 2.输入mex -setup Welcome to mex -setup. This utility wi ...
最新文章
- Oracle之to_char(值,'fm99990.00')
- 立镖机器人浙江_立镖在美国获2020 NextGen供应链解决方案奖
- 强大的Canvas开源库Fabric.js简介与开发指南
- 神经网络与深度学习——TensorFlow2.0实战(笔记)(五)(Matplotlib绘图基础<1>python)
- Windows To Ghost系统封装之必备软件集 - 好压
- 一文读懂云上DevOps能力体系
- Mysql 声明变量
- NAT网络映射设置:允许局域网用户访问VMWare内linux的SSH
- android.os.parcelable,Android中Parcelable如可快速生成
- 事务日志的物理和逻辑构架
- 来自平时工作中的css知识的积累---持续补充中
- ios13 文件 连接服务器 您已离线,iOS13,终于可以屏蔽系统更新了
- 备战数学建模48-数学规划模型终结篇(全)(攻坚战13)
- vector2Drawable(批量将png图片转换成android使用的矢量图 )
- 致加西亚的信 名言佳句
- mdf导入虚拟主机mysql_MYSQL导入mdf文件
- 轻松获得oblog2.52的WebShell
- 泊松过程2 | 泊松过程扩展
- Codeforces Round #783 (Div. 2) A-F
- 树、二叉树、满二叉树、完全二叉树、二叉树的重要性质及其存储结构