编写程序在架构上参考了博主slandarer的文章matlab扫雷小游戏
一、构建棋盘
首先在matlab中构建一个新的脚本文件,建立一个figure,可以简单的添加目录标题美化视图,可以用循环输出按钮的方式建立一个长20格,宽15格的扫雷棋盘。代码如下
clear
global row col mines times number visit h flag F success f
f=0;
row=15;col=20;mines=45;
total=mines;times=1;
remine=row*col;
flag=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 off
hold 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(flag)]);
for m=1:row
for n=1:col
h(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.0