Fill the entire 2D array (Tetris board) with Tetris chunks, no spaces (Java)

Say I have a 2D int array.

int[][] board = new int[10][20]; public void initBoard() { for(int r = 0; r < 10; r++) for(int c = 0; c < 20; c++) board[r][c] = 0; } 

0 means no. Pieces are represented by 1-7;

1 - Z Shape

2 - S Form

3 - line shape

4 - T Shape

5 - Box shape

6 - L Shape

7 - Back L Shape

What is the best way to fill the entire array with random forms and not leave spaces.

Note. I have a game, I'm just trying to adapt it to something else, still using the Tetris gameplay.

+4
source share
2 answers

It is not as simple as it seems. This is an NP-hard problem . Similar pixel rectangles , you can start with a slightly simpler problem.

0
source

This is a really difficult question that you are asking. In “Computer Science”, it is known as the “Packaging Problem,” and there are many possible algorithms and possible approaches, depending on the exact nature of what you want to accomplish.

In the general case, this problem is complex, very difficult, in fact it is NP-difficult to find the optimal general solution. For more information, check out the research article by Demaine et al from MIT .

0
source

All Articles