【问题标题】:How to place a two-dimensional Arraylist to another 2d ArrayList (Tetris)?如何将二维 Arraylist 放置到另一个 2d ArrayList(俄罗斯方块)?
【发布时间】:2021-07-28 09:16:32
【问题描述】:

喏,

我正在创建一个俄罗斯方块算法,我被这个逻辑卡住了。

我有一个看起来像这样的板的 2d ArrayList,

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 0
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 1
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 2
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 3
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 5
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 6
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 7
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 9
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 11
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 12
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 13
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 14
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0] = 15
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0] = 16
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0] = 17
[1, 1, 0, 1, 1, 1, 1, 1, 1, 0] = 18
[1, 1, 0, 1, 1, 1, 1, 1, 1, 0] = 19
[1, 1, 0, 1, 1, 1, 1, 1, 1, 1] = 20

我想像这样将 tetromino 的 2d arrayList 放在上述数组的第 [3] 列中。

[1, 1, 0]
[0, 1, 1]

or
[1, 0, 0]
[1, 1, 1]... 

我怎样才能有效地将它放在板上? 因此,如果我们将样品 1 放入其中,棋盘矩阵将如下所示。

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 0
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 1
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 2
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 3
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 5
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 6
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 7
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 9
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 11
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = 12
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0] = 13
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0] = 14
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0] = 15
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0] = 16
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0] = 17
[1, 1, 0, 1, 1, 1, 1, 1, 1, 0] = 18
[1, 1, 0, 1, 1, 1, 1, 1, 1, 0] = 19
[1, 1, 0, 1, 1, 1, 1, 1, 1, 1] = 20

【问题讨论】:

  • 你尝试使用索引了吗

标签: java arrays arraylist multidimensional-array tetris


【解决方案1】:

您始终可以编写自己的方法来执行此操作。这是您要实现的目标的 jshell 输出:

jshell> void insert(int[][]  board, int[][] tile, int tileStartX, int tileStartY) {
   ...>     if (tile == null || tile.length == 0 || tile[0].length == 0) {
   ...>         return;
   ...>     }
   ...>     if (tileStartX < 0 || tileStartY < 0 || tileStartX + tile.length >= board.length || tileStartY + tile[0].length >= board[0].length) {
   ...>         throw new IllegalArgumentException("Tile out of bounds");
   ...>     }
   ...>     for (int[] row: tile) {
   ...>         int y = tileStartY;
   ...>         for (int elem: row) {
   ...>             board[tileStartX][y++] = elem;
   ...>         }
   ...>         tileStartX++;
   ...>     }
   ...> }
|  modified method insert(int[][],int[][],int,int)

jshell> int[][] a = new int[10][10];
a ==> int[10][] { int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0,  ... 0, 0, 0, 0, 0, 0, 0, 0 } }

jshell> int[][] b = new int[][]{{1, 1, 0}, {0, 1, 1}}
b ==> int[2][] { int[3] { 1, 1, 0 }, int[3] { 0, 1, 1 } }

jshell> insert(a, b, 2, 3)

jshell> Arrays.stream(a).forEach(r -> System.out.println(Arrays.toString(r)))
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

jshell>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 2013-03-07
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多