如何在 Java 2D 数组中推进行优先并环绕行?

问题描述 投票:0回答:1

对于一个小型 Java 应用程序,我有一个名为 FIELD 的常量

SIZE
x
SIZE
2D 字符数组,我试图通过它移动两个对象(由字符符号表示)。我的 Field 类中有两种方法,但其中一种无法正常工作。

我希望

moveObject
方法将给定对象移动给定的空间量,并在必要时环绕行。意思是,如果对象连续达到 SIZE - 1 索引,它应该向下移动并在下一行中从 0 继续。它还应该停在 SIZE-1、SIZE-1 索引处并停止移动。

我的方法不能正常工作,你能修复它吗?

public void moveObject(Obj obj, int spaces) {
        // Get the current position of the object
        int oldRow = obj.getRow();
        int oldCol = obj.getCol();

        // Clear the current position of the player on the board
        field[oldRow][oldCol] = ' ';

        // Calculate the total number of spaces moved
        int totalSpaces = oldRow * SIZE + oldCol + spaces;

        // Calculate the new position of the object
        int newRow = (totalSpaces / SIZE) % SIZE;
        int newCol = totalSpaces % SIZE;

        // Update the object's position
        player.row = newRow;
        player.col = newCol;

        // Get the piece of the object
        char symbol = object.getSymbol();

        // Set the new position of the object on the field
        field[newRow][newCol] = symbol;
    }

我还可以提供该字段的其他(显示)方法或我正在运行的测试的输出,这些方法也因此失败。

每次移动对象后,当我检查它们的位置(行和列)时,我的对象会跳到看似随机的位置。正如我所说,它应该保持一致并环绕行。

java arrays multidimensional-array char
1个回答
0
投票

“...我有一个常量

SIZE
x
SIZE
2D 字符数组...

...我希望
moveObject
方法将给定对象移动给定的空间量,并在必要时环绕行。意思是,如果对象连续达到 SIZE - 1 索引,它应该向下移动并在下一行中从 0 继续。它还应该停在 SIZE-1、SIZE-1 索引处并停止移动。 ...”

尝试仅使用常规的一维数组

这是一个例子。
这并不准确,只是一个演示。

mn 分别是 rowscolumns
而且,我将使用 0 来表示空白区域。

move参数mn将是您要移动的部分的坐标,z将是要移动的位置数量。

首先将 mn 转换为 d 的索引 i
i + 1 开始遍历 d,对于每个空白空间,增加 k 直到 k = z
遍历完后,如果 j = d.length,则从 j 中减去 1
继续另一次遍历,将所有值从 j 开始向右移动 k

class Grid {
    int m, n, d[];

    Grid(int m, int n) {
        d = new int[(this.m = m) * (this.n = n)];
    }

    void move(int m, int n, int z) {
        int i = (m * this.n) + n, j, k, l;
        for (j = i + 1, k = 0; j < d.length; j++)
            if (d[j] == 0 && ++k == z) break;
        if (j == d.length) j--;
        for (l = j; l > i; l--) d[l] = d[l - k];
        d[l] = 0;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        Formatter f = new Formatter(s);
        for (int i = 0, t, n = d.length; i < n; i++) {
            if (i != 0 && i % this.n == 0) f.format("%n");
            if ((t = d[i]) == 0) f.format(" . ");
            else f.format("% d ", t);
        }
        f.flush();
        return s.toString();
    }
}

这是一个示例,我将放置 6 值,从 5 和 2 开始,并将它们移动 4 位置。

Grid g = new Grid(10, 10);
Arrays.fill(g.d, 52, 58, 1);
System.out.printf("before%n%s%n%n", g);
g.move(5, 2, 4);
System.out.printf("after%n%s", g);
before
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  1  1  1  1  1  1  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 

after
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  1  1  1  1 
 1  1  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 

而且,这是另一个示例,我将在第 9 行放置另外 6 块,并尝试将其中一半移动到 10 位置。

Grid g = new Grid(10, 10);
Arrays.fill(g.d, 92, 98, 1);
System.out.printf("before%n%s%n%n", g);
g.move(9, 5, 10);
System.out.printf("after%n%s", g);
before
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  1  1  1  1  1  1  .  . 

after
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  .  .  .  .  .  .  .  . 
 .  .  1  1  1  .  .  1  1  1 
© www.soinside.com 2019 - 2024. All rights reserved.