I am creating a game for my school project, so I am not here for any kind of code, just a global idea on how to do what I am trying to do…
I have Pieces and each piece as is own color and Type like this :
Piece 1 – Yellow – Simple
Piece 2 – Yellow – Explosive
….
Piece 10 – Blue – ChangeColor
At start the game will be field with random pieces from this list (a list created earlier with just one kind of each piece), if we have 3 or more in a row the game as to remove them and count the points, but if one of the 3 (or more) has any “power effect” than the pieces arround must react to that power (hard to explain with my low english level but I think you got it :S..)
For now my game only had simple pieces, and for that I used and enum like this :
public enum CorPeca
{
AMARELO,
AZUL,
VERMELHO;
}
On the board class I was doing this :
this.arrayPecas = new ArrayList<Peca>();
for (CorPeca corPeca : CorPeca.values())
this.arrayPecas.add(new Peca(corPeca));
and on the Pieces class i was doing this
public Peca(CorPeca tipoPeca)
{
this.definirPeca(tipoPeca);
}
public CorPeca getCorPeca() { return this.corPeca; }
public char getCharPeca() { return this.charPeca; }
public void definirPeca(CorPeca tipoPeca)
{
switch (tipoPeca)
{
case AMARELO:
this.charPeca = 'a';
this.corPeca = CorPeca.AMARELO;
break;
case AZUL:
this.charPeca = 'u';
this.corPeca = CorPeca.AZUL;
break;
case VERMELHO:
this.charPeca = 'e';
this.corPeca = CorPeca.VERMELHO;
break;
}
}
But I was thinking of a good method to easily add those “Power Pieces”, but more than that, being able to add more colors and more “Powers” in the future…
I was thinking about creating subclasses and using extend, but the whole idea just don’t fill in my mind since we can’t extends several classes…
So can anyone give me a global idea on how I can archive that ?
The classic OOP way to do this would be to define an abstract base class that represents all pieces and contains common functionality (such as a list of powers):
Note that
abstractmeans that you can’t directly instantiate this class – you can only make instances of concrete subclasses.Then extend this class to implement each specific piece:
You can still create an
ArrayList<Piece>and populate it with something like:Then if you want to apply the power of a specific piece, just do:
You’ll obviously need to customise this code a lot to make it work in your game, but hopefully it gives you the general idea. Note that there are lots of other ways of achieving the same goals – I’ve just described the “classic” OOP way.
P.S. Apologies for all code being in English, but your English is far better than my Portugese 🙂