How to complete this if-else statement for minesweeper.

41 ビュー (過去 30 日間)
Joshua
Joshua 2024 年 11 月 8 日 22:30
コメント済み: Walter Roberson 2024 年 11 月 9 日 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 件のコメント
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 件のコメント
Joshua
Joshua 2024 年 11 月 9 日 1:49
i need to use the if else statement
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