MATLAB Answers

How to create a minesweeper board using a matrix

8 views (last 30 days)
Aaron Chen
Aaron Chen on 17 May 2021
Commented: Rik on 17 May 2021
Introduction:
I've currently been working on a project based on the game Minesweeper by creating a Minesweeper game board using a 9x9 matrix. The following code I've entered:
clear;
clc;
grid = zeros(9)
for i = 1:5
% Enter mine coordinates
row_coord = input('Enter a row coordinate(1-9): ');
col_coord = input('Enter a column coordinate(1-9): ');
fprintf('\n');
% Designate mine coordinates with number 9
grid(row_coord, col_coord) = 9;
end
[row, col] = size(grid)
for r = 1:row % Process all columns in each and every row
for c = 1:col
if r==1 && c==1 %First corner (top left)
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 2X2 coordinate vicinity
grid(r, c) == length(find(grid(r:r+1, c:c+1), 9))
end
elseif r==1 && c==9 %Second corner (top right)
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 2X2 coordinate vicinity
grid(r, c) = length(find(grid(r:r+1, c-1:c), 9))
end
elseif r==9 && c==1 %Third corner (bottom left)
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 2X2 coordinate vicinity
grid(r, c) = length(find(grid(r-1:r, c:c+1), 9))
end
elseif r==9 && c==9 %Fourth corner (bottom right) r==end
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 2X2 coordinate vicinity
grid(r, c) = length(find(grid(r-1:r, c-1:c), 9))
end
elseif r==1 && c>=2 && c<=8 % First Row, Columns 2-8
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 2x3 coordinate vicinity
grid(r, c) = length(find(grid(r:r+1, c-1:c+1), 9))
end
elseif r==9 && c>=2 && c<=8 % Last (Bottom) Row, Columns 2-8
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 2x3 coordinate vicinity
grid(r, c) = length(find(grid(r-1:r, c-1:c+1), 9))
end
elseif c==1 && r>=2 && r<=8 % Left Column, Rows 2-8
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 3x2 coordinate vicinity
grid(r, c) = length(find(grid(r-1:r+1, c:c+1), 9))
end
elseif c==9 && r>=2 && r<=8 % Last Right Column, Rows 2-8
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 3x2 coordinate vicinity
grid(r, c) = length(find(grid(r-1:r+1, c-1:c), 9))
end
else
if grid(r, c) == 9 % If coordinate is a mine, leave coordinate unmodified
elseif grid(r, c) == 0 % If coordinate is not a mine, redesignate coordinate as the number of mines in it's 3x3 coordinate vicinity
grid(r, c) = length(find(grid(r-1:r+1, c-1:c+1), 9))
%if length(find(grid(r-1:r+1, c-1:c+1), 9)) ~= 0
%grid(r, c) = length(find(grid(r-1:r+1, c-1:c+1), 9))
% elseif length(find(grid(r-1:r+1, c-1:c+1), 9)) == 0
% grid(r, c) = 0
% end
end
end
end
end
Explanation:
The program processes the board all columns for each and every row. The program first collects five mine locations from input, designating the mine coordinates in the matrix as the number 9. The four corners are processed by analyzing the surrounding two rows and two columns for mines if it is not a mine. The top and bottom rows are analyzed in two rows and three columns, and the left and right columns are analyzed in three rows and two columns. The rest of the coordinates are analyzed three rows and three columns. If the coordinate is a mine, then it is left unmodified and the program proceeds to process the nest coordinate. If the coordinate is not a mine, the program analyzes the three rows and three columns surrounding the respective coordinate for mines, involving , redesignating the coordinate with the number of mines surrounding it.
Problem:
When tested, the program does not process every coordinate correctly, and will often print/display inaccurate numbers. For example if a coordinate in a row is surrounded by a single mine, all subsequent coordinates in the same row will be redesignated with the number 1. Other numbers include 5, 4, or 3, even if the mines are widely distributed among the board. An example of this bug is this:
The mine coordinates were (1, 1), (3, 3), (5, 5), (7, 7), and (9, 9). As demonstrated by the above result the coordinates surrounding the mines do not accurately display the number of mines.
If anyone is able to provide assistance debugging my coding, please feel free to.

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 May 2021
Aaron - note the top left corner code
grid(r, c) == length(find(grid(r:r+1, c:c+1), 9))
Here you are comparing with == and should be using the assignment operator as
grid(r, c) = length(find(grid(r:r+1, c:c+1), 9));
(I've added the semi-colon at the end of the line to suppress output.) Now consider how you are using find as
find(grid(r:r+1, c:c+1), 9)
This will (from the documentation) return the first 9 indices corresponding to the nonzero elements in the grid. So this is why you are getting different values for the elements that aren't mines. Instead, you want to compare these values to 9
find(grid(r:r+1, c:c+1) == 9)
Try the above and see what happens!
  1 Comment
Aaron Chen
Aaron Chen on 17 May 2021
Just altered and tested the code and it works, thank you very much!

Sign in to comment.

More Answers (1)

Rik
Rik on 17 May 2021
The name Matlab stands for 'matrix laboratory'. It would be a shame not to use the array processing powers.
If you set the locations of the mines with 1 (e.g. as a logical array), you can use a convolution to create your grid variable:
mines=false(9,9);
mines(1,1)=true;mines(3,3)=true;mines(5,5)=true;mines(7,7)=true;mines(9, 9)=true;
grid=convn(mines,ones(3,3),'same');
grid(mines)=9;
disp(grid)
9 1 0 0 0 0 0 0 0 1 2 1 1 0 0 0 0 0 0 1 9 1 0 0 0 0 0 0 1 1 2 1 1 0 0 0 0 0 0 1 9 1 0 0 0 0 0 0 1 1 2 1 1 0 0 0 0 0 0 1 9 1 0 0 0 0 0 0 1 1 2 1 0 0 0 0 0 0 0 1 9
These two lines of code should replace your entire code. A change of strategy ofter provides larger gains than optimizing a single chosen strategy.

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by