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