⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minesweepgame.m

📁 用来进行扫雷的自己设计的小游戏
💻 M
📖 第 1 页 / 共 2 页
字号:
function minesweepgame(varargin)
%MINESWEEPGAME Starts a new mine-sweep game
%
%   minesweepgame(m, n, k);
%       starts a mine-sweep game on a m x n field containing k mines.
%
%   Examples:
%       minesweepgame(20, 20, 50);
%
%   minesweepgame(level);
%       starts a mine-sweep game of specified level.
%       'beginner':        9 x 9 field with 10 mines
%       'intermediate':    16 x 16 field with 40 mines
%       'advanced:         16 x 30 field with 99 mines
%
%   Examples:
%       minesweepgame('beginner');  or minesweepgame beginner;
%       minesweepgame('intermediate'); or minesweepgame intermediate;
%       minesweepgame('advanced'); o minesweepgame advanced;
%
%   minesweepgame;
%       starts the game at beginner level.
%
%   Created by Dahua Lin, on Aug 24 for fun.
%

%% main skeleton

% some global shared settings

gamelevels = struct( ...
    'name', {'beginner', 'intermediate', 'advanced'}, ...
    'size', {[9 9], [16 16], [16 30]}, ...
    'nmines', {10, 40, 99})';

mfield_limits = struct( ...
    'hlim', [9 24],  ... % min and max of allowed number of rows
    'wlim', [9 30],  ... % min and max of allowed number of columns
    'nlim', [10, 668]);  % min and max of allowed number of mines

% cell state constants
s_close  = 0;
s_open   = 1;
s_tagged = 2;


% construct the figure (window)

hfig = figure('Tag', 'MineSweepGame', 'Name', 'Mine Sweep Game', ...
    'Visible', 'off', 'Units', 'pixels', ...
    'MenuBar', 'none', 'ToolBar', 'none', 'Resize', 'off');

set(hfig, ...
    'WindowButtonDownFcn', @on_window_mousedown, ...  
    'DeleteFcn', @on_close);

layout = get_layout_spec();
visspec = get_visual_spec();

% create the mine field

mfield = create_minefield(varargin{:});

% start a new game and enter the main-loop
[game, ui, vhmap, htimer] = start_newgame();
on_status_updated();
on_timer();


%% Field creating function

    function fconf = get_field_config(level)
        % Get field configuration of a specified level
        %
        %   fconf has the following fields
        %       - m:        the number of rows
        %       - n:        the number of columns
        %       - k:        the number of mines
        %       - level:    the standard name of level
        %
        
        level = lower(level);
        [b, i] = ismember(level, {gamelevels.name});
        
        if b
            fconf = struct( ...
                'm', gamelevels(i).size(1), ...
                'n', gamelevels(i).size(2), ...
                'k', gamelevels(i).nmines, ...
                'level', gamelevels(i).name);            
        else
            error('minesweepgame:invalidarg', ...
                'Unknown level name %s', level);
        end        
    end


    function check_field_config(m, n, k)
        % Check the validity of a field configuration
        
        % check whether it is integer scalar
        pint = @(x) isnumeric(x) && isscalar(x) && x > 0 && x == fix(x);
        assert(pint(m), 'minesweepgame:invalidarg', ...
            'the number of rows (m) should be a positive integer.');
        assert(pint(n), 'minesweepgame:invalidarg', ...
            'the number of columns (n) should be a positive integer.');
        assert(pint(k), 'minesweepgame:invalidarg', ...
            'the number of mines (k) should be a positive integer.');        
        
        % check value limit
        l = mfield_limits;
        assert(m >= l.hlim(1) && m <= l.hlim(2), 'minesweepgame:invalidarg', ...
            'the number of rows should be between %d and %d', l.hlim(1), l.hlim(2));
        assert(n >= l.wlim(1) && n <= l.wlim(2), 'minesweepgame:invalidarg', ...
            'the number of columns should be between %d and %d', l.wlim(1), l.wlim(2));
        assert(k >= l.nlim(1) && k <= l.nlim(2), 'minesweepgame:invalidarg', ...
            'the number of mines should be between %d and %d', l.nlim(1), l.nlim(2));

        assert(k < m * n, 'minesweepgame:invalidarg', ...
            'the number of mines should be less than the total number of cells.');
    end

    
    function [M, nnm] = deploy_mines(m, n, k)
        % Deploy mines to a field (determine where to place the mines)
        %
        %   M:  the m x n logical matrix of mine indicators
        %   nnm:    the matrix of numbers of neighboring mines
        %
        %   This function uses the pure random way
        %
        
        M = false(m, n);
        M(randsample(m * n, k)) = 1;
        
        nnm = conv2(double(M), [1 1 1; 1 0 1; 1 1 1], 'same');
    end


    function mf = create_minefield(varargin)
        % Create a mine field 

        % get configuration                
        
        if nargin == 0
            fc = get_field_config('beginner');
            
        elseif nargin == 1
            level = varargin{1};
            assert(ischar(level), 'minesweepgame:invalidarg', ...
                'level should be a string');
            fc = get_field_config(level);                                

        elseif nargin == 3
            m = varargin{1};
            n = varargin{2};
            k = varargin{3};            

            check_field_config(m, n, k);            
            fc = struct('m', m, 'n', n, 'k', k, 'level', '');

        else
            error('minesweepgame:invalidarg', 'Invalid input arguments.');
        end

        % deploy mines

        [M, nnm] = deploy_mines(fc.m, fc.n, fc.k);
        
        % group the information to output

        mf = struct( ...
            'nrows', fc.m, ...
            'ncolumns', fc.n, ...
            'nmines', fc.k, ...
            'level', fc.level, ...
            'is_mine', M, ...
            'nnbmines', nnm);
    end


    function mf = recreate_minefield(mf0)
        % create a new mine field with the same configuration as mf0
        
        if ~isempty(mf0.level)
            mf = create_minefield(mf0.level);
        else
            mf = create_minefield(mf0.nrows, mf0.ncolumns, mf0.nmines);
        end
        
    end


%% Game functions

    function g = init_gamestates()
        
        % initialize the states of a game        
        g.maxopen = mfield.nrows * mfield.ncolumns - mfield.nmines;
        g.nopen = 0;                 % the number of open cells
        g.nremain = mfield.nmines;   % the number of untagged mines
        g.status = 'waitstart';
        
        % the map of cell states
        %   0 - close
        %   1 - open
        %   2 - tagged        
        g.smap = zeros(mfield.nrows, mfield.ncolumns);    
        
        % highlighted cell
        g.hlcell = [];
    end


    function restart_game()          
        mfield = recreate_minefield(mfield);
        game = init_gamestates();
        clear_all_gelems();        
        stop(htimer);      
        
        on_status_updated();
        on_timer();
    end


    function [game, ui, vhmap, htimer] = start_newgame()
                
        % initialize game states                
        game = init_gamestates();        
        
        % create UI components
        ui = create_ui_components(hfig, mfield, layout, visspec, gamelevels);        
        
        % initialize visual elements
        vhmap = cell(mfield.nrows, mfield.ncolumns);
        
        % create timer        
        htimer = create_timer();
        
        % set callback
        set_callbacks(ui, htimer);
        
        % show figure
        if strcmp(get(hfig, 'Visible'), 'off')
            movegui(hfig);
            set(hfig, 'Visible', 'on');
        end    
        
    end


    function close_game()
        clear_all_gelems();
        stop(htimer);
        delete(htimer);
    end


    function switch_to_level(level)
        mfield = create_minefield(level);
        
        figure(hfig);
        clf;
        
        stop(htimer);
        delete(htimer);
        [game, ui, vhmap, htimer] = start_newgame();    
        
        on_status_updated();
        on_timer();
    end
    

    function C = get_propagate_cells(i0, j0)
       % get the cells than can be open by propagating from (i0, j0)
       % not including i0, j0
       
       % construct data structures
       m = mfield.nrows;
       n = mfield.ncolumns;
       
       om = false(m, n);
       q = zeros(m * n, 2);
       
       % push (i0, j0) to the queue
       q(1, :) = [i0, j0];
       qi0 = 1;
       qi1 = 1;    
       om(i0, j0) = 1;
       
       % traverse
       while qi0 <= qi1
           
           % pop the first element
           i = q(qi0, 1);
           j = q(qi0, 2);
           qi0 = qi0 + 1;                      
           
           if mfield.nnbmines(i, j) == 0
               % add neighbors               
               nis = [i-1, i-1, i-1, i, i, i+1, i+1, i+1]';
               njs = [j-1, j, j+1, j-1, j+1, j-1, j, j+1]';
               
               % filter out out-of-bound candidates
               bf = nis >= 1 & nis <= m & njs >= 1 & njs <= n;
               nis = nis(bf);
               njs = njs(bf);
               idx = sub2ind([m, n], nis, njs);
               
               % filter out mines and those that have been added to queue
               bf = ~mfield.is_mine(idx) & ~om(idx);
               
               % add remaining candidates to queue
               if any(bf)
                   nis = nis(bf);
                   njs = njs(bf);
                   idx = idx(bf);
                   nn = numel(nis);
                   
                   q(qi1+1:qi1+nn, 1) = nis;
                   q(qi1+1:qi1+nn, 2) = njs;
                   qi1 = qi1 + nn;
                   
                   om(idx) = 1;
               end
           end                                     
       end % while
       
       C = q(2:qi1, :);
       
    end


    function do_open_cell(i, j, allow_propagate)
        % open a cell
        
        if strcmp(game.status, 'waitstart')
            start_timing();
        end        
        
        if game.smap(i, j) == s_close
           vis_open_cell(i, j);
           game.smap(i, j) = s_open;   
           game.nopen = game.nopen + 1;
           
           if ~mfield.is_mine(i, j)
               if allow_propagate && mfield.nnbmines(i, j) == 0
                   % do propagate open
                   C = get_propagate_cells(i, j);
                   for k = 1 : size(C, 1)
                       do_open_cell(C(k,1), C(k,2), false);
                   end
               end
               
               if game.nopen == game.maxopen
                   game.status = 'done';
                   stop(htimer);
               end
           else
               game.status = 'failed';
               stop(htimer);
               
               % reveal the incorrectly-tagged cells
               [ti, tj] = find(game.smap == s_tagged);
               for k = 1 : length(ti)
                   if ~mfield.is_mine(ti(k), tj(k))
                       add_marker(ti(k), tj(k), visspec.x_marker);
                   end
               end                                  
           end
                                 
           on_status_updated();
        end                        
    end


    function do_toggle_tag(i, j)   
        % toggle the tag of a cell     
        
        if strcmp(game.status, 'waitstart')

⌨️ 快捷键说明

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