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
You can always write your own method to do this. Here is the jshell output for the thing you are trying to achieve: