欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Matlab復(fù)刻掃雷小游戲

 更新時(shí)間:2022年03月02日 15:38:05   作者:slandarer  
windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本程序參考《掃雷》的規(guī)則進(jìn)行了簡化,用Matlab實(shí)現(xiàn),感興趣的小伙伴可以學(xué)習(xí)一下

效果圖

點(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排序的方法

    C++ 關(guān)于STL中sort()對(duì)struct排序的方法

    本篇文章介紹了,關(guān)于STL中sort()對(duì)struct排序的方法。需要的朋友參考下
    2013-04-04
  • C++構(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)用

    這篇文章主要介紹了C++構(gòu)造函數(shù)+復(fù)制構(gòu)造函數(shù)+重載等號(hào)運(yùn)算符調(diào)用,文章敘述詳細(xì),具有一定的的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • windows下如何安裝OpenCL

    windows下如何安裝OpenCL

    這篇文章主要介紹了windows下如何安裝OpenCL,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • C++學(xué)習(xí)小結(jié)之語句

    C++學(xué)習(xí)小結(jié)之語句

    本文給大家匯總介紹了下C++中比較基礎(chǔ)的知識(shí)--語句,常用的語句都有詳細(xì)介紹和附上了相關(guān)示例,十分實(shí)用,有需要的小伙伴可以參考下
    2015-07-07
  • C++和C的混合編譯的項(xiàng)目實(shí)踐

    C++和C的混合編譯的項(xiàng)目實(shí)踐

    本文主要介紹了C++和C的混合編譯的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C語言超詳細(xì)解析函數(shù)棧幀

    C語言超詳細(xì)解析函數(shù)棧幀

    在C語言中,每一個(gè)正在運(yùn)行的函數(shù)都有一個(gè)棧幀與其對(duì)應(yīng),棧幀中存儲(chǔ)的是該函數(shù)的返回地址和局部變量。從邏輯上講,棧幀就是一個(gè)函數(shù)執(zhí)行的環(huán)境:函數(shù)參數(shù)、函數(shù)的局部變量、函數(shù)執(zhí)行完后返回到哪里等等
    2022-03-03
  • 詳解C++如何高效利用CPU緩存

    詳解C++如何高效利用CPU緩存

    高效利用CPU緩存是編寫高性能C++代碼的關(guān)鍵之一,所以這篇文章小編主要來和大家介紹一下C++如何實(shí)現(xiàn)高效利用CPU緩存,需要的可以參考一下
    2024-02-02
  • C++中智能指針unique_ptr的實(shí)現(xiàn)詳解

    C++中智能指針unique_ptr的實(shí)現(xiàn)詳解

    智能指針本質(zhì)上并不神秘,其實(shí)就是?RAII?資源管理功能的自然展現(xiàn)而已,這篇文章主要為大家詳細(xì)介紹了如何實(shí)現(xiàn)?C++中智能指針的?unique_ptr,需要的可以了解下
    2024-01-01
  • c語言程序設(shè)計(jì)文件操作方法示例(CreateFile和fopen)

    c語言程序設(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)

    利用C++實(shí)現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點(diǎn)。需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2013-10-10

最新評(píng)論