How to complete this if-else statement for minesweeper.

41 visualizaciones (últimos 30 días)
Joshua
Joshua el 8 de Nov. de 2024 a las 22:30
Comentada: Walter Roberson el 9 de Nov. de 2024 a las 1:55
% Step2: add the numbers to each bomb location based on number of bombs its
% touching
BoardFINAL = board;
for r = 1:1:row
for c = 1:1:col
if Board(r,c) ~= -1
if r == 1 && c == 1
AROUND = [Board(r,c+1),Board(r+1,c),Board(r+1,c+1)];
BOMBcount = abs(sum(AROUND));
BoardFINAL(r,c) = BOMBcount;
elseif r == 1 && c == col
elseif
elseif
elseif r == 1
elseif
elseif
elseif
else
AROUND = [Board(r-1,c-1),Board(r-1,c),Board(r-1,c+1),Board(r,c-1),Board(r,c+1),Board(r+1,c-1),Board(r+1,c),Board(r+1,c+1)]
BOMBcount = abs(sum(AROUND));
BoardFINAL = (r,c) = BOMBcount;
end
end
end
end
I was able to figure out the first part but stuck on the rest.
  1 comentario
Walter Roberson
Walter Roberson el 8 de Nov. de 2024 a las 22:58
Suppose Board(1,2) = 1 and Board(2,1) = -1 and Board(2,2) = 0, and suppose Board(1,1) is not -1. Then sum([1,-1,0]) would be 0. The bomb count for Board(1,1) would be determined to be 0, even though there are bombs adjacent to it.
It seems clear that you should not be taking abs(sum(AROUND))

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Nov. de 2024 a las 23:02
You can do the calculations without if/elseif .
Use max(1,r-1:r+1) to clip against the top margin. Use min(row, r-1:r+1) to clip against the bottom margin.
Do likewise for columns.
The resulting range of subscripts will be suitable for indexing just the appropriate box at each point.
  3 comentarios
Joshua
Joshua el 9 de Nov. de 2024 a las 1:49
i need to use the if else statement
Walter Roberson
Walter Roberson el 9 de Nov. de 2024 a las 1:55
elseif r == 1 && c == col
AROUND = [Board(r,c-1),Board(r+1,c),Board(r+1,c-1)];
and so on.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by