华容道小游戏.docx
《华容道小游戏.docx》由会员分享,可在线阅读,更多相关《华容道小游戏.docx(22页珍藏版)》请在冰豆网上搜索。
华容道小游戏
华容道小游戏
基于GUI界面的华容道小游戏(横刀立马)效果图如下:
实现5x4方格界面,初始化为华容道经典横刀立马局势(如下图)。
人物用数字来表示,0代表空方格,1代表曹操,2代表黄忠,3代表张飞,4代表马超,5代表赵云,6代表关羽,7代表小兵。
曹操占4个方格,黄忠、张飞、马超、赵云、关羽分别占2个方格,小兵占1个方格。
1、游戏用鼠标玩,点击人物的任一方格后,该人物被选中,随后点击空方格,如果可以移动,则选中的人物移动到方格处。
2、游戏的目标是曹操从最下面一行的中间走出,就算成功。
3、游戏能够显示已完成步数。
functionvarargout=untitled(varargin)
%UNTITLEDMATLABcodeforuntitled.fig
%UNTITLED,byitself,createsanewUNTITLEDorraisestheexisting
%singleton*.
%
%H=UNTITLEDreturnsthehandletoanewUNTITLEDorthehandleto
%theexistingsingleton*.
%
%UNTITLED('CALLBACK',hObject,eventData,handles,...)callsthelocal
%functionnamedCALLBACKinUNTITLED.Mwiththegiveninputarguments.
%
%UNTITLED('Property','Value',...)createsanewUNTITLEDorraisesthe
%existingsingleton*.Startingfromtheleft,propertyvaluepairsare
%appliedtotheGUIbeforeuntitled_OpeningFcngetscalled.An
%unrecognizedpropertynameorinvalidvaluemakespropertyapplication
%stop.Allinputsarepassedtountitled_OpeningFcnviavarargin.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelpuntitled
%LastModifiedbyGUIDEv2.518-Jul-201823:
26:
39
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@untitled_OpeningFcn,...
'gui_OutputFcn',@untitled_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&&ischar(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforeuntitledismadevisible.
functionuntitled_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstountitled(seeVARARGIN)
%Choosedefaultcommandlineoutputforuntitled
handles.output=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakesuntitledwaitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=untitled_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
%---Executesonbuttonpressinpushbutton1.
functionpushbutton1_Callback(hObject,eventdata,handles)
globalA;
A=[2113;
2113;
4665;
4775;
7007];
cla
holdon
%在1的位置上写上曹操
[IJ]=find(A==1);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'r')
text(0.5*(x1+x2)-0.5,0.5*(y1+y2),'曹操','fontsize',28)
%在2的位置上写上黄忠
[I,J]=find(A==2);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'黄','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'忠','fontsize',28)
%在3的位置上写上张飞
[I,J]=find(A==3);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'张','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'飞','fontsize',28)
%在4的位置上写上马超
[I,J]=find(A==4);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'马','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'超','fontsize',28)
%在5的位置上写上赵云
[I,J]=find(A==5);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y1),'赵','fontsize',28)
text(0.5*(x1+x2)-0.26,0.5*(0.5*(y1+y2)+y2),'云','fontsize',28)
%在6的位置上写上关羽
[I,J]=find(A==6);
x1=min(J)-1;
x2=max(J);
y1=5-(min(I)-1);
y2=5-max(I);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'y')
text(0.5*(x1+0.5*(x1+x2))-0.26,0.5*(y1+y2),'关','fontsize',28)
text(0.5*(0.5*(x1+x2)+x2)-0.26,0.5*(y1+y2),'羽','fontsize',28)
%在7的位置上写上小卒
[I,J]=find(A==7);
fori=1:
length(I)
x1=J(i)-1;
x2=J(i);
y1=5-(I(i)-1);
y2=5-I(i);
drawrect([x1,y1],[x2,y1],[x2,y2],[x1,y2],'g')
text(0.5*(x1+x2)-0.26,0.5*(y1+y2),'卒','fontsize',28)
end
%画背景
line([04],[00],'color','b','linewidth',4)
line([04],[55],'color','b','linewidth',4)
line([00],[05],'color','b','linewidth',4)
line([44],[05],'color','b','linewidth',4)
fori=1:
4
line([04],[ii],'color','b','linestyle','--')
end
fori=1:
3
line([ii],[05],'color','b','linestyle','--')
end
axisequal
axis([0405])
axisoff
functiondrawrect(x1,x2,x3,x4,color)
x=[x1
(1)x2
(1)x3
(1)x4
(1)];
y=[x1
(2)x2
(2)x3
(2)x4
(2)];
fill(x,y,color)
%hObjecthandletopushbutton1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonmousepressoveraxesbackground.
functionaxes1_ButtonDownFcn(hObject,eventdata,handles)
%hObjecthandletoaxes1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesduringobjectcreation,aftersettingallproperties.
functionaxes1_CreateFcn(hObject,eventdata,handles)
%hObjecthandletoaxes1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
%Hint:
placecodeinOpeningFcntopopulateaxes1
%---Executesonbuttonpressinpushbutton2.
functionpushbutton2_Callback(hObject,eventdata,handles)
step_number=0;
globalA
while1
str=[num2str(step_number),'步'];
set(handles.edit2,'string',str);
ifA(5,2)==1&&A(5,3)==1
ch=menu('曹操成功逃出华容道!
如果要继续玩,按“是”,否则按“否”','是','否');
switchch
case1
untitled
case2
return
end
end
[xpos,ypos]=ginput
(1);
col=ceil(xpos);
row=5-ceil(ypos)+1;
juese=A(row,col);
switchjuese
case1%点击了曹操
[I,J]=find(A==1);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&A([rn,rm],ln-1)==[0;0]
A([rn,rm],ln-1)=[1;1];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[1;1];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向下移
ifrn>1&(A(rn-1,[ln,lm])==[0,0])
A(rn-1,[ln,lm])=[1,1];
A(rn+1,[ln,lm])=[0,0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向上移
ifrm<5&A(rm+1,[ln,lm])==[0,0]
A(rm+1,[ln,lm])=[1,1];
A(rm-1,[ln,lm])=[0,0];
drawmap(A)
step_number=step_number+1;
end
case2%点击了黄忠
[I,J]=find(A==2);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&A([rn,rm],ln-1)==[0;0]
A([rn,rm],ln-1)=[2;2];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[2;2];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
ifrn>1&A(rn-1,ln)==0
ifrm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch=menu('请选择移到的方向:
','上','下')
switchch
case1%上移
A(rn-1,ln)=2;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
case2%下移
A(rm+1,ln)=2;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
else%只能上移
A(rn-1,ln)=2;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
elseifrm<5&A(rm+1,ln)==0
A(rm+1,ln)=2;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
case3%张飞
[I,J]=find(A==3);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&(A([rn,rm],ln-1)==[0;0])
A([rn,rm],ln-1)=[3;3];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[3;3];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
ifrn>1&A(rn-1,ln)==0
ifrm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch=menu('请选择移到的方向:
','上','下')
switchch
case1%上移
A(rn-1,ln)=3;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
case2%下移
A(rm+1,ln)=3;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
else%只能上移
A(rn-1,ln)=3;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
elseifrm<5&A(rm+1,ln)==0
A(rm+1,ln)=3;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
case4%马超
[I,J]=find(A==4);
rm=max(I);
rn=min(I);
lm=max(J);
ln=min(J);
%判断是否能向左移
ifln>1&(A([rn,rm],ln-1)==[0;0])
A([rn,rm],ln-1)=[4;4];
A([rn,rm],lm)=[0;0];
drawmap(A)
step_number=step_number+1;
end
%判断是否能向右移
iflm<4&A([rn,rm],lm+1)==[0;0]
A([rn,rm],lm+1)=[4;4];
A([rn,rm],ln)=[0;0];
drawmap(A)
step_number=step_number+1;
end
ifrn>1&A(rn-1,ln)==0
ifrm<5&A(rm+1,ln)==0%如果又能上移又能下移,则要点击的部位
ch=menu('请选择移到的方向:
','上','下')
switchch
case1%上移
A(rn-1,ln)=4;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
case2%下移
A(rm+1,ln)=4;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
else%只能上移
A(rn-1,ln)=4;
A(rn+1,ln)=0;
drawmap(A)
step_number=step_number+1;
end
elseifrm<5&A(rm+1,ln)==0
A(rm+1,ln)=4;
A(rm-1,ln)=0;
drawmap(A)
step_number=step_number+1