matlab实现扫雷小游戏

matlab实现扫雷小游戏

主语:

clear all;
row=10;
col=10;
num=30;
jieshu=0;
%global flag;
flag=zeros(row,col);
%生成0矩阵
flag1=ones(row,col);
%生成1矩阵
minenum=zeros(row,col);
%生成0矩阵
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>1
          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
%对整个10x10的方格的边界情况进行分类处理
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,''与WINDOWS自带的扫雷不同的是:雷用黑色标记,右击用红色代表小红旗'',''fontsize'',12,''fontname'',''????'');',...
       'hold on; text(-0.12,-0.07,''输了后会有音乐和语言提示,赢了后,会有语音提示!'',''fontsize'',12,''fontname'',''宋体'') ; axis off ']);
uimenu(uh2,'label','制作信息','callback','msgbox(''copyright:Aining  '')');
%菜单设计和背景颜色设计
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
%对鼠标操作进行控制与反应

函数如下:

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(4);
    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

效果:

请添加图片描述

  • 4
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeff one

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值