如何使用对的所有可能组合(+-1,+-2)进行循环 [英] How to do for loop with all possible combinations of pairs (+- 1, +- 2)

查看:72
本文介绍了如何使用对的所有可能组合(+-1,+-2)进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在画一个国际象棋骑士的可能路径,并且一个情况看起来像这样:

I'm drawing possible paths of a knight in chess and one case looks like this:

if (boundsOK(x + 1, y + 2)) {
    temp = boardArray[x + 1][y + 2];

    if (isLegalMove(x, y, x + 1, y + 2) != MoveType.NONE) {
        moves.add(x);
        moves.add(y);
        moves.add(x + 1);
        moves.add(y + 2);

        move(x + 1, y + 2, x, y);
    }
    boardArray[x + 1][y + 2] = temp;
}

现在而不是1和2,我想构造一个可以尝试的循环

Now instead of 1 and 2, I want to construct a loop which would try out combinations:

 1  2

-1  2

 1 -2

-1 -2


 2  1

-2  1

 2 -1

-2 -1

但我不知道该怎么做没有不必要的如果。

But I don't know how to do it without unnecesary if's. Is there at least a smart way to do it?

推荐答案

您可以创建Vector类或类似的类(或使用任何Pair-像类型),用您的值填充列表并对其进行迭代(无需过多考虑性能):

You can create a Vector class or similar (or use any Pair-like type), fill a list with your values and iterate over it (without paying much thought to performance):

var moves = List.of(
        new Move(1,2),
        new Move(-1,2),
        new Move(1,-2),
        new Move(-1,-2),
        new Move(2,1),
        new Move(-2,1),
        new Move(2,-1),
        new Move(-2,-1));

for (var move : moves) {
    var x = move.getX();
    var y = move.getY();
    testMove(x, y) … // or refactor your method to receive a Move instance directly
}






如果您确实要保存一些行(您在打高尔夫球吗?),则可以创建带有循环的实例,但这并不是真的使代码更好(从可读性的角度,从性能的角度,从键入的字符数而言):


If your are really trying to save some lines (are you code golfing?), you could create the instances with a loop, but that doesn't really make the code better (neither from a readability standpoint, nor from performance, nor from the number of characters to type):

var moves = new ArrayList<Move>();
for (int x : List.of(1,-1)) {
    for (int y : List.of(2,-2)) {
        moves.add(new Move(x,y));
    }
}
for (int x : List.of(2,-2)) {
    for (int y : List.of(1,-1)) {
        moves.add(new Move(x,y));
    }
}






再想一想,如果我们注意到移动总是必须包含数字1和2且从不存在(±1,±1)或(±2)的事实,那么它可能可以浓缩为2个循环和1个条件循环。 ,±2):


Thinking a bit more, it can probably be condensed to 2 loops and 1 conditional, if we notice the fact that the moves always have to contain the numbers 1 and 2 and there are never moves (±1,±1) or (±2,±2):

var moves = new ArrayList<Move>(8);
var offsets = List.of(-2,-1,1,2);
for (int x : offsets) {
    for (int y : offsets) {
        if (Math.abs(x) != Math.abs(y)) {
            moves.add(new Move(x,y));
        }
    }
}

但我仍然认为最好走KISS(保持简单,愚蠢)路线,并简单地写下所有可能的动作。意图很明显,行数几乎相同(而且您不必想出巧妙的方法来计算移动)。

But still, I think it is favorable to go the KISS (keep it simple, stupid) route and simply write out all possible moves. The intent is immediately clear and it is about the same number of lines (and you don't have to come up with clever ways to "compute" the moves).

这篇关于如何使用对的所有可能组合(+-1,+-2)进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆