Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 950 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: 56
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.

1  
2  
3  
4  
5  
6  
7  
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 ).

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

« JGO Bitwise Duke »


Medals: 158
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 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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!