check array for zeros using for increment

1 回表示 (過去 30 日間)
Louis Grainger
Louis Grainger 2021 年 4 月 27 日
回答済み: VBBV 2021 年 4 月 27 日
I am trying to create a game of battleship, given an array populated by zeros, when a value is changed (places a ship), I want to check if a ship is already there array(row, col) ~= 0. but this only works when the origin is populated. I can't seem to change the code in a regard where it checks each element startRow to endRow. is there a better way to implement this check?
%input is direction, row, col
check = true
switch direction
case 0 %n
startRow = row;
endRow = startRow - (shipLength - 1);
for i = endRow:startRow
if array(row,col) ~= 0
check = false;
end
end

採用された回答

VBBV
VBBV 2021 年 4 月 27 日
%if true
if array(row,col) ~= 0
check(i) = false;
end
Use loop index

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by