clearmine.m

来自「使用matlab写的类似windows的扫雷游戏」· M 代码 · 共 1,109 行 · 第 1/4 页

M
1,109
字号
                    end
                    break;
                end
            end
        end
    else 
        for num = 1: length( SafeIndex )
            TempIndex = SafeIndex(num) ;
            if UnionIndex(TempIndex) == 0
                if mod( TempIndex, RowNumber) == 1
                    AroundIndex = TempIndex + [1 -RowNumber -RowNumber+1 RowNumber RowNumber+1] ;
                elseif mod( TempIndex, RowNumber) == 0
                    AroundIndex = TempIndex + [-1 -RowNumber -RowNumber-1 RowNumber RowNumber-1] ;
                else
                    AroundIndex = TempIndex + [-1 1 -RowNumber -RowNumber-1 -RowNumber+1 RowNumber-1 RowNumber RowNumber+1] ;
                end
                AroundIndex(AroundIndex<=0 | AroundIndex>RowNumber*ColNumber) = [] ;
                if ~isempty(AroundIndex)
                    RealNotSureIndex = find(UnionIndex(AroundIndex) <= 0) ;
                    AroundIndex = AroundIndex(RealNotSureIndex) ;
                end
                TempMaxUnion = TempMaxUnion + 1 ;
                UnionIndex(TempIndex) = TempMaxUnion ;
                NextNotSureIndex = 0 ;
                if  ~isempty(AroundIndex)   
                    UnionIndex(AroundIndex) = -1 ;
                    NextNotSureIndex = AroundIndex(1) ;
                    NotSureNumber = NotSureNumber + length(AroundIndex) ;
                end
                break;
            end
        end
    end
end
UnionIndex = UnionIndex( ~isinf(UnionIndex) ) ;
setappdata( handles.NewClearMine, 'SafeIndex', SafeIndex ) ;
setappdata( handles.NewClearMine, 'UnionIndex', UnionIndex ) ;

% 保存剩余非雷格子序号
SolveArray = 1: numel(MineNumberArray) ;
SolveArray = setdiff(SolveArray, UsefulIndex) ;
setappdata( handles.NewClearMine, 'SolveArray', SolveArray ) ;



% -----------------------------------------------------------------------
function BeginCalculate_Callback( h ) 

handles = guidata( h ) ;

CountSecond = getappdata( handles.NewClearMine, 'CountSecond' ) ;
if isempty(CountSecond)
    CountSecond = 0 ;
end
while(1)    
    PassTime = sprintf('%d',ceil(CountSecond)) ;
    pause(1) ;
    if ishandle( handles.Time )
        GameOver = getappdata( handles.NewClearMine, 'GameOver' ) ;
        if isempty(GameOver)
            setappdata( handles.NewClearMine, 'GameOver', 0 ) ;
        end
        if GameOver == 1
            break ;
        end
        CountSecond = CountSecond + 1 ;
        set( handles.Time, 'String', PassTime ) ;        
        setappdata( handles.NewClearMine, 'CountSecond', CountSecond ) ;
    else
        break
    end    
end



% -------------------------------------------------------------------------
function CustomMenu_Callback( h )

handles = guidata( h ) ;

GameInfo = getappdata( handles.NewClearMine, 'GameInfo' ) ;

if isempty(GameInfo)
    GameInfo.RowNumber = 10 ;
    GameInfo.ColNumber = 10 ;
    GameInfo.MineNumber = 10 ;
end

RowNumber = GameInfo.RowNumber ;
ColNumber = GameInfo.ColNumber ;
MineNumber = GameInfo.MineNumber ;

FigureHandle = figure( 'Name', '自定义雷区', 'Menubar', 'none', 'WindowStyle', 'modal',...
    'NumberTitle', 'off', 'Position', [300 300 130 100], 'Resize', 'off', 'Visible', 'off' );

setappdata( FigureHandle, 'PassHandle', handles.NewClearMine ) ;

uicontrol( 'sty', 'text', 'str', '高度', 'pos', [10 70 30 20] ) ;
uicontrol( 'sty', 'text', 'str', '宽度', 'pos', [10 40 30 20] ) ;
uicontrol( 'sty', 'text', 'str', '雷数', 'pos', [10 10 30 20] ) ;
uicontrol( 'sty', 'edit', 'tag', 'height', 'str', num2str(RowNumber), 'pos', [50 70 30 20] ) ;
uicontrol( 'sty', 'edit', 'tag', 'width', 'str', num2str(ColNumber), 'pos', [50 40 30 20] ) ;
uicontrol( 'sty', 'edit', 'tag', 'number', 'str', num2str(MineNumber), 'pos', [50 10 30 20] ) ;
uicontrol( 'str', '确定', 'pos', [90 70 30 20],...
    'cal', ['RowNumber = str2num(get(findobj(''tag'', ''height''), ''str''));',...
        'ColNumber = str2num(get(findobj(''tag'', ''width''), ''str''));',...
        'MineNumber = str2num(get(findobj(''tag'', ''number''), ''str''));',...
        '[Msg, status] = Game_NewClearMine( ''CheckDefineValid'', RowNumber, ColNumber, MineNumber);',...
        'if status==0,warndlg(Msg),return,end;',...
        'PassHandle = getappdata( gcf, ''PassHandle'' );',...
        'GameInfo.RowNumber=RowNumber;GameInfo.ColNumber=ColNumber;GameInfo.MineNumber=MineNumber;',...
        'setappdata( PassHandle, ''GameInfo'', GameInfo);',...   
        'close(gcf);',...
        'Game_NewClearMine( ''init_FigureContent'', PassHandle );'] ) ;
uicontrol( 'str', '取消', 'pos', [90 10 30 20], 'cal', 'close(gcf)' ) ;

movegui( FigureHandle, 'center' ) ;
set( FigureHandle, 'Visible', 'on' ) ;




% -------------------------------------------------------------------------
function  [Msg, status] = CheckDefineValid( RowNumber, ColNumber, MineNumber) 

status = 0 ;
Msg = '' ;
if isempty(RowNumber) | RowNumber <= 0 | length(RowNumber) > 1
    Msg = '无效的高度' ;
    return 
end

if isempty(ColNumber) | ColNumber <= 0 | length(ColNumber) > 1
    Msg = '无效的宽度' ;
    return 
end

if isempty(MineNumber) | MineNumber <= 0 | length(MineNumber) > 1 
    Msg = '无效的雷数' ;
    return 
end

if  RowNumber < 5 |  RowNumber > 20
    Msg = '高度必须在5到20间' ;
    return 
end

if  ColNumber < 5 |  ColNumber > 40 
    Msg = '宽度必须在5到40间' ;
    return 
end

if  MineNumber >= ColNumber*RowNumber
    Msg = '雷数大于等于格子总数' ;
    return 
end

status = 1 ;


% -------------------------------------------------------------------------
function Matrix = com_GetMineImage

Matrix = ones(21,21) * 0.75 ;
Matrix(6:15,6:15) = 0 ;
Matrix([5,16],8:13) = 0 ;
Matrix(8:13,[5,16]) = 0 ;
Matrix([4,17],10:11) = 0 ;
Matrix([10,11],4:17) = 0 ;
Matrix = repmat( Matrix, [1,1,3] ) ;


% -------------------------------------------------------------------------
function Matrix = com_GetFlagImage

Matrix = ones(21,21) * 0.75 ;
Matrix(4:18,9:10) = 1  ;
Matrix([5,9],11) = 1 ;
Matrix([6,8],[11:13]) = 1 ;
Matrix(7,10:15) = 1 ;

Matrix2 = Matrix ;
Matrix2(Matrix2 == 1) = 0 ;

Matrix(:,:,2) = Matrix2 ;
Matrix(:,:,3) = Matrix2 ;


% -------------------------------------------------------------------------
function Matrix = com_GetFaceImage( Type )

if strcmp( Type, 'Smile' )
    Matrix(:,:,1) = [[
            255   255   255   255   255   255   255   255   255   255   255   255   255
            255   255   255   255   255   255   255   255   255   255   255   255   255
            255   255   192   192   192   192   192   192   192   192   192   192   192
            255   255   192   192   192   192   192   192   192   192   192   192   192
            255   255   192   192   192   192   192   192   192   192     0     0     0
            255   255   192   192   192   192   192   192     0     0   255   255   255
            255   255   192   192   192   192   192     0   255   255   255   255   255
            255   255   192   192   192   192     0   255   255   255   255   255   255
            255   255   192   192   192     0   255   255   255   255   255   255   255
            255   255   192   192   192     0   255   255   255     0     0   255   255
            255   255   192   192     0   255   255   255   255     0     0   255   255
            255   255   192   192     0   255   255   255   255   255   255   255   255
            255   255   192   192     0   255   255   255   255   255   255   255   255
            255   255   192   192     0   255   255   255   255   255   255   255   255
            255   255   192   192     0   255   255   255     0   255   255   255   255
            255   255   192   192   192     0   255   255   255     0   255   255   255
            255   255   192   192   192     0   255   255   255   255     0     0     0
            255   255   192   192   192   192     0   255   255   255   255   255   255
            255   255   192   192   192   192   192     0   255   255   255   255   255
            255   255   192   192   192   192   192   192     0     0   255   255   255
            255   255   192   192   192   192   192   192   192   192     0     0     0
            255   255   192   192   192   192   192   192   192   192   192   192   192
            255   192   128   128   128   128   128   128   128   128   128   128   128
        ],  [
            255   255   255   255   255   255   255   255   255   255
            255   255   255   255   255   255   255   255   255   192
            192   192   192   192   192   192   192   192   192   128
            192   192   192   192   192   192   192   192   192   128
            0     0   192   192   192   192   192   192   192   128
            255   255     0     0   192   192   192   192   192   128
            255   255   255   255     0   192   192   192   192   128
            255   255   255   255   255     0   192   192   192   128
            255   255   255   255   255   255     0   192   192   128
            255     0     0   255   255   255     0   192   192   128
            255     0     0   255   255   255   255     0   192   128
            255   255   255   255   255   255   255     0   192   128
            255   255   255   255   255   255   255     0   192   128
            255   255   255   255   255   255   255     0   192   128
            255   255   255     0   255   255   255     0   192   128
            255   255     0   255   255   255     0   192   192   128
            0     0   255   255   255   255     0   192   192   128
            255   255   255   255   255     0   192   192   192   128
            255   255   255   255     0   192   192   192   192   128
            255   255     0     0   192   192   192   192   192   128
            0     0   192   192   192   192   192   192   192   128
            192   192   192   192   192   192   192   192   192   128
            128   128   128   128   128   128   128   128   128   128]];
    
    Matrix(:,:,2) = [[
            255   255   255   255   255   255   255   255   255   255   255   255   255
            255   255   255   255   255   255   255   255   255   255   255   255   255
            255   255   192   192   192   192   192   192   192   192   192   192   192
            255   255   192   192   192   192   192   192   192   192   192   192   192
            255   255   192   192   192   192   192   192   192   192     0     0     0
            255   255   192   192   192   192   192   192     0     0   255   255   255
            255   255   192   192   192   192   192     0   255   255   255   255   255
            255   255   192   192   192   192     0   255   255   255   255   255   255
            255   255   192   192   192     0   255   255   255   255   255   255   255
            255   255   192   192   192     0   255   255   255     0     0   255   255
            255   255   192   192     0   255   255   255   255     0     0   255   255
            255   255   192   192     0   255   255   255   255   255   255   255   255
            255   255   192   192     0   255   255   255   255   255   255   255   255
            255   255   192   192     0   255   255   255   255   255   255   255   255
            255   255   192   192     0   255   255   255     0   255   255   255   255
            255   255   192   192   192     0   255   255   255     0   255   255   255
            255   255   192   192   192     0   255   255   255   255     0     0     0
            255   255   192   192   192   192     0   255   255   255   255   255   255
            255   255   192   192   192   192   192     0   255   255   255   255   255
            255   255   192   192   192   192   192   192     0     0   255   255   255
            255   255   192   192   192   192   192   192   192   192     0     0     0
            255   255   192   192   192   192   192   192   192   192   192   192   192
            255   192   128   128   128   128   128   128   128   128   128   128   128
        ],[
            255   255   255   255   255   255   255   255   255   255
            255   255   255   255   255   255   255   255   255   192
            192   192   192   192   192   192   192   192   192   128
            192   192   192   192   192   192   192   192   192   128
            0     0   192   192   192   192   192   192   192   128
            255   255     0     0   192   192   192   192   192   128
            255   255   255   255     0   192   192   192   192   128
            255   255   255   255   255     0   192   192   192   128
            255   255   255   255   255   255     0   192   192   128
            255     0     0   255   255   255     0   192   192   128
            255     0     0   255   255   255   255     0   192   128
            255   255   255   255   255   255   255     0   192   128

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?