How to design a MTG-style card game?

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 :slight_smile:

That strategy pattern approach would probably be a good idea, but you wouldn’t have a class for every card, you’d have a single Card class which would hold a bunch of different behaviours (or null) for each event. Then you’d just have a factory function to create each one.


public static Card createMoltarTheGreatCard()
{
  Card card = new Card("Moltar the Great");
  card.setEnterPlayBehaviour( new StartsTappedBehaviour() );
  card.setTurnBehaviour( new DoubleDamageBehaviour() );
  return card;
}

(I’m not terribly familiar with Magic, so you’ll have to forgive the crappy example).

Hopefully then you’d have a lot of behaviours that are reused between cards, and only a single Card class. Bear in mind though that Magic is a very complicated game, with very complicated and special-cased rules (“untap this card on the third sunday, unless SpecificOtherCardY is in play, in which case retap the forth SpecificOtherCardZ thrice widdershins on a thursday”). If you’re just starting out with Java it’s probably way over your head (frankly I’d deem it over my head as well ;D ).

Don’t have much to say, except that enforcing MTG rules is a very difficult problem. The core rules are not simple, and many hooks are required to implement all the cards.

FWIW, I wrote the Arcane project which you can find on the forums at slightlymagic.net. It’s a Java lib that handles many common tasks for dealing with MTG cards and rules. Nate’s Deck Builder is built with it. A guy in the community named Snacko has been maintaining the Deck Builder for years, so you would probably want to get the Arcane project from there.