My project for ChiPy’s Python Mentorship is an attempt to program a card game like Magic: the Gathering in Python and then to use machine learning techniques in order to have the computer solve for the best possible strategy. In high school and my first couple years of college, I played Magic: the Gathering to the point where I became sick of it. I was intrigued by the tournament aspect and trying to solve an incredibly complex game. While my desire to compete in tournaments or play has pretty much disappeared for me, I’ve always sort of regretted not being able to “solve” the game. In the last couple years, I’ve started learning to program and always thought it would be neat to reprogram the game in python. I’ve been inspired reading about programs like Deep Blue and the recent developments in Go using Google’s DeepMind, and have had a desire for a while to learn more about data Science and machine Learning for awhile.

The Project So Far

The first part of the project is coming along smoothly. At present, I have made a simple version of the game where a player can play against a computer “AI”. The basic idea of Magic: the Gathering is that both players summon creatures which they use to attack their opponents and take their life points from 20 down to 0. In order to summon creatures, you need to play land cards which roughly act as a clock so that as the game progresses, players can summon stronger creatures.

Presently, the rules for the game I have programmed are as follows.

  1. Each player starts with a 40 card deck made up of lands and creatures, 20 life points, and an empty field, hand and discard pile.
  2. Both players shuffle and draw 7 cards from their respective decks.
  3. During each players turn they can play land and summon creatures whose costs total up to the number of lands the player has.
  4. Players can attack the opponent with the creatures they summon, and the attacking player’s opponent can block with one of their creatures. The creature with lower power will be destroyed (in case of a tie, both are destroyed).
  5. Each creature can only block once per turn, and only if they didn’t attack during their controller’s last turn.
  6. If a creature’s attack is unblocked, the player it’s attacking loses life equal to its power.
  7. If a players life points hit zero, they lose.

After a lot of testing, I have a fully functioning game. The code here is a text based version of the game I described that runs in Python 3. Typing quit on your turn’s prompt screen will quit the program.

https://github.com/nickhattwick/PlayingWithCards/blob/master/gamep3.py


Next Steps

From here there a few ways that I want to improve and continue my project. One thing I’d like to do eventually is make the game more complex and closer to Magic. Creatures in Magic have various aspects such as cost, power, toughness, and name, as well as special abilities unique to each creature. In my version, the first for variables are represented by a single number and there are no special effects that manipulate the game outside of the basic rules described above. The next step I want to do to improve the game’s complexity is to make some spell cards which will do various things such as destroying a creature, drawing extra cards, or powering up a creature. I’ve started to work on that, though adding complexity to the game is probably not the next step in the project.

Before that, I’d like to start doing some basic machine learning. This is the part of the project I’m most nervous and excited about as it’s something that I’ve been trying to learn about for awhile and one of the main topics I hoped to work on in this mentorship program. I’ve tried watching tutorials on machine learning and data science in the past, but realized that I was lacking the mathematical knowledge I needed to make use of it. Even if data science could show me shortcuts for different equations and algorithms, it wouldn’t do a lot of good if I didn’t understand the equations and what I was using them for.

One of the last things I was working on before the mentorship program started  was going through statistics tutorials and starting to work on calculus with MIT’s Open Course Ware and I feel prepared to tackle machine learning again. The project I’ve done so far is roughly the extend of my current knowledge of programming, so I’m diving into new territory one way or another. So, the next step is to set the stage to start testing out some machine learning algorithms, which means that I’ll be reworking the code in my current game to be more versatile. At present, the game is made so that a player plays against computer that uses set moves. While I’m pleased with the game, the computer can’t test out and see the effects of them.

My mentor suggested we use “controllers” and keep the basic game code the same. The game logic will be a program that will call out to a player controller or a computer controller for it’s input. This will help to clean the program up as well as allow for computer versus computer matches which will be essential for having the computer get in a lot of games and test data. Currently, I’m working separating the code out of one file. For the future if the main game imports a deck from another file it will open up a lot more possibilities. For now, I’m restructuring the game I have to be ready for the next step. I am currently working on that in here.

https://github.com/nickhattwick/PlayingWithCards/tree/master/splittest

Overall, things are going well. I’m getting in to new territory, but learning is why I applied for this program. I owe a huge thanks to my mentor, Nik, for taking the time to help me on this project, showing me better way of doing things, being willing to learn these new topics with me, and teaching me a lot more about Python. I’ve already learned a lot from this program and look forward to learning.

 

 

Leave a Reply

Trending

Discover more from NikCreate

Subscribe now to keep reading and get access to the full archive.

Continue reading