利用Matlab復(fù)刻掃雷小游戲
效果圖
點(diǎn)擊幫助切換插旗功能:
游戲失?。?/p>
完整代碼
function SLsXpbombs global Row; Row=10;%雷區(qū)行數(shù) global Col; Col=10;%雷區(qū)列數(shù) global MineMap; MineMap=[]; %地雷圖 global aroundMap;aroundMap=[];%周圍地雷數(shù) global MarkMap; MarkMap=ones([Row,Col]); %標(biāo)記圖 global MineNum; MineNum=8;%地雷總數(shù) global FlagNum; FlagNum=0; %旗子總數(shù) global times; times=1; %挖雷次數(shù) %global Surplus; Surplus=MineNum;%剩余地雷=地雷總數(shù)-旗子總數(shù) global tool;tool='掃雷'; global XPBlabel XPBimage scoreImage; XPBFig=uifigure('units','pixels',... 'position',[500,200,260,310],... 'Numbertitle','off',... 'menubar','none',... 'resize','off',... 'name','掃雷'); XPBfaceIm=uiimage(XPBFig); XPBfaceIm.Position=[130-13,275,26,26]; XPBfaceIm.ImageSource='image\face1.png';%'image\none.png'; XPBfaceIm.UserData=0; set(XPBfaceIm,'ImageClickedFcn',@restart) uilabel(XPBFig,'Text','','BackgroundColor',[0 0 0],'Position',[20-1,270,66,40]); uilabel(XPBFig,'Text','','BackgroundColor',[0 0 0],'Position',[+260-40-44-1,270,66,40]); for i=1:3 scoreImage(1,i)=uiimage(XPBFig,'ImageSource','image\0.png',... 'Position',[20+(i-1)*22,270,20,40]); scoreImage(2,i)=uiimage(XPBFig,'ImageSource','image\0.png',... 'Position',[(i-1)*22+260-40-44,270,20,40]); end;drawScore(MineNum,MarkMap) XPBMenu=uimenu(XPBFig); XPBMenu.Text='幫助'; XPBMenu_1=uimenu(XPBMenu); XPBMenu_1.Text='插旗'; set(XPBMenu_1,'MenuSelectedFcn',@MenuSelected) function MenuSelected(~,~) switch 1 case strcmp(XPBMenu_1.Text,'插旗'),XPBMenu_1.Text='掃雷';tool='插旗'; case strcmp(XPBMenu_1.Text,'掃雷'),XPBMenu_1.Text='插旗';tool='掃雷'; end end for m=1:Row for n=1:Col XPBlabel(m,n)=uilabel(XPBFig,'Text','','HorizontalAlignment','center',... 'FontSize',16,'FontWeight','bold',... 'BackgroundColor',0.85*[1,1,1],'Position',[10+1+(m-1)*24,10+1+(n-1)*26,24-2,26-2]); XPBimage(m,n)=uiimage(XPBFig,'ImageSource','image\button.png',... 'Position',[10+(m-1)*24,10+(n-1)*26,24,26],'UserData',[m,n],'ImageClickedFcn',@clickButton); end end %========================================================================== function clickButton(obj,~) clickPos=obj.UserData; if strcmp(tool,'插旗') switch MarkMap(clickPos(1),clickPos(2)) case 1,set(XPBimage(clickPos(1),clickPos(2)),'visible','on','ImageSource','image\flagbutton.png'); case -1,set(XPBimage(clickPos(1),clickPos(2)),'visible','on','ImageSource','image\button.png'); end MarkMap(clickPos(1),clickPos(2))=-MarkMap(clickPos(1),clickPos(2)); drawScore(MineNum,MarkMap) return; end if times==1,[MineMap,aroundMap]=createMap(clickPos);drawNum(aroundMap);times=inf;end if MarkMap(clickPos(1),clickPos(2))==-1,return;end set(XPBimage(clickPos(1),clickPos(2)),'visible','off'); MarkMap(clickPos(1),clickPos(2))=0; switch MineMap(clickPos(1),clickPos(2)) case 1,gameOver(MineMap,clickPos); case 0,searchZone(aroundMap,clickPos); end drawScore(MineNum,MarkMap) if all(all(abs(MarkMap)==MineMap)) win() end end %========================================================================== function drawScore(MineNum,MarkMap) MarkMap(MarkMap==-1)=0; score1=num2str(MineNum);L1=length(score1); score2=num2str(sum(sum(MarkMap)));L2=length(score2); for ii=1:3 if ii<=3-L1 set(scoreImage(1,ii),'ImageSource','image\0.png') else tempStr=score1(ii-(3-L1)); set(scoreImage(1,ii),'ImageSource',['image\',tempStr,'.png']); end if ii<=3-L2 set(scoreImage(2,ii),'ImageSource','image\0.png') else tempStr=score2(ii-(3-L2)); set(scoreImage(2,ii),'ImageSource',['image\',tempStr,'.png']); end end end function win(~,~) for mm=1:Row for nn=1:Col if MineMap(mm,nn)==1 set(XPBimage(mm,nn),'visible','on','ImageSource','image\flagbutton.png'); end end end XPBfaceIm.ImageSource='image\face3.png'; end function gameOver(MineMap,pos) for mm=1:Row for nn=1:Col set(XPBimage(mm,nn),'visible','off'); if MineMap(mm,nn)==1 set(XPBimage(mm,nn),'visible','on','ImageSource','image\mine.png'); end end end set(XPBlabel(pos(1),pos(2)),'BackgroundColor',[1 0 0]); XPBfaceIm.ImageSource='image\face2.png'; end function searchZone(aroundMap,pos) if aroundMap(pos(1),pos(2))~=0,return;end begins=pos; [nonea,noneb]=find(aroundMap==0); none=[nonea,noneb]; listZone=[begins;begins+[1,0];begins+[-1,0]; begins+[0,1];begins+[0,-1]; begins+[-1,1];begins+[-1,-1]; begins+[1,1];begins+[1,-1]]; while ~isempty(intersect(none,listZone,'rows')) [a,b,~]=intersect(none,listZone,'rows'); begins=[a;begins];none(b,:)=[]; ad=length(sum(begins,2)); listZone=[begins;begins+ones(ad,1)*[1,0];begins+ones(ad,1)*[-1,0]; begins+ones(ad,1)*[0,1];begins+ones(ad,1)*[0,-1]; begins+ones(ad,1)*[-1,1];begins+ones(ad,1)*[-1,-1]; begins+ones(ad,1)*[1,1];begins+ones(ad,1)*[1,-1]]; listZone=unique(listZone,'rows'); end listZone(sum(listZone<1,2)>0,:)=[]; listZone(sum(listZone>10,2)>0,:)=[]; listZone=round(listZone); for ii=1:size(listZone,1) set(XPBimage(listZone(ii,1),listZone(ii,2)),'visible','off') MarkMap(listZone(ii,1),listZone(ii,2))=0; end end function restart(~,~) MineMap=[]; aroundMap=[]; MarkMap=ones([Row,Col]); times=1; tool='掃雷'; for mm=1:Row for nn=1:Col set(XPBlabel(mm,nn),'Text','','BackgroundColor',0.85*[1,1,1]) set(XPBimage(mm,nn),'ImageSource','image\button.png','visible','on'); end end drawScore(MineNum,MarkMap) XPBfaceIm.ImageSource='image\face1.png'; end %========================================================================== function drawNum(aroundMap) for mm=1:Row for nn=1:Col switch aroundMap(mm,nn) case 0 case 1,set(XPBlabel(mm,nn),'Text','1','FontColor',[0 0 1]) case 2,set(XPBlabel(mm,nn),'Text','2','FontColor',[0,0.7,0]) case 3,set(XPBlabel(mm,nn),'Text','3','FontColor',[0.8,0,0]) case 4,set(XPBlabel(mm,nn),'Text','4','FontColor',[0,0,0.6]) case 5,set(XPBlabel(mm,nn),'Text','5','FontColor',[0.5,0,0]) case 6,set(XPBlabel(mm,nn),'Text','6','FontColor',[0,0.6,0]) case 7,set(XPBlabel(mm,nn),'Text','7','FontColor',[0.75,0,0]) case 8,set(XPBlabel(mm,nn),'Text','8','FontColor',[0.4,0,0]) end end end end %第一次挖雷時(shí)生成地雷分布圖(這樣第一次不會(huì)碰到雷) function [randMap,surrMap]=createMap(pos) %生成地雷分布圖 randMap=rand([Row,Col]); randMap(pos(1),pos(2))=inf; [~,St]=sort(randMap(:)); randMap=(randMap<=randMap(St(MineNum))); %生成周圍地雷數(shù)分布圖 frameMap=zeros([Row+2,Col+2]); xSet=2:Row+1;ySet=2:Col+1; frameMap(xSet,ySet)=randMap; surrMap(xSet-1,ySet-1)=frameMap(xSet-1,ySet+1)+frameMap(xSet-1,ySet)+... frameMap(xSet-1,ySet-1)+frameMap(xSet,ySet+1)+... frameMap(xSet,ySet-1)+frameMap(xSet+1,ySet+1)+... frameMap(xSet+1,ySet)+frameMap(xSet+1,ySet-1); surrMap(randMap==1)=0; end %========================================================================== end
以上就是利用Matlab復(fù)刻掃雷小游戲的詳細(xì)內(nèi)容,更多關(guān)于Matlab掃雷游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++ 關(guān)于STL中sort()對(duì)struct排序的方法
本篇文章介紹了,關(guān)于STL中sort()對(duì)struct排序的方法。需要的朋友參考下2013-04-04C++構(gòu)造函數(shù)+復(fù)制構(gòu)造函數(shù)+重載等號(hào)運(yùn)算符調(diào)用
這篇文章主要介紹了C++構(gòu)造函數(shù)+復(fù)制構(gòu)造函數(shù)+重載等號(hào)運(yùn)算符調(diào)用,文章敘述詳細(xì),具有一定的的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03C++中智能指針unique_ptr的實(shí)現(xiàn)詳解
智能指針本質(zhì)上并不神秘,其實(shí)就是?RAII?資源管理功能的自然展現(xiàn)而已,這篇文章主要為大家詳細(xì)介紹了如何實(shí)現(xiàn)?C++中智能指針的?unique_ptr,需要的可以了解下2024-01-01c語言程序設(shè)計(jì)文件操作方法示例(CreateFile和fopen)
c主要的文件操作函數(shù)有:CreateFile,CloseHandle,ReadFile,WriteFile,SetFilePointer,GetFileSize。其中的讀寫操作是以字符為單位,獲得文件大小也是以字符為單位。2013-12-12利用C++實(shí)現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點(diǎn)
利用C++實(shí)現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點(diǎn)。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-10-10