To get more familiar with Java, I decided to make a Tetris clone.
As I finished the Tiles
beans class, I went to the Tetromino
class, which is basically made of different sets of 4 Tiles
objects (I'm grouping them with ArrayLists
). Each Tetromino
has a different colour and shape.
Would it be better practice to identify what Tetromino I'm trying to make right in the Tetromino
class, or should I make a different class for each different kind of Tetromino available, probably extending the Tetromino
class?
Example
(Please note that these are just design-illustrative made to represent my question)
Instead of something like:
public class Tetromino {
private ArrayList<Tiles> tiles = new ArrayList<Tiles>();
public Tetromino(char shape) {
switch(shape) {
case 'L':
createLShapedTetr();
break;
/* etcetera */
}
}
private void createLShapedTetr(){
/*
Sets color for bricks
Adds each brick (with their X and Y coordinates) to the arrayList
*/
}
/* other methods, such as spinPiece() */
}
Do something like this:
public class Tetromino {
private ArrayList<Tiles> tiles = new ArrayList<Tiles>();
/* Tetrominoes basic methods that every Tetr does */
}
public class TetrominoShapeJ extends Tetromino {
/* sets colour, space and spin-method in here */
}