xiaoing.com©

开花即结果 提问即答案

用MATLAB写个扫雷游戏
原始问题:

用MATLAB写个扫雷游戏

clc;clear;close all;

%% 初始化游戏参数
% 设置地雷数量以及地图大小(长宽)
bombNum = 20; % 地雷数量
mapSize = 8; % 地图大小,长宽均为8格
map = zeros(mapSize); % 生成一个全0的地图

%% 随机放置地雷
% 随机生成 bombNum 个在 mapSize 范围内的坐标
randPos = randi([1, mapSize], [bombNum, 2]);

for ii=1:length(randPos)

 map(randPos(ii, 1), randPos(ii, 2)) = -1;    % 根据随机坐标赋值 -1   表示该位置是雷   

end

%% 根据雷的位置,赋予周围八个方向的数字

for ii=2:mapSize-1 % 去掉边界处理

 for jj=2:mapSize-1       

     if map(ii, jj) == -1            % 该位置是雷,则跳过       

         continue;       

     else            % 该位置不是雷          

         aroundBombNum = 0 ;           % 统计该位置周围八个方向上的地雷数量          

         for kk=-1:1                % 遍历该位置周围八个方向               

             for ll=-1:1                    if map(ii+kk, jj+ll) == -1                        aroundBombNum = aroundBombNum + 1 ;                    end                end            end            if aroundBombNum ~= 0               map(ii,jj) = aroundBombNum ;            end        end    end end      %% 显示扫雷界面 figure('Name','扫雷','NumberTitle','off'); imagesc (map); colormap (flipud (gray)); axis equal off ; title('扫 雷') ;
用MATLAB写个扫雷游戏
https://www.xiaoing.com/zhidao/8095.html
AI / 模型Siliconflow
时间 / DATEApril 11, 2023, 7:06 PM
语言zh-CN
IP / 区域广东 韶关