Question

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

Était-ce utile?

La solution 2

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top