matlab 扫雷命令,Matlab版扫雷
我发现有些人平常闲着的时候会玩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
end2.搜索蔓延函数如下:
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
end1.开始界面
2.输了的界面
3.赢了的界面
原文:http://blog.csdn.net/tengweitw/article/details/20228303
matlab 扫雷命令,Matlab版扫雷相关推荐
- matlab 曲线命令,matlab软件基本的曲线拟合函数命令
matlab软件基本的曲线拟合函数命令 MATLAB软件提供了基本的曲线拟合函数的命令. 曲线拟合就是计算出两组数据之间的一种函数关系,由此可描绘其变化曲线及估计非 采集数据对应的变量信息.1.线性拟 ...
- matlab 矩阵命令,matlab中的矩阵的基本运算命令
matlab中的矩阵的基本运算命令 (2013-07-19 08:45:49) 1.1 矩阵的表示 1.2 矩阵运算 1.2.14 特殊运算 1.矩阵对角线元素的抽取 函数 diag 格式 X = d ...
- matlab 控制命令,Matlab绘图基本控制命令
图形的控制与表现 (Figurecontrol and representation) MATLAB提供的用于图形控制的函数和命令: axis: 人工选择坐标轴尺寸. clf:清图形窗口. ginpu ...
- matlab 矩阵命令,matlab矩阵运算命令
有哪位高人总结了matlab中的矩阵的基本运算命令?还有有关极限.积 1.1 矩阵的表示 1.2 矩阵运算 1.2.14 特殊运算 1.矩阵对角线元素的抽取 函数 diag 格式 X = diag(v ...
- matlab barh命令,matlab中的bar, barh
绘制直方图(水平和垂直的) 语法 bar(Y) bar(x,Y) bar(...,width) bar(...,'style') bar(...,'bar_color') bar(axes_handl ...
- matlab mex命令,matlab 调用mex
装了visual studio 2015,可是在matlab中调用失败了. >> mex -setup Error using mex No supported compiler or S ...
- matlab image命令,Matlab 图像处理相关函数命令大全
一.通用函数: colorbar显示彩色条 语法:colorbar \ colorbar('vert') \ colorbar('horiz') \ colorbar(h) \ h=colorbar( ...
- matlab menu命令,Matlab基本函数-menu函数
1.menu函数:产生菜单 2.用法说明 m = menu('title','n1','n2',......,'nn') 函数显示以字符串变量'title'为标题的菜单,选项为字符串变量:'n1',. ...
- matlab meshc函数_有那些相见恨晚的MATLAB绘图命令
有那些相见恨晚的MATLAB绘图命令 MATLAB的绘图功能可谓非常的强大,常用的有plot,plot3,subplot,scatter等等,这些函数有许多的参数设置,可以画出各种需求的图.但是这些强 ...
最新文章
- Linux——33条小技巧
- Html内容超出标记宽度后自动隐藏
- 一寸、两寸证件照photoshop批处理动作
- 由隐藏层节点数引起的网络准确率的不规则变化02
- 操作多个表_1_记录集的叠加
- 使用的postman心得
- R语言观察日志(part9)--RMarkdown之输出格式
- 赞扬别人团建评论_赞扬精心设计:基于属性的测试如何帮助我成为更好的开发人员...
- java开发工程师学什么专业,Java核心知识点
- BugkuCTF-PWN题pwn5-overflow2超详细讲解
- android httpget 参数,安卓通过httpget传入json参数,请求数据
- ecshop php5.5分类文字不显示,在PHP5.5下ecshop修正文件报错归类
- python装饰器class_Python装饰器dataclass详解
- vue分享卡住_【面经分享】华为测试工程师面经
- linux搭建虚拟ftp,linux下搭建ftp服务器(虚拟用户)
- 笔记本无线网卡资源管理器服务器,笔记本无线网卡怎么打开 笔记本无线上网卡找不到网络解决方法...
- 针对BT防御方案——飞鱼星路由器
- 奥拉星插件flash下载手机版下载安装_flash控件手机版
- Python 中文数字对照表 输入一个数字,转换成中文数字。比如:1234567890 -> 壹贰叁肆伍陆柒捌玖零。【简单易懂,代码可以直接运行】
- 华夏文化-让你心醉又心碎的陶笛--故乡的原风景
热门文章
- USACO(含training section)水题合集[5/未完待续]
- DB2行转列(多维度)
- GNU make manual 翻译( 一百五十五)
- Eclipse JPBC library安装及测试
- 手把手教你用Scrapy爬取知乎大V粉丝列表
- 爬虫小工具合集|不会编程也能爬数据
- Apache Dubbo 3.0.0 正式发布!全面拥抱云原生
- Java!越来越像Kotlin了!!
- 图解 Kafka,看本篇就足够啦
- Pull Request的正确打开方式(如何在GitHub上贡献开源项目)