标注: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