Magic The Gathering, maths and AI
Tags: magic, ai, maths, games
Published: 2017-09-25 22:24:10

---
title: Magic The Gathering, maths and AI
published: true
description: My thoughts on Magic The Gathering from a mathematical perspective.
tags: Magic, AI, maths, games
---

When I was a kid I used to play Magic the Gathering in the comic store against the shopkeeper, with my pre-made green/red deck. Currently I re-started playing MTG firstly because I got interested in tokens creation and _alters_ , but also returned playing with a control _white-black_ deck. But now, as an engineering student, I became curious about **how an AI would play Magic** (theoretically) and what kind of maths are involved in this game.

![](http://img1.meristation.as.com/files/imagenes/general/magic.png)

For the ones who don't know about **MTG**, it's a card game created by a computer mathematician in the nineties, that turned out to be very popular years after. Currently, this game host tons of tournaments worldwide, and there's a serious amount of money involved.     

Artificial Intelligence development has evolved through the last years greatly, creating amazing agents, such as the one [which plays Go](https://www.theguardian.com/technology/2017/may/25/alphago-google-ai-victory-world-go-number-one-china-ke-jie) or the one which is [created to negotiate](https://futurism.com/a-facebook-ai-unexpectedly-created-its-own-unique-language/), as Turing speculated in his study [_Computer Machinery and Intelligence_](https://www.csee.umbc.edu/courses/471/papers/turing.pdf), AI is expecting great evolves in the future. 

Magic the Gathering itself involves lots of rules. Not only it has a huge amount of rules of how proceeding in the game, but also each card has its own rules to be considered. But even if the AI is able to store all of that information in a huge database, there are more issues to face. 

**First of all the information of each game is incomplete**. This means, even knowing your own deck (or even having knowledge of the opponent deck) there's a random component, as the first turn starts grabbing seven random cards from the top of the deck and next turns taking one. If the opponent's deck is unknown (a plausible scenario) it's even worse.

Actually, I'm lying a bit here. An Ai can handle "_easily_" the random component of a known range of strategies with a limited numbers of cards. An example I found nice is Hanafuda Koi Koi, a game in which players are meant to organize cards to create compositions. I found [this repository](https://github.com/xingdi-eric-yuan/hanafuda), for example. Anyway this kind of game could be solved using a MAX-MIN programming strategy updating the information in the table each time. 

For example, this is a MIN-MAX way of approaching a game (tip-tap-toe) :

![](https://qph.ec.quoracdn.net/main-qimg-d40fa96a33d58e91e844e3430cefad35-c)

**So what's the matter with Magic?** 

Strategy is not that clear. Seeking the greater points (as in Hanafuda) can lead in a mathematical analysis of the table, but strategy is more complex in this case. Surely there are some patterns players _usually_ take, and can be analysed studying lots of games (like the ones in the tournaments) but it's pretty common players creating their own strategy and decks, which at the same time creates a growing _almost_ unlimited number of strategies.Not only this is problematic, but also the player can try to improvise a strategy in the middle of the game. 

![](https://images.vice.com/vice/images/articles/meta/2015/05/07/the-mystical-universe-of-magic-the-gathering-000-1431011776.jpg?crop=1xw:0.9991119005328597xh;center,center&resize=1050:*)

Also the way a player can approach an opponent is not determinant, unless the action is clearly developing a one turn strategy, but player is more likely to develop a 2+ turns strategy that can vary depending on the opponent's new hand each turn. In a **limited game of perfect information** (such as [chess](https://medium.freecodecamp.org/simple-chess-ai-step-by-step-1d55a9266977)) this 2+ turn analysis is possible because all the information is already in the table (all the tokens and possible movements) but that's not the case in MTG, as we already explained.     

Again here's a MIN-MAX example of a chess movement.

![](https://cdn-images-1.medium.com/max/1600/1*UA5VlNs7s4gl80VknA099w.jpeg)

We already said we can found a countless number of strategies in a single game of MTG, and I'd like to explain some of the patterns I saw in the game. First of all let me explain a _simple_ detail. Game strategy is open to the players, but there are certain roles assigned to each _color_. This mean, cards of the same color tend to follow a specific strategy, which can be manipulated and bend in convenience, but usually determines the style of a deck. White, black and blue are usually meant for _control_ (specially blue); red and green are more used as direct attacking. Again this is a general idea, as it could be changed in specific strategies. 

Once this is said, let me ask you something. Have you ever guys heard about the "_Chicken Game_" ? This is a very representative game (alongside the Prisoner's dilemma) which explain an specific situation of two players.

The game is easy: Two drivers are faced one against another in their cars. Both of them speed up, and the points of the game are distributed like this:

- If they crush because none of them move both get 0 points (duh)
- If both of them go away from the crush in the last moment, they both get 3 points.
- If one of them goes away form the crush and the other one stays in the same trajectory, the first one gets 1 point and the second one 5 points. 


This can be seen for example in Rebel Without a cause!

![](https://1.bp.blogspot.com/-K_Dv_lCPFsI/VvN3SZ10M4I/AAAAAAAAvg0/8c-USEstNWYJF_z12vrfyHMVNQJ3cYkEQ/s640/19%2B%25282%2529.gif)

This is a very hardcore game indeed, but it can be applied in daily matters such as politics or negotiations. This example only describes the dominant desire of players in the game, and surely this can also be seen in MTG. I can see this kind of pattern when two similar decks (or certain turns) are being played, and this could involve, for example, similar colors. This situation, tho, implies similar strategies, which are not necessary implied by the same color.   

And speaking of Chickens there's also another mathematical study of behavior, commonly called "_Hawks and Doves_". In this example, Hawks have advantage in direct attack, but having many of them cause conflict between hawks, decreasing its number and having again balance with doves. 
![](http://imgc.allpostersimages.com/images/P-473-488-90/86/8692/9UVU300Z/posters/the-hawk-dove-model-of-game-theory-illustrating-the-costs-and-benefits-of-fighting-over.jpg)

I saw this in a certain strategy of a control that causes life loss by it's user each turn but creates an advantage such as tokens (creatures that can be generated, mostly with low attack) with no support of a card that heals the damage. If the opponent lets the first player have the life loss card on the table, this player will surely be damaged each turn but also have table control with tons of creatures. Hawks resemble to the time the player has the card on the table, and doves are the opponent. If the opponent destroys the card when possible, he/she is removing the constant life loss of his/her enemy, but also taking away the constant table control.

![](https://images-na.ssl-images-amazon.com/images/I/51PahMyHu-L._SL500_AC_SS350_.jpg)

Proceeding in this kind of problems not only requires a logical calculus that an AI could certainly approach, but also some kind of cooperation with the enemy in order to find a way to be a dominant player without slipping on going extremely aggressive.This is a subtle subjective action that can be described for some humans as "intuition" or in its absence "experience". Experience can be gained by an **AI the same as humans**, studying, winning and losing tons of times, but learning this pattern in a general term is very difficult for such an artificial agent. If I was asked to create a shortcut for a MTG agent in this matter, I would take a list of every card that tends to generate this strategy (like the one above) and create an specific way of acting, but again this can lead in a stupid action depending on the context.

To sum up, strategies with imperfect unlimited games with random components are very difficult to manage by an AI agent, and the game can have several strategies already studied for the real life issues. If Skynet takes our world, dare it with a Magic play!