Search code examples
javaarrays2dfilltetris

Fill entire 2D Array (Tetris board) with Tetris pieces, no spaces left over (Java)


Let's 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 piece The pieces are represented by 1-7;

1 - Z Shape

2 - S Shape

3 - Line Shape

4 - T Shape

5 - Box Shape

6 - L Shape

7 - Backwards L Shape

What is the best way to fill the entire array with random shapes and no spaces left over.

Note: I have the game working, I'm just trying to adapt it to something different while still using the Tetris gameplay


Solution

  • It's not so easy as it seems. It's NP-hard problem in fact. Packing rectangles is similar, you can start with that a little bit simpler problem.