How to complete this if-else statement for minesweeper.

41 Ansichten (letzte 30 Tage)
Joshua
Joshua am 8 Nov. 2024 um 22:30
Kommentiert: Walter Roberson am 9 Nov. 2024 um 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 Kommentar
Walter Roberson
Walter Roberson am 8 Nov. 2024 um 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))

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 8 Nov. 2024 um 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 Kommentare
Joshua
Joshua am 9 Nov. 2024 um 1:49
i need to use the if else statement
Walter Roberson
Walter Roberson am 9 Nov. 2024 um 1:55
elseif r == 1 && c == col
AROUND = [Board(r,c-1),Board(r+1,c),Board(r+1,c-1)];
and so on.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by