function xpbombs(arg1,arg2,arg3)
%定义全局变量
global h minefield cntmines rows cols t nummines remaining
global HEIGHT WIDTH BWIDTH BHEIGHT textHndl
global flagHndl bomb hidden flag hb nhb hflag
%h
%h是13*13的矩阵,它的元素为个按钮的句柄
%minefield
%雷区数据(矩阵),1有炸弹,0没有炸弹
%cntmines
%相邻按钮中有多少个炸弹
%rows
%按钮的行数
%cols
%按钮的列数
%t
%文本框的句柄
%nummines
%炸弹总数
%remaining
%剩余的炸弹数量
%HEIGHT
%按钮的高
%WIDTH
%按钮的宽
%BWIDTH
%相邻按钮的水平距离
%BHEIGHT
%相邻按钮的垂直距离
%textHndl
%文本框(显示剩余炸弹的数量)的句柄
%flagHndl
%Flag复选框的句柄
%bomb
%炸弹的位图矩阵(定义炸弹的形状)
%hidden
%灰色炸弹的位图矩阵
%flag
%标记的位图矩阵
%hb
%按钮中,是炸弹的按钮的句柄
%nhb
%按钮中,是炸弹的按钮的句柄的数量
%hflag
%标记图形的句柄
%=============================
%若无输入参数,则自动将第一个参数设置为start
if nargin<1
arg1='start';
end;
%=============================
if strcmp(arg1,'start');
%设置雷区的按钮为8*8
rows=8;
cols=8;
%设定炸弹的总数为13
nummines=13;
%设定按钮的宽、高及水平、垂直间距
BWIDTH=35;
SWIDTH=0;
BHEIGHT=35;
SHEIGHT=0;
WIDTH=BWIDTH+SWIDTH;
HEIGHT=BHEIGHT+SHEIGHT;
%炸弹的位图矩阵
bomb=[
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
1 1 1 2 1 1 2 2 2 2 2 1 1 2 1 1 1 1
1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1
1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1
1 1 1 2 2 2 1 1 2 2 2 2 2 2 1 1 1 1
1 1 1 2 2 2 1 1 2 2 2 2 2 2 1 1 1 1
1 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 1 1
1 1 1 2 2 2 1 1 2 2 2 2 2 2 1 1 1 1
1 1 1 2 2 2 1 1 2 2 2 2 2 2 1 1 1 1
1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1
1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1
1 1 1 2 1 1 2 2 2 2 2 1 1 2 1 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
];
%设置灰度炸弹的位图矩阵
hidden=bomb+2*(bomb==1);
%设置标记的位图矩阵
flag=[
3 3 3 3 3 3 2 3 3 3 3 3
3 3 3 3 3 1 2 3 3 3 3 3
3 3 3 1 1 1 2 3 3 3 3 3
3 1 1 1 1 1 2 3 3 3 3 3
3 3 3 1 1 1 2 3 3 3 3 3
3 3 3 3 3 1 2 3 3 3 3 3
3 3 3 3 3 3 2 3 3 3 3 3
3 3 3 3 3 3 2 3 3 3 3 3
3 3 3 3 3 3 2 3 3 3 3 3
3 3 3 3 3 2 2 2 3 3 3 3
3 3 3 3 2 2 2 2 2 3 3 3
3 3 3 2 2 2 2 2 2 2 3 3
];
%雷区与图形窗口左端的距离
xoff=45;
%==============================
%图形窗口的位置
pos=get(0,'defaultfigureposition');
%建立图形窗口
figure(...
'name','XPBombs',...
'numbertitle','off',...
'resize','off',...
'visible','off',...
'color',192/255*[1 1 1],...
'WindowButtonDownFcn','xpbombs(''WindowButtonDownFcn'')',...
'WindowButtonUpFcn','xpbombs(''WindowButtonUpFcn'')');
%红、黑、灰、白
colormap([1 0 0;0 0 0;.65 .65 .65;1 1 1]);
%建立坐标轴
a=axes(...
'Units','pixels',...
'PlotBoxAspectRatio',[1 1 1],...
'position',[WIDTH+xoff,HEIGHT,cols*WIDTH-SWIDTH,rows*HEIGHT-SHEIGHT],...
'color','none',...
'box','on',...
'XLim',[0 cols*WIDTH-SWIDTH],...
'YLim',[0 rows*HEIGHT-SHEIGHT],...
'XColor','k','YColor','k',...
'YDir','reverse',...
'Tag','mainaxes',...
'Xtick',[],'Ytick',[]);
%将''hold''的状态设置为''on'',以便于在坐标系中绘制按钮、炸弹、标记等
hold on;
%将按钮、文本框、标记的句柄初始值设置为0
h=zeros(rows,cols);
t=zeros(rows,cols);
hflag=zeros(rows,cols);
%建立雷区的按钮,将m,n设置为用户数据
for m=1:rows
for n=1:cols
h(m,n)=uicontrol(...
'style','pushbutton',...
'units','pixels',...
'position',[n*WIDTH+xoff,m*HEIGHT,BWIDTH,BHEIGHT],...
'userdata',[m,n]);
end;
end;
%炸弹图形句柄的数量 ???????????
nhb=0;
%绘制雷区的边框
for m=1:rows
line(...
'XData',[0 cols*WIDTH],...
'YData',[m*HEIGHT,m*HEIGHT],...
'color','k','linewidth',1);
end;
for n=1:cols
line(...
'XData',[n*WIDTH,n*WIDTH],...
'YData',[0,rows*HEIGHT],...
'color','k','linewidth',1);
end;
%创建文本框,将文本框的置于雷区的左上方,文本框显示炸弹的数量
textHndl=uicontrol(...
'style','text',...
'units','pixels',...
'backgroundcolor',192/255*[1 1 1],...
'fontsize',[24],...
'fontweight','bold',...
'position',[WIDTH+xoff (rows+2)*HEIGHT 2*WIDTH WIDTH],...
'string',num2str(nummines));
%=======================================
%制作控制按钮
%定义控制按钮的位置
yInitPos=0.90;
top=0.95;
left=0.8;
bottom=0.05;
btnWid=0.15;
btnHt=0.10;
%相邻控制按钮的垂直间距
spacing=0.04;
%======================================
%背景框
frmBorder=0.02;
yPos=0.05-frmBorder;
frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
uicontrol(...
'style','frame',...
'units','normalized',...
'position',frmPos,...
'backgroundcolor',[0.50 0.50 0.50]);
%========================================
%创建Flag复选框
btnNumber=1;
yPos=top-(btnNumber-1)*(btnHt+spacing);
labelStr='Flag';
callbackstr='';
btnPos=[left yPos-btnHt btnWid btnHt];
flagHndl=uicontrol(...
'style','checkbox',...
'units','normalized',...
'position',btnPos,...
'string',labelStr,...
'callback',callbackstr);
%========================================
%创建New按钮,单击New按钮将执行xpbombs('newgame')指令,
%即将newgame作为参数执行xpbombs指令,开始一局新的游戏
btnNumber=2;
yPos=top-(btnNumber-1)*(btnHt+spacing);
labelStr='New';
callbackstr='xpbombs(''newgame'')';
btnPos=[left yPos-btnHt btnWid btnHt];
uicontrol(...
'style','pushbutton',...
'units','normalized',...
'position',btnPos,...
'string',labelStr,...
'callback',callbackstr);
%===========================================
%Info按钮,点击Info按钮将执行xpbombs('info')指令显示帮助信息
labelStr='Info';
callbackstr='xpbombs(''info'')';
btnPos=[left bottom+btnHt+spacing btnWid btnHt];
uicontrol(...
'style','pushbutton',...
'units','normalized',...
'position',btnPos,...
'string',labelStr,...
'callback',callbackstr);
%============================================
%Close按钮,单击Close按钮执行close(gcf)指令,退出游戏
labelStr='Close';
callbackstr='close(gcf)';
btnPos=[left bottom btnWid btnHt];
uicontrol(...
'style','pushbutton',...
'units','normalized',...
'position',btnPos,...
'string',labelStr,...
'callback',callbackstr);
%在雷区按钮、文本框和控制按钮都建立完毕后,是窗口可见
set(gcf,'visible','on','color',192/255*[1 1 1]);
xpbombs('newgame') ;
%若xpbombs指令的第一个参数为newgame
elseif strcmp(arg1,'newgame')
axes(findobj(gcf,'Ta