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