6
votes
\$\begingroup\$

I have a representation for each initial shape as an Enum with points and another for each rotated shape in the same Enum.

Here is an example of the latter Enum:

// 
//     * *
//     *
//     * 
J_SHAPE_SOUTH(new Point[] {
        new Point(2, 0),
        new Point(2, 1),
        new Point(2, 2),
        new Point(3, 2)
}),

// 
//   * *  
//     *  
//     *  
L_SHAPE_SOUTH(new Point[] {
        new Point(1, 2),
        new Point(2, 0),
        new Point(2, 1),
        new Point(2, 2)
});

I was then wondering whether to move the rotated shapes into individual Enums, say RotatedJShape, or simply keep all the rotated shapes in the same Enum. My concern is that some shapes like the I-shape only has one rotated condition, so it seems pointless to have an Enum for that shape.

What would be the most suitable approach in my case in terms of scalability and code correctness?

\$\endgroup\$
9
votes
\$\begingroup\$

Ages ago I did a Tetris clone, and my idea for a data structure was this:

A circular linked list of matrices.

How do you rotate? Walk the list...

You can have a single array of these lists (one per each block type), and your game logic's notion of the "current piece" is a reference to something inside that table. That way the game logic can have no knowledge of the different shapes, and you declare a small array that defines what the shapes are.

\$\endgroup\$
5
votes
\$\begingroup\$

How are you rotating them now? Would it make more sense to change from an enum to a class with a Rotate method instead?

\$\endgroup\$
2
  • \$\begingroup\$ Swapping through the list of rotated pieces. I wanted an easy approach, that's why I did it this way. Rotating would require a somewhat complex logic. \$\endgroup\$ – whirlwin May 6 '11 at 6:55
  • 3
    \$\begingroup\$ @Whirlwin: I believe 'complex' is somewhat exaggerating. :) Besides you asked for a scalable approach. If you ever want to support other blocks, this is the most scalable. \$\endgroup\$ – Steven Jeuris May 7 '11 at 15:09
2
votes
\$\begingroup\$

Have an enum like you do for the pieces.

That is okay.

Points is more work than you need to do.

The piece in play is an instance, with a reference to the original enum.

class FallingBlock {
    BlockEnum originalBlock;
    Point location;
    Shape shapeToDraw; // a copy of the original shape so you can manipulate it.
}

The shape instance can be rotated, scaled etc.. Java2D is quite helpful. You could do it all with AffineTransforms upon original shapes , and not have an original shape and a copy but this way is less complicated. And you can have arbitrarily complicated shapes! You keep the original for the points value, and so you can display it in a "current block" pane.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.