function B = minesweeper(A)
%MINESWEEPER Computes the numbered grid for a game of minesweeper
%   B = MINESWEEPER(A) computes the numbered grid for a game
%   of minesweeper. A is grid containing zeros (no mines) and -1
%   (mines). Each element in B contains the number of adjacent
%   grid positions containing a mine in A, or -1 if there is a mine
%   at that position in A.
%
%   For simplicity, the first and last rows and columns of B do
%   not need to be computed (e.g, they can be all zeros).

[nrows, ncols] = size(A);
B = A;
for row = 2:(nrows - 1)
    for col = 2:(ncols - 1)
        B(row, col) = mines(A, row, col);
    end
end


function n = mines(A, row, col)
%MINES Count the number of mines adjacent to A(row, col)

if A(row, col) == -1
    n = -1;
else
    neighborhood = A((row - 1):(row + 1), (col - 1):(col + 1));
    % neighbourhood is a 3x3 matrix containing all zeros and -1s
    % n is just the number of -1s in neighborhood
    n = sum(abs(neighborhood(:)));
end
