Friday, January 25, 2008

Python

In this column I’m going to get a little bit technical. To some of you this column will feel like you are trying to swim across the Atlantic Ocean, while others will consider this just a dip in the kiddie pool.

One reader sent me a version of Magic that worked remarkably well. You play against another player over the Internet. It has 80 cards and even includes complex cards like Goblin Sharpshooter (untaps when a creature goes into the graveyard) and Doran, the Siege Tower (assigns combat damage according to toughness).

I checked out the card code and it was short and elegant, it is 100% times better than the way that I currently program cards. The language that he chose was Python. Python is often related to Perl and is sort of a duct-tape, do-anything type of language. Python lets you do slick tricks like having an array of methods. (Yes you can do that in other languages, but Python makes it easy.)

Python, like many scripting languages, is a loosely typed. That means that an object can hold a string, integer, function, method or a class. You don’t have to declare an objects type. In Python you would write “a = 10” and in other languages you would write “int a = 10”, see the difference? C, C++, Java, and many others are strongly-typed languages, an int is always int, it cannot be anything else. Python also doesn’t use semicolons which I find refreshing.

The implementation of Doran, the Siege Tower was especially short considering that it does something very complicated. In the Python, Doran replaces the method for combat damage. It sounds simple, but that is very hard, if not impossible, to do in other languages. This way of programming Doran is powerful because the combat code doesn’t need to know about Doran and yet Doran and still affect combat.

If I had to currently program Doran, the combat code would have to check if Doran was in play. This way of programming is adequate but the combat code can get very messy with a large number of cards. The Python way of programming Doran didn’t change the combat source code at all so the combat code could still be simple and easy to read.

I am just starting to learn Python but it isn’t perfect. There are no private variables which strikes me as very peculiar. There are no interfaces, empty classes with only method signatures, like there are in Java and C#.

All things considered, I like Python better than Java and I plan to use Python in my future programming projects. Currently I feel like I’m programming on ice when I use Python because I am so familiar with Java. Python still feels slippery to me, but in time I will get used to it. I believe that Python’s fast-and-easy feel is immensely flexible and powerful.

I am also intrigued by Python’s claims of shorter code versus Java. Shorter code to me means less debugging (there are fewer lines of code that could be buggy) and less writing time (which means I can write more in the same amount of time). And although I have enjoyed my marathon sessions of 4 hours of programming just to add 12 new cards, I would like to be a little more efficient. Hopefully Python will enable me to actually write code that other people can understand instead of the messy code that currently inhabits MTG Forge.

If you actually made it to the end of this column, you are doing pretty good. :) What other blog talks about Doran, the Siege Tower and loosely typed programming languages?

19 comments:

  1. Can this reader please stand up and take a bow?

    ReplyDelete
  2. Dynamic typing is great when you want to quickly prototype new ideas - and it also let's you test small parts of your design independently from other code. The problem with it (from a programmer perspective) is that conventions are purely programmer defined, so if you're looking at new code, often times it's hard to follow what's going on since you're not exactly sure what values a variable could take on (this is where good commenting is essential).
    Also, you don't need private variables in python because you can later decide to change the attribute access if you have the need (for example, if you want to restrict access, or cache the value, or even compute it from scratch everytime).

    ReplyDelete
  3. The array of methods pattern is a very powerful paradigm to work with, one that works very well for a programming challenge that MtG poses. While it is powerful, it requires you to think through your implementation a bit differently then it would with Java. This was a pattern I had used when thinking through the architecture of an implementation of MtG with an AI. But, while it exists in Java, it was not core and was added on, which caused me to struggle with the implementation. It seemed like the language forced to much overhead to achieve the desired results. Python has it natively, and I just haven't had to time to explore it to see how it would work with MtG with an AI. It would certainly be an interesting challenge to add an AI to the program you mention. What is the name of this python implementation?

    ReplyDelete
  4. The program I am looking at, I am just reading through the card code. I like how his Python Magic cards were so short. In Java everything has to be very long, I guess because of the strong typing and lack of powerful primitives (no lists, dictionaries).

    I like how my Java Magic program runs, but it takes a lot of "framework" code in order to get everything running. I know reading someone else's Python code may be hard, but reading my messy Java code is hard also.

    I am trying to learn Python and PyGame together. I am also concentrating on Python's idioms and trying to stay away from my Java tendacies (no setters and getters). Although I will still probably put 95 percent of my Python code is classes.

    Supposedly Python will also let me distribute a pure executable, which might help out my Windows uers. Although no one has every complained that they couldn't run my program because they didn't have Java.

    ReplyDelete
  5. The name of the Python implementation was just named "MTG", he hasn't decided on a name yet. He plans to release it under the GNU open source license.

    ReplyDelete
  6. I am actually personally interested in a python version as it my run on my Nokia N800 tablet. Currently, Java isn't supported and, oh, how I would like to play a game (against the computer) on something that can fit in my pocket. Granted, this python code, which you mention here doesn't have an AI, but it could be added.
    You mention pygame, so the interface isn't defined by pre-cut widgets, per-say, like say, swing for Java?

    ReplyDelete
  7. Any word on the python implementation? I am very interested in trying it out and possibly contributing. As a .NET/Java programmer I find that using Python for MTG may work out much better. "Traditional" languages (C#, Java, C++) require more knowledge of the game/model upfront. This is fine for regular card games where the rules are defined at the start of the game. For MTG, the rules change with every card that is played. Python lends itself to that situation. Anyways, it will be great to see any more details about this new effort.

    ReplyDelete
  8. I'll put the source up soon - I just need to come up with a new name. Any ideas?

    ReplyDelete
  9. Just to throw one out that doesn't get any hits on Google: pyMageForge

    ReplyDelete
  10. Currently the Python implementation I know of only lets you play against another human player. I want to personally code MTG Forge 2.0 in Python.

    The Python gui is built on PyGame and PGU. Everything is drawn on the screen like a videogame.

    As for a name, you could try PyMagic, Python Magic, pMagic, Python Mage, I'm not really sure. MTG Forge is like the lame-est name ever, so I probably shouln't be trying to help anyone else name there program, lol.

    ReplyDelete
  11. This is great news! I understand it is intended for two human opponents but am anxious to see if it would work on my tablet as is. Network play is the type of thing that the internet tablet would be good for. Of course, I am sure that the user interface would have to be adapted for the smaller screen, but we all need a little project....

    ReplyDelete
  12. I'd rather not use a prefix of py- in the name, since I've never liked that practice.

    I've come up with some names -

    Spellfire - unfortunately the name of a discontinued TSR trading card game
    Moxfire - too similar to firemox though
    Pentaclus
    Magia
    Magicus
    MagiaSmede - magic (italian) forge (danish)
    Incantis

    I'm leaning toward Magicus or Incantis. All the english words are taken, so I have to bastardize some foreign words.

    ReplyDelete
  13. Of those you listed, Magicus and Incantis are the better choices. Both would work well, but for a personal choice I kind of like Incantis. Its different, fresh even.

    ReplyDelete
  14. Yeah, I'm leaning toward Incantis, although I can't decided between Incantis or Incantus. It gives the feeling of casting spells. Now I just have to setup a project page somewhere (I'm leaning toward google code since all my code is already in a subversion repository). I bet soon after I put it up I'm going to be smacked down by WotC legal (soon being a couple of weeks).

    Sorry forge, for hijacking this comment section to talk about my program.

    ReplyDelete
  15. Not that I didn't have anything better to do, I searched for the differences between tis and tus. The only real understandable reference was Spanish use of tus as being masculine form. So, a vote for Incantus.

    ReplyDelete
  16. No problem, hijack all you want. I would love to have a webpage and forums where people could talk about Magic programs and just Magic in general.

    ReplyDelete
  17. I saw the Incantus video on Youtube. The interface looks fantastic! Is the code available?

    ReplyDelete
  18. Aw, and my company blocks youtube. Any site that has a screenshot? I would love to see it!

    ReplyDelete
  19. hey if you will do with python, let me know, because I want to help.

    ReplyDelete

Note: Only a member of this blog may post a comment.