Friday, August 29, 2008

Random Rules Questions

Occasionally I have random questions about the rules. I could post my questions to a forum but I have you, my faithful legion of readers, to answer my questions. And by the way, I hate optional abilities that begin with “you may”, because it requires additional programming.

1. Reya Dawnbringer – has a triggered ability that says, “At the beginning of your upkeep you may return target creature card from your graveyard to play.” Since this ability is optional, when do you decide if you want to or not? Do you decide at the beginning of your upkeep whether or not to use Reya’s ability?

2. Angel's Feather says “Whenever a player plays a white spell, you may gain 1 life.” When do you decide if you want to use this ability or not? Do you have only one window of opportunity to use this ability once a spell is on the stack?

3. Ok this isn’t strictly a rules question, but what is Heart of Light (10th Edition) good for? Heart of Light is a creature enchantment that says, “Prevent all damage that would be dealt to and dealt by enchanted creature.” You can target one of your creatures to make him an all-star blocker and I guess you can target one your opponent’s creatures that you couldn’t handle. Is this card a variation of Pacifism? This card just confuses me.

4. What happens when Orcish Artillery’s ability goes on the stack and then your opponent steals Orcish Artillery. Do you or your opponent receive 3 damage? (In case you don’t know, Orcish Artillery’s ability says, “tap: Deal 2 damage to target creature or player and 3 damage to you.”)

Wednesday, August 27, 2008

Stack – Cards versus Spells

Sometimes thinking about a Magic card really confuses me. Generally a card is a spell right? And if that card is a creature, that spell generates a creature. The confusing part is that the card represents the spell for the creature and the creature itself. This isn’t confusing if you have physical card, but it does confuse me, a Magic programmer. And cards don’t go on the stack, only spells do.

The stack is the only zone that doesn’t hold cards. Technically the stack only holds spells and abilities. MTG Forge version 1 tries to stay true to the rules, no Card objects can be added to the stack, only SpellAbilities. (All spells and abilities are represented by a SpellAbility object. This object implements the functionality of the card. Using Wrath of God as an example, its SpellAbility would destroy all creatures.)

Since Magic is all about moving cards, it is difficult when you play a card to move it from your hand to the stack, since the stack doesn’t hold cards. I decided that I would let the stack hold cards and each Card object would hold a SpellAbility object. All I am really doing is swapping Card and SpellAbility. Currently only SpellAbility objects are added to the stack but and they hold a reference to the card that they represent.

Allowing the stack to hold cards makes moving cards much, much easier. The stack is a zone but you had to treat it as a special exception since it didn’t hold cards. Now I can write a simple moveCard() function that will work all the time.

moveCard(Card card, String zoneTo, String zoneFrom)

p.s.
Incantus, another Magic program, adheres strictly to the rules and each card has characterstistics. Below is taken from Magic’s comprehensive rules.

201.2. An object’s characteristics are name, mana cost, color, type, subtype, supertype, expansion symbol, rules text, abilities, power, and toughness.

Tuesday, August 26, 2008

Tortuga

I'm not sure where to post this image, so I'll just stick it here. Tortuga is a card that I made up and yes it is very unbalanced and too powerful.

Basically Tortuga is the Library of Alexandria (tap: draw a card. Play this ability only if you have 7 cards in your hand.) with another ability that says, "tap: Discard a card, then draw a card." I thought it was fun to discard and then draw. This ability lets you draw cards for free if you hand is empty.

Monday, August 25, 2008

Telengard

I wasn't sure how to get up with you, Telengard. I saw your post about programming the AI for Dreamblade. I played the computer demo for Dreamblade and it was very fun. Do you have a playable version yet? You can post a comment or e-mail me (mtgrares yahoo com).

Shared Zones and Incorrect Rules

The good news is that I’m making real progress on MTG Forge version 2. I was programming the code for the different zones and I came to the conclusion that Magic is all about moving cards. You draw a card. You play a card. You destroy a creature. Practically everything you do in Magic (and any card game) involves moving cards.

My other great realization was concerning zones. Some zones like “in play” are shared while others like your hand and graveyard are not shared. While I like to keep as close to the rules as I can, I decided to make all zones shared, including your hand and graveyard.

Cards in your hand will be cards that you control (because you can steal your opponent’s cards occasionally) while all cards in your graveyard will be cards that you own. I was very tired of writing code like “move card from Player’s Hand to Player’s Graveyard”. Now I can just write “move card from Hand to Graveyard.” While this seems like a minor issue, MTG Forge is full of statements that just move cards. If I can make those statements shorter, everyone is happier.

p.s.
I might really cheat and write “move card to Graveyard” and let the program find the zone that the card is currently in.

Wednesday, August 20, 2008

AI and Combat

As I work on MTG Forge version 2, I keep thinking about the artificial intelligence (AI). How can I, a guy who isn’t an expert in AI, write and program a better AI? The AI could attack or block better, but how?

One of the key phases in Magic is creature combat but making any combat choice is complicated. The cards you see in play only make up a small part of the puzzle. The AI can only see the creatures in play, so it has to make all of its decisions blindfolded. It doesn’t have years of experience or intuition to rely on. (Technically the AI can also see the cards in your hand and deck but trying to use that information is extremely complicated.)

So this leads me to the question, “How good can the AI be if it only considers the board?” The AI could comprehensively consider all the creatures in play and determine how to maximize the situation. Basically the AI would have function that evaluated the board and it would try to maximize it, but would this really help the AI play better?

The next level of AI would consider the board position and the cards in his hand, thus enabling him to effectively use Giant Growth and Terror. This type of AI would be a definite improvement over its primitive ancestor. The AI would seem more “alive” and would occasionally make a surprising play. (One of my goals is to have the AI surprise the player because it makes the game more interesting.)

Obviously there could be another level of AI that also considered the type of deck it was using and the type of deck it was playing against. I’m not sure if I’ll get to this level of sophistication, but I will keep it in mind.

Articles about the AI generate the most comments, so feel free to post your opinion. And if you are an expert in AI or write AI for videogames, please e-mail me.

--Forge (mtgrares yahoo com)

Tuesday, August 19, 2008

Beta Version

I posted a new beta version of MTG Forge here. Hopefully it doesn't have any major bugs, but if it does just leave a comment. Over 100 new cards were added pumping the total up to a total of 849 cards.

The "download card pictures" option should work and I love the new layout that Nantuko submitted, it makes MTG Forge look better. (Instead of showing colored boxes for the cards, the card images are scaled down and shown.) Also thanks to another reader that sent me the code to make rotating images faster. Thanks to Rob Cashwalker for submitting 50 or so cards also.

Monday, August 18, 2008

Inspiration and Source Code

I am very impressed whenever someone leaves a comment like “I liked MTG Forge so much that I am working on my own implementation of Star Wars.” Hopefully that person spawns another computer programming project and the whole community is better for it. I always considered imitation the sincerest form of flattery. I would love to play computer simulations of other trading card games.

I’m also glad that MTG Forge’s source code is inspirational. The source code is almost 1 MB in size with CardFactory taking up 50% of the total length. (CardFactory is so long because it holds all of the code for the cards.) Some parts of the code are easy to understand like the Card object. Other parts of the program are a little harder to figure out, like Input which processes all of the mouse events, like clicking on a card. Unfortunately the source code is undocumented and only has a few comments here and there. Thankfully MTG Forge can be updated, like the new user interface if you downloaded the beta version.

The software architecture was literally built from the ground up. I had a vague goal in mind, but not a specific plan. I did build two or three previous versions that had too many problems so I had to start over. Other than using global variables, MTG Forge’s architecture is pretty good. I tried to write flexible code that could be changed later, like writing a Combat class so I could add a second combat phase when planeswalkers were introduced.

As always keep programming,
Forge

Friday, August 15, 2008

Graphic Changes

I love this screenshot that Nantuko's submitted. It looks great and it is only a small change. I've gotten really stuck trying to write a decent user interface. This screenshot is fabulous, so I'm going to use that code so I can get into the nitty-gritty of programming cards and other Magic rules like phases and combat.

(By the way, most programmers *hate* writing user interfaces. They have to be idiot proof for the end-user and they also have to be *pretty*. Most programmers are almost color blind and they think gray is a pretty color. Needless to say, much of MTG Forge is gray.)

Thursday, August 14, 2008

New Beta Version

I just posted a new beta version of MTG Forge. (It doesn't download all of the new card pictures yet.) It has about 30 new cards and an updated user interface (thanks to Nantuko). You can see a screenshot of the new user interface here. Also thanks to Rob Cashwalker for the cards he submitted.

Download - http://code.google.com/p/cardforge/downloads/list

Wednesday, August 13, 2008

Error Checking

Most computer programs have some sort of error checking. In the world of computers, errors are a fact of life. Error checking is complicated because you have to guess where errors might occur.

MTG Forge does some very simple error checking, sometimes also called “sanity check.” This type of error checking just checks for the most outrageous types of errors, like if a creature is being destroyed but that creature is not found. If an error is found, a short message is shown to the user. The user can continue playing MTG Forge if the error is small or the user can restart. Restarting doesn’t fix all of MTG Forge’s errors but it will reset the program. The idea is that hopefully the error was a fluke and it won’t happen again for awhile.

Every computer program could have more error checking. In some computer programs the error checking can be 50 percent of the overall code. Many errors occur when the program is reading or writing a file. File operations are complicated and can generate many different types of errors. File operations may work correctly when the file is small but fail once the file gets bigger.

MTG Forge has an architectural error that causes it to slow down after a few games. I didn’t discover this error until the program was almost finished. Some errors, like this one, are hard to find and impossible to fix. This mistake is like if the foundation of a house was cracked, the foundation is the one of the most important parts of a house. All computer programmers try to write error free code, but errors are bound to occur.

Monday, August 11, 2008

Rolling Your Own 2

I’m making good progress making my own graphics library that will display the user interface. I can show multiple cards and move them. I feel like I am just creating what has been done hundreds of times since videogames were invented, but that’s ok. But that is the problem is writing your own code or “rolling your own.”

I’m still using Java of course. And I am just writing a little bit of code on top of the existing Java interface. The part of Java that creates user interfaces is called “Swing”, I don’t know why they named it that. So all I have to do is to generate the card picture and let Swing do all of the hard work of actually drawing it on the screen.

Each card is considered to be a sprite and when you click on a card, you are moving a sprite. All of the sprites are held in a “Sprite List” which is similar to an array. When the user click on a card, the xy coordinates are given to the sprite list and if a card is in that area, the user will be able to move that card. Everything is all pretty simple and straightforward so far.

For the mana pool on the left side of the screen, I may cheat and try to let Swing do all the work. It has a label class that can show a picture and some text, which would let me easily create the mana pool.

Wednesday, August 6, 2008

Rolling Your Own

When programming you either have the choice of using someone else’s code or writing your own. In this example, I will be talking about a graphic library that would help me write the user interface. I can either use someone else’s or try to write my own.

The downside of using someone else’s code is that you have to practically memorize the documentation in order to get anything done. You also have to understand the logic of how to use the code. Just reading the documentation alone rarely answers all of your questions, just like the encyclopedia doesn’t explain grammar or how to write a book.

Writing your own code library is often called “rolling your own.” The user interface for MTG Forge is going to be very simple. User’s can move cards. Life points and the mana pool will be shown for both players. These requirements are very, very simple compared with most videogames. The benefits of “rolling your own” is that you don’t have to read any confusing documentation and that you often get things working up and running more quickly.

After trying a simple graphics library, I gave up and decided to roll my own. All I have to do is read a card image, reduce its size, draw a white rectangle, and draw the card name. The attack and defense as well as the damage and assigned combat damage will also be shown on each card. (Sometimes you want to know how much damage is assigned to your creature during combat.) No animation has to be done and everything else is pretty straightforward.

This card picture is taken from MTG Forge’s version 2 user interface. The red number represents the damage and the blue number represents the assigned combat damage.

Monday, August 4, 2008

The Value of a Finished Product

Work on MTG Forge 2.0 is going very slowly, but version 1 took me a while to write. With version 1, I would write some small snippet of code at night, 100 to 200 lines. Eventually after a couple of months, 6 or so, the whole project started to “click” and everything started working. It takes faith at the beginning to know that you will eventually get to the end.

I have this idea in my head and hopefully I can explain it. Let’s say that version 1 only got 90% done. And let’s say that the project only had a text user interface (like DOS) but everything else worked. How interesting would that be? Most users would find the project practically unusable. The value of that last 10 percent, whatever it actually is, essentially makes the rest of the project usable.

The same thing applies to a movie. If a movie was missing the ending, the whole movie is practically worthless. The finished product is everything.

I’m sure everybody has started a project but never finished it. Usually that project isn’t worth anything until it is actually completed. The same thing applies to computer programming projects like MTG Forge. Even if I get 50 percent done, it really isn’t worth much. Only the shrink-wrapped, final product is useful.

Friday, August 1, 2008

What do you think of Magic Online?

I used to enjoy Magic Online but I became tired of it. I didn’t have the expensive rares needed to build the decks that I wanted. I didn’t like people disconnecting when they were losing. And I didn’t like playing against pro tour decks in the casual room. I just wanted to play a good game of Magic, nothing more nothing less.

People are rallying against rares and have begun playing Peasant Magic online. Peasant Magic includes just commons. And while some people just want to win, win, win others just want to enjoy a good game against a fair opponent.

MTG Forge lets me experience the best side of Magic, casual competition. If I win, I feel good and if I lose, I laugh because the computer played the exact card he needed. And while it is insane for the computer to play Wrath of God on two consecutive turns, it does make me smile.

p.s.
All games are influenced by the people you play with. You can play the dumbest game with your friends and still have a great time.

p.s.s.
I never drafted online because I knew that I wasn’t very good. I didn’t want to waste my 15 dollars.

So what do you think of Magic Online?