Hello, i have another rather basic question. While learning Java, i was coding a card-base game, somewhat similar to Magic: The Gathering. For those who don’t know, MTG uses a very simple basic mechanic at it’s root (you have lands - resources- and creatures and spells that can be played using these resources). The complex thing is that almost every card can bend, tweak or completely alter these basic rules while it is in play (one card could say, for example, that my opponent’s creatures comes in play tapped).
So, while learning, my first try was to create a Card class that would contain all the basic information of the card - it’s name, cost, power, toughness, flavor text, etc, and create these cards by hand. For example:
Card dwarf = new Card(“Dwarf”, “2RR”, 2 , 2, “The dwarf is cool.”, etc);
But while reading about design patterns, it became quite clear that this approach was completely wrong. It could work, but i would have to hard-code each card’s ability, losing a lot of flexibility in my code, and it would be really hard to alter the card’s basic rules dynamically.
Then i read about the “Strategy design pattern”. Each card could have a set of Behaviors interfaces (EnterPlayBehavior, DieBehavior, AttackBehavior, and so on) that could be implemented by some specific abilities (for example, EnterPlayTapped could implement EnterPlayBehavior, making the card enter the game tapped). The problem is that each card would need to be a class in it’s own.
public class Dwarf(){
EnterPlayBehavior enterPlayBehavior;
OtherBehaviors otherBehaviors;
public Dwarf(){
EnterPlayBehavior = new EnterPlayTapped();
OtherBehaviors = new SomeBehavior();
public void setEnterPlayBehavior(EnterPlayBehavior b){
enterPlayBehavior = b;
}
//and so on for each behavior
}
It would be feasible and rather flexible (and good OO from what i’ve seen) but how can you manage to have over 500 classes that are only for your cards in your code? And then another 20 or so Behaviors interfaces, with around 5 or so different variations on these behaviors?
While, i’ll continue reading on design patterns, i’m quite puzzled by how anyone would do this. Any input is greatly appreciated