4

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

  • 1
    Define "best". Using the least amount of blocks? That would probably consist of using mainly / exclusively the shapes with the most blocks. There are many, many options, just try to fill 2-3 rows at a time, it should be easy enough. – Bernhard Barker Mar 18 '13 at 20:32
  • Best as in easiest / most efficient. No block amount requirement. I'd rather it use an equal amount (as much as possible) of each block every time. – Nick Marinelli Mar 18 '13 at 20:49
  • The simplest is just to fill it with box shapes or line shapes isn't it? – Sam Holder May 31 '14 at 11:55

2 Answers2

3

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.

JohnnyO
  • 3,018
  • 18
  • 30
  • Yeah I've been searching for a few hours and running my own tests with little results. I might just make a few predefined arrays to work with. – Nick Marinelli Mar 18 '13 at 20:37
  • 1
    In order to understand if what you want to do is even possible, you need to be a little more specific about *exactly* what you are trying to accomplish. – JohnnyO Mar 18 '13 at 20:39
1

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.

Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • Nothing is ever as easy as it seems when it comes to Computer Science haha. Yeah I'll probably just make a bunch of predefined arrays instead. – Nick Marinelli Mar 18 '13 at 20:38
  • Yeah, you can try "engineer's method" ;) Predefined or just random throwing the blocks... Or both - make small rectangles from couple of pieces and then randomly place them in each cell of grid made from rectangles. – Adam Stelmaszczyk Mar 18 '13 at 20:49
  • I... did not think of that. That's a great idea. – Nick Marinelli Mar 18 '13 at 20:52