Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to design a MTG-style card game?  (Read 2316 times)
0 Members and 1 Guest are viewing this topic.
Offline ahottev

Senior Newbie

« Posted 2010-10-21 12:44:01 »

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 Smiley
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #1 - Posted 2010-10-21 13:25:40 »

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 Grin ).

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #2 - Posted 2010-10-21 20:42:06 »

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 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.

Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (160 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (902 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!