I'm building a Tetris game in Java using some software design patterns. Basically, I created a factory algorithm that retrieves a string to figure out what type of tetris object to make when it's given a request by the main game loop, see code:
public class TVShapeFactory {
protected TVShape tvShape = null;
protected GameContainer gc;
TVShapeFactory(GameContainer gc) {
this.gc = gc;
}
public TVShape createShape(String shape) {
if (shape=="TVIShape") {
tvShape = new TVIShape(gc);
}
else if (shape=="TVOShape") {
tvShape = new TVOShape(gc);
}
else if (shape=="TVLShape") {
tvShape = new TVLShape(gc);
}
else if (shape=="TVZShape") {
tvShape = new TVZShape(gc);
}
else if (shape=="TVJShape") {
tvShape = new TVJShape(gc);
}
else if (shape=="TVSShape") {
tvShape = new TVSShape(gc);
}
else if (shape=="TVTShape") {
tvShape = new TVTShape(gc);
}
else {
System.out.println("Error: invalid type of shape");
}
return tvShape;
}
}
If you don't know what gc is, it's simply a part of Slick 2D's library and is used to work with the full game environment. Anyways, I created a few other methods to randomly generate a shape for the game (such that the main game loop receives a random shape every time, but I feel like RNG doesn't cut it, I want to make it harder. I noticed there's a famous tetris algorithm called Bastet tetris, but it didn't make sense to me. Any suggestions you guys have for making a HARD tetris shape generation algorithm? Here's my simple RNG algorithm:
public TVShape getRandomShape() {
TVShape[] shapes = new TVShape[7];
shapes[0] = createShape("TVIShape");
shapes[1] = createShape("TVOShape");
shapes[2] = createShape("TVLShape");
shapes[3] = createShape("TVZShape");
shapes[4] = createShape("TVJShape");
shapes[5] = createShape("TVSShape");
shapes[6] = createShape("TVTShape");
int index = new Random().nextInt(shapes.length);
return shapes[index];
}