借鉴了很多人扫雷的版本
这个扫雷程序第一步不会碰到雷,而且掀开方块的速度比较快

程序

function XPBOMBS2
clear
row=15;col=20;mines=45;
total=mines;times=1;
remine=row*col;
flags=0;
hf=figure('NumberTitle','off','Name','扫雷','menubar','none','position',[370,130,600,500]);
uh1=uimenu('label','帮助');
uimenu(uh1,'label','游戏规则','callback',['msgbox(''和windows自带的扫雷一个样,嗯哒'')'])
colormap([1 0 0;0 0 0;.65 .65 .65;1 1 1]);
axis offhold on;
C=uicontrol(gcf,'style','text','unit','normalized',...'position',[0.45,0.86,0.09,0.078],'fontsize',17,...'BackgroundColor',0.85*[1 1 1],...'string','o');
H=uicontrol(gcf,'style','text','unit','normalized',...'position',[0.58,0.86,0.3,0.078],'fontsize',12,...'BackgroundColor',0.85*[1 1 1],...'string',['total:' num2str(total)]);
F=uicontrol(gcf,'style','text','unit','normalized',...'position',[0.1,0.86,0.3,0.078],'fontsize',12,...'BackgroundColor',0.85*[1 1 1],...'string',['flag:' num2str(flags)]);
for m=1:row for n=1:colh(m,n)=uicontrol(gcf,'style','push',...'foregroundColor',0.7*[1 1 1],...'BackgroundColor',0.7*[1 1 1],...'fontsize',15,'fontname','time new roman',...'Unit','normalized','position',[0.013+0.045*n,0.86-0.054*m,0.043,0.052],...'Visible','on',...'callback',@pushcallback,'ButtonDownFcn',@buttoncallback);  end
endfunction first_time(place)mine=rand(row,col);A=ones(row,col);[x,y]=find(A==1);postions=[x,y];places=[place;place+[1,0];place+[-1,0];place+[1,1];place+[0,1];place+[-1,1];place+[1,-1];place+[0,-1];place+[-1,-1]];places=intersect(postions,places,'rows');pos=places(:,1)+(places(:,2)-1).*row;mine(pos)=1;[~,index]=sort(mine(:));mine=(mine<=mine(index(mines)));Y=zeros(row+2,col+2);x=2:row+1;y=2:col+1;Y(x,y)=mine;Y(x,y)=Y(x-1,y-1)+Y(x-1,y)+Y(x-1,y+1)+Y(x,y-1)+Y(x,y+1)+Y(x+1,y-1)+Y(x+1,y)+Y(x+1,y+1);around=Y(x,y);end
    function pushcallback(hobject,~)a = get(hobject,'position');hang=double((a(2)-0.86)/(-0.054))-0.0001;lie=double((a(1)-0.013)/0.045)-0.0001;place=ceil([hang,lie]);if times==1first_time(place)times=0;endif mine(place(1),place(2))==1&&~strcmp(get(hobject,'string'),'!')[p,q]=find(mine==1);bombs=p+row*(q-1);set(h(bombs),'style','text','string','x','ForegroundColor','k','backgroundcolor',0.85*[1,1,1]);set(hobject,'style','text','string','X','ForegroundColor',[0.8 0 0],'backgroundcolor',0.85*[1,1,1]);buttonName2=questdlg('It is a pity that you lose','Game over','close','restart','close');if isempty(buttonName2)buttonName2='end';endif strcmp(buttonName2,'restart')clf;close;XPBOMBS2();else if strcmp(buttonName2,'close')close;endendendif (mine(place(1),place(2))==0)&&(around(place(1),place(2))~=0)&&~strcmp(get(hobject,'string'),'!')drawnum(hobject,place)endif (mine(place(1),place(2))==0)&&(around(place(1),place(2))==0)&&~strcmp(get(hobject,'string'),'!')begins=place;[whitea,whiteb]=find(around==0);white=[whitea,whiteb];next=[begins;begins+[1,0];begins+[-1,0];begins+[0,1];begins+[0,-1]];while ~isempty(intersect(white,next,'rows'))[a,b,~]=intersect(white,next,'rows');begins=[a;begins];white(b,:)=[];ad=length(sum(begins,2));next=[begins;begins+ones(ad,1)*[1,0];begins+ones(ad,1)*[-1,0];begins+ones(ad,1)*[0,1];begins+ones(ad,1)*[0,-1]];enddrawbegins=begins(:,1)+row*(begins(:,2)-ones(ad,1));set(h(drawbegins),'style','text','string','','backgroundcolor',0.85*[1,1,1]);control(drawbegins)=0;colors=[begins;begins+ones(ad,1)*[1,0];begins+ones(ad,1)*[-1,0];begins+ones(ad,1)*[0,1];begins+ones(ad,1)*[0,-1];begins+ones(ad,1)*[1,1];begins+ones(ad,1)*[1,-1];begins+ones(ad,1)*[-1,1];begins+ones(ad,1)*[-1,-1]];colors=unique(colors,'rows');[txa,txb]=find(around~=0);tx=[txa,txb];txcolors=intersect(tx,colors,'rows');for i=1:length(sum(txcolors,2))drawnum(h(txcolors(i,1),txcolors(i,2)),txcolors(i,1:2))endendend
    function buttoncallback(hobject,~)if strcmp(get(gcf,'SelectionType'),'alt')if ~strcmp(get(hobject,'style'),'text')if ~strcmp(get(hobject,'string'),'!')set(hobject,'string','!','ForegroundColor',[0.9,0,0])flags=flags+1;set(F,'string',['flag:' num2str(flags)]);else set(hobject,'string',' ')flags=flags-1;set(F,'string',['flag:' num2str(flags)])endendendendfunction drawnum(hobject,place)if around(place(1),place(2))==1,set(hobject,'style','text','string',1,'ForegroundColor','b','backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==2,set(hobject,'style','text','string',2,'ForegroundColor',[0,0.7,0],'backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==3,set(hobject,'style','text','string',3,'ForegroundColor',[0.8,0,0],'backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==4,set(hobject,'style','text','string',4,'ForegroundColor',[0,0,0.6],'backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==5,set(hobject,'style','text','string',5,'ForegroundColor',[0.5,0,0],'backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==6,set(hobject,'style','text','string',6,'ForegroundColor',[0,0.6,0],'backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==7,set(hobject,'style','text','string',7,'ForegroundColor',[0.75,0,0],'backgroundcolor',0.85*[1,1,1]);endif around(place(1),place(2))==8,set(hobject,'style','text','string',8,'ForegroundColor',[0.4,0,0],'backgroundcolor',0.85*[1,1,1]);endcontrol(place(1),place(2))=0;if control==mine[p,q]=find(mine==1);bombs=p+row*(q-1);set(h(bombs),'string','!','ForegroundColor',[0.9,0,0]);buttonName1=questdlg('you win. What do you mean to do?','Congratulations','close','restart','close');if isempty(buttonName1)buttonName1='end';endif strcmp(buttonName1,'restart')clf;close;XPBOMBS2();else if strcmp(buttonName1,'close')close;endendendend
mine=zeros(row,col);
control=ones(row,col);
around=zeros(row,col);
end

将这几段代码复制到同一个m文件即可

matlab 扫雷小游戏相关推荐

  1. 如何利用C#开发“扫雷”小游戏

    本文详细说明了利用C#语言开发"扫雷"小游戏的开发过程.

  2. 用C语言实现扫雷小游戏(附上思路+项目展示+源代码)

    文章目录 前言 一.扫雷小游戏整体思路讲解. 二.game.c各游戏功能函数的讲解 1.InitBoard 初始化数组函数讲解 2.DisplayBoard 打印格子函数讲解 3.Setmine 电脑 ...

  3. C语言编写扫雷小游戏

    该博文为原创文章,未经博主同意不得转载,如同意转载请注明博文出处 本文章博客地址:https://cplusplus.blog.csdn.net/article/details/104992377 扫 ...

  4. 也分享自己做的JS扫雷小游戏

    看了草根程序猿分享的JS扫雷小游戏 想起去年的时候自己也做了一个 于是也拿出来分享之 先上截图~ 引用了jQuery,节省了很多鼠标点击上的判断 界面显然都是照搬Windows的扫雷啦 详细的内容注释 ...

  5. 使用C语言写一个扫雷小游戏

    前言 相信扫雷游戏小伙伴们肯定都玩过吧,学习了C语言中的数组.函数等基础内容之后就可以自己写一个简易的扫雷小游戏了,今天就我写扫雷小游戏的过程及思路写一篇博客,希望大家看完我的博客能有所收获. 软件及 ...

  6. 如何开发一个扫雷小游戏?

    如何用C#开发一个扫雷小游戏? 十分自豪的说,计算机编程就是变魔术,每一个coder都是一个魔术师. 初学C#的时候,我相信很多人都和我一样,学会了基本语法,掌握了基本的数据结构,也见过了不少微软提供 ...

  7. rust游戏亮度怎么调亮点_之前用Rust写的扫雷小游戏

    这次来分享一下之前用Rust写的一个扫雷小游戏,目前能在Windows下运行.Github仓库: https://github.com/crlf0710/charlesmine-rs​github.c ...

  8. 用C语言实现一个简单的扫雷小游戏(附全代码及教程)

    本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下: 首先,创建一个text.c文件: 编写主函数: int main() {test();return 0; } 定义test ...

  9. C语言实现扫雷小游戏 纯小白 非黑窗口

    C语言实现一个普通的扫雷小游戏 纯小白所编(含代码非黑窗口!) 扫雷 主要功能 1.创建一个图形界面 2.了解扫雷游戏的原理 3.随机生成雷的位置 4.为整个数组加密,并在雷周围的位置加一 5.导入图 ...

  10. C/C++语言扫雷小游戏(eaxyX图形库的应用)

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.扫雷的玩法,以及实现功能介绍 二.代码实现 1.引入库 2.宏定义部分 3.初始化游戏界面 4.加载图片 5.绘制 ...

最新文章

  1. 【VMCloud云平台】SCCM(四)域内推送代理
  2. winform中获取指定文件夹下的所有图片
  3. jquery php 抽奖,jquery实现抽奖系统
  4. 为什么大公司一定要使用微服务?
  5. Java 9.while语句
  6. SpringMVC使用AOP
  7. MapReduce-流量统计求和-排序-Mapper和Reducer编写
  8. 更新node_kubernetes证书更新
  9. Docker集群管理之Swarm介绍
  10. 服务器是怎么工作的?(一)——DHCP工作原理剖析
  11. canvas 绘制贪吃蛇游戏 1
  12. 项目经理,别让猴子跳回背上!
  13. python毕业设计总结范文大全_毕业设计总结【范文】
  14. 幼儿园大班下学期幼小衔接工作计划
  15. php青蛙跳井代码,初八葬一青蛙跳井怪穴------行龙点劲!
  16. 用easyx图形库做一个简单的c++小游戏---贪吃蛇
  17. 人脸识别: 人脸数据集大全
  18. 文字识别(输入为自然场景中的图像)
  19. 【conda】conda环境的复制移植的两种方法
  20. 我的世界服务器物品怎么上锁,我的世界教你如何将箱子上锁方法解答

热门文章

  1. Python中的if __name__ == '__main__'是什么意思呢
  2. 已解决:网易邮箱smtphz.qiye.163.com在阿里云环境发不出邮件的问题
  3. RecyclerView 报Scrapped or attached views may not be recycled. as Scrap:false isAttached:true异常
  4. postgresql 数据库执行计划 Nested Loop
  5. 思科模拟器-利用VTP管理VLAN
  6. LeetCode | 0665. Non-decreasing Array非递减数列【Python】
  7. 编译原理归纳学习——去除晦涩
  8. Arduino Uno 与 触摸模块 ttp223 实验详录
  9. ME525做网络收音机和学外文用了……(安卓4.4.4系统,20190817更新)
  10. 非负大整数加法---网易校招附加题