# How to replace cell and neighbors that are zero in a matrix with nan

4 views (last 30 days)
Alex Jackson on 9 Oct 2021
Edited: DGM on 10 Oct 2021
Hello,
I am attempting to code a basic version of minesweeper on MATLAB and I've got it working good so far. The next step for me is for when the player selects a point on the game board that has no bomb and no bombs around it, it should display all of those empty squares around it. My thoughts are, to make sense to the player, it should replace the square you selected and the neighboring sqaures (including those that dont directly touch it - up until it does neighbor a bomb) into NaN. For example:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 % empty gameboard is shown to player
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
% player selects row 2 column 2 and there are no bombs around it
NaN Nan Nan 1 0 0
NaN Nan Nan 1 0 0
NaN Nan Nan Nan 1 0 % empty spaces up until the boxes that neighor bombs are displayed
Nan NaN Nan 2 0 0
1 Nan 1 3 0 0
0 4 0 0 0 0
0 0 0 0 0 0
I've got the code working to display the boxes with neighboring bombs (i.e. the boxes with numbers in them) but I cant figure out how to replace the empty squares until they reach the boxes neighboring bombs into NaNs.
In case it helps, here's the code that figures out the moves, how you win, and how you lose. The mines are randomly placed.
while X==0% %while the player hasnt found a mine
coords = input('type the coordinates in brackets: ');
rows = coords(1);
columns = coords(2);
if answer_matrix(rows,columns)==0 % answer matrix is matrix that contains bombs (0 means no bomb 1 means bomb)
player_matrix(rows,columns)=neighbours(rows,columns); %player matrix is matrix show to player
disp(player_matrix) %neighbors is matrix that contains every square with a number (neighboring bombs)
win_matrix(rows,columns)=0;
moves = moves + 1
X=1;
disp('you win!!')
end
disp('oops that is a mine')
break
end
end

Matt J on 9 Oct 2021
Edited: Matt J on 9 Oct 2021
Perhaps as follows:
A=[ 0 0 0 1 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 2 0 0
1 0 1 3 0 0
0 4 0 0 0 0
0 0 0 0 0 0 ];
loc=[2,2];
fillregion=imfill( A>0 ,loc,4) & A==0;
A(fillregion)=nan
A = 7×6
NaN NaN NaN 1 0 0 NaN NaN NaN 1 0 0 NaN NaN NaN NaN 1 0 NaN NaN NaN 2 0 0 1 NaN 1 3 0 0 0 4 0 0 0 0 0 0 0 0 0 0

DGM on 9 Oct 2021
Edited: DGM on 9 Oct 2021
If you have IPT, this becomes a simple segmentation problem:
A = [0 0 0 1 0 0;
0 0 0 0 1 0;
0 0 0 2 0 0;
1 0 1 3 0 0;
0 4 0 0 0 0;
0 0 0 0 0 0];
% get 4-connected label array
% this lists all contiguous blobs of true values in the input
L = bwlabel(A==0,4)
L = 6×6
1 1 1 0 2 2 1 1 1 1 0 2 1 1 1 0 2 2 0 1 0 0 2 2 2 0 2 2 2 2 2 2 2 2 2 2
% for a sample point, fill the given blob
pickpt = [2 2]; % row, col
A(L == L(pickpt(1),pickpt(2))) = NaN
A = 6×6
NaN NaN NaN 1 0 0 NaN NaN NaN NaN 1 0 NaN NaN NaN 2 0 0 1 NaN 1 3 0 0 0 4 0 0 0 0 0 0 0 0 0 0
DGM on 10 Oct 2021
Oof. I mean... anything's possible without IPT, but you'd have to implement similar functionality in your own code at the cost of increasing the complexity of your programming task.
That said, both of our examples use tools from IPT (imfill, bwlabel). A lot of simplified workarounds to avoid those two functions might use morphological tools which are also part of IPT.
If it's acceptable to require a different dependency instead of IPT, then MIMT has bwlabelFB(), which should serve as a direct replacement for bwlabel(). This is a wrapper for gp_bwlabel() with a minor tweak to match IPT bwlabel() behavior.
The underlying function gp_bwlabel() is from Alec Jacobson's geometry processing toolbox; both MIMT and GPT are available freely on the FEX:
I've attached the three files necessary from both MIMT and GPT. With this, the same example can be done without IPT.
A = [0 0 0 1 0 0;
0 0 0 0 1 0;
0 0 0 2 0 0;
1 0 1 3 0 0;
0 4 0 0 0 0;
0 0 0 0 0 0];
% get 4-connected label array
% this lists all contiguous blobs of true values in the input
L = bwlabelFB(A==0,4);
% for a sample point, fill the given blob
pickpt = [2 2]; % row, col
A(L == L(pickpt(1),pickpt(2))) = NaN
A = 6×6
NaN NaN NaN 1 0 0 NaN NaN NaN NaN 1 0 NaN NaN NaN 2 0 0 1 NaN 1 3 0 0 0 4 0 0 0 0 0 0 0 0 0 0
So you don't need to include the whole of MIMT or GPT, just those three files.
If you decide that you don't want any foreign support files, feel free to consider these three files as a reference implementation.

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!