0

我正在尝试为流行的独立游戏 Minecraft 创建一个服务器插件。到目前为止,我所拥有的是一种非递归方法,可以找到距离玩家位置 2 个空间的所有块。我正在寻找一种方法来有效地做到这一点。我希望能够指定距离。这是我目前拥有的:

Block b = player.getLocation().getBlock();
b.getRelative(BlockFace.NORTH).getRelative(BlockFace.NORTH).setType(Material.FIRE);
b.getRelative(BlockFace.NORTH).getRelative(BlockFace.EAST).setType(Material.FIRE);
b.getRelative(BlockFace.EAST).getRelative(BlockFace.EAST).setType(Material.FIRE);
b.getRelative(BlockFace.EAST).getRelative(BlockFace.SOUTH).setType(Material.FIRE);
b.getRelative(BlockFace.WEST).getRelative(BlockFace.WEST).setType(Material.FIRE);
b.getRelative(BlockFace.SOUTH).getRelative(BlockFace.SOUTH).setType(Material.FIRE);
b.getRelative(BlockFace.SOUTH).getRelative(BlockFace.WEST).setType(Material.FIRE);
b.getRelative(BlockFace.NORTH).getRelative(BlockFace.WEST).setType(Material.FIRE);

你能帮帮我吗?谢谢你。

4

1 回答 1

1

如果我对问题的理解正确,您需要一种非递归算法来在距d原点的给定曼哈顿距离处查找 2D 方形网格上的所有点。这些点位于一个平铺的正方形上,它们可以很容易地按顺序生成。例如:

public class Points {

    public static void points(int d) {
        int px = d;
        int py = 0;
        int dx = -1, dy = 1;
        int n = d * 4;
        for( int i = 0; i < n; i++ ) {
            if( px == d && dx > 0 ) dx = -1;
            else if( px == -d && dx < 0 ) dx = 1;
            if( py == d && dy > 0 ) dy = -1;
            else if( py == -d && dy < 0 ) dy = 1;
            px += dx;
            py += dy;
            doSomething(px, py);
        }
    }

    private static void doSomething(int px, int py) {
        System.out.printf("(%2d,%2d)\n", px, py);
        // do whatever you need 
    }

    public static void main(String[] args) {
        points(2);
    }
}

这打印

( 1, 1)
( 0, 2)
(-1, 1)
(-2, 0)
(-1,-1)
( 0,-2)
( 1,-1)
( 2, 0)

您只需要doSomething()根据传递的坐标在您的操作中进行编码。

于 2011-06-29T19:19:19.410 回答