Page 1 of 1

Programming a Magic: The Gathering game

Posted: Sun Oct 17, 2010 9:34 pm UTC
by Bowshewicz
To any advanced player of Magic: The Gathering who is also a programmer:

If you have ever read through the comprehensive rules, it seems almost like the game was designed with a specific programming strategy in mind. The most obvious example is the use of "the stack" in the game behaving exactly like the data structure called a stack, but it goes well beyond that.

As an exercise to improve my logical aptitude as it applies to programming, I often think about how I would implement software that allows users to play Magic: The Gathering. The software would implement all game rules as they appear in the comprehensive rules document (You can download it from their website).

Most of the game rules fit perfectly into common programming styles; for example, triggered abilities could be implemented using Events. I consistently run into a problem, however, when I get to implementing a certain feature of abilities: Where does the ability function? Rules 112.6d-i are where I'm currently getting stuck. I can't seem to think of a general solution that doesn't rely on writing code specific to certain cards.

Please note that I currently have no intention to actually implement any type of Magic: The Gathering software. It is purely a thought exercise. My intention in asking here is to merely to entertain.

Edit: I apologize for the confusion with the rule number. I was using the April version of the file. I am interested in rules 112.5c through 112.5j

Re: Programming a Magic: The Gathering game

Posted: Mon Oct 18, 2010 2:13 am UTC
by Yakk
These rules?
http://www.wizards.com/magic/comprules/ ... 090708.pdf
There is no 112.6d-i.

Do you mean 112.6d-i?

Magic cards are written in English, but you either have to parse that English or translate it to code in order for the game to understand the ability. The easiest method is to translate it into an easier to understand format... either that, or take advantage of the artificial way that the cards are written, and write a special purpose parser.

Once that happens, cards end up as objects with properties hanging off of them. These properties denote when they are active -- what zone a given property is active in is described by that set of rules.

Re: Programming a Magic: The Gathering game

Posted: Mon Oct 18, 2010 3:59 am UTC
by Bowshewicz
Thanks for providing that link. I suppose the best way to do this would be to look for certain phrases on the cards. So the phrase "from (firstZone) to (secondZone)" means it is an example of 112.5i, and it functions only in firstZone.

112.5i An ability whose cost or effect specifies that it moves the object it’s on out of a particular zone functions only in that zone, unless that ability’s trigger condition, or a previous part of that ability’s cost or effect, specifies that the object is put into that zone.


So, the format for abilities would be pretty complex:

112.5a: [Characteristic] are/get/have [New Characteristic].
112.5b: Activate this ability only [Zone/Condition].
112.5c: [This Card] costs [Cost] less/more to cast [Condition].
112.5d: Cast [This Card] only [Condition].
112.5e: ???
112.5f: [This Card] enters the battlefield [???].
112.5g: ???
112.5h: ??? ( How to tell whether the trigger condition can be met or not?)
112.5i: [???] [This Card] from [Zone] to [New Zone].
112.5j: ???

I have no idea how I would generalize this for all abilities. Thank you for your help and for jump-starting my thinking processes.

Re: Programming a Magic: The Gathering game

Posted: Sat Oct 30, 2010 8:08 pm UTC
by pepsi
I've done this before.

Even if you have a script that can read the cards some mechanics are damn hard to script for. Storm is hard to script for. The same goes for any 5 color strategy since you have to plan out your next few turns differently depending on what sources you draw into.

You learn some interesting things from running simulations though so it's really worth the effort. For example even if only 3 cards exist (a G source, a 2/2 for 1G, and a 3/3 for 2G) a meta game forms were certain combinations of those 3 cards curb stomp other combinations but there isn't one that's ever best.

Re: Programming a Magic: The Gathering game

Posted: Mon Nov 08, 2010 4:44 pm UTC
by dosboot
Bowshewicz wrote:Thanks for providing that link. I suppose the best way to do this would be to look for certain phrases on the cards. So the phrase "from (firstZone) to (secondZone)" means it is an example of 112.5i, and it functions only in firstZone.


What problem are you trying to solve? Is it processing English card text into a formally defined language or is it designing the formal language and related programming abstractions?