如何将二维 Arraylist 放置到另一个 2d ArrayList(俄罗斯方块)?

How to place a two-dimensional Arraylist to another 2d ArrayList (Tetris)?

再见,

我正在创建一个俄罗斯方块算法,我坚持这个逻辑。

我有一个 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

您始终可以编写自己的方法来执行此操作。这是您要实现的目标的 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>