Explaining Programming Concepts using Trading Card Game Mechanics

One of the things I love to do is to program computers in various languages, and another thing I love to do is to sit down with a couple friends and play a nice Trading Card Game, such as Magic: The Gathering, or Yu-Gi-Oh. In doing both of these, I came to the realization that both activities require strong logical thinking, and, in a lot of ways, they're very similar. I believe that someone could teach a child or young adult about programming concepts in an introductory language by playing a Trading Card Game. I'll be discussing Yu-Gi-Oh and Magic: The Gathering since those are the two I know best; however, you could explain this with other games as well including the Pokemon Trading Card Game, Hearthstone, and more.

I'll be including brief overviews of the games I just described, but I'm not going to be giving full tutorials. Each of the two games do a better job explaining it either through online manuals or by digital games; Magic: The Gathering has "Magic: The Gathering Arena" (which is available for Windows PCs), and Yu-Gi-Oh has "Yu-Gi-Oh Duel Links," available on Mac, PC, iOS, and Android.

A Brief Introduction To Both Games

Both games have an element of being able to play monsters on your side of the field, attacking with those monsters, and trying to reduce your opponent's life to 0 (or winning through an alternate win condition card, both games have a ton of them).

Both games are turn-based and have a turn flow, in which you draw a card, apply pre-turn effects, play cards, battle, optionally play more cards, and declare end of turn.

Monsters can have effects that occur either when they're brought out or on another trigger, which heavily play in to the logical portion of how trading card games are much like programming.

How Magic: The Gathering Differs

The terminology is slightly different from Yu-Gi-Oh, as well as the mechanic used for bringing monsters out onto the field.

Magic: The Gathering games can start with 20 Life, and, in some formats such as Commander, games can start with 40.

In Magic: The Gathering, turn flow is consistent of:

  1. An upkeep, where tapped lands and creatures are untapped.
  2. A draw phase, where the turn player draws a card.
  3. Main Phase 1, where the turn player may cast creatures, planeswalkers, or sorcery cards using lands they tap for mana (for example, tapping a Forest will give a player 1 "green" land, which they can use either for a colorless mana or for a green mana)
  4. The Battle Phase, where the turn player may make attacks on opponents or their planeswalkers. This consists of the Attack Declaration step, where the turn player declares attackers (these creatures are tapped unless they have vigilance); the Blocker Declaration step, where the opponent can choose blockers for those attackers (most blockers can only block one creature; however, an attacking creature can have any amount of blockers block it); and the Damage Step, where damage is applied (creatures with First Strike deal damage before anyone else).
  5. Main Phase 2, where the turn player may do the same thing they did in Main Phase 1 if they can do anything else.
  6. The End Step, where the turn player declares End of Turn.

Anything that is tapped is turned sideways. Tapped creatures cannot block. Tapped lands cannot be used for mana until they are untapped (which is usually during that player's upkeep). Additionally, instants and creatures with Flash can be cast any time, even during an opponent's turn. Finally, creatures cannot attack or be tapped the turn they are cast.

How Yu-Gi-Oh Differs

In Yu-Gi-Oh, there isn't a mana system; however, you may only Normal Summon face-up or Set face-down sideways once per turn (usually). A Normal Summon or Set can only be done with monsters that have four or less stars on them; a monster that has five or six must have a Tribute and be Tribute Summoned, and finally, a monster that has seven or more must have two Tributes.

Special Summoning can only be one when a card effect says so (which is way more often than you would think), or when you can summon a monster from the Extra Deck using Fusion Summoning, Xyz Summoning, Synchro Summoning, Pendulum Summoning, or Link Summoning. Some Special Summons have restrictions, and many effects can only be activated once per turn.

Yu-Gi-Oh games usually start with 8,000 Life Points. In some speedier formats such as Duel Links or Speed Duel, games can start with 4,000 Life Points.

Turn flow usually consists of:

  1. A Draw Phase, where the turn player draws a card.
  2. A Standby Phase, where some effects can activate.
  3. A Main Phase 1, where the turn player can Normal Summon, activate Spells, set Traps, Special Summon, or turn their monsters from Attack Position to Defense Position (and vise versa; this can only be done once per turn per monster).
  4. A Battle Phase, where the turn player can declare attacks on their opponent's monsters'.
  5. A Main Phase 2, where the turn player can do the same things they did during their Main Phase 1. Note: Speedier formats of Yu-Gi-Oh don't have a Main Phase 2 (including Duel Links and Speed Duel), and during these formats, you'd end your turn after the Battle Phase.
  6. An End Phase, where the turn player declares End of Turn.

Spells and Traps can be played or Set any amount of times per turn. Set Quick-Play Spells and Traps can be played during your opponent's turn as well as on any phase of your turn. Quick-Play Spells can be played from the hand on your turn, and Traps must be Set for one turn before they can be played.

Finally, on the first player's turn, they may not draw, nor may they enter Battle Phase or Main Phase 2.

Now that we've explained the basics of both games, it's time to see how they relate to programming languages as a whole.

Program Structure

Turns start with a Draw Phase or Upkeep, and may have a set of actions that can be completed. Finally, they end with an End Phase.

This is similar to a program structure because programming languages also start, follow a list of instructions, and then end. Some cards even have effects that trigger at specific phases, such as Yu-Gi-Oh!'s "Lava Golem." It's Special Summoned to the opponent's side of the field by tributing two of their monsters, and then, during the controller's Standby Phase, they take 1,000 damage. Since your opponent would control it, they'd take the damage, which could come to your advantage, although they can still attack with Lava Golem and it's a very hefty beater.

Sequence Control

A lot of cards also read in the format, "if X, then Y, otherwise Z." One of my favorite examples is Slash Draw, which reads:

"Discard 1 card; send cards from the top of your Deck to the GY, equal to the number of cards your opponent controls, then draw 1 card, and if you do, show it, then send it to the GY if it is "Slash Draw", and if you do, destroy as many cards on the field as possible, then inflict 2000 damage to your opponent for each card destroyed and sent to the GY by this effect. Otherwise, shuffle cards from your GY into the Deck, equal to the number of cards sent from your Deck to the GY by this effect."

This means that, if you luck out and draw a Slash Draw off of this effect, it could come close to an instant win; otherwise, shuffle cards into your deck.

Loops

Loops are prominent in both games. In Yu-Gi-Oh, cards that cause loops are typically on the banlist. A lot of loops in the game center around an older card, Cannon Soldier, which simply reads, "You may Tribute a monster; your opponent takes 500 damage."

These loops would revolve around repetitively Special Summoning a monster from the Graveyard with another card's effect (the latest one was Firewall Dragon), Tributing it with Cannon Soldier for 500 damage, and doing it again and again until your opponent either dies or flips the table. Plays like that are generally considered unfair, and, as a result, Firewall Dragon is now banned from the game (meaning you cannot use it in a Duel).

I don't know too much about loops in Magic: The Gathering; however, an arbitrary example would be, during a player's upkeep, cause their opponent to take 3 damage, and have two enchantments that read:

  • When you deal damage, gain life equal to that amount.
  • When you gain life, deal damage equal to that amount.

This would cause you to infinitely be dealing damage, and win the game.

Going Back To Programming

Programming usually uses loops to its advantage, and trading card games usually finish the loops when the opponent dies. We can call this an Exit Condition.

Similarly, we can program using loops to find a Greatest Common Denominator of two numbers using Euclid's Algorithm. Usage of the algorithm is described better at the following link: http://sites.math.rutgers.edu/~greenfie/gs2004/euclid.html.

Essentially, Euclid's Algorithm is recursive, meaning a lot of the method calls are to itself and starting back over from the beginning.

This relates to the loop in the card game in the sense that the loop described earlier instantly wins a game by repetitive subtraction of a player's Life Points. Setting up the loop can be tricky at times, though you can sometimes win a game of Yu-Gi-Oh on the first turn of the game.

To view or add a comment, sign in

More articles by Daniel Dalton

Others also viewed

Explore content categories