Monday, April 28, 2008

Blue Moon – Human vs AI

I’m interested in trading card games (TCG) that you can play on the computer. I wish somewhere on the Internet there was a complete list, but I haven’t been able to locate it. So I’ll mention any TCG games that I find.

There is a computer implementation of the card game Blue Moon. In the computer version you play against the AI and you each use one of the 8 pre-built decks. Unfortunately the computer version doesn’t let you let you construct your own deck, but each deck has different strengths and weaknesses.

The Blue Moon program also has all of the beautiful card art included and is even endorsed by Blue Moon’s creator. While learning a new set of rules can be irritating and confusing, Blue Moon at least has an “assist” feature that tells you what you should do, so you are never stuck. You may also want to check out author's homepage which includes the source code, its in C or C++ I think.

Blue Moon Download
1. PDF Rules
2. GTK Library – a graphic library so it can run on Windows
3. Blue Moon for Windows

Friday, April 25, 2008

What is Computer Programming?

I know this question is totally obvious to those who actually program, but to others it is a mystery. Personally I don’t know how to change the oil on my car, but I can make a computer play Magic, lol.

Computer programming is in many ways very tedious. Every dot, character, and bracket has to be perfect otherwise the program won’t compile. But the “fun” doesn’t stop there. Even when the program compiles, it might not do what you want it to do. You get to have even more “fun” when you discover a bug and try to fix it.

Fixing a computer bug or error takes three steps. One, to fix a bug you have to be able to constantly reproduce it, “Does Giant Growth let you target a land?” Two, then you change the code hopefully fixing the problem. Three, you test you code to see if it is fixed. If the bug isn’t fixed, and that is often the case, you change more code and then try again. Occasionally a programmer will skip the last step for some crazy reason that makes sense now but not later, “I’m sure it is fixed so I don’t need to test it.” Sometimes bug fixing can be very rewarding and other times it is totally frustrating.

The best way to know what computer programming really is, is to look at some code. The code below was written in about an hour using Pythin (a programming language like Java) and the graphic library PyGame and PGU. (A graphic library lets you draw on the screen using sprites like in a videogame.) The code just shows a dialog, sometimes called a popup box, that displays a short message and lets the user select yes or no. The reason that the code is so long is that there are several details to take care of, like what if the user closes the dialog? This is interpreted as selecting the no button.

The code below would look different if it were written in another computer language like Java. This code is slightly different since it is drawing on the screen like a videogame. A typical dialog box would use the Windows or Macintosh library to draw itself. This dialog has to draw itself on top of other components that are being drawn. A dialog box in a videogame is very different than a dialog in Word or Internet Explorer.

I view computer programming like doing a crossword puzzle. Some people love doing them, but personally I hate them. The same goes for programming, I love it, but others hate it. I enjoy solving a million tiny problems when I am programming. It probably helps that I have a hint of obsessive compulsion with a dash of perfectionism.

Many computer programmers are also shy and socially awkward, watch the TV show “Beauty and the Geek”. Some programmers play music instruments. Trying to mind-meld with the computer is difficult, so most programmers have highly developed concentration skills. Sometimes when I am reading a magazine article, I forget what day it is. Many, and probably all, programmers love Star Trek and in particular Spock. Did you ever think that Data was just a different form of Spock? In closing, programming is hard, lol :)
def showYesNoDialog(stringTitle, stringMessage, yes=None, no=None):
doc = gui.Document(width=300, align=-1)
doc.block(align=0)
doc.add(Label(stringMessage))

yesButton = gui.button.Button("Yes")
noButton = gui.button.Button("No")

doc.block(align=0)
doc.add(yesButton)
doc.add(Spacer(50,10))
doc.add(noButton)

d = Dialog(Label(stringTitle), doc)

#this looks funny
#clickYes and clickNo have to have an argument,
#otherwise there will be an error
#also, have to reset CLOSE function otherwise
#clickNo will always be called
def clickYes(nothing):
d.connect(gui.CLOSE, lambda(x) : None, None)
d.close()
if yes is not None:
yes()

def clickNo(nothing):
d.connect(gui.CLOSE, lambda(x) : None, None)
d.close()
if no is not None:
no()

yesButton.connect(gui.CLICK, clickYes, None)
noButton .connect(gui.CLICK, clickNo, None)

#closing the dialog executes the "no" fuction
d.connect(gui.CLOSE, clickNo, None)

#show dialog
d.open()

Wednesday, April 23, 2008

Read This

Sometimes I might post too many articles. If you want to read about programming hybrid mana costs in Python checkout Programming Mana Cost 2 and 3.

User Interface Design

Designing a good user interface (UI) is difficult. The overall goal of the UI is that it is easy to use. Unfortunately defining easy to use is very difficult. One guy’s coffee is another man’s latte, i.e. people have different ideas about what is easy to use.

The UI also needs to be functional. A basic UI for Magic should let the user click on a card, target a player, and view all graveyards. This is my no means a complete list. More possible operations include selecting a color, selecting which type of mana a land should produce, choosing phases stops, and various prompts like “Do you want to pay 2 life?”.

The UI for Magic is easy in the sense that not much is going on, some people would call that boring :) A card game’s UI tends to be very simple compared with 3D shooters and build-conquer games like Starcraft. In my UI the user can click on a card, a player, or an icon. Clicking on an icon lets you select a mana color or view your graveyard. Obviously clicking on a card produces different effects, like if you clicked on a land or a creature. The UI doesn’t do any processing when the mouse is clicked but just passes the information onto another part of the program. Currently all mouse clicks are processed by the Input class which uses the State Pattern.

I’m designing a UI using Python and PyGame, and yes it will be a lot better than the current one. I was trying to decide what are the most common types of prompts that are needed during a Magic game. Well, tutor effects like Demonic Tutor are fairly common so we need to let the user look at a large number of cards and then select one. Technically tutors allow you not to select any card, so sometimes choosing a card is optional. I won’t bore you with any more details, but my UI will initially support the 6 types of prompts below.

A good UI is totally separate from the rest of the project, which makes updating either the UI or the rest of the project easier. The more dependences that a piece of code has, the harder it is to update without breaking something else. Dependencies are necessary but you should try to minimize them.

Types of Prompts – all prompts also will display a message
1. Just show the user an informative message and an OK button, “You play first”
2. Message with Yes and No buttons, “Do you want to play first?”
3. Reveal one card, OK button
4. Show multiple cards – no selection, the user just looks, OK button
5. Show multiple cards – the user must choose one card, OK button
6. Show multiple cards – the user may optionally choose a card, OK and Cancel button

p.s.
User interfaces take hours to design and minutes to criticize. Separation is good from a design point of view, but how many videogames have more than one UI?

Monday, April 21, 2008

Magic for Beginners

I assume 90% of MTG Forge users already know how to play Magic, while this may be true, I’m still ignoring the other 10%. The best way to learn Magic is to download the Windows Demo. It will show you how to play the game. Anatomy of a Magic Card and the Rulebook will help solidify your understanding after you get the basics down. Tactical Protocol talks about some basic strategy and the Magic Academy should be all the reading a newbie needs for a couple of months.

And for a nice overview of Magic: The Gathering, check out Wikipedia.

Windows Demo 85 MB
Anatomy of a Magic Card
PDF Rulebook
Tactical Protocol – how to get the maximum usage out of your cards
Magic Academy – a set of 50 articles for the beginner, just read thorough a few of the articles and they should help out your game.

For questions about specific cards:
Faqs for all Magic Sets
Searchable Rules - If I have a question about a card, I look here first, although it doesn’t cover newer cards.

Shadowmoor Visual Spoiler

You can look at all of the card art on one webpage.

Visual Spoilers for all sets
Shadowmoor Visual Spoiler

Programming Mana Cost 3

My previous pseudocode looks correct but there were a still a few errors I had to fix. First, you have to internally reverse the mana cost because otherwise the colorless part will “eat” the colored mana the rest of the cost needs. For example, 2WW needs to be reversed to WW2. So when you add mana, the white mana part is checked first. In other words, when you pay W toward the cost 2WW the result should be 2W not 1WW. And when you display the cost to the user, the output has to be reversed again.

Below I will example how to use my Python code that handles mana costs. Some examples of valid mana costs are
2
1 W
X U
GW
RB RB
2/G 2/G 2/G

Each part is separated with a space, which just makes it easier to split up. GW means you can pay G or W and 2/G means that you can pay either 2 or G. The class ManaCost is the class that you will be using. ManaCost is low level and a graphical component is supposed to use ManaCost and allow the user to select the appropriate land or mana. The graphical component would also tap the land if the mana was needed. An example of how to use ManaCost is given below.

c = ManaCost()
c.setManaCost(“2 W W”)
c.isNeeded(“W”) #true
c.addMana(“W”)
print c.toString() #2 W
c.addMana(“W”)
c.addMana(“U”)
c.addMana(“U”)
print c.isPaid() #true

Feel free to use this in your own projects. This has been tested thoroughly comes with a wide variety of test cases: TestManaCost and TestManaPart.

Download Python Code

Friday, April 18, 2008

Programming Mana Cost 2

No picture, some random internal error. Computer programming is all about breaking a big problem into smaller, easier ones. "Divide and Conquer" Skip the first paragraph if you already know about mono-hybrid mana.

Previously I have talked about programming normal mana costs (2WW), but Shadowmoor features new, unusual costs like (2/B), you may pay 2 of any mana or B. Shadowmoor also brings back hybrid costs like GB, you may pay G or B. The reason I mention all of this, is because these new costs mean more programming (which isn’t necessarily a bad thing).

I have done some additional programming and the good news is that I can process all mana costs that Wizards has ever printed, except for the weird snow mana that Coldsnap introduced. I wanted to talk about the logic that my code uses.

Everything came together when I started thinking about dividing the mana cost into smaller parts. So “2WW” would have the parts: 2, W, W. Each individual part would know what kind of mana it needed. So the object ManaPart would have a only a few methods as shown below. The method needsMana() returns true if that mana is needed. The object ManaPart greatly simplifies the task of programming X and hybrid costs.

ManaPart
needsMana(String) : boolean
isPaid() : boolean
toString() : String

But wait!! I haven’t shown you the magic yet. Each ManaPart object also has a method named correctManaPart(String part) that returns either true or false. This method answers the question “Does this object handle this kind of mana?” So the ManaPart that handles X costs always handles the X regardless of the rest of the cost. So I could easily handle snow mana, because it would involve just adding a new ManaPart.

I’m going to try to describe the pseudocode below, because I stink at writing it. TotalManaCost is given a cost like 2WW creates the correct ManaPart for each part. In this example, the parts are “2”, “W”, and “W” so three ManaPart objects are created.

TotalManaCost
setCost(String cost):

Split cost into mana parts
for p in parts:
for m in ManaParts:
If m.correctCost(p)
totalCost.add(create ManaPart)

addMana(String mana):
loop through totalCost to see which ManaPart needs mana


Download Python Code – I’ll explain it in more detail in my next post.

Monday, April 14, 2008

Marvel Trading Card Game

I finally bought this for my PC for the low price of 10 dollars and I had high expectations for it. Trading card videogames don’t come around very often and the VS game engine doesn’t need lands or summoning sickness. Even though the presentation is impressive with little attack icons and changeable wallpaper, the game is not very fun for some reason.

The reason why the final game experience is lacking is because some of the small niceties (yes that is a real word) are left out. One, you can’t forfeit a game. While this is a minor thing, I end up playing through games just so I can close the program. Two, the program waits for the computer to play his cards and overall you do a lot of waiting. MTG Forge takes this to the other extreme and you don’t have to wait on the computer at all.

Three, the deck editor shows you of the cards that you can use as well as all of the cards in your deck. So if I have one copy of “Big Warrior” and I add it to my deck, it still shows up in my list of cards. The deck editor is also hard to use because you have a hand icon instead of a regular mouse arrow. Fourth, when you click on a card you have to play it, you can’t cancel. This is really frustrating for a beginner like me. Finally, this videogame is very picky about the video cards it can use. I bought a new one just to run this game and it still hangs up sometimes.

The single player game is setup so that you can either pick a “Hero” or “Villian” storyline. You start with a basic starter deck and you earn points to buy more cards. Overall this sounds very interesting, but even the beginning matches are hard. Maybe it’s just me, but even with all the cards (I used a cheat) I still had a hard time beating the first few computer opponents. Granted, I am trying to learn the VS rules by playing the game, but that usually works. I remember learning to play Magic through their interactive tutorial. In conclusion, I believe the Marvel/DC VS trading card game is very good and deserves a better videogame than this one.

ps.
This makes me wonder why Shandalar is so playable? Is it because Magic is so good or because it was programmed well, or both?

Friday, April 11, 2008

When is MTG Forge Updated?

I occasionally get emailed this question and although the answer seems blatantly obvious to me, I’ll try to thoroughly answer the question. I release a new version of MTG Forge usually once every month or two. I try to add a few cards and fix a few bugs in each version. A reader sent me 20 or so creatures (thanks!!) so I’ll add then to the next release. I fixed the drafting code, which is currently broken. The wrong picture was downloaded for Imperious Perfect. I actually fix a ton of bugs myself since I play MTG Forge so much. When I play MTG Forge it is called testing :)

I know randomly adding new cards might seem lazy (crazy), but you have to remember that this is only a hobby after all. I’m not sure of any freeware program that has regular updates. I add cards that are interesting to me. I was thinking about adding Chameleon Colossus but I can’t do protection yet, so I wasn’t sure was it worth the effort. Sometimes I get inspired so I’ll add a card like Serra Avatar. I also tend to add high value or popular cards. When Thoughseize was getting so much publicity, it was a simple card so I added it.

Like I have said many times before, MTG Forge can only handle relatively simple cards. No cards that generate mana like Dark Ritual. No land or creature can generate more than 1 mana. A few static and triggered abilities like Glorious Anthem and Reach of Branches. MTG Forge only has around 10 cards with static and triggered abilities. And abilities that trigger on upkeep if they don’t require a target like Juzam Djinn. Counters like +1/+1 are not implemented and no protection either.

MTG Forge 2.0 should allow me to program at least twice the number of cards. Cue the cheesy background music, “Twice the cards, four times the excitement.” Yes you did read that correctly, version 2.0 will be exponentially more fun, you may never buy cardboard again, lol.

Annoying Rares

Rare cards annoy me. It always seems like I could assembled my supreme deck if I just had a few more rare cards. In my mind, each rare card equals $$. I could continue to tell you what I think, but The Ferrett wrote the definitive primer on rares.

In Defense Of Rares

To a lesser extent Mark Rosewater (head card guru) wrote articles explaining card rarity.

Common Courtesy
Rare, but Well Done

One of the reasons why I wrote MTG Forge is because I wanted to use rare, fun cards. So I guess without rares, maybe MTG Forge wouldn’t ever exist, who knows? Much like being mana screwed, rares are a part of Magic.

(Mana screw is a really unnecessary part of the game. You should be able to play any blue card and it would magically become an Island like the Marvel/DC VS card game. I might implement this as an optional feature in MTG Forge.)

Wednesday, April 9, 2008

AI Tricks

I wanted to go over same of the tricks that the AI uses in order to seem smart. Admittedly, the AI is a basic opponent and is hopefully a little smarter than your little brother. (link to AI Good Enough?)

If you are a frequent reader, you will already know the computer’s land is smoothed, to read more see my previous column. The computer’s AI is divided up into three main parts. The general “brain” that pays for card costs, plays a land, and calls all the other routines. Then there is the combat code that handles when to attack and block. Finally, some code is added to each card that tells the computer when it can be played. The AI code in each card also sets the targets.

The AI built into each card accounts for most of the computer’s intelligence. The computer is only as smart as the few lines of code that are added to each card. A card like Shock is difficult for the computer to play effectively because of the wide variety of game situations. The AI for Shock basically targets a 2/2 flyer or the human player if it would kill him. If the computer’s life is under 7 (an arbitrary number), the computer will target a random creature that it can kill. A card like Ancestral Recall (draw three cards) is much easier for the computer to use well.

Wrath of God is a very situational card that the computer has a hard time evaluating. The AI code will play Wrath of God if he would destroy 2 more human creatures than computer creatures, or if the computer is under 7 life. The AI won’t play Wrath if you, the human player, do not have any creatures in play. The computer can win more easily with straightforward cards like Epic Proportions (enchantment, target creature gets +5/+5 and trample.)

Previously, I have also talked about how playing cards during the Main1 or Main2 phase helps the AI. (link to AI and Phases)

I’m not sure if anyone is really interested, but the “brain” is the Java class ComputerAI_General. ComputerUtil_Attack2 and ComputerUtil_Block2 handle combat. The reason the “2” is there is because they are the second version. The AI card code is handled by the methods canPlayAI() and chooseTargetAI(). Every spell and ability is represented by the class SpellAbility. Each SpellAbility class has the methods canPlayAI() and chooseTargetAI(). Wrath of God doesn’t have any targets, so chooseTargetAI() is not needed.


//Wrath of God, taken from CardFactory
public boolean canPlayAI()
{
CardList human = new CardList
(AllZone.Human_Play.getCards());

CardList computer = new CardList
(AllZone.Computer_Play.getCards());


human = human.getType("Creature");
computer = computer.getType("Creature");

/*the computer will at least destroy 2 more human creatures*/


return computer.size() < human.size()-1 ||

(AllZone.Computer_Life.getLife() < 7
&& !human.isEmpty());


}//end canPlayAI()

Monday, April 7, 2008

New Version

New version of MTG Forge 04-2008. 27 new cards, simple creatures with one or two keywords. Thanks to Rob Cashwalker for contributing these creatures and a whole bunch of others too.

Download:
Windows Installer - Nice Windows installer

Zip File - Java jar file for Mac, Linux, etc.. Also includes Windows exe in case you had problems with the installer

(To save the decks that you created, keep a copy of the file “all-decks2”.)

I thought everyone could see my e-mail on the top of my blog, but I guess only I can. In case someone wants to e-mail me, Forge, mtgrares yahoo com

AI Fix

I fixed this in the most recent version of MTG Forge, but the computer AI used to stumble when playing mono-colored decks. The AI seemed slow and would actually play the same land twice, the cards ID numbers were the same. The problem was in the code that smoothed the computer’s land, also known as mana threading. The code presumed a two color deck. I fixed the code and now it will worked with 1,2,3,4 or 5 color decks. With a 2 color deck the computer’s mana is threaded like 1,2,1,2,1,2.

The computer’s land is threaded in a very particular order. The computer’s opening hand has no lands and then draws a land for the next 5 turns. You may be asking yourself, why? The idea is to increase the number of spells the computer has an opportunity to play. On the computer’s first turn he has 7 spells and 1 land. So hopefully he has a one mana spell in his hand. On the computer’s second turn, he draws a land and plays it. So now he has 6 or 7 spells in his hand, depending if he played a spell. The computer’s hand is all spells, increasing the “quality” of cards he has. Being technical, I guess the computer has “virtual card advantage,” but I don’t really know, lol.

The computer draws an additional land on his 8th and 11th turn. The computer will never have more than 7 lands in play at a time, assuming he doesn’t play cards that fetch lands. Threading the computer’s land like Forest, Mountain, Forest, Mountain, gives it an advantage. Assuming a 2 color deck, the computer can play any double-costed spell like RR as early as turn 3 and always by turn 4.

Wednesday, April 2, 2008

Firemox – Human vs Human

In case you didn’t know, you can play Magic against another human player over the Internet using the rules. The program is named Firemox (formerly the Magic-Project). It is Java based and runs on Windows, Mac, Linux, etc… It has thousands of cards and all 5 Planeswalkers. New cards are being added all the time and you can add your own cards by using XML.

Stinky Previews

What’s that awful smell? Oh, its preview week. Yes it is that week where writers paid by Wizards preview a card from the new set and then tells the world how great it is. Now, I like previews as much as the next guy and I’m already salivating over the upcoming Iron Man movie (eat your heart out x-fans), but preview week on Wizard’s website is just pitiful.

The writer has to artificially incorporate the new card without making the whole thing seem like a commercial. The “good stuff” is left out, meaning that any new insight into the world of Magic is thrown onto the cutting room floor. After the new set of cards is released, the hot preview card seems to cool down, although Wizards did a good job predicting that Countryside Crusher and Chameleon Colossus would be big hits (at least in the casual market). Preview week maximizes the hype and minimizes the info.

Even my favorite StarCityGames writer The Ferrett succumbed to preview week and had to write about how Stonehewer Giant was so great in multiplayer Magic. Stonehewer had his 5 minutes of fame and then was never heard from again. Rest in multiplayer peace Stonehewer.

Even though I am bashing Wizards for its preview week, it is still good business. It generates interest, which is a hard commodity to buy. Personally I mildly enjoy previews. I checkout mtgsalvation.com, which collects spoiler information and admire the new artwork. I am always on the lookout for what new mechanic I might have to code, so I’m glad that Beseech the Queen and hybrid mana (pay G or U or 2) have already been previewed. Much like the never ending stories in comic books, Magic will continue. (We all have memories of our favorite trading card game that got cancelled and no new sets will ever be released again, WWE, Star Trek, old Star Wars, Dot Hack, Doomsday, Dune, and many others.)

p.s.
Even though this is week 1 of previews (you mean this lasts longer than just 1 week) Godhead of Awe does look pretty awesome.