0
    for (int x = 0; x <= battleField.getCols(); x++){
                for (int y = 0; y <= battleField.getRows(); y++){

                    if ((battleField.get(x,y) == team) && ((x + y)
                            != battleField.get(row, col)))
                    {
                                .
                                .
                                .
                    }
               }
   }

BattleField.get(row, col) 将返回我在网格上的位置现在我的问题是我如何跳过我自己检查我的位置在哪里?因为当 for 循环检查 x 和 y 时,它会遍历整个网格。我需要做什么?

4

3 回答 3

1

假设您当前位置的变量名为current_xand current_y,那么您可以在循环中执行类似的操作...

bool isMyPosition = (x == current_x) && (y == current_y);

if ( !isMyPosition 
    && (battleField.get(x,y) == team)
    && ((x + y) != battleField.get(row, col)))
{
    ...
}
于 2013-10-22T21:23:28.257 回答
0

代替

((x + y) != battleField.get(row, col)) 

我想你想要

(x != row && y != col)

如果您确实想要第一部分,则可以将其添加到内部 for 循环的开头:

for (int x = 0; x <= battleField.getCols(); x++){
            for (int y = 0; y <= battleField.getRows(); y++){

                if(x == row && y == col) // This will check if the loop is looking at your position
                    continue; // This will go to the next step in the "y" for loop, essentially skipping the rest of the code in this block

                if ((battleField.get(x,y) == team) && ((x + y)
                        != battleField.get(row, col)))
                {
                            .
                            .
                            .
                }
           }

}

于 2013-10-22T21:27:59.040 回答
0

如果(问题。IsAbout(EscapingOnCheck))

大多数语言都有某种continue可以在满足条件时用来逃避循环的方法。

别的

将当前位置保存在一些变量中,并添加另一个嵌套循环(为了调试)或添加 && 子句(为了减少代码)

于 2013-10-22T21:27:59.783 回答