Nick Marinelli
Nick Marinelli

Reputation: 43

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

Upvotes: 4

Views: 5018

Answers (2)

JohnnyO
JohnnyO

Reputation: 3068

This is actually a really complicated question you are asking. In Computer Science, it is known as a Packing Problem, and there are lots of possible algorithms and possible approaches, depending on the exact nature of what it is you want to accomplish.

In the general case, this problem is hard, really hard, in fact, it is NP-hard to find an optimal general solution. For more information, check out the research paper by Demaine et al from MIT.

Upvotes: 3

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19847

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.

Upvotes: 1

Related Questions