Attempting to Play Tron With Python

Quick Recap

This is a continuation of a project and the first part can be found here. https://cole-kepford.medium.com/learning-python-to-play-magic-the-gathering-7bee3930ed70
For a quick recap I went over the surface level differences between the programming languages Java and Python. The purpose of this was to use learning Python as an excuse to attempt solving a problem I’ve wanted to figure out for a long time. The problem was to play the Magic the Gathering (MTG) deck Tron. This article will go over the further knowledge I’ve gained about Python, the script I wrote to play Tron without an opponent, and the troubles I encountered.

Taming the Python

I finally discovered something in Python that I heavily dislike and prefer Java/earlier languages for. In Python you get super fancy and easy enhanced for loops. However, there is no actual standardized for loop you’d be used to from third generation languages.

for(int i = 0; i < arr.length; i++) {
}
while removedcards < 7 - handsize and i < len(hand):  if(hand[i] == "tower" or hand[i] == "mine" or hand[i] == "pp"):    deck.append(hand.pop(i))    removedcards += 1  i += 1
def onetronland(hand):  if("tower" in hand or "mine" in hand or "pp" in hand):    return Truereturn False

Assembling Tron

It would be ridiculous to go over every line of code in the script so I’ll be summarizing sections with examples. If you want to see the whole thing, it can be found here https://github.com/ColeKepford/Tron_Goldfish

Mulliganing

The first task to accomplish with playing a game of MTG is to decide if you’re going to keep or mulligan your hand. The way the current mulligan rules work in MTG is you draw a hand of seven cards and if you mulligan you draw a new hand of seven. If you keep the hand you put cards on the bottom equal to how many times you’ve mulliganed. To solve this, I set up varying conditions for keeping a hand based off how many mulligans you’ve taken.

  • Any hand with two Tron lands and a way to search for them on turn two are kept
  • Hands not meeting the above two conditions are mulled to at least five
  • Once at five cars simply have two Tron pieces and a “star” is a keep
    (link to card) https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=489912
  • On four cards one Tron land and star or tutor effect is a keep
  • Three card hands have the same conditions
  • Once on two or less cards simply having a Tron land in it is a keep

Playing the Deck

Once a hand has been kept it’s time to see if it can combo. The first thing that’s checked for is if you have any lands in play to produce mana. This is followed by determining if you have any unique Tron lands you can play from your hand. Then the script checks if you’ve assembled Tron in play, if so the script ends on that turn. Then the script checks if you have certain cards in play and what to do with them in the following order.

  • Do you not have the combo and have a search effect you can use. If yes, use them
  • Do you have an Ancient Stirrings and can you cast it?
  • Do you have any star effects you can cast and then use?
  • Have you drawn any Tron lands and not played a land yet?
  • Have you drawn any non-Tron lands and not played a land yet?

Doesn’t Totally Work

Unfortunately I’ve been having some issues that are preventing it from being fully functional. If running it through a client or through the console it assembles the combo and doesn’t go through the turn cycle. However, when going through it step by step in a debugger it runs correctly. With some tinkering and proper break points I’ll be able to determine where it’s breaking, but currently it won’t work without some finagling.

Conclusion

Overall, it’s been a really fun time getting to screw around in a new user-friendly language to try and solve a problem I found interesting. This was a problem that became significantly harder to solve than I initially thought it would. Figuring out the proper order to bottom cards definitely took a lot longer than I expected it to. I’ll echo my opinion from the last post. If you’re new to programming or want to limit how many characters you need to type to solve a problem, Python is certainly a very good choice.