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

79 Views Asked by At

Ciao,

I'm creating a Tetris Algorithm, I'm stuck with this logic.

I have a 2d ArrayList of the board that looks like this,

[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

I want to place the 2d arrayList of tetromino like this in column [3] of the above array.

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

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

How can I efficiently placed it on the board? So, the board Matrix will look like this if we place sample piece 1 in it.

[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
1

There are 1 best solutions below

0
On BEST ANSWER

You can always write your own method to do this. Here is the jshell output for the thing you are trying to achieve:

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>