利用MATLAB作出扫雷的程序

标注:matlab版本是 7.0

程序暂时例图如下:

初始界面:

碰到第一个雷区

扫雷完成

拥有多个感叹号的情况

 

下面是代码展示:

 

function saolei_main 
n=8;
total = 8*8;
bombnum=9;
X=minefield(n,bombnum);
figure('NumberTitle','off','Name','SaoLe','menubar','none');
P=zeros(n,n);
H = uicontrol(gcf,'style','text','unit','normalized',...
    'position',[0.24+0.053*4.5, 0.8 , 0.05,0.07],'fontsize',17,...
   'string','O' );
I = uicontrol(gcf,'style','text','unit','normalized',...
    'position',[0.24+0.053, 0.8 , 0.15,0.07],'fontsize',17,...
   'string',['rest:' num2str(bombnum)]);
G = uicontrol(gcf,'style','text','unit','normalized',...
    'position',[0.24+0.053*6.1, 0.8 , 0.15,0.07],'fontsize',17,...
   'string',['rest:' num2str(total)]);
for i1=1:n;
    for j1=1:n;
        P(i1,j1)=uicontrol(gcf,'style','push','string',' ',...          'unit','normalized','position',[0.24+0.053*j1,0.8-0.073*i1,0.05,0.07],...     'fontsize',17,'callback',@pushcallback,'ButtonDownFcn',@buttoncallback);
    end    
end
    function pushcallback(hobject,eventdata)
         if(get(hobject,'string') ~='!' & get(H ,'string') == 'O')
            a = get(hobject,'position');
            hang = int8((a(2)-0.8)/(-0.073));
            lie = int8((a(1)-0.24)/0.053);             
            if(X(hang,lie) == 100)     
                 set(hobject,'style','text','string','X','ForegroundColor','B');
                  set(H ,'string','X','ForegroundColor','r');
                  bombnum = bombnum-1;
            elseif(X(hang,lie) == 0)
                  set(hobject,'style','text');
                  total = total - 1;
            else          set(hobject,'style','text','string',num2str(X(hang,lie)),'ForegroundColor','k');
                  total = total - 1;
            end
         end
         set(I,'string',['rest:' num2str(bombnum)]);
         set(G,'string',['rest:' num2str(total)]);
         if total == 9
              set(H,'string','√');
         end
    end
    function buttoncallback(hobject,eventdata)
         a= get(hobject,'style');        
         if a(1)=='p' & get(H ,'string') == 'O'
            if get(hobject,'string')=='!'
                set(hobject,'string',' ','ForegroundColor','r');
                bombnum = bombnum + 1;
            else
              set(hobject,'string','!','ForegroundColor','r');
               bombnum = bombnum - 1;
            end
         end  
         set(I,'string',['rest:' num2str(bombnum)]);
    end
end
function X=minefield(n,bombnum)
    mine=rand(n,n);
    [q,index]=sort(mine(:));
    mine=(mine<=mine(index(bombnum)));
    Y=zeros(n+2,n+2);
    k=2:n+1;
    Y(k,k)=mine;
    Y(k,k)=Y(k-1,k-1)+Y(k-1,k)+Y(k-1,k+1)+Y(k,k-1)+Y(k,k+1)+Y(k+1,k-1)+Y(k+1,k)+Y(k+1,k+1);
    X=Y(k,k);
    [i,j]=find(mine==1);
    for p=1:size(i)
        X(i(p),j(p))=100;
    end
end

 

  • 24
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值