Learning Python to Play Magic the Gathering

The purpose of this project is to learn the programming language Python and use it to write an algorithm to play the first three turns of a specific Magic the Gathering (MTG) deck. I wished to learn Python because it’s the most common language associated with Data Science which I have an interest in. The purpose of the algorithm is to play a specific MTG called Tron. It is called Tron because when it assembles a certain combination of cards the deck becomes much more powerful. This is a problem I’ve always wanted to try and solve so I’ll use this as an opportunity. The first half of this blog will be detailing how Python works, and how it differs from Java. While the second half of the blog will be detailing the problems to solve with creating the algorithm.

Differences between Python and Java

Syntax Differences

Java Message

public class Learning 
public static void main(String args[])
String array[] = {"I\'m gonna", "learn some", "Java" , "420"}; for (String i : array)
System.out.println(i); } }}

Python Message

message = ["I'm gonna", "learn some", "Python", 420]for i in message:    print(i)

You’ll notice some big differences. In Python statements don’t require a semicolon to end them. This is accomplished because whitespace is actually used as syntax in Python. A new line can determine the next statement and an indentation determines what’s within a prior statement. In Java to print out this message a class had to be declared. Then the Main method is invoked to tell the JVM what to run. Even the print statement is simpler in Python. Another huge difference is that Java is a static language while Python is a dynamic one. In the Java array the number 420 had to be declared as a string because the data type of the array is String. In the Python you can put whatever data you want into an array. In static language your variables cannot change. Where as in a dynamic language the data types aren’t declared and can change during runtime. Let’s try something a bit more complicated with a bubble sort in either language.

Java Bubblesort

static void bubbleSort(int arr[]){    int n = arr.length;    for (int i = 0; i < n-1; i++)        for (int j = 0; j < n-i-1; j++)            if (arr[j] > arr[j+1])            {                int temp = arr[j];                arr[j] = arr[j+1];                arr[j+1] = temp;            }}
public static void main (String [] args)
int arr[] = {69, 420, 42, 10000, 314, 316, 101};
bubbleSort(arr); System.out.println("Sorted funky numbers is:"); for(int i: arr) { System.out.println(i); }}

Python BubbleSort

def bubbleSort(arr):    n = len(arr)    for i in range(n-1):       for j in range(0, n-i-1):            if arr[j] > arr[j+1] :                arr[j], arr[j+1] = arr[j+1], arr[j]arr = [69, 420, 42, 10000, 414, 316, 101]bubbleSort(arr)print ("Sorted funky numbers is:")for i in range(len(arr)):print ("%d" %arr[i]),

Some big differences to point out. Datatypes on variables don’t need to be declared in Python as the interpreter determines the most optimal data type at runtime. A function in Python is created by using the def keyword. The len() function returns the length of a given array. A main method is not required. Not that for statements in Java were ever that hard to understand, but having code that says

for i in range(n-1):

is a lot easier to read from a non coding background than the standard Java for loop. A really cool thing I learned you could do is assign multiple variables different values in the same statement.

arr[j], arr[j+1] = arr[j+1], arr[j]

What Do I Prefer?

Playing Tron

What is Magic the Gathering?

The Deck

When at least one of each copy of these lands is in play the lands produce a total of seven mana or respectively three, two, and two colorless/generic mana. Generic mana can be a combination of any color(s) of mana. The entire purpose of the deck is to assemble the above combination of lands as quickly as possible and then play cards with very high mana costs. The algorithm I will be writing will try and determine the rate at which the deck can assemble “Tron” by turn three as well as having a payoff to play with the large mana pool.

The cards that enable Tron to assemble it’s combo so quickly are (lands are colorless cards):

Problems to Solve

In Conclusion

Video of Tron Playthrough and Talkthrough of Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store