2 回答

TA貢獻1909條經驗 獲得超7個贊
我只是最終做了很長的路。
for(int c=0; c<=9; c++)//if number 1
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c-1][r-1]<20) Sprite.mine_2 [c][r] +=1; //top left
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number2
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c-1][r ]<20) Sprite.mine_2 [c][r] +=1; //left
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number3
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c-1][r+1]<20) Sprite.mine_2 [c][r] +=1; //buttom left
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number4
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c ][r+1]<20) Sprite.mine_2 [c][r] +=1; //buttom
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number5
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c+1][r+1]<20) Sprite.mine_2 [c][r] +=1; //buttom right
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number6
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c+1][r ]<20) Sprite.mine_2 [c][r] +=1; // right
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}
for(int c=0; c<=9; c++)// number7
{
for(int r=0; r<=9; r++)
{
try
{
if(Sprite.mine_1[c+1][r-1]<20) Sprite.mine_2 [c][r] +=1; // top right
}
catch(ArrayIndexOutOfBoundsException exception)
{
}
}
}

TA貢獻1840條經驗 獲得超5個贊
由于多級循環,此解決方案將非常耗時,但這仍然可以檢查特定 c 和 r 值的所有情況......
for (int c = 0; c <= 9; c++)
{
for (int r = 0; c <= 9; r++)
{
for (int i = -1; c <= 1; i++)
{
for (int j = -1; c <= 1; j++)
{
try
{
if(Sprite.mine_1[c+i][r+j] < 20 && ( i != 0 || j != 0 ) )
{
Sprite.count++;
}
catch (Exception e)
{
// print what you want
}
}
}
}
}
對于更好的情況,請嘗試這樣的操作以避免 IndexOutOfBoundException 以 1 到 10 但實際數組為 0 - 11 開始 c 和 r
for (int c = 1; c <= 10; c++)
{
for (int r = 1; c <= 10; r++)
{
for (int i = -1; c <= 1; i++)
{
for (int j = -1; c <= 1; j++)
{
try
{
if(Sprite.mine_1[c+i][r+j] < 20 && ( i != 0 || j != 0 ) )
{
Sprite.count++;
}
catch (Exception e)
{
// print what you want
}
}
}
}
}
添加回答
舉報