前言
法术的仙意,无法遮盖你千年的痴迷。——《仙逆》
\;\\\;\\\;
目录
- 前言
- 拆雷
拆雷
function Bomb(n,mm)
clear;
close;
if nargin ~= 2n = [22,18];mm = 65;
end
if mm + 1 > n(1) * n(2)error('Setting error');
endf1 = figure('Name','Bombs','NumberTitle','off','Color',[1,1,1],...'Position',[100,50,900,600],'WindowButtonDownFcn',@BDF,...'WindowButtonMotionFcn',@BMF,'WindowButtonUpFcn',@BUF);
ax = axes(f1,'XTick',0.5:(n(1)+0.5),'YTick',0.5:(n(2)+0.5),'XColor',...[0.5,0.5,0.5],'YColor',[0.5,0.5,0.5],'Color',[1,1,1],'Parent',f1,...'Position',[0,0,13/14,0.98],'XTickLabel',[],'YTickLabel',[],...'XGrid','on','YGrid','on','XLim',[0,n(1)+1],'YLim',[0,n(2)+1],...'DataAspectRatio',[1,1,1]);
T = uicontrol(f1,'Style','text','BackgroundColor',[1,1,1],'String',...num2str(mm),'Units','normalized','Position',[13/14,0.80,1/14,0.1],...'FontSize',18,'FontWeight','bold');
hold onfor i = n(1):-1:1for j = n(2):-1:1M(i,j) = patch(ax,[i,i+1,i+1,i]-0.5,[j,j,j+1,j+1]-0.5,...[0.8,0.8,0.8],'EdgeColor',[0.7,0.7,0.7]);F(i,j) = image(ax,[i-0.4,i+0.4],[j-0.4,j+0.4],[]);end
endxd = 1;
yd = 1;
xm = 1;
ym = 1;
xu = 1;
yu = 1;
PC = [0.8,0.8,0.8]; % last color
CD = uint8([0,0,225;2,129,2;253,7,7;20,20,158;...128,1,1;0,128,128;10,10,10;128,128,128]); % CData of Number
MT = "";
btn_exit = "";FLAG = zeros(9,9,3,'uint8'); % Draw Flag
FLAG(:,:,1) = [204 204 204 204 204 204 204 204 204;204 0 0 0 0 0 0 0 204;204 204 0 0 0 0 0 204 204;204 204 204 204 0 204 204 204 204;204 204 204 204 255 204 204 204 204;204 204 255 255 255 204 204 204 204;204 204 204 255 255 204 204 204 204;204 204 204 204 255 204 204 204 204;204 204 204 204 204 204 204 204 204];
FLAG(:,:,2) = [204 204 204 204 204 204 204 204 204;204 0 0 0 0 0 0 0 204;204 204 0 0 0 0 0 204 204;204 204 204 204 0 204 204 204 204;204 204 204 204 0 204 204 204 204;204 204 0 0 0 204 204 204 204;204 204 204 0 0 204 204 204 204;204 204 204 204 0 204 204 204 204;204 204 204 204 204 204 204 204 204];
FLAG(:,:,3) = [204 204 204 204 204 204 204 204 204;204 0 0 0 0 0 0 0 204;204 204 0 0 0 0 0 204 204;204 204 204 204 0 204 204 204 204;204 204 204 204 0 204 204 204 204;204 204 0 0 0 204 204 204 204;204 204 204 0 0 204 204 204 204;204 204 204 204 0 204 204 204 204;204 204 204 204 204 204 204 204 204];BOMB = zeros(15,15,3,'uint8'); % draw the Bomb
BOMB(:,:,1) = [255 255 255 255 255 255 255 255 255 255 255 255 255 255 255;255 255 255 255 255 255 255 0 255 255 255 255 255 255 255;255 255 255 255 255 255 255 0 255 255 255 255 255 255 255;255 255 255 0 255 0 0 0 0 0 255 0 255 255 255;255 255 255 255 0 0 0 0 0 0 0 255 255 255 255;255 255 255 0 0 0 0 0 0 0 0 0 255 255 255;255 255 255 0 0 0 0 0 0 0 0 0 255 255 255;255 0 0 0 0 0 0 0 0 0 0 0 0 0 255;255 255 255 0 0 255 255 0 0 0 0 0 255 255 255;255 255 255 0 0 255 255 0 0 0 0 0 255 255 255;255 255 255 255 0 0 0 0 0 0 0 255 255 255 255;255 255 255 0 255 0 0 0 0 0 255 0 255 255 255;255 255 255 255 255 255 255 0 255 255 255 255 255 255 255;255 255 255 255 255 255 255 0 255 255 255 255 255 255 255;255 255 255 255 255 255 255 255 255 255 255 255 255 255 255];
BOMB([9,10],[6,7],[2,3]) = 255;MDATA = zeros(n,'logical'); % If this position was open
BF = zeros(n,'logical'); % Bombs found
NClick = true; % First protect
BDATA = zeros(n); % locate of Bomb
Ndata = zeros(n); % Map information
aa = 1;
bb = 1;
cc = false;function BDF(~,~)m = get(ax,'CurrentPoint');xd = round(m(1,1));yd = round(m(1,2));MT = get(f1,'SelectionType');if xd>=1 && xd<=n(1) && yd>=1 && yd<=n(2) && MDATA(xd,yd) &&...Ndata(xd,yd) > 0 && (MT == "normal" || MT == "alt")[a,b] = specialgrid(xd,yd);C = zeros(n,'logical');C(a,b) = true;[aa,bb] = find(C & ~MDATA & ~BF);for ii = [aa,bb].'M(ii(1),ii(2)).FaceColor = [0.6,0.6,0.6];endcc = true;endif NClickBset = randperm(n(1) * n(2) - 1,mm) + 1;Npoint = (yd - 1) * n(1) + xd;if ismember(Npoint,Bset)Bset(Bset <= Npoint) = Bset(Bset <= Npoint) - 1;endBDATA(Bset) = 1;Ndata = conv2(BDATA,[1,1,1;1,0,1;1,1,1],'same');Ndata(BDATA == 1) = -1;NClick = false;endendfunction BMF(~,~)M(xm,ym).FaceColor = PC;M(xm,ym).FaceAlpha = 1;m = get(ax,'CurrentPoint');x = sort([1,round(m(1,1)),n(1)]);y = sort([1,round(m(1,2)),n(2)]);xm = x(2);ym = y(2);PC = M(xm,ym).FaceColor;M(xm,ym).FaceColor = [0.8,0.8,0.8];M(xm,ym).FaceAlpha = 0.5;endfunction BUF(~,~)if ccfor ii = [aa,bb].'M(ii(1),ii(2)).FaceColor = [0.8,0.8,0.8];endcc = false;endm = get(ax,'CurrentPoint');xu = round(m(1,1));yu = round(m(1,2));if xd==xu && yd==yu && xu>=1 && yu>=1 && xu<=n(1) && yu<=n(2)if ~MDATA(xu,yu)if MT == "normal"if ~BF(xu,yu)if Ndata(xu,yu) == -1BF(xu,yu) = true;M(xu,yu).FaceColor = [1,0,0];PC = [1,0,0];F(xu,yu).CData = BOMB;if btn_exit ~= "end"ENDq(false)endelseMDATA(xu,yu) = true;M(xu,yu).FaceColor = [1,1,1];PC = [1,1,1];if Ndata(xu,yu) == 0swap(xu,yu)elseTEXT(xu,yu)endendendelseif MT == "alt"if ~BF(xu,yu)BF(xu,yu) = true;F(xu,yu).CData = FLAG;T.String = num2str(str2double(T.String) - 1);elseif btn_exit ~= "end"BF(xu,yu) = false;F(xu,yu).CData = [];T.String = num2str(str2double(T.String) + 1);endendelseif Ndata(xu,yu) > 0[a,b] = specialgrid(xu,yu);if sum(BF(a,b),'all') == Ndata(xu,yu) &&...sum(BF(a,b),'all') < sum(~MDATA(a,b),'all')C = zeros(n,'logical');C(a,b) = true;[a,b] = find(C & ~MDATA & ~BF);for ii = [a,b].'MDATA(ii(1),ii(2)) = true;if Ndata(ii(1),ii(2)) == -1BF(ii(1),ii(2)) = true;MDATA(ii(1),ii(2)) = false;M(ii(1),ii(2)).FaceColor = [1,0,0];F(ii(1),ii(2)).CData = BOMB;if btn_exit ~= "end"ENDq(false)endelseM(ii(1),ii(2)).FaceColor = [1,1,1];PC = [1,1,1];if Ndata(ii(1),ii(2)) == 0swap(ii(1),ii(2))elseTEXT(ii(1),ii(2))endendendendendendif (all(BDATA == ~MDATA,'all') ||...all(BDATA == BF,'all')) && btn_exit ~= "end"ENDq(true)endendfunction swap(x,y)C = zeros(n(1),n(2),'logical');C_ = C;C(x,y) = true;while ~all(C == C_,'all')C_ = C;[x,y] = find((~Ndata | BF) & C);for ii = [x,y].'if Ndata(ii(1),ii(2)) == 0[a,b] = specialgrid(ii(1),ii(2));C(a,b) = ~BF(a,b);endendend[x,y] = find(C);for ii = [x,y].'MDATA(ii(1),ii(2)) = true;M(ii(1),ii(2)).FaceColor = [1,1,1];if Ndata(ii(1),ii(2)) ~= 0TEXT(ii(1),ii(2))endendendfunction [a,b] = specialgrid(x,y)if x == 1a = [1,2];elseif x == n(1)a = [n(1)-1,n(1)];elsea = x-1:x+1;endif y == 1b = [1,2];elseif y == n(2)b = [n(2)-1,n(2)];elseb = y-1:y+1;endendfunction TEXT(x,y)text(x-0.2,y,num2str(Ndata(x,y)),'FontSize',15,'FontWeight',...'bold','Color',CD(Ndata(x,y),:));endfunction ENDq(a)if abtn_exit = questdlg('You Win !','YouWin',...'restart','close','restart');if isempty(btn_exit)btn_exit = "end";endif strcmp(btn_exit,'restart')Bomb;elseif strcmp(btn_exit,'close')close; clear;endelsebtn_exit = questdlg('You Lose.',...'YouLose','restart','close','restart');if isempty(btn_exit)btn_exit = 'end';endif strcmp(btn_exit,'restart')Bomb;elseif strcmp(btn_exit,'close')close; clear;endendend
end
\;\\\;\\\;