How to complete this if-else statement for minesweeper.

41 次查看(过去 30 天)
% 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 个评论
Walter Roberson
Walter Roberson 2024-11-8,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))

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2024-11-8,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 个评论
Walter Roberson
Walter Roberson 2024-11-9,1:55
elseif r == 1 && c == col
AROUND = [Board(r,c-1),Board(r+1,c),Board(r+1,c-1)];
and so on.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by