How ‘Magic: The Gathering’ Helped Me Learn Recursion

Liam Hanafee-Areces
The Startup
Published in
4 min readFeb 5, 2021

“A recursive function is a function that calls itself.”

As a new developer, recursion is a topic that I was taught to fear. It has a reputation online and in coding bootcamps as a mind-blowing and difficult concept to master.

Now that I have learned about recursion in the data structures and algorithms course I am taking on Udemy, I can see why. Upon reading the definition of a recursive function, it is very easy to think something along the lines of: “Wait, what do you mean the function calls itself?? How? Why is coding so hard?!?”

However, I am here to help! Well, at least those of you who play ‘Magic: The Gathering’(MtG). In this article, I am going to show you an example of a combo in MtG that shares a lot of similarities with recursive functions. It isn’t an exact comparison, but I think if you play MtG this will be a helpful way of thinking about it.

Try not to let your opponent get both of these cards out.

If you have both of these cards in play, all you need to do to win is gain a life, or cause an opponent to lose a life. There are tons of ways for either of these things to happen in MtG, from attacking an opponent and lowering their life total, to cards that literally say ‘you gain 3 life.’ Once one of the two cards above is triggered, it will cause the other one to trigger, which will cause the first one to trigger, and so on. However, as soon as a player’s life total hits 0, they lose. Otherwise, the loop and the game would go on forever. In a recursive function, this condition that breaks the loop is known as the “base case.”

The base case is one of two essential parts of a recursive function. Without it, we would just end up in an infinite loop. The other essential part of a recursive function is a change in input. In the example we are about to review, the input is a number. If that number never changed, we would never hit the base case and be stuck in a loop. If this is a little confusing still, don’t worry, it’ll make sense soon!

My attempt to mimic the card combo in a function

Now, I know that this is not exactly the same as the two card combo. It would be pretty silly if there were a single MtG card that did this. It would just win the game on the spot. However, for the purposes of this exercise I am going to combine the two effects into one function. The essentials are all still there. In the example above, p1 is the player that has the two card combo in play. Lets walk through the function.

  1. The function takes two arguments, the players’ life totals.
  2. It checks if either life total is 0. If one of them is, “Game Over!” will be printed in the console, and the function will end. This is our base case. In this example, both arguments are 5. Therefore, the function will continue on.
  3. p1’s life total is increased by one, and p2’s life total is decreased by one. Both of their updated life totals are printed into the console, so we can make sure they are being incremented properly.
  4. The function calls itself, this time with the updated life totals as the arguments.

These steps will repeat themselves until the base case is reached, and the function will end there.

Even if you don’t play MtG, I hope that this example and article were helpful to you. Also, I encourage any developers out there who haven’t given the game a chance to check it out. It is easier than ever to get started now that MtG Arena is out on multiple platforms. You will find yourself thinking in a lot of the same ways that you do while coding, and it is equally satisfying when you put all of the pieces together.

Thank you for reading!

--

--